1*cc7d2833Sskrll /* $NetBSD: utilfdt_test.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $ */
2d89652e2Sskrll
3*cc7d2833Sskrll // SPDX-License-Identifier: LGPL-2.1-or-later
4b8ae3907Smacallan /*
5b8ae3907Smacallan * Copyright 2011 The Chromium Authors, All Rights Reserved.
6b8ae3907Smacallan *
7b8ae3907Smacallan * utilfdt_test - Tests for utilfdt library
8b8ae3907Smacallan */
9b8ae3907Smacallan #include <assert.h>
10b8ae3907Smacallan #include <stdlib.h>
11b8ae3907Smacallan #include <stdio.h>
12b8ae3907Smacallan #include <string.h>
13b8ae3907Smacallan #include <stdint.h>
14b8ae3907Smacallan #include <stdarg.h>
15b8ae3907Smacallan
16b8ae3907Smacallan #include <libfdt.h>
17b8ae3907Smacallan #include <util.h>
18b8ae3907Smacallan
19b8ae3907Smacallan #include "tests.h"
20b8ae3907Smacallan #include "testdata.h"
21b8ae3907Smacallan
check(const char * fmt,int expect_type,int expect_size)22b8ae3907Smacallan static void check(const char *fmt, int expect_type, int expect_size)
23b8ae3907Smacallan {
24b8ae3907Smacallan int type;
25b8ae3907Smacallan int size;
26b8ae3907Smacallan
27b8ae3907Smacallan if (utilfdt_decode_type(fmt, &type, &size))
28b8ae3907Smacallan FAIL("format '%s': valid format string returned failure", fmt);
29b8ae3907Smacallan if (expect_type != type)
30b8ae3907Smacallan FAIL("format '%s': expected type='%c', got type='%c'", fmt,
31b8ae3907Smacallan expect_type, type);
32b8ae3907Smacallan if (expect_size != size)
33b8ae3907Smacallan FAIL("format '%s': expected size=%d, got size=%d", fmt,
34b8ae3907Smacallan expect_size, size);
35b8ae3907Smacallan }
36b8ae3907Smacallan
checkfail(const char * fmt)37b8ae3907Smacallan static void checkfail(const char *fmt)
38b8ae3907Smacallan {
39b8ae3907Smacallan int type;
40b8ae3907Smacallan int size;
41b8ae3907Smacallan
42b8ae3907Smacallan if (!utilfdt_decode_type(fmt, &type, &size))
43b8ae3907Smacallan FAIL("format '%s': invalid format string returned success",
44b8ae3907Smacallan fmt);
45b8ae3907Smacallan }
46b8ae3907Smacallan
47b8ae3907Smacallan /**
48b8ae3907Smacallan * Add the given modifier to each of the valid sizes, and check that we get
49b8ae3907Smacallan * correct values.
50b8ae3907Smacallan *
51b8ae3907Smacallan * \param modifier Modifer string to use as a prefix
52b8ae3907Smacallan * \param expected_size The size (in bytes) that we expect (ignored for
53b8ae3907Smacallan * strings)
54b8ae3907Smacallan */
check_sizes(char * modifier,int expected_size)55b8ae3907Smacallan static void check_sizes(char *modifier, int expected_size)
56b8ae3907Smacallan {
57b8ae3907Smacallan char fmt[10], *ptr;
58b8ae3907Smacallan
59b8ae3907Smacallan /* set up a string with a hole in it for the format character */
60b8ae3907Smacallan if (strlen(modifier) + 2 >= sizeof(fmt))
61b8ae3907Smacallan FAIL("modifier string '%s' too long", modifier);
62b8ae3907Smacallan strcpy(fmt, modifier);
63b8ae3907Smacallan ptr = fmt + strlen(fmt);
64b8ae3907Smacallan ptr[1] = '\0';
65b8ae3907Smacallan
66b8ae3907Smacallan /* now try each format character in turn */
67b8ae3907Smacallan *ptr = 'i';
68b8ae3907Smacallan check(fmt, 'i', expected_size);
69b8ae3907Smacallan
70b8ae3907Smacallan *ptr = 'u';
71b8ae3907Smacallan check(fmt, 'u', expected_size);
72b8ae3907Smacallan
73b8ae3907Smacallan *ptr = 'x';
74b8ae3907Smacallan check(fmt, 'x', expected_size);
75b8ae3907Smacallan
76b8ae3907Smacallan *ptr = 's';
77b8ae3907Smacallan check(fmt, 's', -1);
78b8ae3907Smacallan }
79b8ae3907Smacallan
test_utilfdt_decode_type(void)80b8ae3907Smacallan static void test_utilfdt_decode_type(void)
81b8ae3907Smacallan {
82b8ae3907Smacallan char fmt[10];
83b8ae3907Smacallan int ch;
84b8ae3907Smacallan
85b8ae3907Smacallan /* check all the valid modifiers and sizes */
86b8ae3907Smacallan check_sizes("", -1);
87b8ae3907Smacallan check_sizes("b", 1);
88b8ae3907Smacallan check_sizes("hh", 1);
89b8ae3907Smacallan check_sizes("h", 2);
90b8ae3907Smacallan check_sizes("l", 4);
91b8ae3907Smacallan
92b8ae3907Smacallan /* try every other character */
93b8ae3907Smacallan checkfail("");
94b8ae3907Smacallan for (ch = ' '; ch < 127; ch++) {
95b8ae3907Smacallan if (!strchr("iuxs", ch)) {
96b8ae3907Smacallan *fmt = ch;
97b8ae3907Smacallan fmt[1] = '\0';
98b8ae3907Smacallan checkfail(fmt);
99b8ae3907Smacallan }
100b8ae3907Smacallan }
101b8ae3907Smacallan
102b8ae3907Smacallan /* try a few modifiers at the end */
103b8ae3907Smacallan checkfail("sx");
104b8ae3907Smacallan checkfail("ihh");
105b8ae3907Smacallan checkfail("xb");
106b8ae3907Smacallan
107b8ae3907Smacallan /* and one for the doomsday archives */
108b8ae3907Smacallan checkfail("He has all the virtues I dislike and none of the vices "
109b8ae3907Smacallan "I admire.");
110b8ae3907Smacallan }
111b8ae3907Smacallan
main(int argc,char * argv[])112b8ae3907Smacallan int main(int argc, char *argv[])
113b8ae3907Smacallan {
114b8ae3907Smacallan test_utilfdt_decode_type();
115b8ae3907Smacallan PASS();
116b8ae3907Smacallan }
117