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 #ifdef MTWIST_CONFIG
34 #include <mtwist_config.h>
35 #endif
36 
37 
38 #include <stdio.h>
39 
40 #include <mtwist.h>
41 
42 
43 int main(int argc, char *argv[]);
44 
45 
46 #define N_TESTS 1000
47 
48 #define TEST_SEED 54321U
49 
50 /* test set 1: results 0..19 */
51 #define TEST_SET_1_START 0
52 #define TEST_SET_1_END 19
53 #define TEST_SET_1_NRESULTS (TEST_SET_1_END - TEST_SET_1_START + 1)
54 
55 
56 #define ULONG_C(v) (v ## UL)
57 
58 static unsigned long const test_seed_54321_results_1[TEST_SET_1_NRESULTS] = {
59   ULONG_C(3915467345), ULONG_C(2189234826), ULONG_C(2679307290), ULONG_C( 787501152),
60   ULONG_C(3400771556), ULONG_C(3473638550), ULONG_C(1845911630), ULONG_C(4027756818),
61   ULONG_C(2332222920), ULONG_C( 127158527), ULONG_C(1775789767), ULONG_C(3371479562),
62   ULONG_C( 367824108), ULONG_C( 703848432), ULONG_C(3339822589), ULONG_C(1863375487),
63   ULONG_C(2100022882), ULONG_C(2466459787), ULONG_C( 217027622), ULONG_C( 932105407)
64 };
65 #define TEST_SET_1_RESULTS test_seed_54321_results_1
66 
67 /* test set 2: results 622..629 */
68 #define TEST_SET_2_START 622
69 #define TEST_SET_2_END 629
70 #define TEST_SET_2_NRESULTS (TEST_SET_2_END - TEST_SET_2_START + 1)
71 static unsigned long const test_seed_54321_results_2[TEST_SET_2_NRESULTS] = {
72   ULONG_C(2109020469), ULONG_C( 264978304), ULONG_C(3951898066), ULONG_C(3322908472),
73   ULONG_C(2243665931), ULONG_C(3379990241), ULONG_C(1427746768), ULONG_C(3217532946)
74 };
75 #define TEST_SET_2_RESULTS test_seed_54321_results_2
76 
77 /* test set 3: results 990..999 */
78 #define TEST_SET_3_START 990
79 #define TEST_SET_3_END 999
80 #define TEST_SET_3_NRESULTS (TEST_SET_3_END - TEST_SET_3_START + 1)
81 static unsigned long const test_seed_54321_results_3[TEST_SET_3_NRESULTS] = {
82   ULONG_C(4262956485), ULONG_C(2083563531), ULONG_C(1724557607), ULONG_C(4100776152), ULONG_C(4050777500),
83   ULONG_C(3146323433), ULONG_C(2882918002), ULONG_C(3891093309), ULONG_C(1534503088), ULONG_C(1821071197)
84 };
85 #define TEST_SET_3_RESULTS test_seed_54321_results_3
86 
87 
88 
89 int
main(int argc,char * argv[])90 main(int argc, char *argv[])
91 {
92   const char *program = argv[0];
93   int failures = 0;
94   int test;
95   mtwist* mt = NULL;
96 
97   if(argc != 1)
98     return 1;
99 
100   mt = mtwist_new();
101   if(!mt) {
102     fprintf(stderr, "%s: mtwist_new() failed\n", program);
103     failures++;
104     goto tidy;
105   }
106 
107   mtwist_init(mt, TEST_SEED);
108 
109   for(test = 0; test < N_TESTS; test++) {
110     int check = 0;
111     unsigned long expected_v;
112     unsigned long v;
113 
114     v = mtwist_u32rand(mt);
115 
116     if(test >= TEST_SET_1_START && test <= TEST_SET_1_END) {
117       check = 1;
118       expected_v = TEST_SET_1_RESULTS[test - TEST_SET_1_START];
119     } else if(test >= TEST_SET_2_START && test <= TEST_SET_2_END) {
120       check = 1;
121       expected_v = TEST_SET_2_RESULTS[test - TEST_SET_2_START];
122     } else if(test >= TEST_SET_3_START && test <= TEST_SET_3_END) {
123       check = 1;
124       expected_v = TEST_SET_3_RESULTS[test - TEST_SET_3_START];
125     }
126 
127     if(check && v != expected_v) {
128       fprintf(stderr,
129               "%s: Test %3d returned value: %lu expected %lu\n",
130               program, test, v, expected_v);
131       failures++;
132     } else {
133 #if defined(DEBUG) && DEBUG > 1
134       if(check)
135         fprintf(stderr,
136                 "%s: Test %3d returned expected value: %lu OK\n",
137                 program, test, v);
138       else
139         fprintf(stderr, "%s: Test %3d returned value: %lu OK\n",
140                 program, test, v);
141 #endif
142     }
143   }
144 
145   fprintf(stdout, "%s: Returned %d failures\n", program, failures);
146 
147   tidy:
148   if(mt)
149     mtwist_free(mt);
150 
151   return failures;
152 }
153