aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/tests/test_common.c
blob: e6e678162b4550f7576f4735e8613047b4eda947 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "test.h"
#include "util.h"

#include "util.c"


static void check_util(void)
{
	int ret;


	/* Check: sdiv_round_up() */

	ret = sdiv_round_up(10, 1);
	CHECK(ret == 10);
	ret = sdiv_round_up(10, 5);
	CHECK(ret == 2);
	ret = sdiv_round_up(10, 6);
	CHECK(ret == 2);
	ret = sdiv_round_up(10, 9);
	CHECK(ret == 2);
	ret = sdiv_round_up(10, 10);
	CHECK(ret == 1);
	ret = sdiv_round_up(-10, 1);
	CHECK(ret == -10);
	ret = sdiv_round_up(-10, 5);
	CHECK(ret == -2);
	ret = sdiv_round_up(-10, 6);
	CHECK(ret == -1);
	ret = sdiv_round_up(-10, 9);
	CHECK(ret == -1);
	ret = sdiv_round_up(-10, 10);
	CHECK(ret == -1);
	ret = sdiv_round_up(10, -1);
	CHECK(ret == -10);
	ret = sdiv_round_up(10, -5);
	CHECK(ret == -2);
	ret = sdiv_round_up(10, -6);
	CHECK(ret == -1);
	ret = sdiv_round_up(10, -9);
	CHECK(ret == -1);
	ret = sdiv_round_up(10, -10);
	CHECK(ret == -1);
	ret = sdiv_round_up(-10, -1);
	CHECK(ret == 10);
	ret = sdiv_round_up(-10, -5);
	CHECK(ret == 2);
	ret = sdiv_round_up(-10, -6);
	CHECK(ret == 2);
	ret = sdiv_round_up(-10, -9);
	CHECK(ret == 2);
	ret = sdiv_round_up(-10, -10);
	CHECK(ret == 1);


	/* Check: udiv_round_up() */

	ret = udiv_round_up(10, 1);
	CHECK(ret == 10);
	ret = udiv_round_up(10, 5);
	CHECK(ret == 2);
	ret = udiv_round_up(10, 6);
	CHECK(ret == 2);
	ret = udiv_round_up(10, 9);
	CHECK(ret == 2);
	ret = udiv_round_up(10, 10);
	CHECK(ret == 1);


	/* Check: sdiv_round() */

	ret = sdiv_round(10, 1);
	CHECK(ret == 10);
	ret = sdiv_round(10, 5);
	CHECK(ret == 2);
	ret = sdiv_round(10, 6);
	CHECK(ret == 2);
	ret = sdiv_round(10, 9);
	CHECK(ret == 1);
	ret = sdiv_round(10, 10);
	CHECK(ret == 1);
	ret = sdiv_round(15, 6);
	CHECK(ret == 3);
	ret = sdiv_round(-10, 1);
	CHECK(ret == -10);
	ret = sdiv_round(-10, 5);
	CHECK(ret == -2);
	ret = sdiv_round(-10, 6);
	CHECK(ret == -2);
	ret = sdiv_round(-10, 9);
	CHECK(ret == -1);
	ret = sdiv_round(-10, 10);
	CHECK(ret == -1);
	ret = sdiv_round(-15, 6);
	CHECK(ret == -3);
	ret = sdiv_round(10, -1);
	CHECK(ret == -10);
	ret = sdiv_round(10, -5);
	CHECK(ret == -2);
	ret = sdiv_round(10, -6);
	CHECK(ret == -2);
	ret = sdiv_round(10, -9);
	CHECK(ret == -1);
	ret = sdiv_round(10, -10);
	CHECK(ret == -1);
	ret = sdiv_round(15, -6);
	CHECK(ret == -3);
	ret = sdiv_round(-10, -1);
	CHECK(ret == 10);
	ret = sdiv_round(-10, -5);
	CHECK(ret == 2);
	ret = sdiv_round(-10, -6);
	CHECK(ret == 2);
	ret = sdiv_round(-10, -9);
	CHECK(ret == 1);
	ret = sdiv_round(-10, -10);
	CHECK(ret == 1);
	ret = sdiv_round(-15, -6);
	CHECK(ret == 3);


	/* Check: udiv_round() */

	ret = udiv_round(10, 1);
	CHECK(ret == 10);
	ret = udiv_round(10, 5);
	CHECK(ret == 2);
	ret = udiv_round(10, 6);
	CHECK(ret == 2);
	ret = udiv_round(10, 9);
	CHECK(ret == 1);
	ret = udiv_round(10, 10);
	CHECK(ret == 1);
	ret = udiv_round(15, 6);
	CHECK(ret == 3);


	/* Check: pow_int() */

	CHECK_EQ_II(pow_int(0, 1), 0);
	CHECK_EQ_II(pow_int(10, 0), 1);
	CHECK_EQ_II(pow_int(10, 1), 10);
	CHECK_EQ_II(pow_int(10, 2), 100);
	CHECK_EQ_II(pow_int(10, 3), 1000);
	CHECK_EQ_II(pow_int(10, 4), 10000);
	CHECK_EQ_II(pow_int(10, 5), 100000);
	CHECK_EQ_II(pow_int(-10, 0), 1);
	CHECK_EQ_II(pow_int(-10, 1), -10);
	CHECK_EQ_II(pow_int(-10, 2), 100);
	CHECK_EQ_II(pow_int(-10, 3), -1000);
	CHECK_EQ_II(pow_int(-10, 4), 10000);
	CHECK_EQ_II(pow_int(-10, 5), -100000);
	CHECK_EQ_II(pow_int(3, 0), 1);
	CHECK_EQ_II(pow_int(3, 1), 3);
	CHECK_EQ_II(pow_int(3, 2), 9);
	CHECK_EQ_II(pow_int(3, 3), 27);
	CHECK_EQ_II(pow_int(3, 4), 81);
	CHECK_EQ_II(pow_int(3, 5), 243);
	CHECK_EQ_II(pow_int(-3, 0), 1);
	CHECK_EQ_II(pow_int(-3, 1), -3);
	CHECK_EQ_II(pow_int(-3, 2), 9);
	CHECK_EQ_II(pow_int(-3, 3), -27);
	CHECK_EQ_II(pow_int(-3, 4), 81);
	CHECK_EQ_II(pow_int(-3, 5), -243);
}

int main(void)
{
	check_util();

	return 0;
}
bues.ch cgit interface