diff options
Diffstat (limited to 'mps/code/testlib.c')
| -rw-r--r-- | mps/code/testlib.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/mps/code/testlib.c b/mps/code/testlib.c new file mode 100644 index 00000000000..d7ea32271f9 --- /dev/null +++ b/mps/code/testlib.c | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | /* impl.c.testlib: TEST LIBRARY | ||
| 2 | * | ||
| 3 | * $HopeName: MMsrc!testlib.c(trunk.22) $ | ||
| 4 | * Copyright (C) 2000 Harlequin Limited. All rights reserved. | ||
| 5 | * | ||
| 6 | * .purpose: A library of functions that may be of use to unit tests. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "testlib.h" | ||
| 10 | #include "mps.h" | ||
| 11 | #include "mpm.h" | ||
| 12 | #include <math.h> | ||
| 13 | #include <stdlib.h> | ||
| 14 | #ifdef MPS_OS_IA | ||
| 15 | struct itimerspec; /* stop complaints from time.h */ | ||
| 16 | #endif | ||
| 17 | #include <time.h> | ||
| 18 | |||
| 19 | |||
| 20 | /* rnd -- a random number generator | ||
| 21 | * | ||
| 22 | * I nabbed it from "ML for the Working Programmer" | ||
| 23 | * Originally from: | ||
| 24 | * Stephen K Park & Keith W Miller (1988). Random number generators: | ||
| 25 | * good one are to find. Communications of the ACM, 31:1192-1201 | ||
| 26 | */ | ||
| 27 | |||
| 28 | unsigned long rnd(void) | ||
| 29 | { | ||
| 30 | static unsigned long seed = 1; | ||
| 31 | double s; | ||
| 32 | s = seed; | ||
| 33 | s *= 16807.0; | ||
| 34 | s = fmod(s, 2147483647.0); /* 2^31 - 1 */ | ||
| 35 | seed = (unsigned long)s; | ||
| 36 | return seed; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | /* randomize -- randomize the generator, or initialize to replay */ | ||
| 41 | |||
| 42 | void randomize(int argc, char **argv) | ||
| 43 | { | ||
| 44 | int i, k, n; | ||
| 45 | |||
| 46 | if(argc > 1) { | ||
| 47 | n = sscanf(argv[1], "%d", &k); | ||
| 48 | die((n == 1) ? MPS_RES_OK : MPS_RES_FAIL, "randomize"); | ||
| 49 | } else { | ||
| 50 | k = time(NULL) % 32000; | ||
| 51 | printf("Randomizing %d times.\n", k); | ||
| 52 | } | ||
| 53 | |||
| 54 | /* Randomize the random number generator a bit. */ | ||
| 55 | for (i = k; i > 0; --i) | ||
| 56 | rnd(); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | /* verror -- die with message */ | ||
| 61 | |||
| 62 | void verror(const char *format, va_list args) | ||
| 63 | { | ||
| 64 | fflush(stdout); /* synchronize */ | ||
| 65 | vfprintf(stderr, format, args); | ||
| 66 | fprintf(stderr, "\n"); | ||
| 67 | exit(1); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | /* error -- die with message */ | ||
| 72 | |||
| 73 | void error(const char *format, ...) | ||
| 74 | { | ||
| 75 | va_list args; | ||
| 76 | |||
| 77 | va_start(args, format); | ||
| 78 | verror(format, args); | ||
| 79 | va_end(args); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | /* die -- Test a return code, and exit on error */ | ||
| 84 | |||
| 85 | void die(mps_res_t res, const char *s) | ||
| 86 | { | ||
| 87 | if (res != MPS_RES_OK) { | ||
| 88 | error("\n%s: %d\n", s, res); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | /* die_expect -- Test a return code, and exit on unexpected result */ | ||
| 94 | |||
| 95 | void die_expect(mps_res_t res, mps_res_t expected, const char *s) | ||
| 96 | { | ||
| 97 | if (res != expected) { | ||
| 98 | error("\n%s: %d\n", s, res); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | /* cdie -- Test a C boolean, and exit on error */ | ||
| 104 | |||
| 105 | void cdie(int res, const char *s) | ||
| 106 | { | ||
| 107 | if (!res) { | ||
| 108 | error("\n%s: %d\n", s, res); | ||
| 109 | } | ||
| 110 | } | ||