Fix all -Wdiscarded-qualifiers warning instances
[lttng-tools.git] / tests / unit / test_utils_parse_size_suffix.c
1 /*
2 * Copyright (C) 2013 Simon Marchi <simon.marchi@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
11
12 #include <tap/tap.h>
13
14 #include <common/utils.h>
15
16 /* For error.h */
17 int lttng_opt_quiet = 1;
18 int lttng_opt_verbose = 3;
19 int lttng_opt_mi;
20
21 struct valid_test_input {
22 const char *input;
23 uint64_t expected_result;
24 };
25
26 /* Valid test cases */
27 static struct valid_test_input valid_tests_inputs[] = {
28 { "0", 0 },
29 { "1234", 1234 },
30 { "0x400", 1024 },
31 { "0300", 192 },
32 { "16k", 16384 },
33 { "128K", 131072 },
34 { "0x1234k", 4771840 },
35 { "32M", 33554432 },
36 { "1024G", 1099511627776ULL },
37 { "0X400", 1024 },
38 { "0x40a", 1034 },
39 { "0X40b", 1035 },
40 { "0x40C", 1036 },
41 { "0X40D", 1037 },
42 { "0x40e", 1038 },
43 { "0X40f", 1039 },
44 { "00", 0 },
45 { "0k", 0 },
46 { "0K", 0 },
47 { "0M", 0 },
48 { "0G", 0 },
49 { "00k", 0 },
50 { "00K", 0 },
51 { "00M", 0 },
52 { "00G", 0 },
53 { "0x0", 0 },
54 { "0X0", 0 },
55 { "0x0k", 0 },
56 { "0X0K", 0 },
57 { "0x0M", 0 },
58 { "0X0G", 0 },
59 { "0X40G", 68719476736ULL },
60 { "0300k", 196608 },
61 { "0300K", 196608 },
62 { "030M", 25165824 },
63 { "020G", 17179869184ULL },
64 { "0xa0k", 163840 },
65 { "0xa0K", 163840 },
66 { "0XA0M", 167772160 },
67 { "0xA0G", 171798691840ULL },
68 };
69 static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
70
71 /* Invalid test cases */
72 static const char *invalid_tests_inputs[] = {
73 "",
74 " ",
75 "-1",
76 "k",
77 "4611686018427387904G",
78 "0x40g",
79 "08",
80 "09",
81 "0x",
82 "x0",
83 "0xx0",
84 "07kK",
85 "0xk",
86 "0XM",
87 "0xG",
88 "0x0MM",
89 "0X0GG",
90 "0a",
91 "0B",
92 };
93
94 static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
95
96 static void test_utils_parse_size_suffix(void)
97 {
98 uint64_t result;
99 int ret;
100 int i;
101
102 /* Test valid cases */
103 for (i = 0; i < num_valid_tests; i++) {
104 char name[100];
105 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
106
107 ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result);
108 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
109 }
110
111 /* Test invalid cases */
112 for (i = 0; i < num_invalid_tests; i++) {
113 char name[100];
114 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
115
116 ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result);
117 ok(ret != 0, name);
118 }
119 }
120
121 int main(int argc, char **argv)
122 {
123 plan_tests(num_valid_tests + num_invalid_tests);
124
125 diag("utils_parse_size_suffix tests");
126
127 test_utils_parse_size_suffix();
128
129 return exit_status();
130 }
This page took 0.031107 seconds and 4 git commands to generate.