test: core: add bstr tests; update rope tests

This commit is contained in:
2026-02-03 14:47:25 +00:00
parent 2632feac32
commit 0d5a186d80
2 changed files with 67 additions and 0 deletions

View File

@@ -1,6 +1,60 @@
#include <blue/core/rope.h> #include <blue/core/rope.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
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) int main(void)
{ {
b_rope a = B_ROPE_CHAR('a'); b_rope a = B_ROPE_CHAR('a');
@@ -19,6 +73,8 @@ int main(void)
b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]); b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]);
print_rope(&str, 0);
char cstr[1024]; char cstr[1024];
b_rope_to_cstr(&str, cstr, sizeof cstr); b_rope_to_cstr(&str, cstr, sizeof cstr);
b_rope_destroy(&str); b_rope_destroy(&str);

View File

@@ -1,3 +1,4 @@
#include <blue/core/bstr.h>
#include <blue/core/stream.h> #include <blue/core/stream.h>
#include <stdio.h> #include <stdio.h>
@@ -6,5 +7,15 @@ int main(void)
b_stream_read_line_s(b_stdin, b_stdout); b_stream_read_line_s(b_stdin, b_stdout);
b_stream_write_char(b_stdout, '\n'); 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; return 0;
} }