diff --git a/CMakeLists.txt b/CMakeLists.txt index 91440d5..a0982e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,3 +46,17 @@ foreach (module ${b_modules}) endforeach (test_file) endif () endforeach (module) + +file(GLOB test_sources test/*.c) +list(REMOVE_ITEM test_sources "${CMAKE_CURRENT_SOURCE_DIR}/test/units.c") + +foreach (test_file ${test_sources}) + get_filename_component(test_name ${test_file} NAME_WE) + add_executable(blue-${test_name} ${test_file}) + + set_target_properties(blue-${test_name} PROPERTIES FOLDER "Tests") + + foreach (module ${b_modules}) + target_link_libraries(blue-${test_name} blue-${module}) + endforeach (module) +endforeach (test_file) diff --git a/test/cat.c b/test/cat.c new file mode 100644 index 0000000..5c876d8 --- /dev/null +++ b/test/cat.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +int main(int argc, const char **argv) +{ + if (argc < 2) { + return -1; + } + + const char *path_cstr = argv[1]; + b_path *path = b_path_create_from_cstr(path_cstr); + + b_file *file = NULL; + b_status status = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); + if (!B_OK(status)) { + fprintf(stderr, "cannot open file %s\n", path_cstr); + return -1; + } + b_stream *src_stream, *dest_stream; + + b_file_open_stream(file, &src_stream); + + b_string *str = b_string_create(); + b_stream_pipeline *pipeline; + status = b_string_open_stream(str, &dest_stream); + + b_stream_pipeline_create(1024, &pipeline); + + size_t nr_read; + b_stream_read_all_bytes_s(src_stream, dest_stream, pipeline, &nr_read); + + printf("%s\n", b_string_ptr(str)); + return 0; +} diff --git a/test/json-read.c b/test/json-read.c new file mode 100644 index 0000000..21d26a5 --- /dev/null +++ b/test/json-read.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +int main(int argc, const char **argv) +{ + if (argc < 2) { + return -1; + } + + const char *path_cstr = argv[1]; + b_path *path = b_path_create_from_cstr(path_cstr); + + b_file *file = NULL; + b_status status = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); + if (!B_OK(status)) { + fprintf(stderr, "cannot open file %s\n", path_cstr); + return -1; + } + + b_stream *src_stream; + + b_file_open_stream(file, &src_stream); + + b_serial_ctx *ctx; + b_serial_ctx_create(&ctx); + + b_object *data; + status = b_serial_ctx_deserialise( + ctx, B_SERIAL_FORMAT_JSON, src_stream, &data, 0); + if (!B_OK(status)) { + fprintf(stderr, "cannot read data\n"); + return -1; + } + + b_to_string(B_OBJECT(data), b_stdout); + b_stream_write_char(b_stdout, '\n'); + + b_release(data); + b_serial_ctx_destroy(ctx); + + return 0; +}