vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / test52.c
blob39090d67ec51835214dae31d9a46ec62a4525d2f
1 #define _POSIX_SOURCE 1
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <signal.h>
8 #include <math.h>
10 #define ROUNDS 20
11 #define SWAPS 40
12 #define MAX_ERROR 5
14 int pipefdc[2];
15 int pipefdp[2];
16 int subtest = 0, errct = 0;
17 int child_is_dead = 0;
19 void dead_child(int n);
20 void do_child(void);
21 void do_parent(void);
22 void do_calcs(void);
23 void err(int n);
24 void quit(void);
26 void err(int n)
28 printf("Subtest %d, error %d\n", subtest, n);
30 if (errct++ > MAX_ERROR) {
31 printf("Too many errors; test aborted\n");
32 exit(1);
36 void quit(void)
38 if (errct == 0) {
39 printf("ok\n");
40 exit(0);
41 } else {
42 printf("%d errors\n", errct);
43 exit(1);
47 void do_calcs(void)
49 float a, b, c, d, e;
50 float foo, bar;
51 int i;
52 subtest = 3;
54 a = 1.1;
55 b = 2.2;
56 c = 3.3;
57 d = 4.4;
58 e = 5.5;
60 foo = a * b; /* 2.42 */
61 bar = c * d; /* 14.52 */
63 i = 0;
64 while(i < ROUNDS) {
65 foo += c; /* 5.72 */
66 foo *= d; /* 25.168 */
67 foo /= e; /* 4.5760 */
68 bar -= a; /* 13.42 */
69 bar *= b; /* 29.524 */
70 bar /= e; /* 5.3680 */
72 /* Undo */
73 foo *= e;
74 foo /= d;
75 foo -= c;
77 bar *= e;
78 bar /= b;
79 bar += a;
81 i++;
84 if (fabs(foo - (a * b)) > 0.0001) err(1);
85 if (fabs(bar - (c * d)) > 0.0001) err(2);
88 void dead_child(int n)
90 int status;
91 subtest = 4;
92 n = n;
94 if (wait(&status) == -1) err(1);
96 if (!WIFEXITED(status)) {
97 err(2);
98 quit();
99 } else {
100 errct += WEXITSTATUS(status);
101 child_is_dead = 1;
105 void do_child(void)
107 char buf[2];
108 int s;
110 s = 0;
111 close(pipefdp[0]);
112 close(pipefdc[1]);
114 while(s < SWAPS) {
115 do_calcs();
117 /* Wake up parent */
118 write(pipefdp[1], buf, 1);
120 /* Wait for parent to wake me up */
121 read(pipefdc[0], buf, 1);
123 s++;
125 exit(0);
128 void do_parent(void)
130 int s;
131 char buf[2];
132 struct sigaction sa;
133 subtest = 2;
135 sa.sa_handler = dead_child;
136 sa.sa_flags = 0;
137 if (sigaction(SIGCHLD, &sa, NULL) == -1) err(1);
139 s = 0;
140 close(pipefdp[1]);
141 close(pipefdc[0]);
143 while(s < SWAPS) {
144 /* Wait for child to wake me up */
145 read(pipefdp[0], buf, 1);
147 do_calcs();
149 /* Wake up child */
150 write(pipefdc[1], buf, 1);
151 s++;
154 while(child_is_dead == 0) { fflush(stdout); } /* Busy wait */
156 quit();
159 int main(void)
161 pid_t r;
162 subtest = 1;
164 #ifdef __GNUC__
165 printf("Test 52 (GCC) ");
166 #else
167 printf("Test 52 (ACK) ");
168 #endif
169 fflush(stdout);
171 if (pipe(pipefdc) == -1) err(1);
172 if (pipe(pipefdp) == -1) err(2);
174 r = fork();
175 if(r < 0) {
176 err(3);
177 } else if(r == 0) {
178 /* Child */
179 do_child();
180 } else {
181 /* Parent */
182 do_parent();
185 return(0); /* Never reached */