• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestRandom.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/math/Random.h>
00020 #include <spl/math/Sample.h>
00021 #include <spl/math/SampleTest.h>
00022 
00023 #ifdef DEBUG
00024 
00025 static void _TestRandomInner(Random *rnd, bool rndCheckMem)
00026 {
00027         int count = 6000;
00028         double *data = new double[count];
00029         
00030         for ( int x = 0; x < count; x++ )
00031         {
00032                 data[x] = rnd->NextDouble();
00033         }
00034         
00035         Sample stats(data, count);
00036         
00037         UNIT_ASSERT( "Random count", stats.N() == count );
00038         UNIT_ASSERT( "Random mean", stats.Mean() > .46 && stats.Mean() < .53 );
00039         UNIT_ASSERT( "Random STD", stats.StDevSample() > 0 );
00040         UNIT_ASSERT( "Random Max", stats.Max() < 1 );
00041         UNIT_ASSERT( "Random range", stats.Min() != stats.Max() );
00042         
00043         DEBUG_CLEAR_MEM_CHECK_POINTS();
00044         DEBUG_NOTE_MEM( data );
00045         stats.CheckMem();
00046         if ( rndCheckMem )
00047         {
00048                 DEBUG_NOTE_MEM( rnd );
00049         }
00050         DEBUG_DUMP_MEM_LEAKS();
00051         UNIT_ASSERT_MEM_NOTED("Random");
00052         
00053         delete[] data;
00054 }
00055 
00056 static void _TestRandom1()
00057 {
00058         Random rnd;
00059         _TestRandomInner(&rnd, false);
00060         
00061         DEBUG_CLEAR_MEM_CHECK_POINTS();
00062         DEBUG_DUMP_MEM_LEAKS();
00063         UNIT_ASSERT_MEM_NOTED("Random test 1.1");
00064 
00065         Log::SWriteOkFail( "Random test 1" );
00066 }
00067 
00068 static void _TestRandom2()
00069 {
00070         const int SIZE = 4000;
00071         Random rnd;
00072 
00073         int x;
00074         double d1[SIZE];
00075         double d2[SIZE];
00076 
00077         for ( x = 0; x < SIZE; x++ )
00078         {
00079                 d1[x] = rnd.NextDouble();
00080                 ASSERT(d1[x] >= 0);
00081         }
00082         for ( x = 0; x < SIZE; x++ )
00083         {
00084                 d2[x] = rnd.NextDouble();
00085                 ASSERT(d1[2] >= 0);
00086         }
00087 
00088         SampleTest test(d1, SIZE, d2, SIZE);
00089 
00090         double med = test.DataSet1().Median();
00091         UNIT_ASSERT("Median off", med > 0.47 && med < 0.53);
00092         med = test.DataSet2().Median();
00093         UNIT_ASSERT("Median off", med > 0.47 && med < 0.53);
00094 
00095         med = test.DataSet1().Mean();
00096         UNIT_ASSERT("Mean off", med > 0.47 && med < 0.53);
00097         med = test.DataSet2().Mean();
00098         UNIT_ASSERT("Mean off", med > 0.47 && med < 0.53);
00099 
00100         double prop;
00101         double t = test.Levene(&prop);
00102         UNIT_ASSERT("Variances should be homogenous", t < .2);
00103 
00104         DEBUG_CLEAR_MEM_CHECK_POINTS();
00105         test.CheckMem();
00106         DEBUG_DUMP_MEM_LEAKS();
00107         UNIT_ASSERT_MEM_NOTED("Random test 2.1");
00108 
00109         Log::SWriteOkFail( "Random test 2" );
00110 }
00111 
00112 void _TestRandom()
00113 {
00114         _TestRandom1();
00115         DEBUG_CLEAR_MEM_CHECK_POINTS();
00116         DEBUG_DUMP_MEM_LEAKS();
00117 
00118         _TestRandom2();
00119         DEBUG_CLEAR_MEM_CHECK_POINTS();
00120         DEBUG_DUMP_MEM_LEAKS();
00121 }
00122 
00123 #endif