-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
38 lines (33 loc) · 1.01 KB
/
Copy pathtest.c
File metadata and controls
38 lines (33 loc) · 1.01 KB
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
#include <stdio.h>
double fcalc(const char **str, int *err, int _);
const double EPSILON = 0.00001;
unsigned int fails, total;
void test(const char *name, const char *str, double er, int ee)
{
total++;
int e;
double r = fcalc(&str, &e, 0);
double dr = r - er;
if ((dr > 0.0 ? dr : -dr) > EPSILON || e != ee)
{
printf("Test '%s' failed; Expected %f/%d, got %f/%d\n", name, er, ee, r, e);
fails++;
}
}
int main()
{
test("Integer", "11", 11, 0);
test("Real number", "11.32", 11.32, 0);
test("Sub-0-Real", ".32", 0.32, 0);
test("Negation", "-42", -42, 0);
test("Addition", "1.2 + 5", 6.2, 0);
test("Subtraction", "3 - 1.4", 1.6, 0);
test("Multiplication", "10 * 5", 50, 0);
test("Division", "50 / 10", 5, 0);
test("Precedence", "3 - 2 * 5", -7, 0);
test("Pi", "pi", 3.14159265358979, 0);
test("Parentheses", "3 * (1 + 2)", 9, 0);
test("Unmatched (", "(1 + 2", 0.0, -1);
printf("Performed %d tests, %d failures, %d%% success rate.\n", total, fails, 100 - fails * 100 / total);
return fails == 0 ? 0 : -1;
}