summaryrefslogtreecommitdiffstats
path: root/src/dtc/tests/utilfdt_test.c
blob: 274c3d68e9539e5b77374697c385c6e81d8dcfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2011 The Chromium Authors, All Rights Reserved.
 *
 * utilfdt_test - Tests for utilfdt library
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>

#include <libfdt.h>
#include <util.h>

#include "tests.h"
#include "testdata.h"

static void check(const char *fmt, int expect_type, int expect_size)
{
	int type;
	int size;

	if (utilfdt_decode_type(fmt, &type, &size))
		FAIL("format '%s': valid format string returned failure", fmt);
	if (expect_type != type)
		FAIL("format '%s': expected type='%c', got type='%c'", fmt,
		     expect_type, type);
	if (expect_size != size)
		FAIL("format '%s': expected size=%d, got size=%d", fmt,
		     expect_size, size);
}

static void checkfail(const char *fmt)
{
	int type;
	int size;

	if (!utilfdt_decode_type(fmt, &type, &size))
		FAIL("format '%s': invalid format string returned success",
		     fmt);
}

/**
 * Add the given modifier to each of the valid sizes, and check that we get
 * correct values.
 *
 * \param modifier	Modifer string to use as a prefix
 * \param expected_size	The size (in bytes) that we expect (ignored for
 *			strings)
 */
static void check_sizes(char *modifier, int expected_size)
{
	char fmt[10], *ptr;

	/* set up a string with a hole in it for the format character */
	if (strlen(modifier) + 2 >= sizeof(fmt))
		FAIL("modifier string '%s' too long", modifier);
	strcpy(fmt, modifier);
	ptr = fmt + strlen(fmt);
	ptr[1] = '\0';

	/* now try each format character in turn */
	*ptr = 'i';
	check(fmt, 'i', expected_size);

	*ptr = 'u';
	check(fmt, 'u', expected_size);

	*ptr = 'x';
	check(fmt, 'x', expected_size);

	*ptr = 's';
	check(fmt, 's', -1);
}

static void test_utilfdt_decode_type(void)
{
	char fmt[10];
	int ch;

	/* check all the valid modifiers and sizes */
	check_sizes("", -1);
	check_sizes("b", 1);
	check_sizes("hh", 1);
	check_sizes("h", 2);
	check_sizes("l", 4);

	/* try every other character */
	checkfail("");
	for (ch = ' '; ch < 127; ch++) {
		if (!strchr("iuxs", ch)) {
			*fmt = ch;
			fmt[1] = '\0';
			checkfail(fmt);
		}
	}

	/* try a few modifiers at the end */
	checkfail("sx");
	checkfail("ihh");
	checkfail("xb");

	/* and one for the doomsday archives */
	checkfail("He has all the virtues I dislike and none of the vices "
			"I admire.");
}

int main(int argc, char *argv[])
{
	test_utilfdt_decode_type();
	PASS();
}
OpenPOWER on IntegriCloud