Debian Codegraph @

BraheTest/BraheTest.cpp

raw
1 /*
2 Brahe is a heterogenous collection of mathematical tools, written in Standard C.
3
4 Copyright 2011 Scott Robert Ladd. All rights reserved.
5
6 Brahe is user-supported open source software. Its continued development is dependent
7 on financial support from the community. You can provide funding by visiting the Brahe
8 website at:
9
10 http://www.coyotegulch.com
11
12 You may license Brahe in one of two fashions:
13
14 1) Simplified BSD License (FreeBSD License)
15
16 Redistribution and use in source and binary forms, with or without modification, are
17 permitted provided that the following conditions are met:
18
19 1. Redistributions of source code must retain the above copyright notice, this list of
20 conditions and the following disclaimer.
21
22 2. Redistributions in binary form must reproduce the above copyright notice, this list
23 of conditions and the following disclaimer in the documentation and/or other materials
24 provided with the distribution.
25
26 THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 The views and conclusions contained in the software and documentation are those of the
37 authors and should not be interpreted as representing official policies, either expressed
38 or implied, of Scott Robert Ladd.
39
40 2) Closed-Source Proprietary License
41
42 If your project is a closed-source or proprietary project, the Simplified BSD License may
43 not be appropriate or desirable. In such cases, contact the Itzam copyright holder to
44 arrange your purchase of an appropriate license.
45
46 The author can be contacted at:
47
48 [email protected]
49 [email protected]
50 http:www.coyotegulch.com
51 */
52
53 #include "stdafx.h"
54 #include "../src/mathtools.h"
55 #include "../src/prng.h"
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <time.h>
59
60 void fft_test()
61 {
62 brahe_wave_factor_t factors [] =
63 {
64 { 100.0, 20.0 },
65 { 50.0, 40.0 },
66 { 5.0, 100.0 }
67 };
68
69 // banner
70 printf("\n<<<<------------------>>>>\n");
71 printf("<<<<---- FFT Test ---->>>>\n");
72 printf("<<<<------------------>>>>\n");
73
74 // generate a sinusoid
75 printf("generating sinusoid...\n");
76 double * signal = brahe_make_sinusoid(factors, 3, 1024);
77
78 // perform an fft on the sinusoid
79 printf("computing FFT...\n");
80 double * fft = brahe_simple_fft(signal, 1024);
81
82 // see if the fft has peaks in the right places
83 // these indexes will be WRONG if the length of the signal changes!
84 if ((fft[ 0] < 1.0)
85 && (fft[ 5] > 20.0)
86 && (fft[ 8] < 4.0)
87 && (fft[ 10] > 36.0)
88 && (fft[ 42] < 0.6)
89 && (fft[102] > 75.0)
90 && (fft[511] < 0.3))
91 printf("Good data... success!\n");
92 else
93 printf("FFT produced wrong data -- ERROR\n");
94
95 // cleanup
96 free(fft);
97 free(signal);
98 }
99
100 void pretty_test()
101 {
102 int64_t x[] = { 0, -1, 2, 11, 500, 97, -10263, 1768372193, 0xA1B2C3D4E5F6, 1223372036854775808 };
103 char * c;
104 char * t;
105
106 // banner
107 printf("\n<<<<------------------------->>>>\n");
108 printf("<<<<---- Pretty Integers ---->>>>\n");
109 printf("<<<<------------------------->>>>\n");
110
111 for (int i = 0; i < 10; ++i)
112 {
113 c = brahe_pretty_int(x[i], BRAHE_PRETTY_COMMA);
114 t = brahe_pretty_int(x[i], BRAHE_PRETTY_TEXT);
115
116 printf("\n%ld\n%s\n%s\n",x[i], c, t);
117
118 free(t);
119 free(c);
120 }
121 }
122
123 void trig_test()
124 {
125 int i;
126 double a [] = { 1.0, 1.33333333333333333, 1.5 };
127
128 // banner
129 printf("\n<<<<-------------------->>>>\n");
130 printf("<<<<---- Extra Trig ---->>>>\n");
131 printf("<<<<-------------------->>>>\n");
132
133 for (i = 0; i < 3; ++i)
134 {
135 printf("atanh(tanh(%11.8f)) = %11.8f\n", a[i], brahe_atanh(tanh(a[i])));
136 printf("atanh(tanh(%11.8f)) = %11.8f\n", -a[i], brahe_atanh(tanh(-a[i])));
137 printf("asinh(sinh(%11.8f)) = %11.8f\n", a[i], brahe_asinh(sinh(a[i])));
138 printf("asinh(sinh(%11.8f)) = %11.8f\n", -a[i], brahe_asinh(sinh(-a[i])));
139 printf("acosh(cosh(%11.8f)) = %11.8f\n", a[i], brahe_acosh(cosh(a[i])));
140 printf("acosh(cosh(%11.8f)) = %11.8f\n", -a[i], brahe_acosh(cosh(-a[i])));
141 }
142 }
143
144 void lcm_test()
145 {
146 // test data
147 static const size_t TEST_SIZE = 7;
148
149 static const uint64_t test_set1[] =
150 {
151 0, 1, 4, 25, 72, 36, 12
152 };
153
154 static const uint64_t test_set2[] =
155 {
156 0, 4, 6, 30, 90, 125, 15
157 };
158
159 static const uint64_t expected[] =
160 {
161 0, 4, 12, 150, 360, 4500, 60
162 };
163
164 // counts errors
165 size_t i, errcnt = 0;
166 uint64_t result;
167
168 // banner
169 printf("\n<<<<------------------------------->>>>\n");
170 printf("<<<<---- Least Common Multiple ---->>>>\n");
171 printf("<<<<------------------------------->>>>\n");
172
173 for (i = 0; i < TEST_SIZE; ++i)
174 {
175 result = brahe_lcm(test_set1[i],test_set2[i]);
176
177 printf("lcm(%lu,%lu) = %lu (should be %lu)\n",
178 (unsigned long)test_set1[i],
179 (unsigned long)test_set2[i],
180 (unsigned long)result,
181 (unsigned long)expected[i]);
182
183 if (result != expected[i])
184 ++errcnt;
185 }
186 }
187
188 void gcf_test()
189 {
190 // test data
191 static const size_t TEST_SIZE = 6;
192
193 static const uint64_t test_set1[] =
194 {
195 0, 1, 156, 24, 28, 644
196 };
197
198 static const uint64_t test_set2[] =
199 {
200 0, 4, 112, 36, 35, 805
201 };
202
203 static const uint64_t expected[] =
204 {
205 0, 1, 4, 12, 7, 161
206 };
207
208 // counts errors
209 size_t i, errcnt = 0;
210 uint64_t result;
211
212 // banner
213 printf("\n<<<<-------------------------------->>>>\n");
214 printf("<<<<---- Greatest Common Fcator ---->>>>\n");
215 printf("<<<<-------------------------------->>>>\n");
216
217 for (i = 0; i < TEST_SIZE; ++i)
218 {
219 result = brahe_gcf(test_set1[i],test_set2[i]);
220
221 printf("gcf(%lu,%lu) = %lu (should be %lu)\n",
222 (unsigned long)test_set1[i],
223 (unsigned long)test_set2[i],
224 (unsigned long)result,
225 (unsigned long)expected[i]);
226
227 if (result != expected[i])
228 ++errcnt;
229 }
230 }
231
232 #define PRNG_TEST_SIZE 100000000
233 #define NUM_BUCKETS 13
234
235 double test_prng(brahe_prng_type_t prng_type)
236 {
237 uint32_t total, dummy;
238 size_t i;
239 double n, l, s;
240 int counts[NUM_BUCKETS];
241 brahe_prng_state_t prng_state;
242 clock_t start, stop;
243
244 // banner
245 printf("\n<<<<------------------------>>>>\n");
246 printf("<<<<---- Random Numbers ---->>>>\n");
247 printf("<<<<------------------------>>>>\n");
248
249 switch (prng_type)
250 {
251 case BRAHE_PRNG_MARSENNE_TWISTER:
252 printf("\n>>>> MARSENNE TWISTER <<<<\n");
253 break;
254
255 case BRAHE_PRNG_KISS:
256 printf("\n>>>> KISS <<<<\n");
257 break;
258
259 case BRAHE_PRNG_MWC1038:
260 printf("\n>>>> MWC1038 <<<<\n");
261 break;
262
263 case BRAHE_PRNG_CMWC4096:
264 printf("\n>>>> CMWC4096 <<<<\n");
265 break;
266
267 case BRAHE_PRNG_ISAAC:
268 printf("\n>>>> ISAAC <<<<\n");
269 break;
270 }
271
272 // create the generator
273 brahe_prng_init(&prng_state,prng_type,BRAHE_UNKNOWN_SEED);
274
275 // each real function
276 printf("\nrand_real1 - interval [0,1]\n");
277
278 s = 1.1;
279 l = -0.1;
280
281 for (i = 0; i < PRNG_TEST_SIZE; ++i)
282 {
283 n = brahe_prng_real1(&prng_state);
284
285 if (n < s) s = n;
286 if (n > l) l = n;
287 }
288
289 printf(" largest = %15.14f\n smallest = %15.14f\n", l, s);
290
291 // each real function
292 printf("\nrand_real2 - interval [0,1)\n");
293
294 s = 1.1;
295 l = -0.1;
296
297 for (i = 0; i < PRNG_TEST_SIZE; ++i)
298 {
299 n = brahe_prng_real2(&prng_state);
300
301 if (n < s) s = n;
302 if (n > l) l = n;
303 }
304
305 printf(" largest = %15.14f\n smallest = %15.14f\n", l, s);
306
307 // each real function
308 printf("\nrand_real3 - interval (0,1)\n");
309
310 s = 1.1;
311 l = -0.1;
312
313 for (i = 0; i < PRNG_TEST_SIZE; ++i)
314 {
315 n = brahe_prng_real3(&prng_state);
316
317 if (n < s) s = n;
318 if (n > l) l = n;
319 }
320
321 printf(" largest = %15.14f\n smallest = %15.14f\n", l, s);
322
323 // each real function
324 printf("\nrand_real53 - interval [0,1) - 53-bit precision\n");
325
326 s = 1.1;
327 l = -0.1;
328
329 for (i = 0; i < PRNG_TEST_SIZE; ++i)
330 {
331 n = brahe_prng_real53(&prng_state);
332
333 if (n < s) s = n;
334 if (n > l) l = n;
335 }
336
337 printf(" largest = %15.14f\n smallest = %15.14f\n", l, s);
338
339 // check ranges
340 for (i = 0; i < NUM_BUCKETS; ++i)
341 counts[i] = 0;
342
343 for (i = 0; i < PRNG_TEST_SIZE; ++i)
344 ++counts[brahe_prng_index(&prng_state,NUM_BUCKETS)];
345
346 printf("\n");
347
348 total = 0;
349
350 for (i = 0; i < NUM_BUCKETS; ++i)
351 {
352 printf("counts %3d = %10d\n", i, counts[i]);
353 total += counts[i];
354 }
355
356 printf(" total = %10d\n", total);
357
358 // get starting time
359 start = clock();
360
361 // test generation speed
362 for (i = 0; i < PRNG_TEST_SIZE; ++i)
363 dummy = brahe_prng_next(&prng_state);
364
365 // calculate run time
366 stop = clock();
367
368 // free resources
369 brahe_prng_free(&prng_state);
370
371 // done
372 return (double)(stop - start) / (double)CLOCKS_PER_SEC;
373 }
374
375 int _tmain(int argc, _TCHAR* argv[])
376 {
377 fft_test();
378 pretty_test();
379 trig_test();
380 lcm_test();
381 gcf_test();
382
383 double mtwister_time = test_prng(BRAHE_PRNG_MARSENNE_TWISTER);
384 double kissrng_time = test_prng(BRAHE_PRNG_KISS);
385 double mwc1038_time = test_prng(BRAHE_PRNG_MWC1038);
386 double cmwc4096_time = test_prng(BRAHE_PRNG_CMWC4096);
387 double isaac_time = test_prng(BRAHE_PRNG_ISAAC);
388
389 printf("\nALGORITHM TIMING (random numbers / second)\n\n");
390 printf(" Marsenne Twister = %5.2f (%10.0f/sec)\n", mtwister_time, ((double)PRNG_TEST_SIZE / mtwister_time));
391 printf(" KISS = %5.2f (%10.0f/sec)\n", kissrng_time, ((double)PRNG_TEST_SIZE / kissrng_time));
392 printf(" MWC1038 = %5.2f (%10.0f/sec)\n", mwc1038_time, ((double)PRNG_TEST_SIZE / mwc1038_time));
393 printf(" CMWC4096 = %5.2f (%10.0f/sec)\n", cmwc4096_time, ((double)PRNG_TEST_SIZE / cmwc4096_time));
394 printf(" ISAAC = %5.2f (%10.0f/sec)\n", isaac_time, ((double)PRNG_TEST_SIZE / isaac_time));
395
396 return 0;
397 }