File: libcompat_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (133 lines) | stat: -rw-r--r-- 3,958 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <test.h>

#include <cmockery.h>
#include <string.h>


/* Include the tested functions directly from libcompat! */
#include <memmem.c>

#define TEST_SNPRINTF 1
#include <snprintf.c>


/* TODO TEST MORE OF OUR libcompat REPLACEMENTS! */


static void test_memmem()
{
    char *result, *needle, *haystack;

    /* Can't find anything in nothing. */
    needle = "whatever";
    result = memmem("EMPTY", 0, needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* Even nothing is not in nothing. */
    result = memmem("EMPTY", 0, "EMPTY", 0);
    assert_int_equal(result, NULL);

    /* Nothing can be found in anything. */
    haystack = "blah";
    result = memmem(haystack, strlen(haystack), "EMPTY", 0);
    assert_int_equal(result, &haystack[0]);

    /* Find something when exact match. */
    haystack = "blah";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[0]);

    /* Find something when at the beginning. */
    haystack = "blah123";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[0]);

    /* Find something when in the middle. */
    haystack = "123blah123";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[3]);

    /* Find something when in the end. */
    haystack = "12345blah";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[5]);

    /* Partial match is not a match, part 1. */
    haystack = "12345bla";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* Partial match is not a match, part 2. */
    haystack = "bla";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* Partial match is not a match, part 3. */
    haystack = "bla123";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* All the right letters, in the right order, not contiguous. */
    haystack = "bleach";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* Should not be affected by case. */
    haystack = "BLAH";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, NULL);

    /* Don't jump forward too much on incomplete match. */
    haystack = "bblblablah";
    needle   = "blah";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[6]);

    /* Don't jump forward too much, part 2. */
    haystack = "abcabcabcd";
    needle   = "abcabcd";
    result = memmem(haystack, strlen(haystack), needle, strlen(needle));
    assert_int_equal(result, &haystack[3]);
}


static void test_snprintf()
{
    int failures = snprintf_rigorous_test();

    if (failures > 0)
    {
        puts("\n"
            "=== WARNING your system's printf() generates different results ===\n"
            "=== than our printf() in libcompat! ===\n"
            "=== This is not a necessarily a bug, since this test has lots ===\n"
            "=== of requirements to run properly, so testsuite result is not affected. ===\n"
            "=== However it might indicate that printf() in your system's libc is buggy. ===\n"
            );
    }
}


int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_memmem),
        unit_test(test_snprintf)
        /* TODO test rpl_fprintf() outputs short and long strings correctly to a file. */
    };

    int ret = run_tests(tests);

    return ret;
}