1 /*
2 * libnkutils/enum - Miscellaneous utilities, enum module
3 *
4 * Copyright © 2011-2017 Quentin "Sardem FF7" Glidic
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif /* HAVE_CONFIG_H */
29
30 #include <glib.h>
31
32 #include <nkutils-enum.h>
33
34 typedef enum {
35 CENTER,
36 LEFT,
37 RIGHT,
38 _MAX_VALUES
39 } NkEnumTestValues;
40
41 static const gchar * const _nk_enum_tests_values[_MAX_VALUES] = {
42 [CENTER] = "center",
43 [LEFT] = "left",
44 [RIGHT] = "right",
45 };
46
47 typedef struct {
48 const gchar *string;
49 gboolean ignore_case;
50 gboolean prefix;
51 gboolean ret;
52 NkEnumTestValues value;
53 const gchar * const values[_MAX_VALUES];
54 } NkEnumTestData;
55
56 static const struct {
57 const gchar *testpath;
58 NkEnumTestData data;
59 } _nk_enum_tests_list[] = {
60 {
61 .testpath = "/nkutils/enum/full/exists",
62 .data = {
63 .string = "center",
64 .ignore_case = FALSE,
65 .prefix = FALSE,
66 .ret = TRUE,
67 .value = CENTER,
68 }
69 },
70 {
71 .testpath = "/nkutils/enum/full/missing",
72 .data = {
73 .string = "Center",
74 .ignore_case = FALSE,
75 .prefix = FALSE,
76 .ret = FALSE,
77 .value = _MAX_VALUES,
78 }
79 },
80 {
81 .testpath = "/nkutils/enum/full/case",
82 .data = {
83 .string = "Center",
84 .ignore_case = TRUE,
85 .prefix = FALSE,
86 .ret = TRUE,
87 .value = CENTER,
88 }
89 },
90 {
91 .testpath = "/nkutils/enum/prefix/full",
92 .data = {
93 .string = "center",
94 .ignore_case = FALSE,
95 .prefix = TRUE,
96 .ret = TRUE,
97 .value = CENTER,
98 }
99 },
100 {
101 .testpath = "/nkutils/enum/prefix/prefix",
102 .data = {
103 .string = "cen",
104 .ignore_case = FALSE,
105 .prefix = TRUE,
106 .ret = FALSE,
107 .value = _MAX_VALUES,
108 }
109 },
110 {
111 .testpath = "/nkutils/enum/prefix/no-prefix",
112 .data = {
113 .string = "cen",
114 .ignore_case = FALSE,
115 .prefix = FALSE,
116 .ret = FALSE,
117 .value = _MAX_VALUES,
118 }
119 },
120 };
121
122 static void
_nk_enum_tests_func(gconstpointer user_data)123 _nk_enum_tests_func(gconstpointer user_data)
124 {
125 const NkEnumTestData *data = user_data;
126
127 guint64 value = _MAX_VALUES;
128 gboolean r;
129 r = nk_enum_parse(data->string, _nk_enum_tests_values, _MAX_VALUES, data->ignore_case, data->prefix, &value);
130 if ( data->ret )
131 g_assert_true(r);
132 else
133 g_assert_false(r);
134 g_assert_cmpuint(data->value, ==, value);
135 }
136
137 int
main(int argc,char * argv[])138 main(int argc, char *argv[])
139 {
140 g_test_init(&argc, &argv, NULL);
141
142 g_test_set_nonfatal_assertions();
143
144 gsize i;
145 for ( i = 0 ; i < G_N_ELEMENTS(_nk_enum_tests_list) ; ++i )
146 g_test_add_data_func(_nk_enum_tests_list[i].testpath, &_nk_enum_tests_list[i].data, _nk_enum_tests_func);
147
148 return g_test_run();
149 }
150