From 0d5a186d805cfbe96f8f1fffaad98b4da1a3416b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 3 Feb 2026 14:47:25 +0000 Subject: [PATCH] test: core: add bstr tests; update rope tests --- test/core/ropes.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ test/core/streams.c | 11 +++++++++ 2 files changed, 67 insertions(+) diff --git a/test/core/ropes.c b/test/core/ropes.c index db4277b..e035e55 100644 --- a/test/core/ropes.c +++ b/test/core/ropes.c @@ -1,6 +1,60 @@ #include +#include #include +static void print_rope(const struct b_rope *rope, int depth) +{ + for (int i = 0; i < depth; i++) { + printf(" "); + } + + printf("[%x:", rope->r_flags); + + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CHAR) && printf(" CHAR"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR) && printf(" CSTR"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_BORROWED) + && printf(" CSTR_BORROWED"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_STATIC) + && printf(" CSTR_STATIC"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_INT) && printf(" INT"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_UINT) && printf(" UINT"); + (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_COMPOSITE) + && printf(" COMPOSITE"); + (rope->r_flags & B_ROPE_F_MALLOC) && printf(" MALLOC"); + printf("] "); + + switch (B_ROPE_TYPE(rope->r_flags)) { + case B_ROPE_F_CHAR: + printf("%c", rope->r_v.v_char); + break; + case B_ROPE_F_CSTR: + case B_ROPE_F_CSTR_BORROWED: + case B_ROPE_F_CSTR_STATIC: + printf("%s", rope->r_v.v_cstr.s); + break; + case B_ROPE_F_INT: + printf("%" PRIdPTR, rope->r_v.v_int); + break; + case B_ROPE_F_UINT: + printf("%" PRIuPTR, rope->r_v.v_uint); + break; + default: + break; + } + + printf("\n"); + + if (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_COMPOSITE) { + if (rope->r_v.v_composite.r_left) { + print_rope(rope->r_v.v_composite.r_left, depth + 1); + } + + if (rope->r_v.v_composite.r_right) { + print_rope(rope->r_v.v_composite.r_right, depth + 1); + } + } +} + int main(void) { b_rope a = B_ROPE_CHAR('a'); @@ -19,6 +73,8 @@ int main(void) b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]); + print_rope(&str, 0); + char cstr[1024]; b_rope_to_cstr(&str, cstr, sizeof cstr); b_rope_destroy(&str); diff --git a/test/core/streams.c b/test/core/streams.c index 3e236b5..ba55355 100644 --- a/test/core/streams.c +++ b/test/core/streams.c @@ -1,3 +1,4 @@ +#include #include #include @@ -6,5 +7,15 @@ int main(void) b_stream_read_line_s(b_stdin, b_stdout); b_stream_write_char(b_stdout, '\n'); + char s[16]; + b_bstr str; + b_bstr_begin(&str, s, sizeof s); + + b_stream_read_line_s(b_stdin, (b_stream *)&str); + b_stream_write_char((b_stream *)&str, '\n'); + + const char *e = b_bstr_end(&str); + fputs(e, stdout); + return 0; }