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>
20#include <mathematics/chaos.h>
22#include <unit_test/unit_base.h>
23
24using namespace application;
25using namespace basis;
26using namespace mathematics;
27using namespace filesystem;
28using namespace loggers;
29using namespace structures;
30using namespace textual;
31using namespace timely;
32using 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
42class test_chaos : virtual public unit_base, virtual public application_shell
43{
44public:
45 test_chaos() : application_shell() {}
46 DEFINE_CLASS_NAME("test_chaos");
47 virtual int execute();
48};
49
50int 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
94 "probability anomalies should be less than the allowed number");
95 return final_report();
96}
97
98HOOPLE_MAIN(test_chaos, )
99
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
application_shell()
constructs an application_shell to serve as the root of the program.
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
int exclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" < r < "high".
Definition chaos.h:98
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
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.
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>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define randomizer()
#define ANOMALIES_ALLOWED
#define AVG_EXPECTED_PER_BIN
#define MAX_TEST_CYCLES
#define MAX_RANDOM_BINS
#define LOG(to_print)
#define VARIATION_ALLOWED
#define ASSERT_FALSE(a, test_name)
Definition unit_base.h:50