feisty meow concerns codebase  2.140
test_chaos.cpp
Go to the documentation of this file.
1 /*
2 * Name : test_chaos
3 * Author : Chris Koeritz
4 **
5 * Copyright (c) 1992-$now By Author. This program is free software; you can *
6 * redistribute it and/or modify it under the terms of the GNU General Public *
7 * License as published by the Free Software Foundation; either version 2 of *
8 * the License or (at your option) any later version. This is online at: *
9 * http://www.fsf.org/copyleft/gpl.html *
10 * Please send any updates to: fred@gruntose.com *
11 */
12 
13 //#define DEBUG_CHAOS
14 
16 #include <basis/astring.h>
17 #include <basis/functions.h>
18 #include <basis/guards.h>
19 #include <loggers/console_logger.h>
20 #include <mathematics/chaos.h>
22 #include <unit_test/unit_base.h>
23 
24 using namespace application;
25 using namespace basis;
26 using namespace mathematics;
27 using namespace filesystem;
28 using namespace loggers;
29 using namespace structures;
30 using namespace textual;
31 using namespace timely;
32 using namespace unit_test;
33 
34 #define MAX_RANDOM_BINS 40
35 #define MAX_TEST_CYCLES 10008
36 #define AVG_EXPECTED_PER_BIN (double(MAX_TEST_CYCLES) / double(MAX_RANDOM_BINS))
37 #define VARIATION_ALLOWED (AVG_EXPECTED_PER_BIN * 0.1)
38 #define ANOMALIES_ALLOWED (MAX_RANDOM_BINS / 4)
39 
40 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
41 
42 class test_chaos : virtual public unit_base, virtual public application_shell
43 {
44 public:
45  test_chaos() : application_shell() {}
46  DEFINE_CLASS_NAME("test_chaos");
47  virtual int execute();
48 };
49 
50 int test_chaos::execute()
51 {
52  FUNCDEF("execute");
53 #ifdef DEBUG_CHAOS
54  LOG(a_sprintf("average expected=%f, variation allowed=%f",
56 #endif
57  int results[MAX_RANDOM_BINS];
58  for (int k = 0; k < MAX_RANDOM_BINS; k++) results[k] = 0;
60 
61  for (int i = 0; i < MAX_TEST_CYCLES; i++) {
62  // first test if exclusivity is ensured...
63  int res = randomizer.exclusive(0, MAX_RANDOM_BINS - 1);
64  ASSERT_FALSE( (res <= 0) || (res >= MAX_RANDOM_BINS - 1),
65  "exclusive test should not go out of bounds");
66  // then test for our statistics.
67  int base = randomizer.inclusive(-1000, 1000);
68  // pick a base for the number below.
69  res = randomizer.inclusive(base, base + MAX_RANDOM_BINS - 1);
70  ASSERT_FALSE( (res < base) || (res > base + MAX_RANDOM_BINS - 1),
71  "inclusive test should not go out of bounds");
72 //LOG(a_sprintf("adding it to %d bin", res - base));
73  results[res - base]++;
74  }
75 #ifdef DEBUG_CHAOS
76  LOG("Anomalies:");
77 #endif
78  int failed_any = false;
79  for (int j = 0; j < MAX_RANDOM_BINS; j++) {
81  failed_any++;
82 #ifdef DEBUG_CHAOS
83  LOG(astring(astring::SPRINTF, "%d: difference=%f",
84  j, double(results[j] - AVG_EXPECTED_PER_BIN)));
85 #endif
86  }
87  }
88 #ifdef DEBUG_CHAOS
89  if (!failed_any) LOG("None")
90  else LOG(a_sprintf("Saw %d anomalies of %d allowed.", failed_any, ANOMALIES_ALLOWED));
91 #endif
92 
93  ASSERT_FALSE(failed_any > ANOMALIES_ALLOWED,
94  "probability anomalies should be less than the allowed number");
95  return final_report();
96 }
97 
98 HOOPLE_MAIN(test_chaos, )
99 
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
#include <time.h>
Definition: earth_time.cpp:37
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define randomizer()
#define ANOMALIES_ALLOWED
Definition: test_chaos.cpp:38
#define AVG_EXPECTED_PER_BIN
Definition: test_chaos.cpp:36
#define MAX_TEST_CYCLES
Definition: test_chaos.cpp:35
#define MAX_RANDOM_BINS
Definition: test_chaos.cpp:34
#define LOG(to_print)
Definition: test_chaos.cpp:40
#define VARIATION_ALLOWED
Definition: test_chaos.cpp:37
#define ASSERT_FALSE(a, test_name)
Definition: unit_base.h:50