Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for range plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <kdbconfig.h>
11 : #include <limits.h>
12 : #include <locale.h>
13 : #include <stdio.h>
14 : #include <stdlib.h>
15 : #include <string.h>
16 :
17 : #include <tests_plugin.h>
18 :
19 :
20 74 : void testInt (const char * value, int ret, const char * rangeString)
21 : {
22 74 : Key * parentKey = keyNew ("user/tests/range", KEY_VALUE, "", KEY_END);
23 74 : KeySet * ks = ksNew (10, keyNew ("user/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_END), KS_END);
24 74 : KeySet * conf = ksNew (0, KS_END);
25 74 : PLUGIN_OPEN ("range");
26 74 : ksRewind (ks);
27 74 : int rc = plugin->kdbSet (plugin, ks, parentKey);
28 : // fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
29 74 : succeed_if (rc == ret, "failed");
30 74 : ksDel (ks);
31 74 : keyDel (parentKey);
32 74 : PLUGIN_CLOSE ();
33 74 : }
34 :
35 14 : void testUInt (const char * value, int ret, const char * rangeString)
36 : {
37 14 : Key * parentKey = keyNew ("user/tests/range", KEY_VALUE, "", KEY_END);
38 14 : KeySet * ks = ksNew (10,
39 : keyNew ("user/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META, "check/type",
40 : "unsigned long", KEY_END),
41 : KS_END);
42 14 : KeySet * conf = ksNew (0, KS_END);
43 14 : PLUGIN_OPEN ("range");
44 14 : ksRewind (ks);
45 14 : int rc = plugin->kdbSet (plugin, ks, parentKey);
46 : // fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
47 14 : succeed_if (rc == ret, "failed");
48 14 : ksDel (ks);
49 14 : keyDel (parentKey);
50 14 : PLUGIN_CLOSE ();
51 14 : }
52 :
53 6 : void testFloat (const char * value, int ret, const char * rangeString)
54 : {
55 6 : Key * parentKey = keyNew ("user/tests/range", KEY_VALUE, "", KEY_END);
56 6 : KeySet * ks = ksNew (10,
57 : keyNew ("user/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META, "check/type",
58 : "float", KEY_END),
59 : KS_END);
60 6 : KeySet * conf = ksNew (0, KS_END);
61 6 : PLUGIN_OPEN ("range");
62 6 : ksRewind (ks);
63 6 : int rc = plugin->kdbSet (plugin, ks, parentKey);
64 : // fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
65 6 : succeed_if (rc == ret, "failed");
66 6 : ksDel (ks);
67 6 : keyDel (parentKey);
68 6 : PLUGIN_CLOSE ();
69 6 : }
70 :
71 10 : void testHex (const char * value, int ret, const char * rangeString)
72 : {
73 10 : Key * parentKey = keyNew ("user/tests/range", KEY_VALUE, "", KEY_END);
74 10 : KeySet * ks = ksNew (10,
75 : keyNew ("user/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META, "check/type",
76 : "HEX", KEY_END),
77 : KS_END);
78 10 : KeySet * conf = ksNew (0, KS_END);
79 10 : PLUGIN_OPEN ("range");
80 10 : ksRewind (ks);
81 10 : int rc = plugin->kdbSet (plugin, ks, parentKey);
82 : // fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
83 10 : succeed_if (rc == ret, "failed");
84 10 : ksDel (ks);
85 10 : keyDel (parentKey);
86 10 : PLUGIN_CLOSE ();
87 10 : }
88 :
89 4 : void testChar (const char * value, int ret, const char * rangeString)
90 : {
91 4 : Key * parentKey = keyNew ("user/tests/range", KEY_VALUE, "", KEY_END);
92 4 : KeySet * ks = ksNew (10,
93 : keyNew ("user/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META, "check/type",
94 : "char", KEY_END),
95 : KS_END);
96 4 : KeySet * conf = ksNew (0, KS_END);
97 4 : PLUGIN_OPEN ("range");
98 4 : ksRewind (ks);
99 4 : int rc = plugin->kdbSet (plugin, ks, parentKey);
100 : // fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
101 4 : succeed_if (rc == ret, "failed");
102 4 : ksDel (ks);
103 4 : keyDel (parentKey);
104 4 : PLUGIN_CLOSE ();
105 4 : }
106 :
107 2 : int main (int argc, char ** argv)
108 : {
109 2 : printf ("RANGE TESTS\n");
110 2 : printf ("==================\n\n");
111 :
112 2 : init (argc, argv);
113 :
114 2 : char * old_locale = elektraStrDup (setlocale (LC_ALL, NULL));
115 2 : setlocale (LC_ALL, "C");
116 :
117 2 : testInt ("5", 1, "1-10");
118 2 : testInt ("10", 1, "1-10");
119 2 : testInt ("1", 1, "1-10");
120 2 : testInt ("0", -1, "1-10");
121 2 : testInt ("-5", -1, "1-10");
122 2 : testInt ("5", -1, "1-4");
123 :
124 2 : testInt ("2", 1, "-1-10");
125 2 : testInt ("-2", 1, "-3--1");
126 :
127 :
128 2 : testInt ("5", 1, " 1 - 10");
129 2 : testInt ("10", 1, " 1 - 10");
130 2 : testInt ("1", 1, " 1 - 10");
131 2 : testInt ("0", -1, "1- 10");
132 2 : testInt ("-5", -1, "1 -10");
133 2 : testInt ("5", -1, " 1 - 4 ");
134 :
135 2 : testInt ("2", 1, " - 1 - 10");
136 2 : testInt ("-2", 1, "-3 --1");
137 :
138 2 : testInt ("-2", 1, "-3 -- 1");
139 2 : testInt ("-2", 1, "-3 - - 1");
140 2 : testInt ("-2", 1, "-3-- 1");
141 :
142 2 : testInt ("-2", -1, "--3--1");
143 2 : testInt ("-2", -1, "-3---1");
144 :
145 2 : testInt ("5", -1, "0-4,6-9");
146 2 : testInt ("3", 1, "0-4,6-9");
147 2 : testInt ("7", 1, "0-4,6-9");
148 2 : testInt ("0", 1, "0-4,6-9");
149 2 : testInt ("4", 1, "0-4,6-9");
150 2 : testInt ("6", 1, "0-4,6-9");
151 2 : testInt ("9", 1, "0-4,6-9");
152 :
153 2 : testInt ("0", 1, "0,1-3");
154 2 : testInt ("4", 1, "2,3,4,5");
155 2 : testInt ("6", -1, "1,2,3,4");
156 2 : testInt ("9", 1, "0-7,8,9");
157 :
158 2 : testInt ("-2", -1, "-31");
159 2 : testInt ("-6", 1, "1,2,3,4,5,-6");
160 2 : testInt ("-9", -1, "0-7,-8,9");
161 :
162 2 : testUInt ("4", 1, "1-10");
163 2 : testUInt ("-5", -1, "1-10");
164 2 : testUInt ("3", 1, "1-4");
165 :
166 2 : testUInt ("2", -1, "-1-10");
167 2 : testUInt ("-2", -1, "-3--1");
168 :
169 :
170 2 : testFloat ("0.7", 1, "0.1-0.9");
171 2 : testFloat ("0.7", -1, "0.1-0.5");
172 :
173 2 : testFloat ("0.7", 1, "-0.8-0.9");
174 :
175 :
176 2 : testHex ("0A", 1, "00-20");
177 2 : testHex ("1A", -1, "0A-10");
178 2 : testHex ("1A", -1, "00-19,1B-20");
179 2 : testHex ("0A", 1, "00-19,1B-20");
180 2 : testHex ("1F", 1, "00-19,1B-20");
181 :
182 :
183 2 : testChar ("g", -1, "a-f");
184 2 : testChar ("c", 1, "a-f");
185 :
186 : // test edge cases
187 : char number[256];
188 : char range[256];
189 2 : snprintf (number, 256, "%lld", LLONG_MAX);
190 2 : snprintf (range, 256, "%lld - %lld", LLONG_MIN, LLONG_MAX);
191 2 : testInt (number, 1, range);
192 2 : snprintf (number, 256, "%lld", LLONG_MIN);
193 2 : testInt (number, 1, range);
194 :
195 2 : snprintf (number, 256, "%llu", ULLONG_MAX);
196 2 : snprintf (range, 256, "%llu - %llu", 1ULL, ULLONG_MAX);
197 2 : testUInt (number, 1, range);
198 2 : snprintf (number, 256, "%llu", 1ULL);
199 2 : testUInt (number, 1, range);
200 :
201 2 : setlocale (LC_ALL, old_locale);
202 2 : elektraFree (old_locale);
203 2 : print_result ("testmod_range");
204 :
205 2 : return nbError;
206 : }
|