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 fruit_test_common import *
17
18COMMON_DEFINITIONS = '''
19    #include "test_common.h"
20
21    #define IN_FRUIT_CPP_FILE 1
22    #include <fruit/impl/data_structures/semistatic_map.templates.h>
23
24    using namespace std;
25    using namespace fruit::impl;
26    '''
27
28def test_empty():
29    source = '''
30        int main() {
31          MemoryPool memory_pool;
32          vector<pair<int, std::string>> values{};
33
34          SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
35          Assert(map.find(0) == nullptr);
36          Assert(map.find(2) == nullptr);
37          Assert(map.find(5) == nullptr);
38        }
39        '''
40    expect_success(
41        COMMON_DEFINITIONS,
42        source,
43        locals())
44
45def test_1_elem():
46    source = '''
47        int main() {
48          MemoryPool memory_pool;
49          vector<pair<int, std::string>> values{{2, "foo"}};
50
51          SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
52          Assert(map.find(0) == nullptr);
53          Assert(map.find(2) != nullptr);
54          Assert(map.at(2) == "foo");
55          Assert(map.find(5) == nullptr);
56        }
57        '''
58    expect_success(
59        COMMON_DEFINITIONS,
60        source,
61        locals())
62
63def test_1_inserted_elem():
64    source = '''
65        int main() {
66          MemoryPool memory_pool;
67          vector<pair<int, std::string>> values{};
68
69          SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
70          vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
71            {{2, "bar"}},
72            ArenaAllocator<pair<int, std::string>>(memory_pool));
73          SemistaticMap<int, std::string> map(old_map, std::move(new_values));
74          Assert(map.find(0) == nullptr);
75          Assert(map.find(2) != nullptr);
76          Assert(map.at(2) == "bar");
77          Assert(map.find(5) == nullptr);
78        }
79        '''
80    expect_success(
81        COMMON_DEFINITIONS,
82        source,
83        locals())
84
85def test_3_elem():
86    source = '''
87        int main() {
88          MemoryPool memory_pool;
89          vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
90
91          SemistaticMap<int, std::string> map(values.begin(), values.end(), values.size(), memory_pool);
92          Assert(map.find(0) == nullptr);
93          Assert(map.find(1) != nullptr);
94          Assert(map.at(1) == "foo");
95          Assert(map.find(2) == nullptr);
96          Assert(map.find(3) != nullptr);
97          Assert(map.at(3) == "bar");
98          Assert(map.find(4) != nullptr);
99          Assert(map.at(4) == "baz");
100          Assert(map.find(5) == nullptr);
101        }
102        '''
103    expect_success(
104        COMMON_DEFINITIONS,
105        source,
106        locals())
107
108def test_1_elem_2_inserted():
109    source = '''
110        int main() {
111          MemoryPool memory_pool;
112          vector<pair<int, std::string>> values{{1, "foo"}};
113
114          SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
115          vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
116              {{3, "bar"}, {4, "baz"}},
117              ArenaAllocator<pair<int, std::string>>(memory_pool));
118          SemistaticMap<int, std::string> map(old_map, std::move(new_values));
119          Assert(map.find(0) == nullptr);
120          Assert(map.find(1) != nullptr);
121          Assert(map.at(1) == "foo");
122          Assert(map.find(2) == nullptr);
123          Assert(map.find(3) != nullptr);
124          Assert(map.at(3) == "bar");
125          Assert(map.find(4) != nullptr);
126          Assert(map.at(4) == "baz");
127          Assert(map.find(5) == nullptr);
128        }
129        '''
130    expect_success(
131        COMMON_DEFINITIONS,
132        source,
133        locals())
134
135def test_3_elem_3_inserted():
136    source = '''
137        int main() {
138          MemoryPool memory_pool;
139          vector<pair<int, std::string>> values{{1, "1"}, {3, "3"}, {5, "5"}};
140          SemistaticMap<int, std::string> old_map(values.begin(), values.end(), values.size(), memory_pool);
141          vector<pair<int, std::string>, ArenaAllocator<pair<int, std::string>>> new_values(
142              {{2, "2"}, {4, "4"}, {16, "16"}},
143              ArenaAllocator<pair<int, std::string>>(memory_pool));
144          SemistaticMap<int, std::string> map(old_map, std::move(new_values));
145          Assert(map.find(0) == nullptr);
146          Assert(map.find(1) != nullptr);
147          Assert(map.at(1) == "1");
148          Assert(map.find(2) != nullptr);
149          Assert(map.at(2) == "2");
150          Assert(map.find(3) != nullptr);
151          Assert(map.at(3) == "3");
152          Assert(map.find(4) != nullptr);
153          Assert(map.at(4) == "4");
154          Assert(map.find(5) != nullptr);
155          Assert(map.at(5) == "5");
156          Assert(map.find(6) == nullptr);
157          Assert(map.find(16) != nullptr);
158          Assert(map.at(16) == "16");
159        }
160        '''
161    expect_success(
162        COMMON_DEFINITIONS,
163        source,
164        locals())
165
166def test_move_constructor():
167    source = '''
168        int main() {
169          MemoryPool memory_pool;
170          vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
171          SemistaticMap<int, std::string> map1(values.begin(), values.end(), values.size(), memory_pool);
172          SemistaticMap<int, std::string> map = std::move(map1);
173          Assert(map.find(0) == nullptr);
174          Assert(map.find(1) != nullptr);
175          Assert(map.at(1) == "foo");
176          Assert(map.find(2) == nullptr);
177          Assert(map.find(3) != nullptr);
178          Assert(map.at(3) == "bar");
179          Assert(map.find(4) != nullptr);
180          Assert(map.at(4) == "baz");
181          Assert(map.find(5) == nullptr);
182        }
183        '''
184    expect_success(
185        COMMON_DEFINITIONS,
186        source,
187        locals())
188
189def test_move_assignment():
190    source = '''
191        int main() {
192          MemoryPool memory_pool;
193          vector<pair<int, std::string>> values{{1, "foo"}, {3, "bar"}, {4, "baz"}};
194          SemistaticMap<int, std::string> map1(values.begin(), values.end(), values.size(), memory_pool);
195          SemistaticMap<int, std::string> map;
196          map = std::move(map1);
197          Assert(map.find(0) == nullptr);
198          Assert(map.find(1) != nullptr);
199          Assert(map.at(1) == "foo");
200          Assert(map.find(2) == nullptr);
201          Assert(map.find(3) != nullptr);
202          Assert(map.at(3) == "bar");
203          Assert(map.find(4) != nullptr);
204          Assert(map.at(4) == "baz");
205          Assert(map.find(5) == nullptr);
206        }
207        '''
208    expect_success(
209        COMMON_DEFINITIONS,
210        source,
211        locals())
212
213if __name__== '__main__':
214    main(__file__)
215