Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <../../src/libs/elektra/backend.c>
10 : #include <../../src/libs/elektra/mount.c>
11 : #include <../../src/libs/elektra/split.c>
12 : #include <../../src/libs/elektra/trie.c>
13 : #include <tests_internal.h>
14 :
15 0 : KeySet * modules_config (void)
16 : {
17 0 : return ksNew (5, keyNew ("system/elektra/modules", KEY_END), KS_END);
18 : }
19 :
20 :
21 0 : KeySet * simple_config (void)
22 : {
23 0 : return ksNew (5, keyNew ("system/elektra/mountpoints", KEY_END), keyNew ("system/elektra/mountpoints/root", KEY_END),
24 : keyNew ("system/elektra/mountpoints/root/mountpoint", KEY_VALUE, "", KEY_END),
25 : keyNew ("system/elektra/mountpoints/simple", KEY_END),
26 : keyNew ("system/elektra/mountpoints/simple/mountpoint", KEY_VALUE, "user/tests/simple", KEY_END), KS_END);
27 : }
28 :
29 :
30 0 : KeySet * set_us (void)
31 : {
32 0 : return ksNew (50, keyNew ("system/elektra/mountpoints", KEY_END), keyNew ("system/elektra/mountpoints/user", KEY_END),
33 : keyNew ("system/elektra/mountpoints/user/mountpoint", KEY_VALUE, "user", KEY_END),
34 : keyNew ("system/elektra/mountpoints/system", KEY_END),
35 : keyNew ("system/elektra/mountpoints/system/mountpoint", KEY_VALUE, "system", KEY_END), KS_END);
36 : }
37 :
38 :
39 0 : KeySet * root_config (void)
40 : {
41 0 : return ksNew (5, keyNew ("system/elektra/mountpoints", KEY_END), keyNew ("system/elektra/mountpoints/root", KEY_END),
42 : keyNew ("system/elektra/mountpoints/root/mountpoint", KEY_VALUE, "/", KEY_END), KS_END);
43 : }
44 :
45 :
46 2 : static void test_create (void)
47 : {
48 2 : printf ("Test create split\n");
49 :
50 2 : Split * split = splitNew ();
51 2 : succeed_if (split->size == 0, "size should be zero");
52 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "alloc not correct");
53 :
54 2 : succeed_if (split->keysets, "did not alloc keysets array");
55 2 : succeed_if (split->handles, "did not alloc handles array");
56 :
57 32 : for (size_t i = 1; i <= APPROXIMATE_NR_OF_BACKENDS; ++i)
58 : {
59 32 : splitAppend (split, 0, 0, 0);
60 32 : succeed_if (split->size == i, "size should be growing");
61 32 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "should not realloc");
62 : }
63 :
64 2 : splitDel (split);
65 2 : }
66 :
67 2 : static void test_resize (void)
68 : {
69 2 : printf ("Test resize split\n");
70 :
71 2 : Split * split = splitNew ();
72 :
73 2 : exit_if_fail (split, "there must be a split");
74 :
75 2 : succeed_if (split->size == 0, "size should be zero");
76 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "initial size not correct");
77 :
78 2 : splitResize (split);
79 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS * 2, "resize not correct");
80 :
81 2 : splitResize (split);
82 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS * 4, "resize not correct");
83 :
84 2 : splitResize (split);
85 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS * 8, "resize not correct");
86 :
87 2 : splitDel (split);
88 2 : }
89 :
90 2 : static void test_append (void)
91 : {
92 2 : printf ("Test append split\n");
93 :
94 2 : Split * split = splitNew ();
95 2 : exit_if_fail (split, "there must be a split");
96 :
97 2 : succeed_if (split->size == 0, "size should be zero");
98 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "initial size not correct");
99 :
100 32 : for (size_t i = 1; i <= APPROXIMATE_NR_OF_BACKENDS; ++i)
101 : {
102 32 : splitAppend (split, 0, 0, 0);
103 32 : succeed_if (split->size == i, "size should be growing");
104 32 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "should not realloc");
105 : }
106 :
107 32 : for (size_t i = APPROXIMATE_NR_OF_BACKENDS + 1; i <= APPROXIMATE_NR_OF_BACKENDS * 2; ++i)
108 : {
109 32 : splitAppend (split, 0, 0, 0);
110 32 : succeed_if (split->size == i, "size should be growing");
111 32 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS * 2, "should realloc");
112 : }
113 :
114 2 : splitDel (split);
115 2 : }
116 :
117 2 : static void test_remove (void)
118 : {
119 2 : printf ("Test remove from split\n");
120 :
121 2 : Split * split = splitNew ();
122 2 : exit_if_fail (split, "there must be a split");
123 :
124 2 : succeed_if (split->size == 0, "size should be zero");
125 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "initial size not correct");
126 :
127 34 : for (size_t i = 0; i < APPROXIMATE_NR_OF_BACKENDS; ++i)
128 : {
129 32 : splitAppend (split, 0, 0, i);
130 32 : succeed_if (split->size == i + 1, "size should be growing");
131 32 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "should not realloc");
132 : }
133 :
134 2 : splitRemove (split, 3);
135 2 : succeed_if ((int) split->syncbits[0] == 0, "syncbits not correct");
136 2 : succeed_if ((int) split->syncbits[1] == 1, "syncbits not correct");
137 2 : succeed_if ((int) split->syncbits[2] == 2, "syncbits not correct");
138 2 : succeed_if ((int) split->syncbits[3] == 4, "did not remove third?");
139 2 : succeed_if ((int) split->syncbits[4] == 5, "did not remove third?");
140 :
141 2 : splitAppend (split, 0, 0, 100);
142 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS, "should not realloc");
143 2 : splitAppend (split, 0, 0, 200);
144 2 : succeed_if (split->alloc == APPROXIMATE_NR_OF_BACKENDS * 2, "should realloc");
145 :
146 2 : splitRemove (split, 3);
147 2 : succeed_if ((int) split->syncbits[0] == 0, "syncbits not correct");
148 2 : succeed_if ((int) split->syncbits[1] == 1, "syncbits not correct");
149 2 : succeed_if ((int) split->syncbits[2] == 2, "syncbits not correct");
150 2 : succeed_if ((int) split->syncbits[3] == 5, "did not remove third (again)?");
151 2 : succeed_if ((int) split->syncbits[4] == 6, "did not remove third (again)?");
152 :
153 2 : splitRemove (split, 0);
154 2 : succeed_if ((int) split->syncbits[0] == 1, "did not remove zeroth?");
155 2 : succeed_if ((int) split->syncbits[1] == 2, "did not remove zeroth?");
156 2 : succeed_if ((int) split->syncbits[2] == 5, "did not remove zeroth?");
157 2 : succeed_if ((int) split->syncbits[3] == 6, "did not remove zeroth?");
158 2 : succeed_if ((int) split->syncbits[4] == 7, "did not remove zeroth?");
159 :
160 2 : splitDel (split);
161 2 : }
162 :
163 :
164 2 : int main (int argc, char ** argv)
165 : {
166 2 : printf ("SPLIT TESTS\n");
167 2 : printf ("==================\n\n");
168 :
169 2 : init (argc, argv);
170 :
171 2 : test_create ();
172 2 : test_resize ();
173 2 : test_append ();
174 2 : test_remove ();
175 :
176 2 : printf ("\ntest_split RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
177 :
178 2 : return nbError;
179 : }
|