souffle  2.0.2-371-g6315b36
util_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file util_test.cpp
12  *
13  * Test cases for utilities.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
24 #include <algorithm>
25 #include <map>
26 #include <set>
27 #include <string>
28 #include <typeinfo>
29 #include <vector>
30 
31 using namespace souffle;
32 
33 TEST(Util, toString) {
34  EXPECT_EQ("12", toString(12));
35  EXPECT_EQ("Hello", toString("Hello"));
36 }
37 
38 TEST(Util, toVector) {
39  EXPECT_EQ("[1,2,3]", toString(toVector(1, 2, 3)));
40  EXPECT_EQ("[]", toString(toVector<int>()));
41 }
42 
43 TEST(Util, printVector) {
44  std::vector<int> v;
45 
46  EXPECT_EQ("[]", toString(v));
47  v.push_back(12);
48  EXPECT_EQ("[12]", toString(v));
49  v.push_back(14);
50  EXPECT_EQ("[12,14]", toString(v));
51 }
52 
53 TEST(Util, printSet) {
54  std::set<int> v;
55 
56  EXPECT_EQ("{}", toString(v));
57  v.insert(12);
58  EXPECT_EQ("{12}", toString(v));
59  v.insert(14);
60  EXPECT_EQ("{12,14}", toString(v));
61 }
62 
63 TEST(Util, printMap) {
64  std::map<int, std::string> m;
65 
66  EXPECT_EQ("{}", toString(m));
67  m[12] = "Hello";
68  EXPECT_EQ("{12->Hello}", toString(m));
69  m[14] = "World";
70  EXPECT_EQ("{12->Hello,14->World}", toString(m));
71 }
72 
73 TEST(Util, LambdaTraits) {
74  auto lambda = [](int) -> bool { return true; };
75 
76  EXPECT_EQ(typeid(bool).name(), typeid(lambda_traits<decltype(lambda)>::result_type).name());
77  EXPECT_EQ(typeid(int).name(), typeid(lambda_traits<decltype(lambda)>::arg0_type).name());
78 }
79 
80 TEST(Util, LRUCache) {
81  using cache = LRUCache<int, 4>;
82  cache c;
83 
84  // check initial state
85  EXPECT_EQ("0,0,0,0", toString(c));
86 
87  // fill cache with new entries
88  c.access(4);
89  EXPECT_EQ("4,0,0,0", toString(c));
90 
91  c.access(3);
92  EXPECT_EQ("3,4,0,0", toString(c));
93 
94  c.access(7);
95  EXPECT_EQ("7,3,4,0", toString(c));
96 
97  c.access(2);
98  EXPECT_EQ("2,7,3,4", toString(c));
99 
100  c.access(5);
101  EXPECT_EQ("5,2,7,3", toString(c));
102 
103  // access elements present
104  c.access(5);
105  EXPECT_EQ("5,2,7,3", toString(c));
106 
107  c.access(2);
108  EXPECT_EQ("2,5,7,3", toString(c));
109 
110  c.access(7);
111  EXPECT_EQ("7,2,5,3", toString(c));
112 
113  c.access(3);
114  EXPECT_EQ("3,7,2,5", toString(c));
115 
116  c.access(2);
117  EXPECT_EQ("2,3,7,5", toString(c));
118 
119  // test clearing
120  c.clear();
121  EXPECT_EQ("0,0,0,0", toString(c));
122 
123  c.clear(5);
124  EXPECT_EQ("5,5,5,5", toString(c));
125 }
126 
127 TEST(Util, LRUCache_S2) {
128  using cache = LRUCache<int, 2>;
129  cache c;
130 
131  // check initial state
132  EXPECT_EQ("0,0", toString(c));
133 
134  // fill cache with new entries
135  c.access(4);
136  EXPECT_EQ("4,0", toString(c));
137 
138  c.access(3);
139  EXPECT_EQ("3,4", toString(c));
140 
141  c.access(7);
142  EXPECT_EQ("7,3", toString(c));
143 
144  // access elements present
145  c.access(7);
146  EXPECT_EQ("7,3", toString(c));
147 
148  c.access(3);
149  EXPECT_EQ("3,7", toString(c));
150 
151  c.access(7);
152  EXPECT_EQ("7,3", toString(c));
153 
154  // test clearing
155  c.clear();
156  EXPECT_EQ("0,0", toString(c));
157 
158  c.clear(5);
159  EXPECT_EQ("5,5", toString(c));
160 }
161 
162 TEST(Util, LRUCache_S1) {
163  using cache = LRUCache<int, 1>;
164  cache c;
165 
166  // check initial state
167  EXPECT_EQ("0", toString(c));
168 
169  // fill cache with new entries
170  c.access(4);
171  EXPECT_EQ("4", toString(c));
172 
173  c.access(3);
174  EXPECT_EQ("3", toString(c));
175 
176  // access elements present
177  c.access(3);
178  EXPECT_EQ("3", toString(c));
179 
180  // and again not present
181  c.access(2);
182  EXPECT_EQ("2", toString(c));
183 
184  // test clearing
185  c.clear();
186  EXPECT_EQ("0", toString(c));
187 
188  c.clear(5);
189  EXPECT_EQ("5", toString(c));
190 }
191 
192 TEST(Util, LRUCache_S0) {
193  using cache = LRUCache<int, 0>;
194  cache c;
195 
196  // check initial state
197  EXPECT_EQ("-empty-", toString(c));
198 
199  // fill cache with new entries
200  c.access(4);
201  EXPECT_EQ("-empty-", toString(c));
202 
203  c.access(3);
204  EXPECT_EQ("-empty-", toString(c));
205 
206  // access elements present
207  c.access(3);
208  EXPECT_EQ("-empty-", toString(c));
209 
210  // and again not present
211  c.access(2);
212  EXPECT_EQ("-empty-", toString(c));
213 
214  // test clearing
215  c.clear();
216  EXPECT_EQ("-empty-", toString(c));
217 
218  c.clear(5);
219  EXPECT_EQ("-empty-", toString(c));
220 }
221 
222 TEST(Util, Range) {
223  using container = std::vector<int>;
224  using iter = container::const_iterator;
225 
226  std::vector<int> list(9);
227 
228  for (int i = 0; i < 9; i++) {
229  list[i] = i;
230  }
231 
232  range<iter> range(list.begin(), list.end());
233 
234  {
235  int last = -1;
236  for (const auto& p : range.partition(4)) {
237  auto size = p.end() - p.begin();
238  EXPECT_TRUE(2 <= size && size <= 3);
239  for (const auto& cur : p) {
240  EXPECT_EQ(last + 1, cur);
241  last = cur;
242  }
243  }
244  EXPECT_EQ(last, 8);
245  }
246 
247  {
248  int last = -1;
249  for (const auto& p : range.partition(7)) {
250  auto size = p.end() - p.begin();
251  EXPECT_TRUE(1 <= size && size <= 2);
252  for (const auto& cur : p) {
253  EXPECT_EQ(last + 1, cur);
254  last = cur;
255  }
256  }
257  EXPECT_EQ(last, 8);
258  }
259 }
TCB_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.h:198
EXPECT_TRUE
#define EXPECT_TRUE(a)
Definition: test.h:189
souffle::range
A utility class enabling representation of ranges by pairing two iterator instances marking lower and...
Definition: ContainerUtil.h:313
m
var m
Definition: htmlJsChartistMin.h:15
souffle::lambda_traits
A type trait enabling the deduction of type properties of lambdas.
Definition: FunctionalUtil.h:94
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
souffle::LRUCache
An Least-Recently-Used cache for arbitrary element types.
Definition: CacheUtil.h:39
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition: StringUtil.h:234
i
size_t i
Definition: json11.h:663
ContainerUtil.h
souffle::TEST
TEST(SparseArray, Basic)
Definition: brie_test.cpp:45
StringUtil.h
test.h
souffle::toVector
std::vector< T > toVector()
A utility function enabling the creation of a vector with a fixed set of elements within a single exp...
Definition: ContainerUtil.h:128
souffle::range::partition
std::vector< range > partition(int np=100)
Definition: ContainerUtil.h:347
StreamUtil.h
souffle
Definition: AggregateOp.h:25
FunctionalUtil.h
CacheUtil.h
p
a horizontalBars(j=m=void 0===a.axisX.type?new c.AutoScaleAxis(c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})):a.axisX.type.call(c, c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})), l=n=void 0===a.axisY.type?new c.StepAxis(c.Axis.units.y, b.normalized.series, o,{ticks:k}):a.axisY.type.call(c, c.Axis.units.y, b.normalized.series, o, a.axisY)) var p
Definition: htmlJsChartistMin.h:15