added unit tests
Costa Tsaousis (ktsaou) committed
Jan 18, 2018 at 23:34 UTC
84bc5a69aff5670f493c8dcad84e541e62e5405e
1 file changed
+39
src/unit_test.c
+39
@@ -1,5 +1,41 @@
1
#include "common.h"
2
3
+static int check_number_printing(void) {
4
+ struct {
5
+ calculated_number n;
6
+ const char *correct;
7
+ } values[] = {
8
+ { .n = 0, .correct = "0" },
9
+ { .n = 0.0000001, .correct = "0.0000001" },
10
+ { .n = 0.00000009, .correct = "0.0000001" },
11
+ { .n = 0.000000001, .correct = "0" },
12
+ { .n = 99.99999999999999999, .correct = "100" },
13
+ { .n = -99.99999999999999999, .correct = "-100" },
14
+ { .n = 123.4567890123456789, .correct = "123.456789" },
15
+ { .n = 9999.9999999, .correct = "9999.9999999" },
16
+ { .n = -9999.9999999, .correct = "-9999.9999999" },
17
+ { .n = 0, .correct = NULL },
18
+ };
19
+
20
+ char netdata[50], system[50];
21
+ int i, failed = 0;
22
+ for(i = 0; values[i].correct ; i++) {
23
+ print_calculated_number(netdata, values[i].n);
24
+ snprintfz(system, 49, "%0.12Lf", values[i].n);
25
+
26
+ int ok = 1;
27
+ if(strcmp(netdata, values[i].correct) != 0) {
28
+ ok = 0;
29
+ failed++;
30
+ }
31
+
32
+ fprintf(stderr, "'%s' (system) printed as '%s' (netdata): %s\n", system, netdata, ok?"OK":"FAILED");
33
+ }
34
+
35
+ if(failed) return 1;
36
+ return 0;
37
+}
38
+
39
static int check_rrdcalc_comparisons(void) {
40
RRDCALC_STATUS a, b;
41
@@ -1163,6 +1199,9 @@ static int test_variable_renames(void) {
1199
1200
int run_all_mockup_tests(void)
1201
{
1202
+ if(check_number_printing())
1203
+ return 1;
1204
+
1205
if(check_rrdcalc_comparisons())
1206
return 1;
1207