forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_hexnumber.c
139 lines (110 loc) · 4.31 KB
/
testmod_hexnumber.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
/**
* @file
*
* @brief Tests for hexnumber plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <stdlib.h>
#include <string.h>
#include <kdbconfig.h>
#include <tests_plugin.h>
#include "hexnumber.h"
#define CREATE_TEST_KEY(HEX) (keyNew ("user:/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "type", "long", KEY_END))
#define CREATE_TEST_KEY_UNITBASE(HEX) (keyNew ("user:/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "unit/base", "hex", KEY_END))
#define CREATE_TEST_KEY_CUSTOM(HEX, TYPE) (keyNew ("user:/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "type", TYPE, KEY_END))
#define CHECK_TEST_KEY(HEX, DEC) succeed_if_same_string (keyString (ksLookupByName (ks, "user:/tests/hexnumber/" #HEX, 0)), #DEC)
static void test_basics (void)
{
Key * parentKey = keyNew ("user:/tests/hexnumber", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("hexnumber");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "call to kdbGet was not successful");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "call to kdbSet was not successful");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
static void test_default (void)
{
Key * parentKey = keyNew ("user:/tests/hexnumber", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("hexnumber");
KeySet * ks = ksNew (30, CREATE_TEST_KEY (0xF), CREATE_TEST_KEY (0xf), CREATE_TEST_KEY (0x14), CREATE_TEST_KEY (0xFFFFFFFFFFFFFFFF),
CREATE_TEST_KEY (0x0), CREATE_TEST_KEY (0x2), CREATE_TEST_KEY (-0x2), CREATE_TEST_KEY (-0x1),
CREATE_TEST_KEY (test), KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
CHECK_TEST_KEY (0xF, 15);
CHECK_TEST_KEY (0xf, 15);
CHECK_TEST_KEY (0x14, 20);
CHECK_TEST_KEY (0xFFFFFFFFFFFFFFFF, 18446744073709551615);
CHECK_TEST_KEY (0x0, 0);
CHECK_TEST_KEY (0x2, 2);
CHECK_TEST_KEY (-0x2, -0x2);
CHECK_TEST_KEY (-0x1, -0x1);
CHECK_TEST_KEY (test, test);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_customint (void)
{
Key * parentKey = keyNew ("user:/tests/hexnumber", KEY_END);
KeySet * conf = ksNew (10, keyNew ("system:/accept/type/#0", KEY_VALUE, "customint", KEY_END),
keyNew ("system:/accept/type/#1", KEY_VALUE, "othercustomint", KEY_END), KS_END);
PLUGIN_OPEN ("hexnumber");
KeySet * ks = ksNew (30, CREATE_TEST_KEY_CUSTOM (0x1F, "customint"), CREATE_TEST_KEY_CUSTOM (0xFF, "othercustomint"),
CREATE_TEST_KEY_CUSTOM (0x22, "string"), CREATE_TEST_KEY_CUSTOM (0x11, "long"), KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
CHECK_TEST_KEY (0x1F, 31);
CHECK_TEST_KEY (0xFF, 255);
CHECK_TEST_KEY (0x22, 0x22);
CHECK_TEST_KEY (0x11, 17);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_unitbase (void)
{
Key * parentKey = keyNew ("user:/tests/hexnumber", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("hexnumber");
KeySet * ks = ksNew (30, CREATE_TEST_KEY_UNITBASE (0x1F), CREATE_TEST_KEY_UNITBASE (0xFF), KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
CHECK_TEST_KEY (0x1F, 31);
CHECK_TEST_KEY (0xFF, 255);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_force (void)
{
Key * parentKey = keyNew ("user:/tests/hexnumber", KEY_END);
KeySet * conf = ksNew (10, keyNew ("system:/integertypes", KEY_VALUE, "customint;othercustomint", KEY_END),
keyNew ("system:/force", KEY_VALUE, "1", KEY_END), KS_END);
PLUGIN_OPEN ("hexnumber");
KeySet * ks = ksNew (30, CREATE_TEST_KEY_CUSTOM (0x1F, "customint"), CREATE_TEST_KEY_CUSTOM (0xFF, "othercustomint"),
CREATE_TEST_KEY_CUSTOM (0x22, "string"), KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
CHECK_TEST_KEY (0x1F, 31);
CHECK_TEST_KEY (0xFF, 255);
CHECK_TEST_KEY (0x22, 34);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("HEXNUMBER TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_basics ();
test_default ();
test_customint ();
test_unitbase ();
test_force ();
print_result ("testmod_hexnumber");
return nbError;
}