]> git.ozlabs.org Git - ccan/blob - ccan/bytestring/test/run.c
bytestring: Add bytestring_index and bytestring_rindex() functions
[ccan] / ccan / bytestring / test / run.c
1 #include "config.h"
2
3 #include <ccan/bytestring/bytestring.h>
4 #include <ccan/tap/tap.h>
5
6 #define TEST_STRING     "test string"
7 #define TEST_STRING_2   "abc\0def"
8
9 const char str1[] = TEST_STRING;
10 const char *str2 = TEST_STRING;
11
12 int main(void)
13 {
14         struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
15
16         /* This is how many tests you plan to run */
17         plan_tests(42);
18
19         bs = bytestring(str1, sizeof(str1) - 1);
20         ok1(bs.ptr == str1);
21         ok1(bs.len == (sizeof(str1) - 1));
22
23         bs1 = BYTESTRING(TEST_STRING);
24         ok1(bytestring_eq(bs, bs1));
25
26         bs2 = BYTESTRING(TEST_STRING_2);
27         ok1(bs2.len == 7);
28
29         bs3 = bytestring_from_string(str2);
30         ok1(bytestring_eq(bs3, bs));
31
32         bs4 = bytestring_from_string(TEST_STRING_2);
33         ok1(bs4.len == 3);
34
35         bs5 = bytestring_from_string(NULL);
36         ok1(bs5.len == 0);
37         ok1(bs5.ptr == NULL);
38         ok1(bytestring_eq(bs5, bytestring_NULL));
39
40         ok1(bytestring_byte(bs2, 0) == 'a');
41         ok1(bytestring_byte(bs2, 1) == 'b');
42         ok1(bytestring_byte(bs2, 2) == 'c');
43         ok1(bytestring_byte(bs2, 3) == '\0');
44         ok1(bytestring_byte(bs2, 4) == 'd');
45         ok1(bytestring_byte(bs2, 5) == 'e');
46         ok1(bytestring_byte(bs2, 6) == 'f');
47
48         ok1(bytestring_eq(bytestring_slice(bs, 0, 4), BYTESTRING("test")));
49         ok1(bytestring_eq(bytestring_slice(bs, 5, 8), BYTESTRING("str")));
50         ok1(bytestring_eq(bytestring_slice(bs2, 2, 5), BYTESTRING("c\0d")));
51         ok1(bytestring_eq(bytestring_slice(bs2, 0, -1U), bs2));
52         ok1(bytestring_eq(bytestring_slice(bs2, 10, 20), bytestring_NULL));
53         ok1(bytestring_eq(bytestring_slice(bs2, 2, 1), bytestring_NULL));
54
55         ok1(bytestring_starts(bs, BYTESTRING("test")));
56         ok1(bytestring_ends(bs, BYTESTRING("string")));
57         ok1(bytestring_starts(bs2, BYTESTRING("abc")));
58         ok1(bytestring_starts(bs2, BYTESTRING("abc\0")));
59         ok1(bytestring_ends(bs2, BYTESTRING("def")));
60         ok1(bytestring_ends(bs2, BYTESTRING("\0def")));
61         ok1(!bytestring_starts(bs2, BYTESTRING("def")));
62         ok1(!bytestring_ends(bs2, BYTESTRING("abc")));
63
64         ok1(bytestring_index(bs1, ' ') == (bs1.ptr + 4));
65         ok1(bytestring_index(bs1, 't') == (bs1.ptr + 0));
66         ok1(bytestring_index(bs1, 0) == NULL);
67         ok1(bytestring_index(bs2, 0) == (bs2.ptr + 3));
68         ok1(bytestring_index(bs2, 'f') == (bs2.ptr + 6));
69         ok1(bytestring_index(bs2, 'q') == NULL);
70
71         ok1(bytestring_rindex(bs1, ' ') == (bs1.ptr + 4));
72         ok1(bytestring_rindex(bs1, 't') == (bs1.ptr + 6));
73         ok1(bytestring_rindex(bs1, 0) == NULL);
74         ok1(bytestring_rindex(bs2, 0) == (bs2.ptr + 3));
75         ok1(bytestring_rindex(bs2, 'f') == (bs2.ptr + 6));
76         ok1(bytestring_rindex(bs2, 'q') == NULL);
77
78         /* This exits depending on whether all tests passed */
79         return exit_status();
80 }