aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common/check_sort.c
blob: f49710cfb35807e9462ced2144bb2d29cc82eadd (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include "../../src/common/utils.h"
#include "../../src/common/sort.h"


static int
sort_func(void *a, void *b)
{
    return strcmp((char*) a, (char*) b);
}


static void
test_slist_sort_empty(void **state)
{
    bc_slist_t *l = NULL;
    assert_null(bc_slist_sort(l, (bc_sort_func_t) sort_func));
}


static void
test_slist_sort_single(void **state)
{
    bc_slist_t *l = NULL;
    l = bc_slist_append(l, bc_strdup("a"));

    l = bc_slist_sort(l, (bc_sort_func_t) sort_func);

    assert_non_null(l);
    assert_string_equal(l->data, "a");
    assert_null(l->next);

    bc_slist_free_full(l, free);
}


static void
test_slist_sort_sorted(void **state)
{
    bc_slist_t *l = NULL;
    l = bc_slist_append(l, bc_strdup("a"));
    l = bc_slist_append(l, bc_strdup("b"));
    l = bc_slist_append(l, bc_strdup("c"));

    l = bc_slist_sort(l, (bc_sort_func_t) sort_func);

    assert_non_null(l);
    assert_string_equal(l->data, "a");
    assert_string_equal(l->next->data, "b");
    assert_string_equal(l->next->next->data, "c");
    assert_null(l->next->next->next);

    bc_slist_free_full(l, free);
}


static void
test_slist_sort_reverse(void **state)
{
    bc_slist_t *l = NULL;
    l = bc_slist_append(l, bc_strdup("d"));
    l = bc_slist_append(l, bc_strdup("c"));
    l = bc_slist_append(l, bc_strdup("b"));
    l = bc_slist_append(l, bc_strdup("a"));

    l = bc_slist_sort(l, (bc_sort_func_t) sort_func);

    assert_non_null(l);
    assert_string_equal(l->data, "a");
    assert_string_equal(l->next->data, "b");
    assert_string_equal(l->next->next->data, "c");
    assert_string_equal(l->next->next->next->data, "d");
    assert_null(l->next->next->next->next);

    bc_slist_free_full(l, free);
}


static void
test_slist_sort_mixed1(void **state)
{
    bc_slist_t *l = NULL;
    l = bc_slist_append(l, bc_strdup("a"));
    l = bc_slist_append(l, bc_strdup("d"));
    l = bc_slist_append(l, bc_strdup("c"));
    l = bc_slist_append(l, bc_strdup("b"));

    l = bc_slist_sort(l, (bc_sort_func_t) sort_func);

    assert_non_null(l);
    assert_string_equal(l->data, "a");
    assert_string_equal(l->next->data, "b");
    assert_string_equal(l->next->next->data, "c");
    assert_string_equal(l->next->next->next->data, "d");
    assert_null(l->next->next->next->next);

    bc_slist_free_full(l, free);
}


static void
test_slist_sort_mixed2(void **state)
{
    bc_slist_t *l = NULL;
    l = bc_slist_append(l, bc_strdup("c"));
    l = bc_slist_append(l, bc_strdup("b"));
    l = bc_slist_append(l, bc_strdup("a"));
    l = bc_slist_append(l, bc_strdup("d"));

    l = bc_slist_sort(l, (bc_sort_func_t) sort_func);

    assert_non_null(l);
    assert_string_equal(l->data, "a");
    assert_string_equal(l->next->data, "b");
    assert_string_equal(l->next->next->data, "c");
    assert_string_equal(l->next->next->next->data, "d");
    assert_null(l->next->next->next->next);

    bc_slist_free_full(l, free);
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_slist_sort_empty),
        unit_test(test_slist_sort_single),
        unit_test(test_slist_sort_sorted),
        unit_test(test_slist_sort_reverse),
        unit_test(test_slist_sort_mixed1),
        unit_test(test_slist_sort_mixed2),
    };
    return run_tests(tests);
}