forked from Tordek/cheat
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.c
140 lines (111 loc) · 2.16 KB
/
example.c
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
#include <cheat.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
CHEAT_TEST(mathematics_still_work,
cheat_assert(2 + 2 == 4);
cheat_assert_not(2 + 2 == 5);
)
CHEAT_DECLARE(
static double f(double const x,
double const y) {
return 2 / (x * x) - y * y / 2;
}
static bool p(double const x,
double const y,
double const e) {
double d;
if (x < y)
d = y - x;
else /* Turbo C could not parse this without this comment. */
d = x - y;
return d <= e;
}
)
CHEAT_TEST(physics_still_work,
double xn = 3;
double x0 = 1;
size_t n = 128;
double s;
double y0 = 2;
double y;
size_t i;
double x;
s = (xn - x0) / (double )n;
y = y0;
for (i = 0;
i <= n;
++i) {
x = x0 + s * (double )i;
y += s * f(x, y);
}
cheat_assert(p(y, 1, 0.1));
)
CHEAT_DECLARE(
extern size_t size;
)
CHEAT_DECLARE(
size_t size;
static char* heap;
)
CHEAT_SET_UP(
char const* stack;
(void )fputs("Setting up", stderr);
stack = "string";
size = strlen(stack) + 1;
heap = CHEAT_CAST(char*, malloc(size));
memcpy(heap, stack, size);
)
CHEAT_TEAR_DOWN(
(void )fputs(" and tearing down.\n", stderr);
free(heap);
)
CHEAT_TEST(philosophy_never_worked,
char const* stack;
stack = "string";
cheat_assert(heap == stack);
cheat_assert(strcmp(heap, stack) == 0);
)
CHEAT_TEST(test,
(void )fputs(", running a test", stderr);
)
#ifndef OXYGEN_MOLECULE
#define OXYGEN_MOLECULE (0 == 0)
#endif
CHEAT_TEST(chemistry_is_strange,
cheat_assert(OXYGEN_MOLECULE);
)
CHEAT_DECLARE(
enum things {
THIS_TEST CHEAT_COMMA
IMPORTANT_TEST
};
)
CHEAT_IGNORE(important,
cheat_assert(THIS_TEST == IMPORTANT_TEST);
)
CHEAT_IGNORE(unimportant,
cheat_assert(THIS_TEST != IMPORTANT_TEST);
)
CHEAT_SKIP(pointless,
cheat_assert((0 | ~0) == 0);
)
CHEAT_TEST(story,
(void )puts("Here's a touching story.");
(void )puts("Once upon a time I ran a test.");
)
CHEAT_TEST(streams_get_captured,
if (fopen(".PHONY", "r") == NULL) {
(void )fwrite(" [\n", 1, 3, stderr);
perror("fopen");
(void )fputc(']', stderr);
}
)
CHEAT_TEST(crash,
(void )fputs(" and crashing.\n", stderr);
((void (*)(void))NULL)();
)
CHEAT_TEST(the_end,
(void )puts("The end.");
)
CHEAT_TEST(bye, ;)