aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code/testlib.c
diff options
context:
space:
mode:
authorNick Barnes2001-10-31 14:40:56 +0000
committerNick Barnes2001-10-31 14:40:56 +0000
commit7acfca905d76140f4cc0b09c9a12de237de364cd (patch)
tree3ed8babfa3a73d30f29e08ca5d5adcda4ca4e826 /mps/code/testlib.c
parentb7ce4893f9902d57cd67ac9a92fa6c3d5a8fc833 (diff)
downloademacs-7acfca905d76140f4cc0b09c9a12de237de364cd.tar.gz
emacs-7acfca905d76140f4cc0b09c9a12de237de364cd.zip
Branch imports for masters.
Copied from Perforce Change: 23678 ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code/testlib.c')
-rw-r--r--mps/code/testlib.c110
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
15struct 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
28unsigned 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
42void 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
62void 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
73void 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
85void 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
95void 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
105void cdie(int res, const char *s)
106{
107 if (!res) {
108 error("\n%s: %d\n", s, res);
109 }
110}