#include "../include/vstr.h" #include #include #ifdef NDEBUG #define TEST(expr) #else #define TEST(expr) \ do { \ if (expr) { \ printf(__DATE__" " __TIME__ " [%s@%d] " "\x1b[32m" "| PASSED | " "(%s)" "\x1b[0m \n", __FILE__, __LINE__, #expr); \ } else { \ printf(__DATE__" " __TIME__ " [%s@%d] " "\x1b[31m" "| FAILED | " "(%s)" "\x1b[0m \n", __FILE__, __LINE__, #expr); \ } \ } while (0) #endif int main(void){ const char *s = "Hello World"; const char *left = " Hello World"; Vstr v = vstr_n(s,strlen(s)); Vstr l = vstr_n(left,strlen(left)); TEST(strncmp(s,v.buf,v.len) == 0); TEST(strncmp(s,l.buf,l.len) != 0); l = vstr_trim_left(v); TEST(strncmp(s,l.buf,l.len) == 0); Vstr b = vstr_before_delim(l," "); Vstr a = vstr_after_delim(l," "); const char *bs = "Hello"; const char *as = "World"; Vstr bc = vstr_n(bs,strlen(bs)); Vstr ac = vstr_n(as,strlen(as)); TEST(strncmp("Hello",b.buf,b.len) == 0); TEST(strncmp("World",a.buf,a.len) == 0); TEST(vstr_cmp(b,bc) == 0); TEST(vstr_cmp(a,ac) == 0); unsigned char buffer[256]; size_t packed_size = pack_vstr(buffer, &v); printf("Packed size: %lu bytes\n", packed_size); Vstr unpacked_vstr; unpack_vstr(buffer, &unpacked_vstr); printf("Unpacked string: %s\n", unpacked_vstr.buf); printf("Unpacked length: %zu\n", unpacked_vstr.len); free(unpacked_vstr.buf); Vstr array[] = { {5, "Hello"}, {7, "World!"}, {6, "Buffer"} }; size_t count = 3; unsigned char arr_buffer[1024]; size_t arr_packed_size = pack_vstr_array(arr_buffer, array, count); printf("Packed size: %lu bytes\n", arr_packed_size); size_t unpacked_count; Vstr *unpacked_array = unpack_vstr_array(arr_buffer, &unpacked_count); if (!unpacked_array) { printf("Failed to unpack the array.\n"); return 1; } printf("Unpacked %zu Vstr elements:\n", unpacked_count); for (size_t i = 0; i < unpacked_count; i++) { printf(" [%zu]: %s (len=%zu)\n", i, unpacked_array[i].buf, unpacked_array[i].len); free(unpacked_array[i].buf); // Free the string data } free(unpacked_array); return EXIT_SUCCESS; }