Skip to content

Commit 2559aeb

Browse files
committed
Add test cases and convert to string list
1 parent c6fbf3f commit 2559aeb

4 files changed

Lines changed: 138 additions & 63 deletions

File tree

src/config/config.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string.h>
2222

2323
#define MATCH_KEY(s) strcasecmp(name, s) == 0
24+
#define MATCH_VALUE(s) strcasecmp(value, s) == 0
2425

2526
extern u8 FONT_GetFromString(const char *);
2627

@@ -33,8 +34,7 @@ static u16 get_color(const char *value) {
3334
return RGB888_to_RGB565(r, g, b);
3435
}
3536

36-
int assign_int(void* ptr, const struct struct_map *map, int map_size, const char* name, const char* value)
37-
{
37+
int assign_int(void* ptr, const struct struct_map *map, int map_size, const char* name, const char* value) {
3838
for(int i = 0; i < map_size; i++) {
3939
if(MATCH_KEY(map[i].str)) {
4040
int size = map[i].offset >> 12;
@@ -57,10 +57,28 @@ int assign_int(void* ptr, const struct struct_map *map, int map_size, const char
5757
case TYPE_COLOR:
5858
*((u16 *)((u8*)ptr + offset)) = get_color(value); break;
5959
case TYPE_FONT:
60-
*((u8 *)((u8*)ptr + offset)) = FONT_GetFromString(value);
60+
*((u8 *)((u8*)ptr + offset)) = FONT_GetFromString(value); break;
61+
62+
case TYPE_STR_LIST: {
63+
// get the list
64+
i++; // next entry is additional info for string list
65+
const char* const *list = (const char* const *)map[i].str;
66+
unsigned length = map[i].offset;
67+
for (unsigned j = 0; j < length; j++) {
68+
if (list[j] && MATCH_VALUE(list[j]))
69+
*((u8 *)((u8*)ptr + offset)) = j;
70+
}
71+
break;
72+
}
73+
default:
74+
printf("Unknown type: %d\n", size);
6175
}
6276
return 1;
6377
}
6478
}
6579
return 0;
6680
}
81+
82+
#define TESTNAME config
83+
#include "tests.h"
84+

src/config/config.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,34 @@
1818

1919
#include <stddef.h>
2020
enum {
21-
TYPE_U8_LIST = 2,
2221

23-
TYPE_U8 = 0,
24-
TYPE_U16 = 1,
25-
TYPE_U32 = 3,
22+
TYPE_U8 = 0,
23+
TYPE_U16 = 1,
24+
TYPE_U32 = 3,
2625

27-
TYPE_S8 = 4,
28-
TYPE_S16 = 5,
29-
TYPE_S32 = 7,
26+
TYPE_S8 = 4,
27+
TYPE_S16 = 5,
28+
TYPE_S32 = 7,
3029

31-
TYPE_COLOR = 8,
32-
TYPE_FONT = 9,
33-
TYPE_SOURCE = 10,
34-
TYPE_BUTTON = 11,
30+
TYPE_STR_LIST = 8,
31+
TYPE_STR_LIST_OFF_ONE = 9,
32+
33+
TYPE_COLOR = 10,
34+
TYPE_FONT = 11,
35+
TYPE_SOURCE = 12,
36+
TYPE_BUTTON = 13,
3537
};
3638

3739
struct struct_map {const char *str; u16 offset;};
38-
#define MAPSIZE(x) (sizeof(x) / sizeof(struct struct_map))
40+
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
3941
#define OFFSET(s,v) (offsetof(s, v) | ((sizeof(((s*)0)->v) - 1) << 12))
4042
#define OFFSETS(s,v) (offsetof(s, v) | ((sizeof(((s*)0)->v) + 3) << 12))
4143
#define OFFSET_COL(s,v) (offsetof(s, v) | (TYPE_COLOR << 12))
4244
#define OFFSET_FON(s,v) (offsetof(s, v) | (TYPE_FONT << 12))
45+
#define OFFSET_STRLIST(s, v, StrList, StrListSize) \
46+
(offsetof(s, v) | (TYPE_STR_LIST << 12)) }, { (const char*)StrList, StrListSize
4347

4448
int assign_int(void* ptr, const struct struct_map *map, int map_size,
45-
const char* name, const char* value);
49+
const char* name, const char* value);
4650

4751
#endif

src/config/display.c

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ static const char * const ALIGN_VAL[] = {
8787
#define MATCH_START(x,y) strncasecmp(x, y, sizeof(y)-1) == 0
8888
#define MATCH_KEY(s) strcasecmp(name, s) == 0
8989
#define MATCH_VALUE(s) strcasecmp(value, s) == 0
90-
#define NUM_STR_ELEMS(s) (sizeof(s) / sizeof(char *))
9190
#define SET_FLAG(var, value, flag) ((value) ? ((var) | (flag)) : ((var) & ~(flag)))
9291
extern u8 FONT_GetFromString(const char *);
9392
struct display_settings Display;
@@ -107,6 +106,8 @@ static const struct struct_map _seclabel[] =
107106
{FONT_COLOR, OFFSET_COL(struct LabelDesc, font_color)},
108107
{BG_COLOR, OFFSET_COL(struct LabelDesc, fill_color)},
109108
{OUTLINE_COLOR, OFFSET_COL(struct LabelDesc, outline_color)},
109+
{ALIGN, OFFSET_STRLIST(struct LabelDesc, align, ALIGN_VAL, ARRAYSIZE(ALIGN_VAL))},
110+
{BOX, OFFSET_STRLIST(struct LabelDesc, style, BOX_VAL, ARRAYSIZE(BOX_VAL))}
110111
};
111112

112113
static const struct struct_map _secgeneral[] =
@@ -171,50 +172,16 @@ static const struct struct_map _secbackground[] =
171172
};
172173
#endif
173174

174-
static int handle_label(struct LabelDesc *label, const char *name, const char *value)
175-
{
176-
if (assign_int(label, _seclabel, MAPSIZE(_seclabel), name, value) == 1)
177-
return 1;
178-
if(MATCH_KEY(BOX)) {
179-
u8 idx;
180-
for (idx = 0; idx < NUM_STR_ELEMS(BOX_VAL); idx++) {
181-
if(BOX_VAL[idx] && MATCH_VALUE(BOX_VAL[idx])) {
182-
label->style = idx;
183-
// For compatibility reasons,
184-
// use the old alignment values as default
185-
// if the new one is not yet set:
186-
if(label->align == 0) {
187-
switch(idx) {
188-
case LABEL_CENTER: label->align = ALIGN_CENTER; break;
189-
case LABEL_LEFT: label->align = ALIGN_LEFT; break;
190-
case LABEL_RIGHT: label->align = ALIGN_RIGHT; break;
191-
}
192-
}
193-
}
194-
}
195-
}
196-
if(MATCH_KEY(ALIGN)) {
197-
u8 idx;
198-
for (idx = 0; idx < NUM_STR_ELEMS(ALIGN_VAL); idx++) {
199-
if(ALIGN_VAL[idx] && MATCH_VALUE(ALIGN_VAL[idx])) {
200-
label->align = idx;
201-
}
202-
}
203-
}
204-
return 0;
205-
}
206-
207175
static int ini_handler(void* user, const char* section, const char* name, const char* value)
208176
{
209177
u8 idx;
210178
struct display_settings *d = (struct display_settings *)user;
211179
int value_int = atoi(value);
212180

213181
if(MATCH_START(section, FONT) && strlen(section) > sizeof(FONT)) {
214-
for (idx = 0; idx < NUM_STR_ELEMS(FONT_VAL); idx++) {
182+
for (idx = 0; idx < ARRAYSIZE(FONT_VAL); idx++) {
215183
if (FONT_VAL[idx] && 0 == strcasecmp(section + sizeof(FONT), FONT_VAL[idx])) {
216-
handle_label(&d->font[idx], name, value);
217-
return 1;
184+
return assign_int(&d->font[idx], _seclabel, ARRAYSIZE(_seclabel), name, value);
218185
}
219186
}
220187
printf("Couldn't parse font: %s\n", section);
@@ -231,36 +198,36 @@ static int ini_handler(void* user, const char* section, const char* name, const
231198
#endif
232199
return 1;
233200
}
234-
if(assign_int(&d->metrics, _secgeneral, MAPSIZE(_secgeneral), name, value))
201+
if (assign_int(&d->metrics, _secgeneral, ARRAYSIZE(_secgeneral), name, value))
235202
return 1;
236203
}
237204
if(MATCH_START(section, "select")) {
238-
if(assign_int(d, _secselect, MAPSIZE(_secselect), name, value))
205+
if (assign_int(d, _secselect, ARRAYSIZE(_secselect), name, value))
239206
return 1;
240207
}
241208
if(MATCH_START(section, "keyboard")) {
242-
if(assign_int(&d->keyboard, _seckeybd, MAPSIZE(_seckeybd), name, value))
209+
if (assign_int(&d->keyboard, _seckeybd, ARRAYSIZE(_seckeybd), name, value))
243210
return 1;
244211
}
245212
if(MATCH_START(section, "listbox")) {
246-
if(assign_int(&d->listbox, _seclistbox, MAPSIZE(_seclistbox), name, value))
213+
if (assign_int(&d->listbox, _seclistbox, ARRAYSIZE(_seclistbox), name, value))
247214
return 1;
248215
}
249216
if(MATCH_START(section, "scrollbar")) {
250-
if(assign_int(&d->scrollbar, _secscroll, MAPSIZE(_secscroll), name, value))
217+
if (assign_int(&d->scrollbar, _secscroll, ARRAYSIZE(_secscroll), name, value))
251218
return 1;
252219
}
253220
if(MATCH_SECTION("xygraph")) {
254-
if(assign_int(&d->xygraph, _secxygraph, MAPSIZE(_secxygraph), name, value))
221+
if (assign_int(&d->xygraph, _secxygraph, ARRAYSIZE(_secxygraph), name, value))
255222
return 1;
256223
}
257224
#if (LCD_WIDTH == 480) || (LCD_WIDTH == 320)
258225
if(MATCH_SECTION("background")) {
259-
if(assign_int(&d->background, _secbackground, MAPSIZE(_secbackground), name, value))
226+
if (assign_int(&d->background, _secbackground, ARRAYSIZE(_secbackground), name, value))
260227
return 1;
261228
}
262229
#endif
263-
for (idx = 0; idx < NUM_STR_ELEMS(BARGRAPH_VAL); idx++) {
230+
for (idx = 0; idx < ARRAYSIZE(BARGRAPH_VAL); idx++) {
264231
if(MATCH_SECTION(BARGRAPH_VAL[idx])) {
265232
struct disp_bargraph *graph;
266233
enum DispFlags flag;
@@ -279,7 +246,7 @@ static int ini_handler(void* user, const char* section, const char* name, const
279246
graph->fg_color_pos = graph->fg_color_neg = graph->fg_color_zero = get_color(value);
280247
return 1;
281248
}
282-
assign_int(graph, _secbargraph, MAPSIZE(_secbargraph), name, value);
249+
assign_int(graph, _secbargraph, ARRAYSIZE(_secbargraph), name, value);
283250
return 1;
284251
}
285252
}
@@ -307,5 +274,24 @@ u8 CONFIG_ReadDisplay()
307274
#else
308275
char filename[] = "media/config.ini";
309276
#endif
310-
return CONFIG_IniParse(filename, ini_handler, (void *)&Display);
277+
if (CONFIG_IniParse(filename, ini_handler, (void *)&Display)) {
278+
for (int i = 0; i < NUM_LABELS; i++) {
279+
struct LabelDesc *label = &Display.font[i];
280+
// For compatibility reasons,
281+
// use the old alignment values as default
282+
// if the new one is not yet set:
283+
if (label->align == 0) {
284+
switch (label->style) {
285+
case LABEL_CENTER: label->align = ALIGN_CENTER; break;
286+
case LABEL_LEFT: label->align = ALIGN_LEFT; break;
287+
case LABEL_RIGHT: label->align = ALIGN_RIGHT; break;
288+
default: break;
289+
}
290+
}
291+
}
292+
293+
return 1;
294+
}
295+
296+
return 0;
311297
}

src/tests/test_config.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "CuTest.h"
2+
3+
struct config_values {
4+
u8 u8_val;
5+
u16 u16_val;
6+
u32 u32_val;
7+
8+
s8 s8_val;
9+
s16 s16_val;
10+
s32 s32_val;
11+
12+
u16 color_val;
13+
u8 str_index_val;
14+
} TestConfig;
15+
16+
enum {
17+
STR_NONE = 0,
18+
STR_CENTER,
19+
STR_LEFT,
20+
STR_RIGHT
21+
};
22+
23+
static const char * const ALIGN_VAL[] = {
24+
[STR_CENTER] = "center",
25+
[STR_LEFT] = "left",
26+
[STR_RIGHT] = "right",
27+
};
28+
29+
static const struct struct_map _secgeneral[] =
30+
{
31+
{"u8val", OFFSET(struct config_values, u8_val)},
32+
{"u16val", OFFSET(struct config_values, u16_val)},
33+
{"u32val", OFFSET(struct config_values, u32_val)},
34+
{"s8val", OFFSET(struct config_values, s8_val)},
35+
{"s16val", OFFSET(struct config_values, s16_val)},
36+
{"s32val", OFFSET(struct config_values, s32_val)},
37+
{"index", OFFSET_STRLIST(struct config_values, str_index_val, ALIGN_VAL, ARRAYSIZE(ALIGN_VAL))}
38+
};
39+
40+
41+
void TestConfigBasic(CuTest* t) {
42+
memset(&TestConfig, 0, sizeof(TestConfig));
43+
44+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "u8val", "5"));
45+
CuAssertIntEquals(t, 5, TestConfig.u8_val);
46+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "s8val", "-5"));
47+
CuAssertIntEquals(t, -5, TestConfig.s8_val);
48+
49+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "u16val", "5000"));
50+
CuAssertIntEquals(t, 5000, TestConfig.u16_val);
51+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "s16val", "-6005"));
52+
CuAssertIntEquals(t, -6005, TestConfig.s16_val);
53+
54+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "u32val", "755350"));
55+
CuAssertIntEquals(t, 755350, TestConfig.u32_val);
56+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "s32val", "-655350"));
57+
CuAssertIntEquals(t, -655350, TestConfig.s32_val);
58+
59+
CuAssertTrue(t, !assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "notfound", "0"));
60+
}
61+
62+
void TestConfigStringList(CuTest* t) {
63+
memset(&TestConfig, 0, sizeof(TestConfig));
64+
65+
CuAssertTrue(t, assign_int(&TestConfig, _secgeneral, ARRAYSIZE(_secgeneral), "index", "center"));
66+
CuAssertIntEquals(t, STR_CENTER, TestConfig.str_index_val);
67+
}

0 commit comments

Comments
 (0)