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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include <criterion/criterion.h>
#include <criterion/assert.h>
#include <stddef.h>
#include <string.h>
#include "../src/evalexpr.h"
TestSuite(parse_number);
Test(parse_number, parse_42)
{
size_t o;
int actual = parse_number("42", &o);
cr_expect(actual == 42, "Attendu : %d, renvoyé : %d", 42, actual);
cr_expect(o == 1, "Décalage attendu : %d, renvoyé : %zu", 1, o);
}
Test(parse_number, parse_4)
{
size_t o;
int actual = parse_number("4", &o);
cr_expect(actual == 4, "Attendu : %d, renvoyé : %d", 4, actual);
cr_expect(o == 0, "Décalage attendu : %d, renvoyé : %zu", 0, o);
}
TestSuite(my_pow);
Test(my_pow, my_pow00)
{
int actual = my_pow(0, 0);
cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual);
}
Test(my_pow, my_pown0)
{
int actual = my_pow(50, 0);
cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual);
}
Test(my_pow, my_pow0n)
{
int actual = my_pow(0, 42);
cr_expect(actual == 0, "Attendu : %d, renvoyé : %d", 0, actual);
}
Test(my_pow, my_powab)
{
int actual = my_pow(3,3);
cr_expect(actual == 27, "Attendu : %d, renvoyé : %d", 27, actual);
}
Test(my_pow, my_powab_2)
{
int actual = my_pow(4, 2);
cr_expect(actual == 16, "Attendu : %d, renvoyé : %d", 16, actual);
}
Test(my_pow, my_powab_3)
{
int actual = my_pow(10, 3);
cr_expect(actual == 1000, "Attendu : %d, renvoyé : %d", 1000, actual);
}
Test(my_pow, my_pow1n)
{
int actual = my_pow(1, 50);
cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual);
}
TestSuite(RPN);
Test(RPN, evalrpn_easiest)
{
const char test[] = "1 1 +";
int expected = 0;
int retval;
int res = 2;
int actual = evalrpn(test, &retval);
cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual);
cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval);
}
Test(RPN, evalrpn_35)
{
const char test[] = "5 2 2 ^ 3 + *";
int expected = 0;
int retval;
int res = 35;
int actual = evalrpn(test, &retval);
cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual);
cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval);
}
Test(RPN, evalrpn_22)
{
const char test[] = "10 6 9 3 + 0 11 - * / * 17 + 5 +";
int expected = 0;
int retval;
int res = 22;
int actual = evalrpn(test, &retval);
cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual);
cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval);
}
Test(RPN, evalrpn_minus20)
{
const char test[] = "3 4 5 * 3 + -";
int expected = 0;
int retval;
int res = -20;
int actual = evalrpn(test, &retval);
cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual);
cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval);
}
Test(RPN, evalrpn_zero)
{
const char test[] = "3 2 % 9 3 1 2 + * / -";
int expected = 0;
int retval;
int res = 0;
int actual = evalrpn(test, &retval);
cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual);
cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval);
}
TestSuite(Precedence);
Test(Precedence, parenthesis_above_all)
{
struct token pleft = { PAR_LEFT, '(' };
struct token pright = { PAR_RIGHT, ')' };
struct token unplus = { UN_PLUS, '+' };
struct token exp = { EXP, '^' };
struct token mul = { MUL, '*' };
struct token minus = { SUB, '-' };
int eq = opcmp(&pleft, &pright);
int sup = opcmp(&pleft, &unplus);
int inf = opcmp(&unplus, &pright);
int parftw = opcmp(&pleft, &exp);
int par4ever = opcmp(&pright, &mul);
int paragain = opcmp(&pright, &minus);
cr_expect(eq == 0, "Wrong order (equal)");
cr_expect(sup > 0, "Wrong order (>)");
cr_expect(inf < 0, "Wrong order (<)");
cr_expect(parftw > 0, "Wrong order (>)");
cr_expect(par4ever > 0, "Wrong order (>)");
cr_expect(paragain > 0, "Wrong order (>)");
}
Test(Precedence, other_precedence_tests)
{
struct token exp = { EXP, '^' };
struct token mul = { MUL, '*' };
struct token unplus = { UN_PLUS, '+' };
struct token minus = { SUB, '-' };
struct token plus = { ADD, '+' };
int eq = opcmp(&minus, &plus);
int sup = opcmp(&exp, &mul);
int inf = opcmp(&plus, &unplus);
cr_expect(eq == 0, "Wrong order (equal)");
cr_expect(sup > 0, "Wrong order (>)");
cr_expect(inf < 0, "Wrong order (<)");
}
TestSuite(ShuntingTests);
Test(ShuntingTests, shunt_simplest)
{
char *rpn;
const char *expr = "1 + 1";
int actual = shunting_yard(expr, &rpn);
cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual);
cr_expect(strcmp(rpn, "1 1 +") == 0, "Expected '1 1 +', got %s", rpn);
free(rpn);
}
Test(ShuntingTests, shunt_nico)
{
char *rpn;
const char *expr = "1 + 1 + 1";
int actual = shunting_yard(expr, &rpn);
cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual);
cr_expect(strcmp(rpn, "1 1 + 1 +") == 0, "Expected '1 1 + 1 +', got %s", rpn);
free(rpn);
}
Test(ShuntingTests, shunt_harderdaddy)
{
char *rpn;
const char *expr = "5*(2^2+3)";
int actual = shunting_yard(expr, &rpn);
cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual);
cr_expect(strcmp(rpn, "5 2 2 ^ 3 + *") == 0, "Expected '5 2 2 ^ 3 + *', got %s", rpn);
free(rpn);
}
Test(ShuntingTests, shunt_numbers)
{
char *rpn;
const char *expr = "42 + 50";
int actual = shunting_yard(expr, &rpn);
cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual);
cr_expect(strcmp(rpn, "42 50 +") == 0, "Expected '42 50 +', got %s", rpn);
free(rpn);
}
Test(ShuntingTests, shunt_mod)
{
char *rpn;
const char *expr = "42 % 50";
int actual = shunting_yard(expr, &rpn);
cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual);
cr_expect(strcmp(rpn, "42 50 %") == 0, "Expected '42 50 +', got %s", rpn);
free(rpn);
}
|