1 #include <test.h>
2 
3 #include <sysinfo.h>
4 #include <sysinfo.c>
5 
test_uptime(void)6 static void test_uptime(void)
7 {
8     /*
9      * Assume we have been online at least one minute, and less than 5 years.
10      * Should be good enough for everyone...
11      */
12     int uptime = GetUptimeMinutes(time(NULL));
13     printf("Uptime: %.2f days\n", uptime / (60.0 * 24));
14     assert_in_range(uptime, 1, 60*24*365*5);
15 }
16 
FindNextIntegerTestWrapper(char * str,char expected[][KIBIBYTE (1)],int num_expected)17 static void FindNextIntegerTestWrapper(char *str, char expected[][KIBIBYTE(1)], int num_expected)
18 {
19     Seq *seq = SeqNew(num_expected, NULL);
20     char *integer;
21 
22     char *next = FindNextInteger(str, &integer);
23     if (integer != NULL)
24     {
25         SeqAppend(seq, integer);
26     }
27     while (next != NULL && integer != NULL)
28     {
29         next = FindNextInteger(next, &integer);
30         if (integer != NULL)
31         {
32             SeqAppend(seq, integer);
33         }
34     }
35 
36     assert_int_equal(num_expected, SeqLength(seq));
37     for (int i = 0; i < num_expected; i++)
38     {
39         assert_string_equal((char *) SeqAt(seq, i), expected[i]);
40     }
41 
42     SeqDestroy(seq);
43 }
44 
test_find_next_integer(void)45 static void test_find_next_integer(void)
46 {
47     {
48         char str[] = "Ubuntu 20.04.1 LTS";
49         char expected[3][KIBIBYTE(1)] = { "20", "04", "1" };
50         FindNextIntegerTestWrapper(str, expected, 3);
51     }
52     {
53         char str[] = "canonified_5";
54         char expected[1][KIBIBYTE(1)] = { "5" };
55         FindNextIntegerTestWrapper(str, expected, 1);
56     }
57     {
58         char str[] = "";
59         char expected[0][KIBIBYTE(1)] = { };
60         FindNextIntegerTestWrapper(str, expected, 0);
61     }
62     {
63         char str[] = " ";
64         char expected[0][KIBIBYTE(1)] = { };
65         FindNextIntegerTestWrapper(str, expected, 0);
66     }
67     {
68         char str[] = " no numbers in sight ";
69         char expected[0][KIBIBYTE(1)] = { };
70         FindNextIntegerTestWrapper(str, expected, 0);
71     }
72     {
73         char str[] = "0";
74         char expected[1][KIBIBYTE(1)] = { "0" };
75         FindNextIntegerTestWrapper(str, expected, 1);
76     }
77     {
78         char str[] = "1k";
79         char expected[1][KIBIBYTE(1)] = { "1" };
80         FindNextIntegerTestWrapper(str, expected, 1);
81     }
82     {
83         char str[] = "1234";
84         char expected[1][KIBIBYTE(1)] = { "1234" };
85         FindNextIntegerTestWrapper(str, expected, 1);
86     }
87     {
88         char str[] = "1 2 3 4";
89         char expected[4][KIBIBYTE(1)] = { "1", "2", "3", "4" };
90         FindNextIntegerTestWrapper(str, expected, 4);
91     }
92     {
93         char str[] = "Debian 9";
94         char expected[1][KIBIBYTE(1)] = { "9" };
95         FindNextIntegerTestWrapper(str, expected, 1);
96     }
97     {
98         char str[] = "Cent OS 6.7";
99         char expected[2][KIBIBYTE(1)] = { "6", "7" };
100         FindNextIntegerTestWrapper(str, expected, 2);
101     }
102     {
103         char str[] = "CFEngine 3.18.0-2deadbeef";
104         char expected[4][KIBIBYTE(1)] = { "3", "18", "0", "2" };
105         FindNextIntegerTestWrapper(str, expected, 4);
106     }
107 }
108 
main()109 int main()
110 {
111     PRINT_TEST_BANNER();
112     const UnitTest tests[] =
113     {
114         unit_test(test_uptime),
115         unit_test(test_find_next_integer)
116     };
117 
118     return run_tests(tests);
119 }
120