Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for mathcheck plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include "floathelper.h"
11 : #include <kdbconfig.h>
12 : #include <stdio.h>
13 : #include <stdlib.h>
14 : #include <string.h>
15 :
16 : #include <tests_plugin.h>
17 :
18 : #define test(ks, retval) \
19 : { \
20 : Key * parentKey = keyNew ("user/tests/mathcheck", KEY_VALUE, "", KEY_END); \
21 : KeySet * conf = ksNew (0, KS_END); \
22 : PLUGIN_OPEN ("mathcheck"); \
23 : ksRewind (ks); \
24 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == retval, "error"); \
25 : keyDel (parentKey); \
26 : PLUGIN_CLOSE (); \
27 : }
28 :
29 : #define testSet(ks, value) \
30 : { \
31 : Key * parentKey = keyNew ("user/tests/mathcheck", KEY_VALUE, "", KEY_END); \
32 : KeySet * conf = ksNew (0, KS_END); \
33 : PLUGIN_OPEN ("mathcheck"); \
34 : ksRewind (ks); \
35 : plugin->kdbSet (plugin, ks, parentKey); \
36 : succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/mathcheck/sum", 0)), value), "error"); \
37 : keyDel (parentKey); \
38 : PLUGIN_CLOSE (); \
39 : }
40 :
41 36 : static KeySet * create_ks (const char * res, const char * meta)
42 : {
43 36 : return ksNew (5, keyNew ("user/tests/mathcheck/sum", KEY_VALUE, res, KEY_META, "check/math", meta, KEY_END),
44 : keyNew ("user/tests/mathcheck/bla/val1", KEY_VALUE, "100", KEY_END),
45 : keyNew ("user/tests/mathcheck/bla/val2", KEY_VALUE, "50", KEY_END),
46 : keyNew ("user/tests/mathcheck/bla/val3", KEY_VALUE, "3", KEY_END), KS_END);
47 : }
48 :
49 2 : static void test_multiUp (void)
50 : {
51 2 : Key * parentKey = keyNew ("user/tests/mathcheck", KEY_VALUE, "", KEY_END);
52 2 : KeySet * conf = ksNew (0, KS_END);
53 2 : KeySet * ks = ksNew (5,
54 : keyNew ("user/tests/mathcheck/up/sum", KEY_VALUE, "0", KEY_META, "check/math",
55 : ":= + ../val1 + ../../val2 ../val3", KEY_END),
56 : keyNew ("user/tests/mathcheck/up/val1", KEY_VALUE, "1", KEY_END),
57 : keyNew ("user/tests/mathcheck/val2", KEY_VALUE, "2", KEY_END),
58 : keyNew ("user/tests/mathcheck/up/val3", KEY_VALUE, "10", KEY_END), KS_END);
59 :
60 2 : PLUGIN_OPEN ("mathcheck");
61 2 : ksRewind (ks);
62 2 : plugin->kdbSet (plugin, ks, parentKey);
63 2 : succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/mathcheck/up/sum", 0)), "13"), "error");
64 2 : keyDel (parentKey);
65 2 : PLUGIN_CLOSE ();
66 2 : ksDel (ks);
67 2 : }
68 :
69 2 : int main (int argc, char ** argv)
70 : {
71 2 : printf ("MATHCHECK TESTS\n");
72 2 : printf ("==================\n\n");
73 :
74 2 : init (argc, argv);
75 :
76 2 : KeySet * ks = create_ks ("153", "== + ../bla/val1 + ../bla/val2 ../bla/val3");
77 2 : test (ks, 1);
78 2 : ksDel (ks);
79 :
80 2 : ks = create_ks ("250", "< + ../bla/val1 + ../bla/val2 ../bla/val3");
81 2 : test (ks, (-1));
82 2 : ksDel (ks);
83 :
84 2 : ks = create_ks ("250", ">= + @/bla/val1 + @/bla/val2 @/bla/val3");
85 2 : test (ks, 1);
86 2 : ksDel (ks);
87 :
88 2 : ks = create_ks ("2", "== / @/bla/val1 @/bla/val2");
89 2 : test (ks, 1);
90 2 : ksDel (ks);
91 :
92 2 : ks = create_ks ("", ":= / @/bla/val1 @/bla/val2");
93 2 : testSet (ks, "2");
94 2 : ksDel (ks);
95 :
96 2 : ks = create_ks ("1", "== / ../bla/val1 ../bla/val3");
97 2 : test (ks, (-1));
98 2 : ksDel (ks);
99 :
100 2 : ks = create_ks ("3", "== + '1.5' '1.5'");
101 2 : test (ks, 1);
102 2 : ksDel (ks);
103 :
104 2 : ks = create_ks ("4.5", "== + '1.5' + '1.5' '1.5'");
105 2 : test (ks, 1);
106 2 : ksDel (ks);
107 :
108 2 : ks = create_ks ("", ":= + '1.5' + '1.5' '1.5'");
109 2 : testSet (ks, "4.5");
110 2 : ksDel (ks);
111 :
112 2 : ks = create_ks ("1", "== + '1.5' '1.5'");
113 2 : test (ks, (-1));
114 2 : ksDel (ks);
115 :
116 2 : ks = create_ks ("10", "== + ../bla/val3 '7'");
117 2 : test (ks, 1);
118 2 : ksDel (ks);
119 :
120 2 : ks = create_ks ("7", "== + @/bla/nonExisting '7'");
121 2 : test (ks, 1);
122 2 : ksDel (ks);
123 :
124 2 : ks = create_ks ("", ":= + @/bla/nonExisting '7'");
125 2 : testSet (ks, "7");
126 2 : ksDel (ks);
127 :
128 2 : ks = create_ks ("7", "== * @/bla/nonExisting '7'");
129 2 : test (ks, 1);
130 2 : ksDel (ks);
131 :
132 2 : ks = create_ks ("3", "== + ../bla/nonExisting + ../bla/nonExistingToo ../bla/val3");
133 2 : test (ks, 1);
134 2 : ksDel (ks);
135 :
136 2 : ks = create_ks ("", ":= + ../bla/nonExisting + ../bla/nonExistingToo ../bla/val3");
137 2 : testSet (ks, "3");
138 2 : ksDel (ks);
139 :
140 2 : ks = create_ks ("3", "== / @/bla/nonExisting / ../bla/nonExistingToo @/bla/val3");
141 2 : test (ks, 1);
142 2 : ksDel (ks);
143 :
144 2 : ks = create_ks ("3", "== + @/bla/nonExisting / ../bla/val3 ../bla/nonExistingToo");
145 2 : test (ks, 1);
146 2 : ksDel (ks);
147 :
148 2 : test_multiUp ();
149 :
150 2 : print_result ("testmod_mathcheck");
151 :
152 : char buffer[24];
153 2 : elektraFtoA (buffer, sizeof (buffer), (1.5));
154 2 : succeed_if (!(strcmp (buffer, "1.5")), "elektraFtoA failed");
155 2 : fprintf (stderr, "elektraFtoA: val: %g, ret: %s\n", (1.5), buffer);
156 2 : fprintf (stderr, "elektraEFtoF: string: %s, ret: %g\n", buffer, elektraEFtoF (buffer));
157 2 : succeed_if ((elektraEFtoF (buffer) - (1.5)) < 0.00001, "elektraEFtoF failed");
158 2 : return nbError;
159 : }
|