1 /*
2  * A sample test case which can be used as a template.
3  */
4 #include <cppunit/TestCase.h>
5 #include <cppunit/extensions/HelperMacros.h>
6 #include "../../src/node.h"
7 #include "../../src/forest.h"
8 #include "../../src/random/constant_generator.h"
9 
10 class TestNode : public CppUnit::TestCase {
11 
12   CPPUNIT_TEST_SUITE( TestNode );
13 
14   CPPUNIT_TEST( testGettersAndSetters );
15   CPPUNIT_TEST( testIsRoot );
16   CPPUNIT_TEST( testInSample );
17   CPPUNIT_TEST( testSamplesBelow );
18   CPPUNIT_TEST( testLengthBelow );
19   CPPUNIT_TEST( testCountChildren );
20   CPPUNIT_TEST( testLocalNavigation );
21 
22   CPPUNIT_TEST_SUITE_END();
23 
24  private:
25   Forest *forest;
26   ConstantGenerator *rg;
27   Model *model;
28 
29  public:
setUp()30   void setUp() {
31     rg = new ConstantGenerator();
32     model = new Model(0);
33     forest = new Forest(model, rg);
34     forest->createExampleTree();
35   }
36 
tearDown()37   void tearDown() {
38     delete forest;
39     delete rg;
40     delete model;
41   }
42 
testGettersAndSetters()43   void testGettersAndSetters() {
44     Node node1, node2;
45 
46     //height
47     node1.set_height(1);
48     CPPUNIT_ASSERT( node1.height() == 1 );
49 
50     //parent
51     node2.set_parent(&node1);
52     CPPUNIT_ASSERT( node2.parent()->height() == 1 );
53 
54     //Children
55     node2.set_second_child(&node1);
56     CPPUNIT_ASSERT( node2.second_child()->height() == 1 );
57     node2.set_first_child(&node1);
58     CPPUNIT_ASSERT( node2.first_child()->height() == 1 );
59 
60     //local
61     node1.make_local();
62     node2.make_nonlocal(1);
63     CPPUNIT_ASSERT( node1.local() && !node2.local() );
64     node1.make_nonlocal(1);
65     node2.make_local();
66     CPPUNIT_ASSERT( (!node1.local()) && node2.local() );
67 
68     //population
69     CPPUNIT_ASSERT_EQUAL( (size_t)0, node1.population() );
70     node1.set_population(1);
71     CPPUNIT_ASSERT_EQUAL( (size_t)1, node1.population() );
72   }
73 
testIsRoot()74   void testIsRoot(){
75     CPPUNIT_ASSERT( !forest->nodes()->get(0)->is_root() );
76     CPPUNIT_ASSERT( !forest->nodes()->get(1)->is_root() );
77     CPPUNIT_ASSERT( !forest->nodes()->get(2)->is_root() );
78     CPPUNIT_ASSERT( !forest->nodes()->get(3)->is_root() );
79     CPPUNIT_ASSERT( !forest->nodes()->get(4)->is_root() );
80     CPPUNIT_ASSERT( !forest->nodes()->get(5)->is_root() );
81     CPPUNIT_ASSERT( !forest->nodes()->get(6)->is_root() );
82     CPPUNIT_ASSERT( forest->nodes()->get(7)->is_root() );
83     CPPUNIT_ASSERT( forest->nodes()->get(8)->is_root() );
84   }
85 
testInSample()86   void testInSample(){
87     CPPUNIT_ASSERT( forest->nodes()->get(0)->in_sample() );
88     CPPUNIT_ASSERT( forest->nodes()->get(1)->in_sample() );
89     CPPUNIT_ASSERT( forest->nodes()->get(2)->in_sample() );
90     CPPUNIT_ASSERT( forest->nodes()->get(3)->in_sample() );
91     CPPUNIT_ASSERT( !forest->nodes()->get(4)->in_sample() );
92     CPPUNIT_ASSERT( !forest->nodes()->get(5)->in_sample() );
93     CPPUNIT_ASSERT( !forest->nodes()->get(6)->in_sample() );
94     CPPUNIT_ASSERT( !forest->nodes()->get(7)->in_sample() );
95     CPPUNIT_ASSERT( !forest->nodes()->get(8)->in_sample() );
96   }
97 
testSamplesBelow()98   void testSamplesBelow(){
99     CPPUNIT_ASSERT( forest->nodes()->get(0)->samples_below() == 1 );
100     CPPUNIT_ASSERT( forest->nodes()->get(1)->samples_below() == 1 );
101     CPPUNIT_ASSERT( forest->nodes()->get(2)->samples_below() == 1 );
102     CPPUNIT_ASSERT( forest->nodes()->get(3)->samples_below() == 1 );
103     CPPUNIT_ASSERT( forest->nodes()->get(4)->samples_below() == 2 );
104     CPPUNIT_ASSERT( forest->nodes()->get(5)->samples_below() == 2 );
105     CPPUNIT_ASSERT( forest->nodes()->get(6)->samples_below() == 0 );
106     CPPUNIT_ASSERT( forest->nodes()->get(7)->samples_below() == 0 );
107     CPPUNIT_ASSERT( forest->nodes()->get(8)->samples_below() == 4 );
108   }
109 
testLengthBelow()110   void testLengthBelow(){
111     CPPUNIT_ASSERT( forest->nodes()->get(0)->length_below() == 0 );
112     CPPUNIT_ASSERT( forest->nodes()->get(1)->length_below() == 0 );
113     CPPUNIT_ASSERT( forest->nodes()->get(2)->length_below() == 0 );
114     CPPUNIT_ASSERT( forest->nodes()->get(3)->length_below() == 0 );
115     CPPUNIT_ASSERT( forest->nodes()->get(4)->length_below() == 2 );
116     CPPUNIT_ASSERT( forest->nodes()->get(5)->length_below() == 6 );
117     CPPUNIT_ASSERT( forest->nodes()->get(6)->length_below() == 0 );
118     CPPUNIT_ASSERT( forest->nodes()->get(7)->length_below() == 0 );
119     CPPUNIT_ASSERT( forest->nodes()->get(8)->length_below() == 24 );
120   }
121 
testCountChildren()122   void testCountChildren(){
123     CPPUNIT_ASSERT( forest->nodes()->get(0)->countChildren() == 0 );
124     CPPUNIT_ASSERT( forest->nodes()->get(1)->countChildren() == 0 );
125     CPPUNIT_ASSERT( forest->nodes()->get(2)->countChildren() == 0 );
126     CPPUNIT_ASSERT( forest->nodes()->get(3)->countChildren() == 0 );
127     CPPUNIT_ASSERT( forest->nodes()->get(4)->countChildren() == 2 );
128     CPPUNIT_ASSERT( forest->nodes()->get(5)->countChildren() == 2 );
129     CPPUNIT_ASSERT( forest->nodes()->get(6)->countChildren() == 0 );
130     CPPUNIT_ASSERT( forest->nodes()->get(7)->countChildren() == 1 );
131     CPPUNIT_ASSERT( forest->nodes()->get(8)->countChildren() == 2 );
132 
133     CPPUNIT_ASSERT( forest->nodes()->get(0)->countChildren(true) == 0 );
134     CPPUNIT_ASSERT( forest->nodes()->get(1)->countChildren(true) == 0 );
135     CPPUNIT_ASSERT( forest->nodes()->get(2)->countChildren(true) == 0 );
136     CPPUNIT_ASSERT( forest->nodes()->get(3)->countChildren(true) == 0 );
137     CPPUNIT_ASSERT( forest->nodes()->get(4)->countChildren(true) == 2 );
138     CPPUNIT_ASSERT( forest->nodes()->get(5)->countChildren(true) == 2 );
139     CPPUNIT_ASSERT( forest->nodes()->get(6)->countChildren(true) == 0 );
140     CPPUNIT_ASSERT( forest->nodes()->get(7)->countChildren(true) == 0 );
141     CPPUNIT_ASSERT( forest->nodes()->get(8)->countChildren(true) == 2 );
142 
143     forest->nodes()->at(4)->make_nonlocal(1.0);
144     CPPUNIT_ASSERT( forest->nodes()->get(8)->countChildren(true) == 1 );
145   }
146 
testLocalNavigation()147   void testLocalNavigation() {
148     Node* n1 = forest->nodes()->createNode(7.5);
149     n1->make_local();
150     Node* n2 = forest->nodes()->createNode(6.5);
151     n2->make_nonlocal(1.0);
152 
153     Node *n3 = forest->nodes()->at(4),
154          *root = forest->local_root(),
155          *n4 = forest->nodes()->at(5);
156 
157     root->remove_child(n3);
158     forest->addNodeToTree(n1, root, n3, NULL);
159     forest->addNodeToTree(n2, n1, NULL, NULL);
160 
161     CPPUNIT_ASSERT(n1->countChildren() == 2);
162     CPPUNIT_ASSERT(n1->countChildren(true) == 1);
163     CPPUNIT_ASSERT(n1->getLocalParent() == root);
164     CPPUNIT_ASSERT(n3->getLocalParent() == root);
165 
166     CPPUNIT_ASSERT( (root->getLocalChild1() == n3 && root->getLocalChild2() == n4) ||
167                     (root->getLocalChild1() == n4 && root->getLocalChild2() == n3) );
168 
169     CPPUNIT_ASSERT(forest->nodes()->at(0)->getLocalChild1() == NULL);
170     CPPUNIT_ASSERT(forest->nodes()->at(1)->getLocalChild1() == NULL);
171     CPPUNIT_ASSERT(n4->getLocalParent() == root);
172   }
173 };
174 
175 CPPUNIT_TEST_SUITE_REGISTRATION( TestNode );
176