1 // {{{ MIT License
2 
3 // Copyright 2017 Roland Kaminski
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 // IN THE SOFTWARE.
22 
23 // }}}
24 
25 #include "gringo/utility.hh"
26 #include "tests/tests.hh"
27 
28 namespace Gringo { namespace Test {
29 
30 using Gringo::IO::to_string;
31 
32 namespace {
33 
34 struct CloneMe {
CloneMeGringo::Test::__anon362422530111::CloneMe35     CloneMe(int x) : x(x) { }
36     CloneMe(CloneMe const &) = delete;
37     CloneMe(CloneMe &&) = default;
cloneGringo::Test::__anon362422530111::CloneMe38     CloneMe *clone() const { return new CloneMe(x); }
39     int x;
40 };
41 
operator <<(std::ostream & out,CloneMe const & x)42 std::ostream &operator<<(std::ostream &out, CloneMe const &x) {
43     out << "c" << x.x;
44     return out;
45 }
46 
47 } // namespace
48 
49 TEST_CASE("utility", "[base]") {
50     SECTION("to_string") {
51         int x1 { 1 };
52         std::unique_ptr<int> x2 { new int(1) };
53         std::pair<int, int> x3 { 1, 2 };
54         std::tuple<> x4 { };
55         std::tuple<int> x5 { 1 };
56         std::tuple<int,int,int> x6 { 1, 2, 3 };
57         std::vector<int> x7 { 1, 2, 3 };
58         std::vector<std::vector<int>> x8 { {1,2}, {3,4}, {5,6,7}, {8} };
59 
60         REQUIRE("1" == to_string(x1));
61         REQUIRE("1" == to_string(x2));
62         REQUIRE("(1,2)" == to_string(x3));
63         REQUIRE("()" == to_string(x4));
64         REQUIRE("(1)" == to_string(x5));
65         REQUIRE("(1,2,3)" == to_string(x6));
66         REQUIRE("[1,2,3]" == to_string(x7));
67         REQUIRE("[[1,2],[3,4],[5,6,7],[8]]" == to_string(x8));
68     }
69 
70     SECTION("cross_product") {
71         std::vector<std::vector<int>> x1 { };
72         std::vector<std::vector<int>> x2 { {} };
73         std::vector<std::vector<int>> x3 { {1} };
74         std::vector<std::vector<int>> x4 { {1,2} };
75         std::vector<std::vector<int>> x5 { {1,2}, {3} };
76         std::vector<std::vector<int>> x6 { {1,2}, {3,4} };
77         std::vector<std::vector<int>> x7 { {1,2}, {3,4}, {} };
78         std::vector<std::vector<int>> x8 { {0,1}, {0,1}, {0,1} };
79         std::vector<std::vector<int>> x9 { {1,2}, {3,4}, {5,6,7}, {8} };
80         cross_product(x1);
81         cross_product(x2);
82         cross_product(x3);
83         cross_product(x4);
84         cross_product(x5);
85         cross_product(x6);
86         cross_product(x7);
87         cross_product(x8);
88         cross_product(x9);
89         REQUIRE("[[]]" == to_string(x1));
90         REQUIRE("[]" == to_string(x2));
91         REQUIRE("[[1]]" == to_string(x3));
92         REQUIRE("[[1],[2]]" == to_string(x4));
93         REQUIRE("[[1,3],[2,3]]" == to_string(x5));
94         REQUIRE("[[1,3],[2,3],[1,4],[2,4]]" == to_string(x6));
95         REQUIRE("[]" == to_string(x7));
96         REQUIRE("[[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1]]" == to_string(x8));
97         REQUIRE("[[1,3,5,8],[2,3,5,8],[1,4,5,8],[2,4,5,8],[1,3,6,8],[2,3,6,8],[1,4,6,8],[2,4,6,8],[1,3,7,8],[2,3,7,8],[1,4,7,8],[2,4,7,8]]" == to_string(x9));
98     }
99 
100     SECTION("clone") {
101         typedef std::unique_ptr<CloneMe> U;
102         CloneMe x1 { 1 };
103         auto x2(gringo_make_unique<CloneMe>(1));
104         auto x3(std::make_pair(gringo_make_unique<CloneMe>(1), gringo_make_unique<CloneMe>(2)));
105         std::vector<U> x4;
106         x4.emplace_back(gringo_make_unique<CloneMe>(1));
107         x4.emplace_back(gringo_make_unique<CloneMe>(2));
108         x4.emplace_back(gringo_make_unique<CloneMe>(3));
109         std::tuple<U, U, U> x5 { gringo_make_unique<CloneMe>(1), gringo_make_unique<CloneMe>(2), gringo_make_unique<CloneMe>(3) };
110         std::tuple<> x6 { };
111         REQUIRE(to_string(1) == to_string(get_clone(1)));
112         REQUIRE(to_string(x1) == to_string(U(get_clone(&x1))));
113         REQUIRE(to_string(x2) == to_string(get_clone(x2)));
114         REQUIRE(to_string(x3) == to_string(get_clone(x3)));
115         REQUIRE(to_string(x4) == to_string(get_clone(x4)));
116         REQUIRE(to_string(x5) == to_string(get_clone(x5)));
117         REQUIRE(to_string(x6) == to_string(get_clone(x6)));
118     }
119 
120     SECTION("hash") {
121         std::vector<int> x1 { };
122         std::vector<int> x1e { };
123         std::vector<int> x2 { 1, 2, 3 };
124         std::vector<int> x2e { 1, 2, 3 };
125         std::vector<int> x3 { 1, 2, 3, 4 };
126         std::vector<int> x3e { 1, 2, 3, 4 };
127         std::pair<int, int> x4 { 1, 2 };
128         std::pair<int, int> x4e { 1, 2 };
129         std::pair<int, int> x5 { 2, 3 };
130         std::pair<int, int> x5e { 2, 3 };
131         std::tuple<> x6 { };
132         std::tuple<> x6e { };
133         std::tuple<int, int, int> x7 { 1, 2, 4 };
134         std::tuple<int, int, int> x7e { 1, 2, 4 };
135         std::tuple<int, int, int> x8 { 1, 2, 3 };
136         std::tuple<int, int, int> x8e { 1, 2, 3 };
137         std::unique_ptr<int> x9 { new int(1) };
138         std::unique_ptr<int> x9e { new int(1) };
139         std::unique_ptr<int> x10 { new int(2) };
140         std::unique_ptr<int> x10e { new int(2) };
141         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x11 { std::make_pair(std::vector<int> {1, 2, 3}, gringo_make_unique<int>(4)) };
142         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x11e { std::make_pair(std::vector<int> {1, 2, 3}, gringo_make_unique<int>(4)) };
143         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x12 { std::make_pair(std::vector<int> {1, 2, 4}, gringo_make_unique<int>(4)) };
144 
145 
146         REQUIRE(get_value_hash(1) == get_value_hash(1));
147         REQUIRE(get_value_hash(x1) == get_value_hash(x1e));
148         REQUIRE(get_value_hash(x2) == get_value_hash(x2e));
149         REQUIRE(get_value_hash(x3) == get_value_hash(x3e));
150         REQUIRE(get_value_hash(x4) == get_value_hash(x4e));
151         REQUIRE(get_value_hash(x5) == get_value_hash(x5e));
152         REQUIRE(get_value_hash(x6) == get_value_hash(x6e));
153         REQUIRE(get_value_hash(x7) == get_value_hash(x7e));
154         REQUIRE(get_value_hash(x8) == get_value_hash(x8e));
155         REQUIRE(get_value_hash(x9) == get_value_hash(x9e));
156         REQUIRE(get_value_hash(x10) == get_value_hash(x10e));
157         REQUIRE(get_value_hash(x11) == get_value_hash(x11e));
158 
159         REQUIRE(get_value_hash(x1) != get_value_hash(x2));
160         REQUIRE(get_value_hash(x1) != get_value_hash(x3));
161         REQUIRE(get_value_hash(x2) != get_value_hash(x3));
162         REQUIRE(get_value_hash(x4) != get_value_hash(x5));
163         REQUIRE(get_value_hash(x6) != get_value_hash(x7));
164         REQUIRE(get_value_hash(x6) != get_value_hash(x8));
165         REQUIRE(get_value_hash(x7) != get_value_hash(x8));
166         REQUIRE(get_value_hash(x9) != get_value_hash(x10));
167         REQUIRE(get_value_hash(x11) != get_value_hash(x12));
168     }
169 
170     SECTION("equal_to") {
171         std::vector<int> x1 { };
172         std::vector<int> x1e { };
173         std::vector<int> x2 { 1, 2, 3 };
174         std::vector<int> x2e { 1, 2, 3 };
175         std::vector<int> x3 { 1, 2, 3, 4 };
176         std::vector<int> x3e { 1, 2, 3, 4 };
177         std::pair<int, int> x4 { 1, 2 };
178         std::pair<int, int> x4e { 1, 2 };
179         std::pair<int, int> x5 { 2, 3 };
180         std::pair<int, int> x5e { 2, 3 };
181         std::tuple<> x6 { };
182         std::tuple<> x6e { };
183         std::tuple<int, int, int> x7 { 1, 2, 4 };
184         std::tuple<int, int, int> x7e { 1, 2, 4 };
185         std::tuple<int, int, int> x8 { 1, 2, 3 };
186         std::tuple<int, int, int> x8e { 1, 2, 3 };
187         std::unique_ptr<int> x9 { new int(1) };
188         std::unique_ptr<int> x9e { new int(1) };
189         std::unique_ptr<int> x10 { new int(2) };
190         std::unique_ptr<int> x10e { new int(2) };
191         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x11 { std::make_pair(std::vector<int> {1, 2, 3}, gringo_make_unique<int>(4)) };
192         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x11e { std::make_pair(std::vector<int> {1, 2, 3}, gringo_make_unique<int>(4)) };
193         std::tuple<std::pair<std::vector<int>, std::unique_ptr<int>>> x12 { std::make_pair(std::vector<int> {1, 2, 4}, gringo_make_unique<int>(4)) };
194 
195         REQUIRE(is_value_equal_to(x1, x1e));
196         REQUIRE(is_value_equal_to(x2, x2e));
197         REQUIRE(is_value_equal_to(x3, x3e));
198         REQUIRE(is_value_equal_to(x4, x4e));
199         REQUIRE(is_value_equal_to(x5, x5e));
200         REQUIRE(is_value_equal_to(x6, x6e));
201         REQUIRE(is_value_equal_to(x7, x7e));
202         REQUIRE(is_value_equal_to(x8, x8e));
203         REQUIRE(is_value_equal_to(x9, x9e));
204         REQUIRE(is_value_equal_to(x10, x10e));
205         REQUIRE(is_value_equal_to(x11, x11e));
206 
207         REQUIRE(!is_value_equal_to(x1, x2));
208         REQUIRE(!is_value_equal_to(x1, x3));
209         REQUIRE(!is_value_equal_to(x2, x3));
210         REQUIRE(!is_value_equal_to(x4, x5));
211         REQUIRE(!is_value_equal_to(x7, x8));
212         REQUIRE(!is_value_equal_to(x9, x10));
213         REQUIRE(!is_value_equal_to(x11, x12));
214     }
215 }
216 
217 } } // namespace Test Gringo
218 
219