This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
testmod_range.c
206 lines (172 loc) · 5.74 KB
/
testmod_range.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @file
*
* @brief Tests for range plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <kdbconfig.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tests_plugin.h>
void testInt (const char * value, int ret, const char * rangeString)
{
Key * parentKey = keyNew ("user:/tests/range", KEY_VALUE, "", KEY_END);
KeySet * ks = ksNew (10, keyNew ("user:/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_END), KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("range");
ksRewind (ks);
int rc = plugin->kdbSet (plugin, ks, parentKey);
// fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
succeed_if (rc == ret, "failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testUInt (const char * value, int ret, const char * rangeString)
{
Key * parentKey = keyNew ("user:/tests/range", KEY_VALUE, "", KEY_END);
KeySet * ks = ksNew (10,
keyNew ("user:/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META,
"check/type", "unsigned long", KEY_END),
KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("range");
ksRewind (ks);
int rc = plugin->kdbSet (plugin, ks, parentKey);
// fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
succeed_if (rc == ret, "failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testFloat (const char * value, int ret, const char * rangeString)
{
Key * parentKey = keyNew ("user:/tests/range", KEY_VALUE, "", KEY_END);
KeySet * ks = ksNew (10,
keyNew ("user:/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META,
"check/type", "float", KEY_END),
KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("range");
ksRewind (ks);
int rc = plugin->kdbSet (plugin, ks, parentKey);
// fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
succeed_if (rc == ret, "failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testHex (const char * value, int ret, const char * rangeString)
{
Key * parentKey = keyNew ("user:/tests/range", KEY_VALUE, "", KEY_END);
KeySet * ks = ksNew (10,
keyNew ("user:/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META,
"check/type", "HEX", KEY_END),
KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("range");
ksRewind (ks);
int rc = plugin->kdbSet (plugin, ks, parentKey);
// fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
succeed_if (rc == ret, "failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testChar (const char * value, int ret, const char * rangeString)
{
Key * parentKey = keyNew ("user:/tests/range", KEY_VALUE, "", KEY_END);
KeySet * ks = ksNew (10,
keyNew ("user:/tests/range/key", KEY_VALUE, value, KEY_META, "check/range", rangeString, KEY_META,
"check/type", "char", KEY_END),
KS_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("range");
ksRewind (ks);
int rc = plugin->kdbSet (plugin, ks, parentKey);
// fprintf (stderr, "testing: value: %s, expected: %d, got: %d, range: %s\n", value, ret, rc, rangeString);
succeed_if (rc == ret, "failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("RANGE TESTS\n");
printf ("==================\n\n");
init (argc, argv);
char * old_locale = elektraStrDup (setlocale (LC_ALL, NULL));
setlocale (LC_ALL, "C");
testInt ("5", 1, "1-10");
testInt ("10", 1, "1-10");
testInt ("1", 1, "1-10");
testInt ("0", -1, "1-10");
testInt ("-5", -1, "1-10");
testInt ("5", -1, "1-4");
testInt ("2", 1, "-1-10");
testInt ("-2", 1, "-3--1");
testInt ("5", 1, " 1 - 10");
testInt ("10", 1, " 1 - 10");
testInt ("1", 1, " 1 - 10");
testInt ("0", -1, "1- 10");
testInt ("-5", -1, "1 -10");
testInt ("5", -1, " 1 - 4 ");
testInt ("2", 1, " - 1 - 10");
testInt ("-2", 1, "-3 --1");
testInt ("-2", 1, "-3 -- 1");
testInt ("-2", 1, "-3 - - 1");
testInt ("-2", 1, "-3-- 1");
testInt ("-2", -1, "--3--1");
testInt ("-2", -1, "-3---1");
testInt ("5", -1, "0-4,6-9");
testInt ("3", 1, "0-4,6-9");
testInt ("7", 1, "0-4,6-9");
testInt ("0", 1, "0-4,6-9");
testInt ("4", 1, "0-4,6-9");
testInt ("6", 1, "0-4,6-9");
testInt ("9", 1, "0-4,6-9");
testInt ("0", 1, "0,1-3");
testInt ("4", 1, "2,3,4,5");
testInt ("6", -1, "1,2,3,4");
testInt ("9", 1, "0-7,8,9");
testInt ("-2", -1, "-31");
testInt ("-6", 1, "1,2,3,4,5,-6");
testInt ("-9", -1, "0-7,-8,9");
testUInt ("4", 1, "1-10");
testUInt ("-5", -1, "1-10");
testUInt ("3", 1, "1-4");
testUInt ("2", -1, "-1-10");
testUInt ("-2", -1, "-3--1");
testFloat ("0.7", 1, "0.1-0.9");
testFloat ("0.7", -1, "0.1-0.5");
testFloat ("0.7", 1, "-0.8-0.9");
testHex ("0A", 1, "00-20");
testHex ("1A", -1, "0A-10");
testHex ("1A", -1, "00-19,1B-20");
testHex ("0A", 1, "00-19,1B-20");
testHex ("1F", 1, "00-19,1B-20");
testChar ("g", -1, "a-f");
testChar ("c", 1, "a-f");
// test edge cases
char number[256];
char range[256];
snprintf (number, 256, "%lld", LLONG_MAX);
snprintf (range, 256, "%lld - %lld", LLONG_MIN, LLONG_MAX);
testInt (number, 1, range);
snprintf (number, 256, "%lld", LLONG_MIN);
testInt (number, 1, range);
snprintf (number, 256, "%llu", ULLONG_MAX);
snprintf (range, 256, "%llu - %llu", 1ULL, ULLONG_MAX);
testUInt (number, 1, range);
snprintf (number, 256, "%llu", 1ULL);
testUInt (number, 1, range);
setlocale (LC_ALL, old_locale);
elektraFree (old_locale);
print_result ("testmod_range");
return nbError;
}