xref: /netbsd-src/external/gpl2/dtc/dist/tests/utilfdt_test.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: utilfdt_test.c,v 1.1.1.2 2017/06/08 15:59:28 skrll Exp $	*/
2 
3 /*
4  * Copyright 2011 The Chromium Authors, All Rights Reserved.
5  *
6  * utilfdt_test - Tests for utilfdt library
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 
29 #include <libfdt.h>
30 #include <util.h>
31 
32 #include "tests.h"
33 #include "testdata.h"
34 
35 static void check(const char *fmt, int expect_type, int expect_size)
36 {
37 	int type;
38 	int size;
39 
40 	if (utilfdt_decode_type(fmt, &type, &size))
41 		FAIL("format '%s': valid format string returned failure", fmt);
42 	if (expect_type != type)
43 		FAIL("format '%s': expected type='%c', got type='%c'", fmt,
44 		     expect_type, type);
45 	if (expect_size != size)
46 		FAIL("format '%s': expected size=%d, got size=%d", fmt,
47 		     expect_size, size);
48 }
49 
50 static void checkfail(const char *fmt)
51 {
52 	int type;
53 	int size;
54 
55 	if (!utilfdt_decode_type(fmt, &type, &size))
56 		FAIL("format '%s': invalid format string returned success",
57 		     fmt);
58 }
59 
60 /**
61  * Add the given modifier to each of the valid sizes, and check that we get
62  * correct values.
63  *
64  * \param modifier	Modifer string to use as a prefix
65  * \param expected_size	The size (in bytes) that we expect (ignored for
66  *			strings)
67  */
68 static void check_sizes(char *modifier, int expected_size)
69 {
70 	char fmt[10], *ptr;
71 
72 	/* set up a string with a hole in it for the format character */
73 	if (strlen(modifier) + 2 >= sizeof(fmt))
74 		FAIL("modifier string '%s' too long", modifier);
75 	strcpy(fmt, modifier);
76 	ptr = fmt + strlen(fmt);
77 	ptr[1] = '\0';
78 
79 	/* now try each format character in turn */
80 	*ptr = 'i';
81 	check(fmt, 'i', expected_size);
82 
83 	*ptr = 'u';
84 	check(fmt, 'u', expected_size);
85 
86 	*ptr = 'x';
87 	check(fmt, 'x', expected_size);
88 
89 	*ptr = 's';
90 	check(fmt, 's', -1);
91 }
92 
93 static void test_utilfdt_decode_type(void)
94 {
95 	char fmt[10];
96 	int ch;
97 
98 	/* check all the valid modifiers and sizes */
99 	check_sizes("", -1);
100 	check_sizes("b", 1);
101 	check_sizes("hh", 1);
102 	check_sizes("h", 2);
103 	check_sizes("l", 4);
104 
105 	/* try every other character */
106 	checkfail("");
107 	for (ch = ' '; ch < 127; ch++) {
108 		if (!strchr("iuxs", ch)) {
109 			*fmt = ch;
110 			fmt[1] = '\0';
111 			checkfail(fmt);
112 		}
113 	}
114 
115 	/* try a few modifiers at the end */
116 	checkfail("sx");
117 	checkfail("ihh");
118 	checkfail("xb");
119 
120 	/* and one for the doomsday archives */
121 	checkfail("He has all the virtues I dislike and none of the vices "
122 			"I admire.");
123 }
124 
125 int main(int argc, char *argv[])
126 {
127 	test_utilfdt_decode_type();
128 	PASS();
129 }
130