t_chaos.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_chaos                                                        *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 //#define DEBUG_CHAOS
00016 
00017 #include <basis/function.h>
00018 #include <basis/chaos.h>
00019 #include <basis/guards.h>
00020 #include <basis/istring.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <data_struct/static_memory_gremlin.h>
00024 
00025 #define MAX_RANDOM_NUMBER 500
00026 #define MAX_TEST_CYCLES 1083414
00027 #define AVG_EXPECTED (double(MAX_TEST_CYCLES) / double(MAX_RANDOM_NUMBER))
00028 #define VARIATION_ALLOWED (AVG_EXPECTED * 0.05)
00029 
00030 #define LOG(s) EMERGENCY_LOG(program_wide_logger(), s)
00031 
00032 class test_chaos : public application_shell
00033 {
00034 public:
00035   test_chaos() : application_shell(class_name()) {}
00036   IMPLEMENT_CLASS_NAME("test_chaos");
00037   virtual int execute();
00038 };
00039 
00040 int test_chaos::execute()
00041 {
00042 #ifdef DEBUG_CHAOS
00043   LOG(istring(istring::SPRINTF, "average expected=%f, "
00044       "variation allowed=%f", AVG_EXPECTED, VARIATION_ALLOWED));
00045 #endif
00046   int results[MAX_RANDOM_NUMBER];
00047   for (int k = 0; k < MAX_RANDOM_NUMBER; k++) results[k] = 0;
00048   chaos randomizer;
00049 
00050   for (int i = 0; i < MAX_TEST_CYCLES; i++) {
00051     // first test if exclusivity is ensured...
00052     int res = randomizer.exclusive(0, MAX_RANDOM_NUMBER-1);
00053     if ( (res <= 0) || (res >= MAX_RANDOM_NUMBER-1) )
00054       deadly_error(__WHERE__.s(), "exclusive test", "out of bounds");
00055 
00056     // then test for our statistics.
00057     int base = randomizer.inclusive(-1000, 1000);
00058       // pick a base for the number below.
00059     res = randomizer.inclusive(base, base + MAX_RANDOM_NUMBER - 1);
00060     if ( (res < base) || (res > base + MAX_RANDOM_NUMBER - 1) )
00061       deadly_error(__WHERE__.s(), "exclusive test", "out of bounds");
00062     results[res - base]++;
00063   }
00064 #ifdef DEBUG_CHAOS
00065   LOG("Anomalies:");
00066 #endif
00067   int printed_any = false;
00068   for (int j = 0; j < MAX_RANDOM_NUMBER; j++) {
00069     if (absolute_value(results[j] - AVG_EXPECTED) > VARIATION_ALLOWED) {
00070       printed_any++;
00071 #ifdef DEBUG_CHAOS
00072       LOG(istring(istring::SPRINTF, "%d: difference=%f",
00073           j, double(results[j] - AVG_EXPECTED)));
00074 #endif
00075     }
00076   }
00077 #ifdef DEBUG_CHAOS
00078   if (!printed_any) LOG("None");
00079 #endif
00080 
00081   if (printed_any > MAX_RANDOM_NUMBER / 20)
00082     deadly_error(__WHERE__.s(), "probability anomalies",
00083         "more than the allowed number exist");
00084   guards::alert_message("chaos:: works for those functions tested.");
00085   return 0;
00086 }
00087 
00088 HOOPLE_MAIN(test_chaos, )
00089 

Generated on Fri Nov 28 04:29:35 2008 for HOOPLE Libraries by  doxygen 1.5.1