Debian Codegraph @

debian/tests/api

raw
1 #!/bin/sh
2 set -e
3 cd "$AUTOPKGTEST_TMP"
4
5 cat > api_test.c << 'EOF'
6 #include <bstrlib.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #define CHECK(cond, msg) \
11 do { if (!(cond)) { fprintf(stderr, "FAIL: %s\n", msg); exit(1); } } while(0)
12
13 int main(void) {
14 /* creation and length */
15 bstring b = bfromcstr("hello");
16 CHECK(b != NULL, "bfromcstr returned NULL");
17 CHECK(blength(b) == 5, "length of 'hello' should be 5");
18
19 /* concatenation */
20 CHECK(bcatcstr(b, " world") == BSTR_OK, "bcatcstr failed");
21 CHECK(blength(b) == 11, "length after concat should be 11");
22 CHECK(biseqcstr(b, "hello world") == 1, "biseqcstr mismatch after concat");
23
24 /* copy */
25 bstring copy = bstrcpy(b);
26 CHECK(copy != NULL, "bstrcpy returned NULL");
27 CHECK(biseq(b, copy) == 1, "copy should equal original");
28
29 /* substring */
30 bstring sub = bmidstr(b, 6, 5);
31 CHECK(sub != NULL, "bmidstr returned NULL");
32 CHECK(biseqcstr(sub, "world") == 1, "substring should be 'world'");
33
34 /* split */
35 bstring sentence = bfromcstr("one,two,three");
36 struct bstrList *parts = bsplit(sentence, ',');
37 CHECK(parts != NULL, "bsplit returned NULL");
38 CHECK(parts->qty == 3, "bsplit should produce 3 parts");
39 CHECK(biseqcstr(parts->entry[0], "one") == 1, "part[0] should be 'one'");
40 CHECK(biseqcstr(parts->entry[2], "three") == 1, "part[2] should be 'three'");
41
42 /* case conversion */
43 bstring upper = bstrcpy(b);
44 CHECK(btoupper(upper) == BSTR_OK, "btoupper failed");
45 CHECK(biseqcstr(upper, "HELLO WORLD") == 1, "uppercase mismatch");
46
47 /* find/replace */
48 bstring haystack = bfromcstr("aabbaabb");
49 bstring needle = bfromcstr("aa");
50 bstring repl = bfromcstr("x");
51 CHECK(bfindreplace(haystack, needle, repl, 0) == BSTR_OK, "bfindreplace failed");
52 CHECK(biseqcstr(haystack, "xbbxbb") == 1, "bfindreplace result mismatch");
53
54 /* search */
55 bstring hay2 = bfromcstr("foobar");
56 bstring needle2 = bfromcstr("bar");
57 CHECK(binstr(hay2, 0, needle2) == 3, "binstr should find 'bar' at pos 3");
58
59 bdestroy(b);
60 bdestroy(copy);
61 bdestroy(sub);
62 bdestroy(sentence);
63 bdestroy(upper);
64 bdestroy(haystack);
65 bdestroy(needle);
66 bdestroy(repl);
67 bdestroy(hay2);
68 bdestroy(needle2);
69 bstrListDestroy(parts);
70
71 puts("api PASS");
72 return 0;
73 }
74 EOF
75
76 gcc -o api_test api_test.c $(pkg-config --cflags --libs bstring)
77 ./api_test
78 echo "bstring api test PASS"
79