1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
4  *   info_at_agrum_dot_org
5  *
6  *  This library is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #include <gumtest/AgrumTestSuite.h>
23 #include <gumtest/testsuite_utils.h>
24 #include <iostream>
25 
26 #include <agrum/BN/learning/scores_and_tests/scoringCache.h>
27 
28 namespace gum_tests {
29 
30   class ScoringCacheTestSuite: public CxxTest::TestSuite {
31     public:
test1()32     void test1() {
33       gum::learning::IdCondSet<>    set1;
34       gum::learning::ScoringCache<> cache;
35 
36       cache.insert(set1, 2.0);
37       TS_ASSERT(cache.exists(set1))
38       TS_ASSERT_EQUALS(cache.score(set1), 2.0)
39 
40       gum::NodeId node0 = 0;
41       gum::NodeId node1 = 1;
42       gum::NodeId node2 = 2;
43       gum::NodeId node3 = 3;
44       gum::NodeId node4 = 4;
45 
46       std::vector< gum::NodeId > empty;
47 
48       gum::learning::IdCondSet<> set2(node0, empty);
49       cache.insert(set2, 3.2);
50       TS_ASSERT(cache.exists(set2))
51       TS_ASSERT_EQUALS(cache.score(set2), 3.2)
52 
53       TS_ASSERT_THROWS(cache.insert(set2, 5.2), gum::DuplicateElement)
54       TS_ASSERT_EQUALS(cache.size(), std::size_t(2))
55       cache.clear();
56       TS_ASSERT_EQUALS(cache.size(), std::size_t(0))
57 
58       gum::learning::IdCondSet<> xset2(set2);
59       cache.insert(std::move(xset2), 7.3);
60       TS_ASSERT(cache.exists(set2))
61       TS_ASSERT_EQUALS(cache.score(set2), 7.3)
62 
63       std::vector< gum::NodeId > vect{node4, node2, node3};
64       gum::learning::IdCondSet<> set3(node0, node1, vect, true, true);
65       TS_ASSERT(!cache.exists(set3))
66       cache.insert(set3, 1.3);
67       TS_ASSERT(cache.exists(set3))
68       TS_ASSERT_EQUALS(cache.score(set3), 1.3)
69       TS_ASSERT_EQUALS(cache.score(set2), 7.3)
70       TS_ASSERT_EQUALS(cache.size(), std::size_t(2))
71 
72       gum::learning::ScoringCache<> cache2(cache);
73       TS_ASSERT(cache2.exists(set3))
74       TS_ASSERT_EQUALS(cache2.score(set3), 1.3)
75       TS_ASSERT_EQUALS(cache2.score(set2), 7.3)
76       TS_ASSERT_EQUALS(cache2.size(), std::size_t(2))
77 
78       cache2.erase(set2);
79       TS_ASSERT(cache2.exists(set3))
80       TS_ASSERT(!cache2.exists(set2))
81       TS_ASSERT_EQUALS(cache2.score(set3), 1.3)
82       TS_ASSERT_THROWS(cache2.score(set2), gum::NotFound)
83       TS_ASSERT_EQUALS(cache2.size(), std::size_t(1))
84       TS_ASSERT(cache.exists(set3))
85       TS_ASSERT_EQUALS(cache.score(set3), 1.3)
86       TS_ASSERT_EQUALS(cache.score(set2), 7.3)
87       TS_ASSERT_EQUALS(cache.size(), std::size_t(2))
88 
89       gum::learning::ScoringCache<> cache3(std::move(cache2));
90       TS_ASSERT(cache3.exists(set3))
91       TS_ASSERT(!cache3.exists(set2))
92       TS_ASSERT_EQUALS(cache3.score(set3), 1.3)
93       TS_ASSERT_THROWS(cache3.score(set2), gum::NotFound)
94       TS_ASSERT_EQUALS(cache3.size(), std::size_t(1))
95       TS_ASSERT(cache.exists(set3))
96       TS_ASSERT_EQUALS(cache.score(set3), 1.3)
97       TS_ASSERT_EQUALS(cache.score(set2), 7.3)
98       TS_ASSERT_EQUALS(cache.size(), std::size_t(2))
99 
100       gum::learning::ScoringCache<>* cache4 = cache3.clone();
101       TS_ASSERT(cache4->exists(set3))
102       TS_ASSERT(!cache4->exists(set2))
103       TS_ASSERT_EQUALS(cache4->score(set3), 1.3)
104       TS_ASSERT_THROWS(cache4->score(set2), gum::NotFound)
105       TS_ASSERT_EQUALS(cache4->size(), std::size_t(1))
106       TS_ASSERT(cache.exists(set3))
107       TS_ASSERT_EQUALS(cache.score(set3), 1.3)
108       TS_ASSERT_EQUALS(cache.score(set2), 7.3)
109       TS_ASSERT_EQUALS(cache.size(), std::size_t(2))
110 
111       cache3 = cache;
112       TS_ASSERT(cache3.exists(set3))
113       TS_ASSERT_EQUALS(cache3.score(set3), 1.3)
114       TS_ASSERT_EQUALS(cache3.score(set2), 7.3)
115       TS_ASSERT_EQUALS(cache3.size(), std::size_t(2))
116 
117       gum::learning::IdCondSet<> set4(node1, vect, true);
118       TS_GUM_ASSERT_THROWS_NOTHING(cache3.erase(set4));
119       TS_ASSERT_EQUALS(cache3.score(set3), 1.3)
120       TS_ASSERT_EQUALS(cache3.score(set2), 7.3)
121       TS_ASSERT_EQUALS(cache3.size(), std::size_t(2))
122 
123       *cache4 = std::move(cache3);
124       TS_ASSERT(cache4->exists(set3))
125       TS_ASSERT_EQUALS(cache4->score(set3), 1.3)
126       TS_ASSERT_EQUALS(cache4->score(set2), 7.3)
127       TS_ASSERT_EQUALS(cache4->size(), std::size_t(2))
128 
129       delete cache4;
130     }
131   };
132 
133 
134 } /* namespace gum_tests */
135