build: re-enable namespace-specific tests

This commit is contained in:
2026-05-05 21:17:21 +01:00
parent 4b20f8ca0e
commit c29465a97d
27 changed files with 273 additions and 447 deletions
+11
View File
@@ -45,6 +45,17 @@ function(add_fx_assembly)
target_compile_definitions(${assembly_target_name} PRIVATE ${def})
endforeach (def)
foreach (ns ${arg_NAMESPACES})
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
foreach (test_file ${test_sources})
get_filename_component(test_name ${test_file} NAME_WE)
set(test_name ${ns}-${test_name})
string(REPLACE "." "-" test_name ${test_name})
add_executable(${test_name} ${test_file})
target_link_libraries(${test_name} ${assembly_target_name})
endforeach (test_file)
endforeach (ns)
set_target_properties(${assembly_target_name} PROPERTIES
FOLDER "${assembly_name}")
+5 -5
View File
@@ -1,13 +1,13 @@
#include <fx/ds/array.h>
#include <fx/ds/number.h>
#include <fx/collections/array.h>
#include <fx/int.h>
#include <stdio.h>
int main(void)
{
fx_array *array = fx_array_create();
fx_array_append(array, FX_RV_INT(32));
fx_array_append(array, FX_RV_INT(64));
fx_array_append(array, FX_RV_INT(128));
fx_array_append(array, fx_int_create(32));
fx_array_append(array, fx_int_create(64));
fx_array_append(array, fx_int_create(128));
fx_iterator *it = fx_iterator_begin(array);
fx_foreach_ptr(fx_object, obj, it)
+5 -4
View File
@@ -1,11 +1,12 @@
#include <fx/ds/number.h>
#include <fx/double.h>
#include <stdio.h>
int main(void)
{
fx_number *number = fx_number_create_float(6.8);
fx_double *d = fx_double_create(6.8);
printf("double=%lf\n", fx_double_get_value(d));
fx_double_unref(d);
printf("number=%zd\n", FX_NUMBER_IVAL(number));
fx_number_unref(number);
return 0;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/ds/string.h>
#include <fx/string.h>
int main(void)
{
+3 -3
View File
@@ -1,6 +1,6 @@
#include <fx/core/stream.h>
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <stdio.h>
int main(int argc, const char **argv)
+14 -11
View File
@@ -1,8 +1,9 @@
#include <fx/core/bst.h>
#include <fx/core/iterator.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/tree.h>
#include <fx/bst.h>
#include <fx/collections/dict.h>
#include <fx/collections/tree.h>
#include <fx/int.h>
#include <fx/iterator.h>
#include <inttypes.h>
#include <stdio.h>
#define NITEMS 16
@@ -23,18 +24,20 @@ FX_BST_DEFINE_SIMPLE_INSERT(struct bst_item, node, value, put_node)
int main(void)
{
fx_dict *dict = fx_dict_create();
fx_dict_put(dict, "hello", FX_RV_INT(32));
fx_dict_put(dict, "world", FX_RV_INT(64));
fx_dict_put(dict, "more", FX_RV_INT(128));
fx_dict_put(dict, "other", FX_RV_INT(256));
fx_dict_put(dict, "hello", fx_int_create(32));
fx_dict_put(dict, "world", fx_int_create(64));
fx_dict_put(dict, "more", fx_int_create(128));
fx_dict_put(dict, "other", fx_int_create(256));
fx_iterator *it = fx_iterator_begin(dict);
size_t i = 0;
fx_foreach(fx_dict_item *, item, it)
{
printf("item %zu: %s=%d\n", i++, fx_string_get_cstr(item->key),
fx_number_get_int(item->value));
printf("item %zu: %s=%" PRIdPTR "\n",
i++,
fx_string_get_cstr(item->key),
fx_int_get_value(item->value));
}
fx_iterator_unref(it);
-50
View File
@@ -1,50 +0,0 @@
#include <CuTest.h>
#include <fx/ds/string.h>
static void test_string_create(CuTest *tc)
{
fx_string *str = fx_string_create();
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 0, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "", fx_string_get_cstr(str));
fx_string_unref(str);
str = fx_string_create_from_c('A', 8);
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 8, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "AAAAAAAA", fx_string_get_cstr(str));
fx_string_unref(str);
str = fx_string_create_from_cstr("Hello, world!");
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 13, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "Hello, world!", fx_string_get_cstr(str));
fx_string_unref(str);
}
static void test_string_length(CuTest *tc)
{
const char *cstr = "Hello, \033[91;1mworld!";
fx_string *s = fx_string_create_from_cstr(cstr);
CuAssertIntEquals(tc, 13, fx_string_get_size(s, FX_STRLEN_IGNORE_ESC));
CuAssertIntEquals(tc, 20, fx_string_get_size(s, FX_STRLEN_NORMAL));
fx_string_unref(s);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_string_create);
SUITE_ADD_TEST(suite, test_string_length);
return suite;
}
+18 -9
View File
@@ -1,9 +1,9 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
@@ -48,9 +48,13 @@ int main(int argc, const char **argv)
size_t nr_written = 0;
fx_status status = fx_cstream_write(
cstream, source, source_len, &nr_written);
cstream,
source,
source_len,
&nr_written);
if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n",
fprintf(stderr,
"write error: %s\n",
fx_status_description(status));
break;
}
@@ -59,14 +63,19 @@ int main(int argc, const char **argv)
if (compressed) {
fx_cstream_end_compressed_section(
cstream, &nr_written_compressed, &nr_written);
cstream,
&nr_written_compressed,
&nr_written);
}
size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total);
printf("iteration %d: wrote %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_written_compressed, nr_written, tx_total,
i,
nr_written_compressed,
nr_written,
tx_total,
compressed ? "compressed" : "uncompressed");
compressed = !compressed;
+19 -10
View File
@@ -1,9 +1,9 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
@@ -37,10 +37,14 @@ int main(int argc, const char **argv)
memset(buf, 0x0, sizeof buf);
size_t nr_read = 0;
fx_status status
= fx_cstream_read(cstream, buf, sizeof buf - 1, &nr_read);
fx_status status = fx_cstream_read(
cstream,
buf,
sizeof buf - 1,
&nr_read);
if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n",
fprintf(stderr,
"write error: %s\n",
fx_status_description(status));
break;
}
@@ -52,14 +56,19 @@ int main(int argc, const char **argv)
size_t nr_read_compressed = 0;
if (compressed) {
fx_cstream_end_compressed_section(
cstream, &nr_read_compressed, &nr_read);
cstream,
&nr_read_compressed,
&nr_read);
}
size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total);
printf(" * iteration %d: read %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_read_compressed, nr_read, tx_total,
i,
nr_read_compressed,
nr_read,
tx_total,
compressed ? "compressed" : "uncompressed");
printf("%s\n", buf);
+18 -9
View File
@@ -1,7 +1,7 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/compression/compressor.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <stdio.h>
#include <string.h>
@@ -12,8 +12,10 @@ int refill_input_buffer(FILE *fp, fx_ringbuffer *dest)
while (1) {
void *buf;
size_t capacity;
fx_status status
= fx_ringbuffer_open_write_buffer(dest, &buf, &capacity);
fx_status status = fx_ringbuffer_open_write_buffer(
dest,
&buf,
&capacity);
if (status == FX_ERR_NO_SPACE) {
break;
}
@@ -43,8 +45,10 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
while (1) {
const void *buf;
size_t capacity;
fx_status status
= fx_ringbuffer_open_read_buffer(src, &buf, &capacity);
fx_status status = fx_ringbuffer_open_read_buffer(
src,
&buf,
&capacity);
if (status == FX_ERR_NO_DATA) {
break;
}
@@ -68,7 +72,9 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
int main(int argc, const char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1;
}
@@ -101,7 +107,10 @@ int main(int argc, const char **argv)
size_t inbuf_size, outbuf_size;
fx_compressor_get_buffer_size(
compressor_type, mode, &inbuf_size, &outbuf_size);
compressor_type,
mode,
&inbuf_size,
&outbuf_size);
fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size);
fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size);
+23 -10
View File
@@ -1,9 +1,9 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
@@ -15,7 +15,10 @@ static int compress(fx_type_id compressor_type, FILE *in, FILE *out)
fx_cstream *cstream;
fx_status status = fx_cstream_open(
out_stream, compressor_type, FX_COMPRESSOR_MODE_COMPRESS, &cstream);
out_stream,
compressor_type,
FX_COMPRESSOR_MODE_COMPRESS,
&cstream);
if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
@@ -54,7 +57,10 @@ static int decompress(fx_type_id compressor_type, FILE *in, FILE *out)
fx_cstream *cstream;
fx_status status = fx_cstream_open(
in_stream, compressor_type, FX_COMPRESSOR_MODE_DECOMPRESS, &cstream);
in_stream,
compressor_type,
FX_COMPRESSOR_MODE_DECOMPRESS,
&cstream);
if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
@@ -66,9 +72,14 @@ static int decompress(fx_type_id compressor_type, FILE *in, FILE *out)
char buf[4096];
while (1) {
size_t r = 0;
fx_status status = fx_cstream_read(cstream, buf, sizeof buf, &r);
fx_status status = fx_cstream_read(
cstream,
buf,
sizeof buf,
&r);
if (!FX_OK(status)) {
fprintf(stderr, "read error: %s\n",
fprintf(stderr,
"read error: %s\n",
fx_status_description(status));
return -1;
}
@@ -94,7 +105,9 @@ static int decompress(fx_type_id compressor_type, FILE *in, FILE *out)
int main(int argc, const char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1;
}
+5 -2
View File
@@ -1,6 +1,6 @@
#include <fx/core/stream.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/stream.h>
#include <stdio.h>
int main(int argc, const char **argv)
@@ -8,7 +8,10 @@ int main(int argc, const char **argv)
fx_file *dest;
fx_path *path = fx_path_create_from_cstr("data.txt");
fx_result result = fx_file_open(
NULL, path, FX_FILE_WRITE_ONLY | FX_FILE_CREATE, &dest);
NULL,
path,
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
&dest);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
-35
View File
@@ -1,35 +0,0 @@
#include <CuTest.h>
#include <fx/core/stringstream.h>
#include <fx/io/path.h>
#include <stdio.h>
void test_path_1(CuTest *tc)
{
fx_path *path = fx_path_create_from_cstr("C:\\hello\\world\\");
char buf[512];
fx_stringstream *str = fx_stringstream_create_with_buffer(buf, sizeof buf);
fx_object_to_string(path, str);
printf("%s\n", buf);
fx_path *path2 = fx_path_create_from_cstr("path1\\path2\\");
fx_path *path3 = fx_path_create_from_cstr("path3\\path4\\");
const fx_path *paths[] = {path, path2, path3};
fx_path *path4 = fx_path_join(paths, sizeof paths / sizeof paths[0]);
fx_stringstream_reset_with_buffer(str, buf, sizeof buf);
fx_object_to_string(path4, str);
printf("%s\n", buf);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_path_1);
return suite;
}
+10 -9
View File
@@ -1,9 +1,10 @@
#include <fx/core/stream.h>
#include <fx/ds/array.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/string.h>
#include <fx/serial.h>
#include <fx/collections/array.h>
#include <fx/collections/dict.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <stdio.h>
int main(void)
@@ -13,9 +14,9 @@ int main(void)
fx_dict *dict = fx_dict_create();
fx_array *array = fx_array_create();
fx_array_append(array, FX_RV_INT(32));
fx_array_append(array, FX_RV_INT(64));
fx_array_append(array, FX_RV_INT(128));
fx_array_append(array, fx_int_create(32));
fx_array_append(array, fx_int_create(64));
fx_array_append(array, fx_int_create(128));
fx_dict_put(dict, "numbers", FX_RV(array));
+29 -39
View File
@@ -1,9 +1,12 @@
#include <fx/ds/array.h>
#include <fx/ds/datetime.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/string.h>
#include <fx/serial.h>
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/dict.h>
#include <fx/double.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/string.h>
#include <inttypes.h>
void write_tagged_value(fx_object *data);
@@ -50,50 +53,50 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_integer(fx_number *data)
void write_tagged_integer(fx_int *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"integer\", \"value\": \"",
NULL);
if (fx_number_is_inf_positive(data)) {
if (fx_int_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_number_is_inf_negative(data)) {
} else if (fx_int_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_number_is_nan_positive(data)) {
} else if (fx_int_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_number_is_nan_negative(data)) {
} else if (fx_int_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_number_get_longlong(data),
fx_int_get_value(data),
NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_float(fx_number *data)
void write_tagged_float(fx_double *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"float\", \"value\": \"",
NULL);
if (fx_number_is_inf_positive(data)) {
if (fx_double_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_number_is_inf_negative(data)) {
} else if (fx_double_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_number_is_nan_positive(data)) {
} else if (fx_double_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_number_is_nan_negative(data)) {
} else if (fx_double_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
double v = fx_number_get_double(data);
double v = fx_double_get_value(data);
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|| (v >= 1000000000) || (v <= -1000000000)) {
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
@@ -105,14 +108,13 @@ void write_tagged_float(fx_number *data)
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_bool(fx_number *data)
void write_tagged_bool(fx_bool *data)
{
int v = fx_number_get_int8(data);
fx_stream_write_fmt(
fx_stdout,
NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }",
(v > 0) ? "true" : "false",
fx_bool_get_value(data) ? "true" : "false",
NULL);
}
@@ -197,30 +199,18 @@ void write_tagged_value(fx_object *data)
{
if (fx_object_is_type(data, FX_TYPE_DICT)) {
write_tagged_dict(data);
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data);
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data);
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_NUMBER)) {
switch (fx_number_get_number_type(data)) {
case FX_NUMBER_LONGLONG:
write_tagged_integer(data);
break;
case FX_NUMBER_INT8:
write_tagged_bool(data);
break;
case FX_NUMBER_DOUBLE:
write_tagged_float(data);
break;
default:
break;
}
} else if (fx_object_is_type(data, FX_TYPE_INT)) {
write_tagged_integer(data);
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data);
} else if (fx_object_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
}
}
+31 -13
View File
@@ -1,4 +1,4 @@
#include <fx/core/error.h>
#include <fx/error.h>
#include <fx/term/print.h>
#include <stdio.h>
@@ -17,21 +17,28 @@ static const fx_error_definition sample_errors[] = {
FX_ERROR_DEFINITION(SAMPLE_OK, "OK", "Success"),
FX_ERROR_DEFINITION(SAMPLE_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"),
FX_ERROR_DEFINITION_TEMPLATE(
SAMPLE_ERR_FILE_READ_FAILED, "FILE_READ_FAILED",
SAMPLE_ERR_FILE_READ_FAILED,
"FILE_READ_FAILED",
"Failed to read file @i[filepath]",
FX_ERROR_TEMPLATE_PARAM(
"filepath", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")),
"filepath",
FX_ERROR_TEMPLATE_PARAM_STRING,
"%s")),
};
static const fx_error_msg sample_error_msg[] = {
FX_ERROR_MSG_TEMPLATE(
SAMPLE_MSG_A_TEMPLATED_MSG, "A templated message: @e[param1]",
SAMPLE_MSG_A_TEMPLATED_MSG,
"A templated message: @e[param1]",
FX_ERROR_TEMPLATE_PARAM(
"param1", FX_ERROR_TEMPLATE_PARAM_STRING, "%s")),
"param1",
FX_ERROR_TEMPLATE_PARAM_STRING,
"%s")),
};
static const char *sample_code_to_string(
const struct fx_error_vendor *vendor, fx_error_status_code code)
const struct fx_error_vendor *vendor,
fx_error_status_code code)
{
switch (code) {
case SAMPLE_OK:
@@ -56,15 +63,22 @@ static fx_error_vendor sample_vendor = {
static fx_result error_return_3(void)
{
fx_result err = fx_error_with_string(
&sample_vendor, SAMPLE_ERR_IO_FAILURE,
&sample_vendor,
SAMPLE_ERR_IO_FAILURE,
"I/O failure while reading file");
fx_error_add_submsg_string(
err, FX_ERROR_SUBMSG_ERROR, "An @e{error} message");
err,
FX_ERROR_SUBMSG_ERROR,
"An @e{error} message");
fx_error_add_submsg_string(
err, FX_ERROR_SUBMSG_WARNING, "A @w{warning} message");
err,
FX_ERROR_SUBMSG_WARNING,
"A @w{warning} message");
fx_error_add_submsg_template(
err, FX_ERROR_SUBMSG_WARNING, SAMPLE_MSG_A_TEMPLATED_MSG,
err,
FX_ERROR_SUBMSG_WARNING,
SAMPLE_MSG_A_TEMPLATED_MSG,
FX_ERROR_PARAM("param1", "Hello!"));
return err;
@@ -105,10 +119,12 @@ static fx_result some_operation(void)
fx_result result = error_return_2();
if (fx_result_is_error(result)) {
fx_result err = fx_error_with_template(
&sample_vendor, SAMPLE_ERR_FILE_READ_FAILED,
&sample_vendor,
SAMPLE_ERR_FILE_READ_FAILED,
FX_ERROR_PARAM("filepath", "src/Manifest.json"));
fx_error_add_submsg_string(
err, FX_ERROR_SUBMSG_INFO,
err,
FX_ERROR_SUBMSG_INFO,
"An @i{informational} message");
fx_error_caused_by_fx_status(result, FX_ERR_IO_FAILURE);
@@ -122,7 +138,9 @@ static fx_result some_operation(void)
int main(void)
{
fx_set_error_report_function(fx_enhanced_error_reporter, FX_ERROR_REPORT_ALL);
fx_set_error_report_function(
fx_enhanced_error_reporter,
FX_ERROR_REPORT_ALL);
test(PARAM("Hello", 1), PARAM("Goodbye", 2));
+8 -5
View File
@@ -1,6 +1,6 @@
#include <fx/term/tty.h>
#include <fx/string.h>
#include <fx/term/print.h>
#include <fx/ds/string.h>
#include <fx/term/tty.h>
#include <stdio.h>
#define F_GREEN "[green]"
@@ -62,7 +62,7 @@ int main(void)
size_t len = fx_string_get_size(str, FX_STRLEN_IGNORE_MOD);
printf("length = %zu\n", len);
fx_paragraph_format format = { 0 };
fx_paragraph_format format = {0};
format.p_left_margin = 5;
format.p_right_margin = 5;
format.p_flags = FX_PARAGRAPH_DOUBLE_LINE_BREAK;
@@ -72,8 +72,11 @@ int main(void)
fx_i("An informational message\n\nWith multiple lines");
fx_warn("A warning message\nWith multiple lines");
fx_err("An error message\nWith multiple lines");
fx_printf("[red]formatting ignored: '%s'[reset]\n[dark_grey]dark text[reset]\n", "[blue]wow![reset]");
fx_printf(
"[red]formatting ignored: '%s'[reset]\n[dark_grey]dark "
"text[reset]\n",
"[blue]wow![reset]");
return 0;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/core/error.h>
#include <fx/error.h>
int main(void)
{
+54 -15
View File
@@ -1,10 +1,13 @@
#include <fx/core/hash.h>
#include <fx/hash.h>
#include <stdio.h>
#include <string.h>
static void print_digest(
const char *func_name, fx_hash_function func, const char *msg,
size_t msg_len, size_t digest_len)
const char *func_name,
fx_hash_function func,
const char *msg,
size_t msg_len,
size_t digest_len)
{
unsigned char digest[128];
@@ -27,8 +30,10 @@ static void print_digest(
char digest_str[256];
for (size_t i = 0; i < digest_len; i++) {
snprintf(
digest_str + (i * 2), sizeof digest_str - (i * 2),
"%02x", digest[i]);
digest_str + (i * 2),
sizeof digest_str - (i * 2),
"%02x",
digest[i]);
}
printf("%s(%s) = %s\n", func_name, msg, digest_str);
@@ -44,30 +49,64 @@ int main(void)
print_digest("MD5", FX_HASH_MD5, msg, msg_len, FX_DIGEST_LENGTH_MD5);
print_digest("SHA1", FX_HASH_SHA1, msg, msg_len, FX_DIGEST_LENGTH_SHA1);
print_digest(
"SHA224", FX_HASH_SHA2_224, msg, msg_len, FX_DIGEST_LENGTH_SHA2_224);
"SHA224",
FX_HASH_SHA2_224,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA2_224);
print_digest(
"SHA256", FX_HASH_SHA2_256, msg, msg_len, FX_DIGEST_LENGTH_SHA2_256);
"SHA256",
FX_HASH_SHA2_256,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA2_256);
print_digest(
"SHA384", FX_HASH_SHA2_384, msg, msg_len, FX_DIGEST_LENGTH_SHA2_384);
"SHA384",
FX_HASH_SHA2_384,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA2_384);
print_digest(
"SHA512", FX_HASH_SHA2_512, msg, msg_len, FX_DIGEST_LENGTH_SHA2_512);
"SHA512",
FX_HASH_SHA2_512,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA2_512);
print_digest(
"SHA3-224", FX_HASH_SHA3_224, msg, msg_len,
"SHA3-224",
FX_HASH_SHA3_224,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA3_224);
print_digest(
"SHA3-256", FX_HASH_SHA3_256, msg, msg_len,
"SHA3-256",
FX_HASH_SHA3_256,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA3_256);
print_digest(
"SHA3-384", FX_HASH_SHA3_384, msg, msg_len,
"SHA3-384",
FX_HASH_SHA3_384,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA3_384);
print_digest(
"SHA3-512", FX_HASH_SHA3_512, msg, msg_len,
"SHA3-512",
FX_HASH_SHA3_512,
msg,
msg_len,
FX_DIGEST_LENGTH_SHA3_512);
print_digest(
"SHAKE128", FX_HASH_SHAKE128, msg, msg_len,
"SHAKE128",
FX_HASH_SHAKE128,
msg,
msg_len,
FX_DIGEST_LENGTH_SHAKE128);
print_digest(
"SHAKE256", FX_HASH_SHAKE256, msg, msg_len,
"SHAKE256",
FX_HASH_SHAKE256,
msg,
msg_len,
FX_DIGEST_LENGTH_SHAKE256);
return 0;
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/core/random.h>
#include <fx/random.h>
#include <stdio.h>
#define NRAND_NUMBERS 12
+1 -1
View File
@@ -1,5 +1,5 @@
#include <assert.h>
#include <fx/core/ringbuffer.h>
#include <fx/ringbuffer.h>
#include <stdio.h>
#include <string.h>
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/core/rope.h>
#include <fx/rope.h>
#include <inttypes.h>
#include <stdio.h>
+2 -2
View File
@@ -1,5 +1,5 @@
#include <fx/core/bstr.h>
#include <fx/core/stream.h>
#include <fx/bstr.h>
#include <fx/stream.h>
#include <stdio.h>
int main(void)
@@ -1,5 +1,5 @@
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <stdio.h>
#include <stdlib.h>
@@ -8,21 +8,24 @@ int main(void)
printf("-------------\n");
fx_string *str = fx_string_create_from_cstr("Hello, world!\n");
printf("%s\n", fx_string_get_cstr(str));
printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL),
printf("len:%zu, max:%zu\n",
fx_string_get_size(str, FX_STRLEN_NORMAL),
fx_string_get_capacity(str));
fx_string_insert_cstr(str, "WOW!", 4);
printf("-------------\n");
printf("%s\n", fx_string_get_cstr(str));
printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL),
printf("len:%zu, max:%zu\n",
fx_string_get_size(str, FX_STRLEN_NORMAL),
fx_string_get_capacity(str));
fx_string_replace(str, 4, 4, "+");
printf("-------------\n");
printf("%s\n", fx_string_get_cstr(str));
printf("len:%zu, max:%zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL),
printf("len:%zu, max:%zu\n",
fx_string_get_size(str, FX_STRLEN_NORMAL),
fx_string_get_capacity(str));
printf("-------------\n");
@@ -1,5 +1,5 @@
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <stdio.h>
#include <stdlib.h>
@@ -10,7 +10,8 @@ int main(void)
const char *s = fx_string_get_cstr(str);
printf("%s\n", s);
printf("len: %zu\n", fx_string_get_size(str, FX_STRLEN_NORMAL));
printf("codepoints: %zu\n", fx_string_get_size(str, FX_STRLEN_CODEPOINTS));
printf("codepoints: %zu\n",
fx_string_get_size(str, FX_STRLEN_CODEPOINTS));
const char *delims[] = {"в"};
size_t nr_delims = sizeof delims / sizeof delims[0];
-202
View File
@@ -1,202 +0,0 @@
#include <CuTest.h>
#include <fx/core/bst.h>
#include <fx/core/misc.h>
#include <fx/core/queue.h>
#include <fx/core/stringstream.h>
#include <stdlib.h>
#include <time.h>
struct test_tree_node {
int value;
fx_bst_node node;
};
struct test_queue_entry {
int value;
fx_queue_entry entry;
};
FX_BST_DEFINE_SIMPLE_INSERT(struct test_tree_node, node, value, test_tree_insert);
void test_bst_insert(CuTest *tc)
{
fx_bst tree = {0};
struct test_tree_node nodes[3] = {0};
for (int i = 0; i < sizeof nodes / sizeof *nodes; i++) {
nodes[i].value = i;
}
test_tree_insert(&tree, &nodes[0]);
CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left);
CuAssertPtrEquals(tc, NULL, nodes[0].node.n_right);
CuAssertIntEquals(tc, 1, nodes[0].node.n_height);
test_tree_insert(&tree, &nodes[1]);
CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left);
CuAssertPtrEquals(tc, &nodes[1].node, nodes[0].node.n_right);
CuAssertIntEquals(tc, 2, nodes[0].node.n_height);
CuAssertPtrEquals(tc, NULL, nodes[1].node.n_left);
CuAssertPtrEquals(tc, NULL, nodes[1].node.n_right);
CuAssertIntEquals(tc, 1, nodes[1].node.n_height);
test_tree_insert(&tree, &nodes[2]);
CuAssertPtrEquals(tc, &nodes[0].node, nodes[1].node.n_left);
CuAssertPtrEquals(tc, &nodes[2].node, nodes[1].node.n_right);
CuAssertIntEquals(tc, 2, nodes[1].node.n_height);
CuAssertPtrEquals(tc, NULL, nodes[0].node.n_left);
CuAssertPtrEquals(tc, NULL, nodes[0].node.n_right);
CuAssertIntEquals(tc, 1, nodes[0].node.n_height);
CuAssertPtrEquals(tc, NULL, nodes[2].node.n_left);
CuAssertPtrEquals(tc, NULL, nodes[2].node.n_right);
CuAssertIntEquals(tc, 1, nodes[2].node.n_height);
}
void test_bst_iterate(CuTest *tc)
{
static const size_t nr_nodes = 256;
srand(time(NULL));
fx_bst tree = {0};
struct test_tree_node *nodes = calloc(nr_nodes, sizeof *nodes);
CuAssertPtrNotNull(tc, nodes);
for (int i = 0; i < nr_nodes; i++) {
nodes[i].value = rand();
test_tree_insert(&tree, &nodes[i]);
}
int prev = -1;
fx_bst_node *bnode = fx_bst_first(&tree);
while (bnode) {
struct test_tree_node *node
= fx_unbox(struct test_tree_node, bnode, node);
CuAssertPtrNotNull(tc, node);
if (prev == -1) {
prev = node->value;
bnode = fx_bst_next(bnode);
continue;
}
CuAssertTrue(tc, prev <= node->value);
prev = node->value;
bnode = fx_bst_next(bnode);
}
free(nodes);
}
void test_queue_insert(CuTest *tc)
{
struct test_queue_entry entries[5] = {0};
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
entries[i].value = i;
}
fx_queue q = FX_QUEUE_INIT;
fx_queue_push_back(&q, &entries[0].entry);
fx_queue_push_back(&q, &entries[2].entry);
fx_queue_push_back(&q, &entries[4].entry);
fx_queue_insert_after(&q, &entries[3].entry, &entries[2].entry);
fx_queue_insert_before(&q, &entries[1].entry, &entries[2].entry);
CuAssertPtrEquals(tc, NULL, entries[0].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[1].entry, entries[0].entry.qe_next);
CuAssertPtrEquals(tc, &entries[0].entry, entries[1].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[2].entry, entries[1].entry.qe_next);
CuAssertPtrEquals(tc, &entries[1].entry, entries[2].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[3].entry, entries[2].entry.qe_next);
CuAssertPtrEquals(tc, &entries[2].entry, entries[3].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[4].entry, entries[3].entry.qe_next);
CuAssertPtrEquals(tc, &entries[3].entry, entries[4].entry.qe_prev);
CuAssertPtrEquals(tc, NULL, entries[4].entry.qe_next);
}
void test_queue_iterate(CuTest *tc)
{
fx_queue q = FX_QUEUE_INIT;
struct test_queue_entry entries[32] = {0};
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
entries[i].value = i;
fx_queue_push_back(&q, &entries[i].entry);
}
int prev = -1;
struct fx_queue_entry *entry = fx_queue_first(&q);
while (entry) {
struct test_queue_entry *e
= fx_unbox(struct test_queue_entry, entry, entry);
CuAssertPtrNotNull(tc, e);
if (prev == -1) {
prev = e->value;
goto skip;
}
CuAssertTrue(tc, prev < e->value);
prev = e->value;
skip:
entry = fx_queue_next(entry);
}
}
void test_stringstream_1(CuTest *tc)
{
char buf[1024];
fx_stringstream *s = fx_stringstream_create_with_buffer(buf, sizeof buf);
fx_stream_write_cstr(s, "hello", NULL);
fx_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3);
char *end = fx_stringstream_steal(s);
fx_stringstream_unref(s);
CuAssertStrEquals(tc, "hello(32 + 2.3)", end);
}
void test_stringstream_2(CuTest *tc)
{
char buf[1024];
fx_stringstream *s = fx_stringstream_create_with_buffer(buf, sizeof buf);
fx_stream_write_cstr(s, "{\n", NULL);
fx_stream_push_indent(s, 1);
fx_stream_write_cstr(s, "a = 32,\n", NULL);
fx_stream_write_cstr(s, "b = 64\n", NULL);
fx_stream_pop_indent(s);
fx_stream_write_cstr(s, "}", NULL);
char *str = fx_stringstream_steal(s);
fx_stringstream_unref(s);
CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_bst_insert);
SUITE_ADD_TEST(suite, test_bst_iterate);
SUITE_ADD_TEST(suite, test_queue_insert);
SUITE_ADD_TEST(suite, test_queue_iterate);
SUITE_ADD_TEST(suite, test_stringstream_1);
SUITE_ADD_TEST(suite, test_stringstream_2);
return suite;
}
@@ -1,4 +1,4 @@
#include <fx/ds/uuid.h>
#include <fx/uuid.h>
#include <stdio.h>
int main(void)