test_chaos.cpp

Go to the documentation of this file.
00001 /*
00002 *  Name   : test_chaos
00003 *  Author : Chris Koeritz
00004 **
00005 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00006 * redistribute it and/or modify it under the terms of the GNU General Public  *
00007 * License as published by the Free Software Foundation; either version 2 of   *
00008 * the License or (at your option) any later version.  This is online at:      *
00009 *     http://www.fsf.org/copyleft/gpl.html                                    *
00010 * Please send any updates to: fred@gruntose.com                               *
00011 */
00012 
00013 //#define DEBUG_CHAOS
00014 
00015 #include <application/hoople_main.h>
00016 #include <basis/astring.h>
00017 #include <basis/functions.h>
00018 #include <basis/guards.h>
00019 #include <loggers/console_logger.h>
00020 #include <mathematics/chaos.h>
00021 #include <structures/static_memory_gremlin.h>
00022 #include <unit_test/unit_base.h>
00023 
00024 using namespace application;
00025 using namespace basis;
00026 using namespace mathematics;
00027 using namespace filesystem;
00028 using namespace loggers;
00029 using namespace structures;
00030 using namespace textual;
00031 using namespace timely;
00032 using namespace unit_test;
00033 
00034 #define MAX_RANDOM_BINS 40
00035 #define MAX_TEST_CYCLES 10008
00036 #define AVG_EXPECTED_PER_BIN (double(MAX_TEST_CYCLES) / double(MAX_RANDOM_BINS))
00037 #define VARIATION_ALLOWED (AVG_EXPECTED_PER_BIN * 0.1)
00038 #define ANOMALIES_ALLOWED (MAX_RANDOM_BINS / 4)
00039 
00040 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
00041 
00042 class test_chaos : virtual public unit_base, virtual public application_shell
00043 {
00044 public:
00045   test_chaos() : application_shell() {}
00046   DEFINE_CLASS_NAME("test_chaos");
00047   virtual int execute();
00048 };
00049 
00050 int test_chaos::execute()
00051 {
00052   FUNCDEF("execute");
00053 #ifdef DEBUG_CHAOS
00054   LOG(a_sprintf("average expected=%f, variation allowed=%f",
00055       AVG_EXPECTED_PER_BIN, VARIATION_ALLOWED));
00056 #endif
00057   int results[MAX_RANDOM_BINS];
00058   for (int k = 0; k < MAX_RANDOM_BINS; k++) results[k] = 0;
00059   chaos randomizer;
00060 
00061   for (int i = 0; i < MAX_TEST_CYCLES; i++) {
00062     // first test if exclusivity is ensured...
00063     int res = randomizer.exclusive(0, MAX_RANDOM_BINS - 1);
00064     ASSERT_FALSE( (res <= 0) || (res >= MAX_RANDOM_BINS - 1),
00065         "exclusive test should not go out of bounds");
00066     // then test for our statistics.
00067     int base = randomizer.inclusive(-1000, 1000);
00068       // pick a base for the number below.
00069     res = randomizer.inclusive(base, base + MAX_RANDOM_BINS - 1);
00070     ASSERT_FALSE( (res < base) || (res > base + MAX_RANDOM_BINS - 1),
00071         "inclusive test should not go out of bounds");
00072 //LOG(a_sprintf("adding it to %d bin", res - base));
00073     results[res - base]++;
00074   }
00075 #ifdef DEBUG_CHAOS
00076   LOG("Anomalies:");
00077 #endif
00078   int failed_any = false;
00079   for (int j = 0; j < MAX_RANDOM_BINS; j++) {
00080     if (absolute_value(results[j] - AVG_EXPECTED_PER_BIN) > VARIATION_ALLOWED) {
00081       failed_any++;
00082 #ifdef DEBUG_CHAOS
00083       LOG(astring(astring::SPRINTF, "%d: difference=%f",
00084           j, double(results[j] - AVG_EXPECTED_PER_BIN)));
00085 #endif
00086     }
00087   }
00088 #ifdef DEBUG_CHAOS
00089   if (!failed_any) LOG("None")
00090   else LOG(a_sprintf("Saw %d anomalies of %d allowed.", failed_any, ANOMALIES_ALLOWED));
00091 #endif
00092 
00093   ASSERT_FALSE(failed_any > ANOMALIES_ALLOWED,
00094       "probability anomalies should be less than the allowed number");
00095   return final_report();
00096 }
00097 
00098 HOOPLE_MAIN(test_chaos, )
00099 
Generated on Sat Jan 28 04:22:30 2012 for hoople2 project by  doxygen 1.6.3