Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for rgbcolor plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <kdbconfig.h>
11 : #include <kdbtypes.h>
12 : #include <stdio.h>
13 : #include <stdlib.h>
14 : #include <string.h>
15 :
16 : #include <tests_plugin.h>
17 :
18 22 : static void test_normalize_color (const char * color, kdb_unsigned_long_t colorValue)
19 : {
20 22 : Key * parentKey = keyNew ("user/tests/rgbcolor", KEY_END);
21 22 : Key * hexkey = keyNew ("user/test/rgbcolor/testcolor", KEY_VALUE, color, KEY_META, "check/rgbcolor", "any", KEY_END);
22 22 : KeySet * conf = ksNew (0, KS_END);
23 22 : KeySet * ks = ksNew (20, KS_END);
24 : // KeySet * ksGet = ks;
25 :
26 22 : ksAppendKey (ks, hexkey);
27 :
28 22 : PLUGIN_OPEN ("rgbcolor");
29 :
30 22 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "kdbSet did not succeed");
31 22 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "kdbGet did not succeed");
32 :
33 22 : Key * foundKey = ksLookupByName (ks, "user/test/rgbcolor/testcolor", 0);
34 :
35 : char colorStr[11];
36 22 : snprintf (colorStr, 11, "%u", colorValue);
37 :
38 22 : printf ("Test Color Normalization %s, returned value: %s, expected value: %s\n", color, keyString (foundKey), colorStr);
39 22 : succeed_if (!strcmp (keyString (foundKey), colorStr), "Values dont match");
40 :
41 22 : const Key * origValueKey = keyGetMeta (foundKey, "origvalue");
42 22 : succeed_if (origValueKey != NULL, "origvalue is not set");
43 22 : succeed_if (!strcmp (keyString (origValueKey), color), "origvalue does not match actual original value");
44 :
45 22 : ksDel (ks);
46 22 : keyDel (parentKey);
47 :
48 22 : PLUGIN_CLOSE ();
49 22 : }
50 :
51 :
52 34 : static void test_color (const char * color, const int expected_ret)
53 : {
54 34 : Key * parentKey = keyNew ("user/tests/rgbcolor", KEY_END);
55 34 : Key * hexkey = keyNew ("user/test/rgbcolor/testcolor", KEY_VALUE, color, KEY_META, "check/rgbcolor", "any", KEY_END);
56 34 : KeySet * conf = ksNew (0, KS_END);
57 34 : KeySet * ks = ksNew (20, KS_END);
58 :
59 34 : ksAppendKey (ks, hexkey);
60 :
61 34 : PLUGIN_OPEN ("rgbcolor");
62 :
63 34 : int ret = plugin->kdbSet (plugin, ks, parentKey);
64 :
65 34 : printf ("Test Color Validity %s, returned value: %d, expected value: %d\n", color, ret, expected_ret);
66 34 : succeed_if (ret == expected_ret, "failed");
67 :
68 34 : ksDel (ks);
69 34 : keyDel (parentKey);
70 :
71 34 : PLUGIN_CLOSE ();
72 34 : }
73 :
74 2 : static void test_rgbcolor (void)
75 : {
76 2 : test_color ("#0fb", 1);
77 2 : test_color ("#fff", 1);
78 2 : test_color ("#111", 1);
79 :
80 2 : test_color ("#fff111", 1);
81 2 : test_color ("#a1C2bF", 1);
82 :
83 2 : test_color ("#abcd", 1);
84 :
85 2 : test_color ("#aabbccdd", 1);
86 2 : test_color ("#ffffffff", 1);
87 2 : test_color ("#00000000", 1);
88 :
89 2 : test_color ("aliceblue", 1);
90 2 : test_color ("orange", 1);
91 2 : test_color ("red", 1);
92 :
93 2 : test_color ("nonexistentcolor", -1);
94 2 : test_color ("1110", -1);
95 2 : test_color ("a1C2bF", -1);
96 2 : test_color ("#a1C2bZ", -1);
97 2 : test_color ("#00000", -1);
98 :
99 2 : test_normalize_color ("lightcyan", 0xe0ffffff);
100 2 : test_normalize_color ("white", 0xffffffff);
101 2 : test_normalize_color ("black", 0xff);
102 2 : test_normalize_color ("#fff", 0xffffffff);
103 2 : test_normalize_color ("#ffff", 0xffffffff);
104 2 : test_normalize_color ("#ffffff", 0xffffffff);
105 2 : test_normalize_color ("#ffffffff", 0xffffffff);
106 :
107 2 : test_normalize_color ("#aabbccdd", 0xaabbccdd);
108 2 : test_normalize_color ("#abcd", 0xaabbccdd);
109 :
110 2 : test_normalize_color ("#abc", 0xaabbccff);
111 2 : test_normalize_color ("#aabbcc", 0xaabbccff);
112 2 : }
113 :
114 2 : int main (int argc, char ** argv)
115 : {
116 2 : init (argc, argv);
117 :
118 2 : test_rgbcolor ();
119 :
120 2 : print_result ("testmod_rgbcolor");
121 :
122 2 : return nbError;
123 : }
|