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 <tests_internal.h>
10 :
11 2 : static void test_cmpOrder (void)
12 : {
13 2 : Key * k1 = keyNew ("user/a", KEY_META, "order", "20", KEY_END);
14 2 : Key * k2 = keyNew ("user/b", KEY_META, "order", "10", KEY_END);
15 :
16 2 : succeed_if (elektraKeyCmpOrder (0, 0) == 0, "null keys are not equal");
17 2 : succeed_if (elektraKeyCmpOrder (k1, 0) == 1, "not null key is not greater than null key");
18 2 : succeed_if (elektraKeyCmpOrder (0, k1) == -1, "null key is not smaller than not null key");
19 :
20 2 : succeed_if (elektraKeyCmpOrder (k1, k2) > 0, "user/a is not greater than user/b");
21 2 : succeed_if (elektraKeyCmpOrder (k2, k1) < 0, "user/b is not smaller than user/a");
22 :
23 2 : keySetMeta (k2, "order", "20");
24 2 : succeed_if (elektraKeyCmpOrder (k1, k2) == 0, "keys with same order are not equal");
25 2 : succeed_if (elektraKeyCmpOrder (k2, k1) == 0, "keys with same order are not equal");
26 :
27 2 : keySetMeta (k2, "order", 0);
28 2 : succeed_if (elektraKeyCmpOrder (k1, k2) > 0, "key with metadata is not greater than key without");
29 2 : succeed_if (elektraKeyCmpOrder (k2, k1) < 0, "key with metadata is not greater than key without");
30 :
31 2 : keySetMeta (k1, "order", 0);
32 2 : succeed_if (elektraKeyCmpOrder (k1, k2) == 0, "keys without metadata are not equal");
33 2 : succeed_if (elektraKeyCmpOrder (k2, k1) == 0, "keys without metadata are not equal");
34 :
35 2 : keyDel (k1);
36 2 : keyDel (k2);
37 2 : }
38 :
39 2 : static KeySet * set_a (void)
40 : {
41 2 : return ksNew (16, keyNew ("user/0", KEY_END), keyNew ("user/a", KEY_END), keyNew ("user/a/a", KEY_END),
42 : keyNew ("user/a/a/a", KEY_END), keyNew ("user/a/a/b", KEY_END), keyNew ("user/a/b", KEY_END),
43 : keyNew ("user/a/b/a", KEY_END), keyNew ("user/a/b/b", KEY_END), keyNew ("user/a/c", KEY_END),
44 : keyNew ("user/a/d", KEY_END), keyNew ("user/a/x/a", KEY_END), keyNew ("user/a/x/b", KEY_END),
45 : keyNew ("user/a/x/c", KEY_END), keyNew ("user/a/x/c/a", KEY_END), keyNew ("user/a/x/c/b", KEY_END),
46 : keyNew ("user/x", KEY_END), KS_END);
47 : }
48 :
49 2 : static void test_search (void)
50 : {
51 2 : printf ("Testing operation search (internal)\n");
52 :
53 2 : KeySet * a = set_a ();
54 2 : Key * s = keyNew ("user/a", KEY_END);
55 : ssize_t result;
56 :
57 2 : keySetName (s, "user/0");
58 2 : result = ksSearchInternal (a, s);
59 2 : succeed_if (result == 0, "insertpos wrong");
60 :
61 2 : keySetName (s, "user/a");
62 2 : result = ksSearchInternal (a, s);
63 2 : succeed_if (result == 1, "insertpos wrong");
64 :
65 2 : keySetName (s, "user/a/0");
66 2 : result = ksSearchInternal (a, s);
67 2 : succeed_if (result == -3, "insertpos wrong");
68 :
69 2 : keySetName (s, "user/a/a");
70 2 : result = ksSearchInternal (a, s);
71 2 : succeed_if (result == 2, "insertpos wrong");
72 :
73 2 : keySetName (s, "user/a/a/a");
74 2 : result = ksSearchInternal (a, s);
75 2 : succeed_if (result == 3, "insertpos wrong");
76 :
77 2 : keySetName (s, "user/a/a/b");
78 2 : result = ksSearchInternal (a, s);
79 2 : succeed_if (result == 4, "insertpos wrong");
80 :
81 2 : keySetName (s, "user/a/b");
82 2 : result = ksSearchInternal (a, s);
83 2 : succeed_if (result == 5, "insertpos wrong");
84 :
85 2 : keySetName (s, "user/a/b/a");
86 2 : result = ksSearchInternal (a, s);
87 2 : succeed_if (result == 6, "insertpos wrong");
88 :
89 2 : keySetName (s, "user/a/b/b");
90 2 : result = ksSearchInternal (a, s);
91 2 : succeed_if (result == 7, "insertpos wrong");
92 :
93 2 : keySetName (s, "user/a/c");
94 2 : result = ksSearchInternal (a, s);
95 2 : succeed_if (result == 8, "insertpos wrong");
96 :
97 2 : keySetName (s, "user/a/d");
98 2 : result = ksSearchInternal (a, s);
99 2 : succeed_if (result == 9, "insertpos wrong");
100 :
101 2 : keySetName (s, "user/a/x");
102 2 : result = ksSearchInternal (a, s);
103 2 : succeed_if (result == -11, "insertpos wrong");
104 :
105 2 : keySetName (s, "user/a/x/a");
106 2 : result = ksSearchInternal (a, s);
107 2 : succeed_if (result == 10, "insertpos wrong");
108 :
109 2 : keySetName (s, "user/a/x/b");
110 2 : result = ksSearchInternal (a, s);
111 2 : succeed_if (result == 11, "insertpos wrong");
112 :
113 2 : keySetName (s, "user/a/x/c");
114 2 : result = ksSearchInternal (a, s);
115 2 : succeed_if (result == 12, "insertpos wrong");
116 :
117 2 : keySetName (s, "user/a/x/c/a");
118 2 : result = ksSearchInternal (a, s);
119 2 : succeed_if (result == 13, "insertpos wrong");
120 :
121 2 : keySetName (s, "user/a/x/c/b");
122 2 : result = ksSearchInternal (a, s);
123 2 : succeed_if (result == 14, "insertpos wrong");
124 :
125 2 : keySetName (s, "user/x");
126 2 : result = ksSearchInternal (a, s);
127 2 : succeed_if (result == 15, "insertpos wrong");
128 :
129 : /*
130 : Generation of new test cases:
131 : for (int i=0; i< 16; ++i)
132 : {
133 : s = a->array[i];
134 : printf ("keySetName (s, \"%s\");\n", keyName(s));
135 : printf ("result = ksSearchInternal (a, s);\n");
136 : printf ("succeed_if (result == %zd, \"insertpos wrong\");\n\n", ksSearchInternal (a, s));
137 : }
138 : */
139 :
140 2 : keyDel (s);
141 2 : ksDel (a);
142 2 : }
143 :
144 2 : static void test_format (void)
145 : {
146 2 : printf ("Test key format\n");
147 :
148 2 : Key * k = keyNew (0);
149 2 : keySetString (k, "huhu");
150 2 : succeed_if_same_string (keyString (k), "huhu");
151 :
152 2 : keySetStringF (k, "huhu");
153 2 : succeed_if_same_string (keyString (k), "huhu");
154 :
155 2 : keySetStringF (k, "huhu %d", 20);
156 2 : succeed_if_same_string (keyString (k), "huhu 20");
157 :
158 2 : char c1[] = "huhu %d something";
159 2 : keySetStringF (k, c1, 20);
160 2 : c1[5] = '2';
161 2 : c1[6] = '0';
162 2 : succeed_if_same_string (keyString (k), c1);
163 2 : succeed_if (keyGetValueSize (k) == sizeof (c1), "size wrong");
164 :
165 :
166 2 : char c2[] =
167 : "An extremely long string that is way longer then default capture size of 512 or something."
168 : "an extremely long string that is way longer then default capture size of 512 or something."
169 : "an extremely long string that is way longer then default capture size of 512 or something."
170 : "an extremely long string that is way longer then default capture size of 512 or something."
171 : "an extremely long string that is way longer then default capture size of 512 or something."
172 : "an extremely long string that is way longer then default capture size of 512 or something."
173 : "an extremely long string that is way longer then default capture size of 512 or something."
174 : "an extremely long string that is way longer then default capture size of 512 or something!";
175 2 : keySetStringF (k, c2);
176 2 : succeed_if_same_string (keyString (k), c2);
177 2 : succeed_if (keyGetValueSize (k) == sizeof (c2), "size wrong");
178 :
179 :
180 2 : char c3[] =
181 : "%s extremely long string that is way longer then default capture size of 512 or something."
182 : "an extremely long string that is way longer then default capture size of 512 or something."
183 : "an extremely long string that is way longer then default capture size of 512 or something."
184 : "an extremely long string that is way longer then default capture size of 512 or something."
185 : "an extremely long string that is way longer then default capture size of 512 or something."
186 : "an extremely long string that is way longer then default capture size of 512 or something."
187 : "an extremely long string that is way longer then default capture size of 512 or something."
188 : "an extremely long string that is way longer then default capture size of 512 or something!";
189 2 : keySetStringF (k, c3, "AN");
190 2 : c3[0] = 'A';
191 2 : c3[1] = 'N';
192 2 : succeed_if_same_string (keyString (k), c3);
193 : // printf ("%s\n\nXXX\n%s\n", keyString(k), c3);
194 : // printf ("%d - %d\n", keyGetValueSize(k), sizeof(c3));
195 2 : succeed_if (keyGetValueSize (k) == sizeof (c3), "size wrong");
196 :
197 :
198 2 : char c4[] =
199 : "%d extremely long string that is way longer then default capture size of 512 or something."
200 : "an extremely long string that is way longer then default capture size of 512 or something."
201 : "an extremely long string that is way longer then default capture size of 512 or something."
202 : "an extremely long string that is way longer then default capture size of 512 or something."
203 : "an extremely long string that is way longer then default capture size of 512 or something."
204 : "an extremely long string that is way longer then default capture size of 512 or something."
205 : "an extremely long string that is way longer then default capture size of 512 or something."
206 : "an extremely long string that is way longer then default capture size of 512 or something!";
207 2 : keySetStringF (k, c4, 20);
208 2 : c4[0] = '2';
209 2 : c4[1] = '0';
210 2 : succeed_if_same_string (keyString (k), c4);
211 2 : succeed_if (keyGetValueSize (k) == sizeof (c4), "size wrong");
212 :
213 2 : keyDel (k);
214 2 : }
215 :
216 2 : int main (int argc, char ** argv)
217 : {
218 2 : printf ("OPERATION TESTS\n");
219 2 : printf ("==================\n\n");
220 :
221 2 : init (argc, argv);
222 :
223 2 : test_search ();
224 2 : test_cmpOrder ();
225 2 : test_format ();
226 :
227 2 : printf ("\ntest_operation RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
228 :
229 2 : return nbError;
230 : }
|