File: item_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 (124 lines) | stat: -rw-r--r-- 3,087 bytes parent folder | download
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
#include <test.h>

#include <item_lib.h>
#include <eval_context.h>

static void test_prepend_item(void)
{
    Item *ip = NULL, *list = NULL;

    ip = PrependItem(&list, "hello", "classes");
    assert_int_not_equal(ip, NULL);
    assert_int_not_equal(list, NULL);
    DeleteItem(&list, ip);
    assert_int_equal(list, NULL);
}

static void test_list_len(void)
{
    Item *list = NULL;

    PrependItem(&list, "one", "classes");
    PrependItem(&list, "two", NULL);
    PrependItem(&list, "three", NULL);
    assert_int_equal(ListLen(list), 3);
    DeleteItemList(list);
}

/* FIXME: those functions are now internal to cf-agent */
#if 0
static void test_list_select_last_matching_finds_first(void)
{
    EvalContext *ctx = EvalContextNew();
    Item *list = NULL, *match = NULL, *prev = NULL;
    bool result = false;

    AppendItem(&list, "abc", NULL);
    AppendItem(&list, "def", NULL);
    AppendItem(&list, "ghi", NULL);
    AppendItem(&list, "jkl", NULL);

    result = SelectLastItemMatching(ctx, "abc", list, NULL, &match, &prev);
    assert_true(result);
    assert_int_equal(match, list);
    assert_int_equal(prev, CF_UNDEFINED_ITEM);
    DeleteItemList(list);
    EvalContextDestroy(ctx);
}

static void test_list_select_last_matching_finds_last(void)
{
    EvalContext *ctx = EvalContextNew();
    Item *list = NULL, *match = NULL, *prev = NULL;
    bool result;

    AppendItem(&list, "abc", NULL);
    AppendItem(&list, "def", NULL);
    AppendItem(&list, "ghi", NULL);
    AppendItem(&list, "abc", NULL);

    result = SelectLastItemMatching(ctx, "abc", list, NULL, &match, &prev);
    assert_true(result);
    assert_int_equal(match, list->next->next->next);
    assert_int_equal(prev, list->next->next);
    DeleteItemList(list);
    EvalContextDestroy(ctx);
}

static void test_list_select_last_matching_not_found(void)
{
    EvalContext *ctx = EvalContextNew();
    Item *list = NULL, *match = NULL, *prev = NULL;
    bool result;

    AppendItem(&list, "abc", NULL);
    AppendItem(&list, "def", NULL);
    AppendItem(&list, "ghi", NULL);
    AppendItem(&list, "abc", NULL);

    result = SelectLastItemMatching(ctx, "xyz", list, NULL, &match, &prev);
    assert_false(result);
    assert_int_equal(match, CF_UNDEFINED_ITEM);
    assert_int_equal(prev, CF_UNDEFINED_ITEM);
    DeleteItemList(list);
    EvalContextDestroy(ctx);
}
#endif

static void test_list_compare(void)
{
    Item *list1 = NULL, *list2 = NULL;
    bool result;

    result = ListsCompare(list1, list2);
    assert_true(result);

    AppendItem(&list1, "abc", NULL);
    AppendItem(&list1, "def", NULL);

    result = ListsCompare(list1, list2);
    assert_false(result);

    AppendItem(&list2, "def", NULL);
    AppendItem(&list2, "abc", NULL);

    result = ListsCompare(list1, list2);

    assert_true(result);

    DeleteItemList(list1);
    DeleteItemList(list2);
}

int main()
{
    PRINT_TEST_BANNER();
    const UnitTest tests[] =
    {
        unit_test(test_prepend_item),
        unit_test(test_list_len),
        unit_test(test_list_compare)
    };

    return run_tests(tests);
}