minix/test/test52.c
Thomas Veerman a209c3ae12 Fix a ton of compiler warnings
This patch fixes most of current reasons to generate compiler warnings.
The changes consist of:
 - adding missing casts
 - hiding or unhiding function declarations
 - including headers where missing
 - add __UNCONST when assigning a const char * to a char *
 - adding missing return statements
 - changing some types from unsigned to signed, as the code seems to want
   signed ints
 - converting old-style function definitions to current style (i.e.,
   void func(param1, param2) short param1, param2; {...} to
   void func (short param1, short param2) {...})
 - making the compiler silent about signed vs unsigned comparisons. We
   have too many of those in the new libc to fix.

A number of bugs in the test set were fixed. These bugs were never
triggered with our old libc. Consequently, these tests are now forced to
link with the new libc or they will generate errors (in particular tests 43
and 55).

Most changes in NetBSD libc are limited to moving aroudn "#ifndef __minix"
or stuff related to Minix-specific things (code in sys-minix or gen/minix).
2011-11-14 10:07:49 +00:00

189 lines
2.6 KiB
C

#define _POSIX_SOURCE 1
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <math.h>
#define ROUNDS 20
#define SWAPS 40
#define MAX_ERROR 5
int pipefdc[2];
int pipefdp[2];
int subtest = 0, errct = 0;
int child_is_dead = 0;
void dead_child(int n);
void do_child(void);
void do_parent(void);
void do_calcs(void);
void err(int n);
void quit(void);
void err(int n)
{
printf("Subtest %d, error %d\n", subtest, n);
if (errct++ > MAX_ERROR) {
printf("Too many errors; test aborted\n");
exit(1);
}
}
void quit(void)
{
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}
void do_calcs(void)
{
float a, b, c, d, e;
float foo, bar;
int i;
subtest = 3;
a = 1.1;
b = 2.2;
c = 3.3;
d = 4.4;
e = 5.5;
foo = a * b; /* 2.42 */
bar = c * d; /* 14.52 */
i = 0;
while(i < ROUNDS) {
foo += c; /* 5.72 */
foo *= d; /* 25.168 */
foo /= e; /* 4.5760 */
bar -= a; /* 13.42 */
bar *= b; /* 29.524 */
bar /= e; /* 5.3680 */
/* Undo */
foo *= e;
foo /= d;
foo -= c;
bar *= e;
bar /= b;
bar += a;
i++;
}
if (fabs(foo - (a * b)) > 0.0001) err(1);
if (fabs(bar - (c * d)) > 0.0001) err(2);
}
void dead_child(int n)
{
int status;
subtest = 4;
(void) n; /* Avoid warning about unused parameter */
if (wait(&status) == -1) err(1);
if (!WIFEXITED(status)) {
err(2);
quit();
} else {
errct += WEXITSTATUS(status);
child_is_dead = 1;
}
}
void do_child(void)
{
char buf[2];
int s;
s = 0;
close(pipefdp[0]);
close(pipefdc[1]);
while(s < SWAPS) {
do_calcs();
/* Wake up parent */
write(pipefdp[1], buf, 1);
/* Wait for parent to wake me up */
read(pipefdc[0], buf, 1);
s++;
}
exit(0);
}
void do_parent(void)
{
int s;
char buf[2];
struct sigaction sa;
subtest = 2;
sa.sa_handler = dead_child;
sa.sa_flags = 0;
if (sigaction(SIGCHLD, &sa, NULL) == -1) err(1);
s = 0;
close(pipefdp[1]);
close(pipefdc[0]);
while(s < SWAPS) {
/* Wait for child to wake me up */
read(pipefdp[0], buf, 1);
do_calcs();
/* Wake up child */
write(pipefdc[1], buf, 1);
s++;
}
while(child_is_dead == 0) { fflush(stdout); } /* Busy wait */
quit();
}
int main(void)
{
pid_t r;
subtest = 1;
#ifdef __GNUC__
printf("Test 52 (GCC) ");
#else
printf("Test 52 (ACK) ");
#endif
fflush(stdout);
if (pipe(pipefdc) == -1) err(1);
if (pipe(pipefdp) == -1) err(2);
r = fork();
if(r < 0) {
err(3);
} else if(r == 0) {
/* Child */
do_child();
} else {
/* Parent */
do_parent();
}
return(0); /* Never reached */
}