Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (120 sloc) 4.735 kB
ea01e11 @dajobe Initial import
authored
1 /* -*- Mode: c; c-basic-offset: 2 -*-
2 *
3 * test.c - Mersenne Twister tests
4 *
5 * This is free and unencumbered software released into the public domain.
6 *
7 * Anyone is free to copy, modify, publish, use, compile, sell, or
8 * distribute this software, either in source code form or as a compiled
9 * binary, for any purpose, commercial or non-commercial, and by any
10 * means.
11 *
12 * In jurisdictions that recognize copyright laws, the author or authors
13 * of this software dedicate any and all copyright interest in the
14 * software to the public domain. We make this dedication for the benefit
15 * of the public at large and to the detriment of our heirs and
16 * successors. We intend this dedication to be an overt act of
17 * relinquishment in perpetuity of all present and future rights to this
18 * software under copyright law.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * For more information, please refer to <http://unlicense.org/>
29 *
30 */
31
32
33 #include <stdio.h>
34
35 #include <mtwist.h>
36
37
38 int main(int argc, char *argv[]);
39
40
41 #define N_TESTS 1000
42
43 #define TEST_SEED 54321U
44
45 /* test set 1: results 0..19 */
46 #define TEST_SET_1_START 0
47 #define TEST_SET_1_END 19
48 #define TEST_SET_1_NRESULTS (TEST_SET_1_END - TEST_SET_1_START + 1)
49
50
51 #define ULONG_C(v) (v ## UL)
52
53 static unsigned long const test_seed_54321_results_1[TEST_SET_1_NRESULTS] = {
54 ULONG_C(3915467345), ULONG_C(2189234826), ULONG_C(2679307290), ULONG_C( 787501152),
55 ULONG_C(3400771556), ULONG_C(3473638550), ULONG_C(1845911630), ULONG_C(4027756818),
56 ULONG_C(2332222920), ULONG_C( 127158527), ULONG_C(1775789767), ULONG_C(3371479562),
57 ULONG_C( 367824108), ULONG_C( 703848432), ULONG_C(3339822589), ULONG_C(1863375487),
58 ULONG_C(2100022882), ULONG_C(2466459787), ULONG_C( 217027622), ULONG_C( 932105407)
59 };
60 #define TEST_SET_1_RESULTS test_seed_54321_results_1
61
62 /* test set 2: results 622..629 */
63 #define TEST_SET_2_START 622
64 #define TEST_SET_2_END 629
65 #define TEST_SET_2_NRESULTS (TEST_SET_2_END - TEST_SET_2_START + 1)
66 static unsigned long const test_seed_54321_results_2[TEST_SET_2_NRESULTS] = {
67 ULONG_C(2109020469), ULONG_C( 264978304), ULONG_C(3951898066), ULONG_C(3322908472),
68 ULONG_C(2243665931), ULONG_C(3379990241), ULONG_C(1427746768), ULONG_C(3217532946)
69 };
70 #define TEST_SET_2_RESULTS test_seed_54321_results_2
71
72 /* test set 3: results 990..999 */
73 #define TEST_SET_3_START 990
74 #define TEST_SET_3_END 999
75 #define TEST_SET_3_NRESULTS (TEST_SET_3_END - TEST_SET_3_START + 1)
76 static unsigned long const test_seed_54321_results_3[TEST_SET_3_NRESULTS] = {
77 ULONG_C(4262956485), ULONG_C(2083563531), ULONG_C(1724557607), ULONG_C(4100776152), ULONG_C(4050777500),
78 ULONG_C(3146323433), ULONG_C(2882918002), ULONG_C(3891093309), ULONG_C(1534503088), ULONG_C(1821071197)
79 };
80 #define TEST_SET_3_RESULTS test_seed_54321_results_3
81
82
83
84 int
85 main(int argc, char *argv[])
86 {
87 const char *program = argv[0];
88 int failures = 0;
89 int test;
90 mtwist* mt = NULL;
91
92 if(argc != 1)
93 return 1;
94
95 mt = mtwist_new();
96 if(!mt) {
97 fprintf(stderr, "%s: mtwist_new() failed\n", program);
98 failures++;
99 goto tidy;
100 }
101
102 mtwist_init(mt, TEST_SEED);
103
104 for(test = 0; test < N_TESTS; test++) {
105 int check = 0;
106 unsigned long expected_v;
107 unsigned long v;
108
109 v = mtwist_u32rand(mt);
110
111 if(test >= TEST_SET_1_START && test <= TEST_SET_1_END) {
112 check = 1;
113 expected_v = TEST_SET_1_RESULTS[test - TEST_SET_1_START];
114 } else if(test >= TEST_SET_2_START && test <= TEST_SET_2_END) {
115 check = 1;
116 expected_v = TEST_SET_2_RESULTS[test - TEST_SET_2_START];
117 } else if(test >= TEST_SET_3_START && test <= TEST_SET_3_END) {
118 check = 1;
119 expected_v = TEST_SET_3_RESULTS[test - TEST_SET_3_START];
120 }
121
122 if(check && v != expected_v) {
123 fprintf(stderr,
124 "%s: Test %3d returned value: %lu expected %lu\n",
125 program, test, v, expected_v);
126 failures++;
127 } else {
128 #if DEBUG > 1
129 if(check)
130 fprintf(stderr,
131 "%s: Test %3d returned expected value: %lu OK\n",
132 program, test, v);
133 else
134 fprintf(stderr, "%s: Test %3d returned value: %lu OK\n",
135 program, test, v);
136 #endif
137 }
138 }
139
140 fprintf(stderr, "%s: Returned %d failures\n", program, failures);
141
142 tidy:
143 if(mt)
144 mtwist_free(mt);
145
146 return failures;
147 }
Something went wrong with that request. Please try again.