]> git.ozlabs.org Git - ccan/blob - ccan/bytestring/test/run.c
bytestring: Add bytestring_byte() function
[ccan] / ccan / bytestring / test / run.c
1 #include <ccan/bytestring/bytestring.h>
2 #include <ccan/tap/tap.h>
3
4 #define TEST_STRING     "test string"
5 #define TEST_STRING_2   "abc\0def"
6
7 const char str1[] = TEST_STRING;
8 const char *str2 = TEST_STRING;
9
10 int main(void)
11 {
12         struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
13
14         /* This is how many tests you plan to run */
15         plan_tests(16);
16
17         bs = bytestring(str1, sizeof(str1) - 1);
18         ok1(bs.ptr == str1);
19         ok1(bs.len == (sizeof(str1) - 1));
20
21         bs1 = BYTESTRING(TEST_STRING);
22         ok1(bytestring_eq(bs, bs1));
23
24         bs2 = BYTESTRING(TEST_STRING_2);
25         ok1(bs2.len == 7);
26
27         bs3 = bytestring_from_string(str2);
28         ok1(bytestring_eq(bs3, bs));
29
30         bs4 = bytestring_from_string(TEST_STRING_2);
31         ok1(bs4.len == 3);
32
33         bs5 = bytestring_from_string(NULL);
34         ok1(bs5.len == 0);
35         ok1(bs5.ptr == NULL);
36         ok1(bytestring_eq(bs5, bytestring_NULL));
37
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');
45         
46         /* This exits depending on whether all tests passed */
47         return exit_status();
48 }