1 #include <ccan/bytestring/bytestring.h>
2 #include <ccan/tap/tap.h>
4 #define TEST_STRING "test string"
5 #define TEST_STRING_2 "abc\0def"
7 const char str1[] = TEST_STRING;
8 const char *str2 = TEST_STRING;
12 struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
14 /* This is how many tests you plan to run */
17 bs = bytestring(str1, sizeof(str1) - 1);
19 ok1(bs.len == (sizeof(str1) - 1));
21 bs1 = BYTESTRING(TEST_STRING);
22 ok1(bytestring_eq(bs, bs1));
24 bs2 = BYTESTRING(TEST_STRING_2);
27 bs3 = bytestring_from_string(str2);
28 ok1(bytestring_eq(bs3, bs));
30 bs4 = bytestring_from_string(TEST_STRING_2);
33 bs5 = bytestring_from_string(NULL);
36 ok1(bytestring_eq(bs5, bytestring_NULL));
38 ok1(bytestring_byte(bs2, 0) == 'a');
39 ok1(bytestring_byte(bs2, 1) == 'b');
40 ok1(bytestring_byte(bs2, 2) == 'c');
41 ok1(bytestring_byte(bs2, 3) == '\0');
42 ok1(bytestring_byte(bs2, 4) == 'd');
43 ok1(bytestring_byte(bs2, 5) == 'e');
44 ok1(bytestring_byte(bs2, 6) == 'f');
46 ok1(bytestring_eq(bytestring_slice(bs, 0, 4), BYTESTRING("test")));
47 ok1(bytestring_eq(bytestring_slice(bs, 5, 8), BYTESTRING("str")));
48 ok1(bytestring_eq(bytestring_slice(bs2, 2, 5), BYTESTRING("c\0d")));
49 ok1(bytestring_eq(bytestring_slice(bs2, 0, -1U), bs2));
50 ok1(bytestring_eq(bytestring_slice(bs2, 10, 20), bytestring_NULL));
51 ok1(bytestring_eq(bytestring_slice(bs2, 2, 1), bytestring_NULL));
53 /* This exits depending on whether all tests passed */