1 | #include <gtest/gtest.h> |
2 | |
3 | #include <test/cpp/api/support.h> |
4 | #include <torch/torch.h> |
5 | |
6 | template <typename T> |
7 | using OrderedDict = torch::OrderedDict<std::string, T>; |
8 | |
9 | TEST(OrderedDictTest, IsEmptyAfterDefaultConstruction) { |
10 | OrderedDict<int> dict; |
11 | ASSERT_EQ(dict.key_description(), "Key" ); |
12 | ASSERT_TRUE(dict.is_empty()); |
13 | ASSERT_EQ(dict.size(), 0); |
14 | } |
15 | |
16 | TEST(OrderedDictTest, InsertAddsElementsWhenTheyAreYetNotPresent) { |
17 | OrderedDict<int> dict; |
18 | dict.insert("a" , 1); |
19 | dict.insert("b" , 2); |
20 | ASSERT_EQ(dict.size(), 2); |
21 | } |
22 | |
23 | TEST(OrderedDictTest, GetReturnsValuesWhenTheyArePresent) { |
24 | OrderedDict<int> dict; |
25 | dict.insert("a" , 1); |
26 | dict.insert("b" , 2); |
27 | ASSERT_EQ(dict["a" ], 1); |
28 | ASSERT_EQ(dict["b" ], 2); |
29 | } |
30 | |
31 | TEST(OrderedDictTest, GetThrowsWhenPassedKeysThatAreNotPresent) { |
32 | OrderedDict<int> dict; |
33 | dict.insert("a" , 1); |
34 | dict.insert("b" , 2); |
35 | ASSERT_THROWS_WITH(dict["foo" ], "Key 'foo' is not defined" ); |
36 | ASSERT_THROWS_WITH(dict["" ], "Key '' is not defined" ); |
37 | } |
38 | |
39 | TEST(OrderedDictTest, CanInitializeFromList) { |
40 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
41 | ASSERT_EQ(dict.size(), 2); |
42 | ASSERT_EQ(dict["a" ], 1); |
43 | ASSERT_EQ(dict["b" ], 2); |
44 | } |
45 | |
46 | TEST(OrderedDictTest, InsertThrowsWhenPassedElementsThatArePresent) { |
47 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
48 | ASSERT_THROWS_WITH(dict.insert("a" , 1), "Key 'a' already defined" ); |
49 | ASSERT_THROWS_WITH(dict.insert("b" , 1), "Key 'b' already defined" ); |
50 | } |
51 | |
52 | TEST(OrderedDictTest, FrontReturnsTheFirstItem) { |
53 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
54 | ASSERT_EQ(dict.front().key(), "a" ); |
55 | ASSERT_EQ(dict.front().value(), 1); |
56 | } |
57 | |
58 | TEST(OrderedDictTest, FrontThrowsWhenEmpty) { |
59 | OrderedDict<int> dict; |
60 | ASSERT_THROWS_WITH(dict.front(), "Called front() on an empty OrderedDict" ); |
61 | } |
62 | |
63 | TEST(OrderedDictTest, BackReturnsTheLastItem) { |
64 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
65 | ASSERT_EQ(dict.back().key(), "b" ); |
66 | ASSERT_EQ(dict.back().value(), 2); |
67 | } |
68 | |
69 | TEST(OrderedDictTest, BackThrowsWhenEmpty) { |
70 | OrderedDict<int> dict; |
71 | ASSERT_THROWS_WITH(dict.back(), "Called back() on an empty OrderedDict" ); |
72 | } |
73 | |
74 | TEST(OrderedDictTest, FindReturnsPointersToValuesWhenPresent) { |
75 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
76 | ASSERT_NE(dict.find("a" ), nullptr); |
77 | ASSERT_EQ(*dict.find("a" ), 1); |
78 | ASSERT_NE(dict.find("b" ), nullptr); |
79 | ASSERT_EQ(*dict.find("b" ), 2); |
80 | } |
81 | |
82 | TEST(OrderedDictTest, FindReturnsNullPointersWhenPasesdKeysThatAreNotPresent) { |
83 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
84 | ASSERT_EQ(dict.find("bar" ), nullptr); |
85 | ASSERT_EQ(dict.find("" ), nullptr); |
86 | } |
87 | |
88 | TEST(OrderedDictTest, SubscriptOperatorThrowsWhenPassedKeysThatAreNotPresent) { |
89 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
90 | ASSERT_EQ(dict["a" ], 1); |
91 | ASSERT_EQ(dict["b" ], 2); |
92 | } |
93 | |
94 | TEST( |
95 | OrderedDictTest, |
96 | SubscriptOperatorReturnsItemsPositionallyWhenPassedIntegers) { |
97 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
98 | ASSERT_EQ(dict[0].key(), "a" ); |
99 | ASSERT_EQ(dict[0].value(), 1); |
100 | ASSERT_EQ(dict[1].key(), "b" ); |
101 | ASSERT_EQ(dict[1].value(), 2); |
102 | } |
103 | |
104 | TEST(OrderedDictTest, SubscriptOperatorsThrowswhenPassedKeysThatAreNotPresent) { |
105 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
106 | ASSERT_THROWS_WITH(dict["foo" ], "Key 'foo' is not defined" ); |
107 | ASSERT_THROWS_WITH(dict["" ], "Key '' is not defined" ); |
108 | } |
109 | |
110 | TEST(OrderedDictTest, UpdateInsertsAllItemsFromAnotherOrderedDict) { |
111 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
112 | OrderedDict<int> dict2 = {{"c" , 3}}; |
113 | dict2.update(dict); |
114 | ASSERT_EQ(dict2.size(), 3); |
115 | ASSERT_NE(dict2.find("a" ), nullptr); |
116 | ASSERT_NE(dict2.find("b" ), nullptr); |
117 | ASSERT_NE(dict2.find("c" ), nullptr); |
118 | } |
119 | |
120 | TEST(OrderedDictTest, UpdateAlsoChecksForDuplicates) { |
121 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
122 | OrderedDict<int> dict2 = {{"a" , 1}}; |
123 | ASSERT_THROWS_WITH(dict2.update(dict), "Key 'a' already defined" ); |
124 | } |
125 | |
126 | TEST(OrderedDictTest, CanIterateItems) { |
127 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
128 | auto iterator = dict.begin(); |
129 | ASSERT_NE(iterator, dict.end()); |
130 | ASSERT_EQ(iterator->key(), "a" ); |
131 | ASSERT_EQ(iterator->value(), 1); |
132 | ++iterator; |
133 | ASSERT_NE(iterator, dict.end()); |
134 | ASSERT_EQ(iterator->key(), "b" ); |
135 | ASSERT_EQ(iterator->value(), 2); |
136 | ++iterator; |
137 | ASSERT_EQ(iterator, dict.end()); |
138 | } |
139 | |
140 | TEST(OrderedDictTest, EraseWorks) { |
141 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}, {"c" , 3}}; |
142 | dict.erase("b" ); |
143 | ASSERT_FALSE(dict.contains("b" )); |
144 | ASSERT_EQ(dict["a" ], 1); |
145 | ASSERT_EQ(dict["c" ], 3); |
146 | dict.erase("a" ); |
147 | ASSERT_FALSE(dict.contains("a" )); |
148 | ASSERT_EQ(dict["c" ], 3); |
149 | dict.erase("c" ); |
150 | ASSERT_FALSE(dict.contains("c" )); |
151 | ASSERT_TRUE(dict.is_empty()); |
152 | } |
153 | |
154 | TEST(OrderedDictTest, ClearMakesTheDictEmpty) { |
155 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
156 | ASSERT_FALSE(dict.is_empty()); |
157 | dict.clear(); |
158 | ASSERT_TRUE(dict.is_empty()); |
159 | } |
160 | |
161 | TEST(OrderedDictTest, CanCopyConstruct) { |
162 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
163 | OrderedDict<int> copy = dict; |
164 | ASSERT_EQ(copy.size(), 2); |
165 | ASSERT_EQ(*copy[0], 1); |
166 | ASSERT_EQ(*copy[1], 2); |
167 | } |
168 | |
169 | TEST(OrderedDictTest, CanCopyAssign) { |
170 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
171 | OrderedDict<int> copy = {{"c" , 1}}; |
172 | ASSERT_NE(copy.find("c" ), nullptr); |
173 | copy = dict; |
174 | ASSERT_EQ(copy.size(), 2); |
175 | ASSERT_EQ(*copy[0], 1); |
176 | ASSERT_EQ(*copy[1], 2); |
177 | ASSERT_EQ(copy.find("c" ), nullptr); |
178 | } |
179 | |
180 | TEST(OrderedDictTest, CanMoveConstruct) { |
181 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
182 | OrderedDict<int> copy = std::move(dict); |
183 | ASSERT_EQ(copy.size(), 2); |
184 | ASSERT_EQ(*copy[0], 1); |
185 | ASSERT_EQ(*copy[1], 2); |
186 | } |
187 | |
188 | TEST(OrderedDictTest, CanMoveAssign) { |
189 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
190 | OrderedDict<int> copy = {{"c" , 1}}; |
191 | ASSERT_NE(copy.find("c" ), nullptr); |
192 | copy = std::move(dict); |
193 | ASSERT_EQ(copy.size(), 2); |
194 | ASSERT_EQ(*copy[0], 1); |
195 | ASSERT_EQ(*copy[1], 2); |
196 | ASSERT_EQ(copy.find("c" ), nullptr); |
197 | } |
198 | |
199 | TEST(OrderedDictTest, CanInsertWithBraces) { |
200 | OrderedDict<std::pair<int, int>> dict; |
201 | dict.insert("a" , {1, 2}); |
202 | ASSERT_FALSE(dict.is_empty()); |
203 | ASSERT_EQ(dict["a" ].first, 1); |
204 | ASSERT_EQ(dict["a" ].second, 2); |
205 | } |
206 | |
207 | TEST(OrderedDictTest, ErrorMessagesIncludeTheKeyDescription) { |
208 | OrderedDict<int> dict("Penguin" ); |
209 | ASSERT_EQ(dict.key_description(), "Penguin" ); |
210 | dict.insert("a" , 1); |
211 | ASSERT_FALSE(dict.is_empty()); |
212 | ASSERT_THROWS_WITH(dict["b" ], "Penguin 'b' is not defined" ); |
213 | ASSERT_THROWS_WITH(dict.insert("a" , 1), "Penguin 'a' already defined" ); |
214 | } |
215 | |
216 | TEST(OrderedDictTest, KeysReturnsAllKeys) { |
217 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
218 | ASSERT_EQ(dict.keys(), std::vector<std::string>({"a" , "b" })); |
219 | } |
220 | |
221 | TEST(OrderedDictTest, ValuesReturnsAllValues) { |
222 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
223 | ASSERT_EQ(dict.values(), std::vector<int>({1, 2})); |
224 | } |
225 | |
226 | TEST(OrderedDictTest, ItemsReturnsAllItems) { |
227 | OrderedDict<int> dict = {{"a" , 1}, {"b" , 2}}; |
228 | std::vector<OrderedDict<int>::Item> items = dict.items(); |
229 | ASSERT_EQ(items.size(), 2); |
230 | ASSERT_EQ(items[0].key(), "a" ); |
231 | ASSERT_EQ(items[0].value(), 1); |
232 | ASSERT_EQ(items[1].key(), "b" ); |
233 | ASSERT_EQ(items[1].value(), 2); |
234 | } |
235 | |