00001 #ifndef Impala_Core_Feature_Test_TestRandomTree_h
00002 #define Impala_Core_Feature_Test_TestRandomTree_h
00003
00004 #include <cppunit/extensions/HelperMacros.h>
00005 #include <algorithm>
00006
00007 #include "Core/Table/Equals.h"
00008 #include "Core/Feature/RandomForest.h"
00009
00010 namespace Impala
00011 {
00012 namespace Core
00013 {
00014 namespace Feature
00015 {
00016
00017 class TestRandomTree : public CPPUNIT_NS::TestFixture
00018 {
00019 CPPUNIT_TEST_SUITE(TestRandomTree);
00020 CPPUNIT_TEST(testEquality);
00021 CPPUNIT_TEST(testGetCodeword);
00022 CPPUNIT_TEST(testGetSplit);
00023 CPPUNIT_TEST(testReadWrite);
00024 CPPUNIT_TEST(testTableConversion);
00025 CPPUNIT_TEST(testReadForest);
00026 CPPUNIT_TEST_SUITE_END();
00027
00028 public:
00029
00030 void
00031 setUp()
00032 {
00033 }
00034
00035 void
00036 tearDown()
00037 {
00038 }
00039
00040 void
00041 testEquality()
00042 {
00043 RandomTree t1(1);
00044 RandomTree t2(1);
00045 RandomTree t3(4);
00046 CPPUNIT_ASSERT(t1 == t2);
00047 CPPUNIT_ASSERT(t1 != t3);
00048 RandomTree t4(2, 4.1, new RandomTree(1), new RandomTree(2));
00049 RandomTree t5(2, 4.1, new RandomTree(1), new RandomTree(2));
00050 RandomTree t6(3, 4.1, new RandomTree(1), new RandomTree(2));
00051 RandomTree t7(2, 4.2, new RandomTree(1), new RandomTree(2));
00052 RandomTree t8(2, 4.1, new RandomTree(2), new RandomTree(2));
00053 RandomTree t9(2, 4.1, new RandomTree(1), new RandomTree(3));
00054 CPPUNIT_ASSERT(t4 == t5);
00055 CPPUNIT_ASSERT(t4 != t6);
00056 CPPUNIT_ASSERT(t4 != t7);
00057 CPPUNIT_ASSERT(t4 != t8);
00058 CPPUNIT_ASSERT(t4 != t9);
00059 }
00060
00061 void
00062 testGetCodeword()
00063 {
00064 RandomTree t(0, 1.2,
00065 new RandomTree(1, 3.2, new RandomTree(1), new RandomTree(2)),
00066 new RandomTree(1, 1.2, new RandomTree(3), new RandomTree(4)));
00067 Vector::VectorReal64 v1(0.7, 2.7);
00068 Vector::VectorReal64 v2(0.7, 4.7);
00069 Vector::VectorReal64 v3(1.7, 0.7);
00070 Vector::VectorReal64 v4(1.7, 2.7);
00071 CPPUNIT_ASSERT_EQUAL(1, t.GetCodeWord(v1));
00072 CPPUNIT_ASSERT_EQUAL(2, t.GetCodeWord(v2));
00073 CPPUNIT_ASSERT_EQUAL(3, t.GetCodeWord(v3));
00074 CPPUNIT_ASSERT_EQUAL(4, t.GetCodeWord(v4));
00075 }
00076
00077 void
00078 testGetSplit()
00079 {
00080 RandomTree t1(3);
00081 int dim;
00082 double val;
00083 t1.GetSplit(dim, val);
00084 CPPUNIT_ASSERT_EQUAL(-1, dim);
00085 CPPUNIT_ASSERT_EQUAL(-1., val);
00086 RandomTree t2(2, 4., 0, 0);
00087 t2.GetSplit(dim, val);
00088 CPPUNIT_ASSERT_EQUAL(2, dim);
00089 CPPUNIT_ASSERT_EQUAL(4., val);
00090 }
00091
00092 void
00093 testReadWrite()
00094 {
00095 RandomTree t1(0, 1.2,
00096 new RandomTree(1, 3.2, new RandomTree(1), new RandomTree(2)),
00097 new RandomTree(1, 1.2, new RandomTree(3), new RandomTree(4)));
00098 RandomTree t2(8);
00099 RandomTreeTable table(0);
00100 Write(&t1, &table);
00101 CPPUNIT_ASSERT_EQUAL(7, table.Size());
00102 Write(&t2, &table);
00103 CPPUNIT_ASSERT_EQUAL(8, table.Size());
00104
00105 int index=0;
00106 RandomTree* t1r = Read(&table, index);
00107 CPPUNIT_ASSERT(t1 == *t1r);
00108 CPPUNIT_ASSERT_EQUAL(7, index);
00109 RandomTree* t2r = Read(&table, index);
00110 CPPUNIT_ASSERT(t2 == *t2r);
00111 CPPUNIT_ASSERT_EQUAL(8, index);
00112 delete t1r;
00113 delete t2r;
00114 }
00115
00116 void
00117 testReadForest()
00118 {
00119 RandomTree t0(1);
00120 RandomTree t1(2);
00121 RandomTree t2(3);
00122 RandomTreeTable table(0);
00123 Write(&t0, &table);
00124 Write(&t1, &table);
00125 Write(&t2, &table);
00126
00127 RandomForest f = ReadRandomForest(&table);
00128 CPPUNIT_ASSERT_EQUAL((UInt32) 3, (UInt32) f.size());
00129 CPPUNIT_ASSERT(t0 == *f[0]);
00130 CPPUNIT_ASSERT(t1 == *f[1]);
00131 CPPUNIT_ASSERT(t2 == *f[2]);
00132 CPPUNIT_ASSERT_NO_THROW(DeleteForest(f));
00133 }
00134
00135 void
00136 testTableConversion()
00137 {
00138 RandomTreeTable rtt(0);
00139 rtt.Add(-1, 2, 4.3);
00140 rtt.Add(0, -1, -1);
00141 rtt.Add(1, -1, -1);
00142
00143 FeatureTable* ftt = MakeFeatureTable(&rtt);
00144 RandomTreeTable* rttest = MakeRandomTreeTable(ftt);
00145 CPPUNIT_ASSERT(Equals(&rtt, rttest));
00146 delete ftt;
00147 delete rttest;
00148 }
00149
00150 private:
00151 ILOG_CLASS;
00152 };
00153
00154 ILOG_CLASS_INIT(TestRandomTree, Impala.Core.Feature);
00155
00156 CPPUNIT_TEST_SUITE_REGISTRATION( TestRandomTree );
00157
00158 }
00159 }
00160 }
00161
00162 #endif
00163