1#!/usr/bin/env python3
2#  Copyright 2016 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS-IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from absl.testing import parameterized
17from fruit_test_common import *
18
19COMMON_DEFINITIONS = '''
20    #include "test_common.h"
21
22    #define IN_FRUIT_CPP_FILE 1
23    #include <fruit/impl/data_structures/semistatic_map.templates.h>
24
25    using namespace std;
26    using namespace fruit::impl;
27    '''
28
29class TestSemistaticMap(parameterized.TestCase):
30    def test_empty(self):
31        source = '''
32            int main() {
33              MemoryPool memory_pool;
34              vector<pair<int, std::string>> values{};
35
36              SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
37              Assert(map.find(0) == nullptr);
38              Assert(map.find(2) == nullptr);
39              Assert(map.find(5) == nullptr);
40            }
41            '''
42        expect_success(
43            COMMON_DEFINITIONS,
44            source,
45            locals())
46
47    def test_1_elem(self):
48        source = '''
49            int main() {
50              MemoryPool memory_pool;
51              vector<pair<int, std::string>> values{{2, "foo"}};
52
53              SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
54              Assert(map.find(0) == nullptr);
55              Assert(map.find(2) != nullptr);
56              Assert(map.at(2) == "foo");
57              Assert(map.find(5) == nullptr);
58            }
59            '''
60        expect_success(
61            COMMON_DEFINITIONS,
62            source,
63            locals())
64
65    def test_1_inserted_elem(self):
66        source = '''
67            int main() {
68              MemoryPool memory_pool;
69              vector<pair<int, std::string>> values{};
70
71              SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
72              vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
73                {{2, "bar"}},
74                ArenaAllocator<pair<int, std::string>>(memory_pool));
75              SemistaticMap<int, std::string> map(old_map, std::move(new_values));
76              Assert(map.find(0) == nullptr);
77              Assert(map.find(2) != nullptr);
78              Assert(map.at(2) == "bar");
79              Assert(map.find(5) == nullptr);
80            }
81            '''
82        expect_success(
83            COMMON_DEFINITIONS,
84            source,
85            locals())
86
87    def test_3_elem(self):
88        source = '''
89            int main() {
90              MemoryPool memory_pool;
91              vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
92
93              SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
94              Assert(map.find(0) == nullptr);
95              Assert(map.find(1) != nullptr);
96              Assert(map.at(1) == "foo");
97              Assert(map.find(2) == nullptr);
98              Assert(map.find(3) != nullptr);
99              Assert(map.at(3) == "bar");
100              Assert(map.find(4) != nullptr);
101              Assert(map.at(4) == "baz");
102              Assert(map.find(5) == nullptr);
103            }
104            '''
105        expect_success(
106            COMMON_DEFINITIONS,
107            source,
108            locals())
109
110    def test_1_elem_2_inserted(self):
111        source = '''
112            int main() {
113              MemoryPool memory_pool;
114              vector<pair<int, std::string>> values{{1, "foo"}};
115
116              SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
117              vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
118                  {{3, "bar"}, {4, "baz"}},
119                  ArenaAllocator<pair<int, std::string>>(memory_pool));
120              SemistaticMap<int, std::string> map(old_map, std::move(new_values));
121              Assert(map.find(0) == nullptr);
122              Assert(map.find(1) != nullptr);
123              Assert(map.at(1) == "foo");
124              Assert(map.find(2) == nullptr);
125              Assert(map.find(3) != nullptr);
126              Assert(map.at(3) == "bar");
127              Assert(map.find(4) != nullptr);
128              Assert(map.at(4) == "baz");
129              Assert(map.find(5) == nullptr);
130            }
131            '''
132        expect_success(
133            COMMON_DEFINITIONS,
134            source,
135            locals())
136
137    def test_3_elem_3_inserted(self):
138        source = '''
139            int main() {
140              MemoryPool memory_pool;
141              vector<pair<int, std::string>> values{{1, "1"}, {3, "3"}, {5, "5"}};
142              SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
143              vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
144                  {{2, "2"}, {4, "4"}, {16, "16"}},
145                  ArenaAllocator<pair<int, std::string>>(memory_pool));
146              SemistaticMap<int, std::string> map(old_map, std::move(new_values));
147              Assert(map.find(0) == nullptr);
148              Assert(map.find(1) != nullptr);
149              Assert(map.at(1) == "1");
150              Assert(map.find(2) != nullptr);
151              Assert(map.at(2) == "2");
152              Assert(map.find(3) != nullptr);
153              Assert(map.at(3) == "3");
154              Assert(map.find(4) != nullptr);
155              Assert(map.at(4) == "4");
156              Assert(map.find(5) != nullptr);
157              Assert(map.at(5) == "5");
158              Assert(map.find(6) == nullptr);
159              Assert(map.find(16) != nullptr);
160              Assert(map.at(16) == "16");
161            }
162            '''
163        expect_success(
164            COMMON_DEFINITIONS,
165            source,
166            locals())
167
168    def test_move_constructor(self):
169        source = '''
170            int main() {
171              MemoryPool memory_pool;
172              vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
173              SemistaticMap<int, std::string> map1(values.begin(), values.end(), values.size(), memory_pool);
174              SemistaticMap<int, std::string> map = std::move(map1);
175              Assert(map.find(0) == nullptr);
176              Assert(map.find(1) != nullptr);
177              Assert(map.at(1) == "foo");
178              Assert(map.find(2) == nullptr);
179              Assert(map.find(3) != nullptr);
180              Assert(map.at(3) == "bar");
181              Assert(map.find(4) != nullptr);
182              Assert(map.at(4) == "baz");
183              Assert(map.find(5) == nullptr);
184            }
185            '''
186        expect_success(
187            COMMON_DEFINITIONS,
188            source,
189            locals())
190
191    def test_move_assignment(self):
192        source = '''
193            int main() {
194              MemoryPool memory_pool;
195              vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
196              SemistaticMap<int, std::string> map1(values.begin(), values.end(), values.size(), memory_pool);
197              SemistaticMap<int, std::string> map;
198              map = std::move(map1);
199              Assert(map.find(0) == nullptr);
200              Assert(map.find(1) != nullptr);
201              Assert(map.at(1) == "foo");
202              Assert(map.find(2) == nullptr);
203              Assert(map.find(3) != nullptr);
204              Assert(map.at(3) == "bar");
205              Assert(map.find(4) != nullptr);
206              Assert(map.at(4) == "baz");
207              Assert(map.find(5) == nullptr);
208            }
209            '''
210        expect_success(
211            COMMON_DEFINITIONS,
212            source,
213            locals())
214
215if __name__ == '__main__':
216    absltest.main()
217