1 #include <test.h>
2
3 #include <cmockery.h>
4 #include <string.h>
5
6
7 /* Include the tested functions directly from libcompat! */
8 #include <memmem.c>
9
10 #define TEST_SNPRINTF 1
11 #include <snprintf.c>
12
13
14 /* TODO TEST MORE OF OUR libcompat REPLACEMENTS! */
15
16
test_memmem()17 static void test_memmem()
18 {
19 char *result, *needle, *haystack;
20
21 /* Can't find anything in nothing. */
22 needle = "whatever";
23 result = memmem("EMPTY", 0, needle, strlen(needle));
24 assert_int_equal(result, NULL);
25
26 /* Even nothing is not in nothing. */
27 result = memmem("EMPTY", 0, "EMPTY", 0);
28 assert_int_equal(result, NULL);
29
30 /* Nothing can be found in anything. */
31 haystack = "blah";
32 result = memmem(haystack, strlen(haystack), "EMPTY", 0);
33 assert_int_equal(result, &haystack[0]);
34
35 /* Find something when exact match. */
36 haystack = "blah";
37 needle = "blah";
38 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
39 assert_int_equal(result, &haystack[0]);
40
41 /* Find something when at the beginning. */
42 haystack = "blah123";
43 needle = "blah";
44 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
45 assert_int_equal(result, &haystack[0]);
46
47 /* Find something when in the middle. */
48 haystack = "123blah123";
49 needle = "blah";
50 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
51 assert_int_equal(result, &haystack[3]);
52
53 /* Find something when in the end. */
54 haystack = "12345blah";
55 needle = "blah";
56 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
57 assert_int_equal(result, &haystack[5]);
58
59 /* Partial match is not a match, part 1. */
60 haystack = "12345bla";
61 needle = "blah";
62 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
63 assert_int_equal(result, NULL);
64
65 /* Partial match is not a match, part 2. */
66 haystack = "bla";
67 needle = "blah";
68 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
69 assert_int_equal(result, NULL);
70
71 /* Partial match is not a match, part 3. */
72 haystack = "bla123";
73 needle = "blah";
74 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
75 assert_int_equal(result, NULL);
76
77 /* All the right letters, in the right order, not contiguous. */
78 haystack = "bleach";
79 needle = "blah";
80 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
81 assert_int_equal(result, NULL);
82
83 /* Should not be affected by case. */
84 haystack = "BLAH";
85 needle = "blah";
86 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
87 assert_int_equal(result, NULL);
88
89 /* Don't jump forward too much on incomplete match. */
90 haystack = "bblblablah";
91 needle = "blah";
92 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
93 assert_int_equal(result, &haystack[6]);
94
95 /* Don't jump forward too much, part 2. */
96 haystack = "abcabcabcd";
97 needle = "abcabcd";
98 result = memmem(haystack, strlen(haystack), needle, strlen(needle));
99 assert_int_equal(result, &haystack[3]);
100 }
101
102
test_snprintf()103 static void test_snprintf()
104 {
105 int failures = snprintf_rigorous_test();
106
107 if (failures > 0)
108 {
109 puts("\n"
110 "=== WARNING your system's printf() generates different results ===\n"
111 "=== than our printf() in libcompat! ===\n"
112 "=== This is not a necessarily a bug, since this test has lots ===\n"
113 "=== of requirements to run properly, so testsuite result is not affected. ===\n"
114 "=== However it might indicate that printf() in your system's libc is buggy. ===\n"
115 );
116 }
117 }
118
119
main()120 int main()
121 {
122 PRINT_TEST_BANNER();
123 const UnitTest tests[] =
124 {
125 unit_test(test_memmem),
126 unit_test(test_snprintf)
127 /* TODO test rpl_fprintf() outputs short and long strings correctly to a file. */
128 };
129
130 int ret = run_tests(tests);
131
132 return ret;
133 }
134