From e499d8631f9afc29a9fa9c970d636aec701cf29a Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 24 Oct 2025 12:51:54 +0100 Subject: [PATCH] test: update b_stream usage --- compress-test/mix-compress.c | 2 +- compress-test/mix-decompress.c | 2 +- compress-test/stream.c | 4 ++-- core-test/core-units.c | 36 ++++++++++++++-------------------- ds-test/streams.c | 15 +++++++------- ds-test/strings.c | 11 +++++++---- io-test/io-units.c | 9 ++++----- io-test/streams.c | 7 ++----- test/cat.c | 21 ++++++++------------ test/json-read.c | 11 ++++------- test/test.toml | 2 +- test/toml-read.c | 11 ++++------- 12 files changed, 57 insertions(+), 74 deletions(-) diff --git a/compress-test/mix-compress.c b/compress-test/mix-compress.c index 246e6ec..0e3f3a9 100644 --- a/compress-test/mix-compress.c +++ b/compress-test/mix-compress.c @@ -82,7 +82,7 @@ int main(int argc, const char **argv) printf("Done\n"); b_cstream_close(cstream); - b_stream_close(out_stream); + b_stream_unref(out_stream); fclose(out_fp); return 0; diff --git a/compress-test/mix-decompress.c b/compress-test/mix-decompress.c index d256846..6685cc7 100644 --- a/compress-test/mix-decompress.c +++ b/compress-test/mix-decompress.c @@ -76,7 +76,7 @@ int main(int argc, const char **argv) printf("Done\n"); b_cstream_close(cstream); - b_stream_close(in_stream); + b_stream_unref(in_stream); fclose(in_fp); return 0; diff --git a/compress-test/stream.c b/compress-test/stream.c index 5c5eb9c..4a485fb 100644 --- a/compress-test/stream.c +++ b/compress-test/stream.c @@ -43,7 +43,7 @@ static int compress(const b_compression_function *func, FILE *in, FILE *out) b_cstream_end_compressed_section(cstream, NULL, NULL); b_cstream_close(cstream); - b_stream_close(out_stream); + b_stream_unref(out_stream); return 0; } @@ -86,7 +86,7 @@ static int decompress(const b_compression_function *func, FILE *in, FILE *out) b_cstream_end_compressed_section(cstream, NULL, NULL); b_cstream_close(cstream); - b_stream_close(in_stream); + b_stream_unref(in_stream); return 0; } diff --git a/core-test/core-units.c b/core-test/core-units.c index c202392..8ad62b1 100644 --- a/core-test/core-units.c +++ b/core-test/core-units.c @@ -153,39 +153,33 @@ void test_queue_iterate(CuTest *tc) void test_stringstream_1(CuTest *tc) { char buf[1024]; - b_stringstream s; - b_stringstream_begin(&s, buf, sizeof buf); + b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf); - b_stringstream_add(&s, "hello"); - b_stringstream_addf(&s, "(%d + %.1f)", 32, 2.3); + b_stream_write_string(s, "hello", NULL); + b_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3); - const char *x[] = {"ABC", "DEF", NULL}; + char *end = b_stringstream_steal(s); + b_stringstream_unref(s); - b_stringstream_addv(&s, x); - b_stringstream_addvl(&s, x, 2); - - b_stringstream_add_many(&s, "more", "more", "more", NULL); - const char *end = b_stringstream_end(&s); - - CuAssertStrEquals(tc, "hello(32 + 2.3)ABCDEFABCDEFmoremoremore", end); + CuAssertStrEquals(tc, "hello(32 + 2.3)", end); } void test_stringstream_2(CuTest *tc) { char buf[1024]; - b_stringstream s; - b_stringstream_begin(&s, buf, sizeof buf); + b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf); - b_stringstream_add(&s, "{\n"); - b_stringstream_push_indent(&s, 1); + b_stream_write_string(s, "{\n", NULL); + b_stream_push_indent(s, 1); - b_stringstream_add(&s, "a = 32,\n"); - b_stringstream_add(&s, "b = 64\n"); + b_stream_write_string(s, "a = 32,\n", NULL); + b_stream_write_string(s, "b = 64\n", NULL); - b_stringstream_pop_indent(&s); - b_stringstream_add(&s, "}"); + b_stream_pop_indent(s); + b_stream_write_string(s, "}", NULL); - char *str = b_stringstream_end(&s); + char *str = b_stringstream_steal(s); + b_stringstream_unref(s); CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str); } diff --git a/ds-test/streams.c b/ds-test/streams.c index 91e2205..b606cfa 100644 --- a/ds-test/streams.c +++ b/ds-test/streams.c @@ -1,18 +1,19 @@ #include +#include #include #include int main(int argc, const char **argv) { size_t nr_read = 0; - b_string *str = b_string_create(); - b_stream *dest_stream; - b_stream_pipeline *pipeline; - b_status status = b_string_open_stream(str, &dest_stream); - b_stream_pipeline_create(1024, &pipeline); - b_stream_read_all_bytes_s(b_stdin, dest_stream, pipeline, &nr_read); + b_stringstream *dest_stream = b_stringstream_create(); + b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); + b_stream_read_all_bytes_s(b_stdin, dest_stream, buf, &nr_read); printf("done. read %zu bytes total.\n", nr_read); - printf("%s\n", b_string_ptr(str)); + printf("%s\n", b_stringstream_ptr(dest_stream)); + + b_stringstream_unref(dest_stream); + b_stream_buffer_unref(buf); return 0; } diff --git a/ds-test/strings.c b/ds-test/strings.c index 3a495eb..e4c8f66 100644 --- a/ds-test/strings.c +++ b/ds-test/strings.c @@ -28,10 +28,13 @@ int main(void) b_string_unref(str); - b_stringstream strv; - b_stringstream_begin_dynamic(&strv); - b_stringstream_add_many(&strv, "Hello", ", world", "!", NULL); - char *s = b_stringstream_end(&strv); + b_stringstream *strv = b_stringstream_create(); + b_stream_write_string(strv, "Hello", NULL); + b_stream_write_string(strv, ", world", NULL); + b_stream_write_string(strv, "!", NULL); + + char *s = b_stringstream_steal(strv); + b_stringstream_unref(strv); printf("%s\n", s); free(s); diff --git a/io-test/io-units.c b/io-test/io-units.c index f346aae..7310ab4 100644 --- a/io-test/io-units.c +++ b/io-test/io-units.c @@ -7,10 +7,9 @@ void test_path_1(CuTest *tc) { b_path *path = b_path_create_from_cstr("C:\\hello\\world\\"); char buf[512]; - b_stringstream str; - b_stringstream_begin(&str, buf, sizeof buf); + b_stringstream *str = b_stringstream_create_with_buffer(buf, sizeof buf); - b_object_to_string(path, (b_stream *)&str); + b_object_to_string(path, str); printf("%s\n", buf); @@ -21,8 +20,8 @@ void test_path_1(CuTest *tc) b_path *path4 = b_path_join(paths, sizeof paths / sizeof paths[0]); - b_stringstream_begin(&str, buf, sizeof buf); - b_object_to_string(path4, (b_stream *)&str); + b_stringstream_reset_with_buffer(str, buf, sizeof buf); + b_object_to_string(path4, str); printf("%s\n", buf); } diff --git a/io-test/streams.c b/io-test/streams.c index 019a570..b726dfb 100644 --- a/io-test/streams.c +++ b/io-test/streams.c @@ -15,11 +15,8 @@ int main(int argc, const char **argv) } size_t nr_read = 0; - b_stream *dest_stream; - b_stream_pipeline *pipeline; - b_status status = b_file_open_stream(dest, &dest_stream); - b_stream_pipeline_create(1024, &pipeline); - b_stream_read_all_bytes_s(b_stdin, dest_stream, pipeline, &nr_read); + b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); + b_stream_read_all_bytes_s(b_stdin, dest, buf, &nr_read); printf("done. read %zu bytes total.\n", nr_read); return 0; diff --git a/test/cat.c b/test/cat.c index 40ec02f..33d6fd0 100644 --- a/test/cat.c +++ b/test/cat.c @@ -1,7 +1,8 @@ #include +#include +#include #include #include -#include #include int main(int argc, const char **argv) @@ -13,25 +14,19 @@ int main(int argc, const char **argv) const char *path_cstr = argv[1]; b_path *path = b_path_create_from_cstr(path_cstr); - b_file *file = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); + b_file *src = NULL; + b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); if (b_result_is_error(result)) { b_throw(result); 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; - b_status status = b_string_open_stream(str, &dest_stream); - - b_stream_pipeline_create(1024, &pipeline); + b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024); size_t nr_read; - b_stream_read_all_bytes_s(src_stream, dest_stream, pipeline, &nr_read); + b_stream_read_all_bytes_s(src, b_stdout, buf, &nr_read); + + b_stream_buffer_unref(buf); - printf("%s\n", b_string_ptr(str)); return 0; } diff --git a/test/json-read.c b/test/json-read.c index 11f5a67..0643021 100644 --- a/test/json-read.c +++ b/test/json-read.c @@ -12,23 +12,19 @@ int main(int argc, const char **argv) const char *path_cstr = argv[1]; b_path *path = b_path_create_from_cstr(path_cstr); - b_file *file = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); + b_file *src = NULL; + b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); if (b_result_is_error(result)) { b_throw(result); 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; b_status status = b_serial_ctx_deserialise( - ctx, B_SERIAL_FORMAT_JSON, src_stream, &data, 0); + ctx, B_SERIAL_FORMAT_JSON, src, &data, 0); if (!B_OK(status)) { fprintf(stderr, "cannot read data\n"); return -1; @@ -38,6 +34,7 @@ int main(int argc, const char **argv) b_stream_write_char(b_stdout, '\n'); b_object_unref(data); + b_object_unref(src); b_serial_ctx_destroy(ctx); return 0; diff --git a/test/test.toml b/test/test.toml index cf80572..e102567 100644 --- a/test/test.toml +++ b/test/test.toml @@ -1 +1 @@ -tab = 32 +k = "v" diff --git a/test/toml-read.c b/test/toml-read.c index db4d8ae..dbdc516 100644 --- a/test/toml-read.c +++ b/test/toml-read.c @@ -12,23 +12,19 @@ int main(int argc, const char **argv) const char *path_cstr = argv[1]; b_path *path = b_path_create_from_cstr(path_cstr); - b_file *file = NULL; - b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); + b_file *src = NULL; + b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src); if (b_result_is_error(result)) { b_throw(result); 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 = NULL; b_status status = b_serial_ctx_deserialise( - ctx, B_SERIAL_FORMAT_TOML, src_stream, &data, 0); + ctx, B_SERIAL_FORMAT_TOML, src, &data, 0); if (!B_OK(status)) { fprintf(stderr, "cannot read data\n"); return -1; @@ -42,6 +38,7 @@ int main(int argc, const char **argv) b_stream_write_char(b_stdout, '\n'); b_object_unref(data); + b_object_unref(src); b_serial_ctx_destroy(ctx); return 0;