1 #include <gtest/gtest.h> 3 #include <test/cpp/api/support.h> 4 #include <torch/ordered_dict.h> 9 TEST(OrderedDictTest, IsEmptyAfterDefaultConstruction) {
13 ASSERT_EQ(dict.
size(), 0);
16 TEST(OrderedDictTest, InsertAddsElementsWhenTheyAreYetNotPresent) {
20 ASSERT_EQ(dict.
size(), 2);
23 TEST(OrderedDictTest, GetReturnsValuesWhenTheyArePresent) {
27 ASSERT_EQ(dict[
"a"], 1);
28 ASSERT_EQ(dict[
"b"], 2);
31 TEST(OrderedDictTest, GetThrowsWhenPassedKeysThatAreNotPresent) {
35 ASSERT_THROWS_WITH(dict[
"foo"],
"Key 'foo' is not defined");
36 ASSERT_THROWS_WITH(dict[
""],
"Key '' is not defined");
39 TEST(OrderedDictTest, CanInitializeFromList) {
41 ASSERT_EQ(dict.
size(), 2);
42 ASSERT_EQ(dict[
"a"], 1);
43 ASSERT_EQ(dict[
"b"], 2);
46 TEST(OrderedDictTest, InsertThrowsWhenPassedElementsThatArePresent) {
48 ASSERT_THROWS_WITH(dict.
insert(
"a", 1),
"Key 'a' already defined");
49 ASSERT_THROWS_WITH(dict.
insert(
"b", 1),
"Key 'b' already defined");
52 TEST(OrderedDictTest, FrontReturnsTheFirstItem) {
54 ASSERT_EQ(dict.
front().key(),
"a");
55 ASSERT_EQ(dict.
front().value(), 1);
58 TEST(OrderedDictTest, FrontThrowsWhenEmpty) {
60 ASSERT_THROWS_WITH(dict.
front(),
"Called front() on an empty OrderedDict");
63 TEST(OrderedDictTest, BackReturnsTheLastItem) {
65 ASSERT_EQ(dict.
back().key(),
"b");
66 ASSERT_EQ(dict.
back().value(), 2);
69 TEST(OrderedDictTest, BackThrowsWhenEmpty) {
71 ASSERT_THROWS_WITH(dict.
back(),
"Called back() on an empty OrderedDict");
74 TEST(OrderedDictTest, FindReturnsPointersToValuesWhenPresent) {
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);
82 TEST(OrderedDictTest, FindReturnsNullPointersWhenPasesdKeysThatAreNotPresent) {
84 ASSERT_EQ(dict.
find(
"bar"),
nullptr);
85 ASSERT_EQ(dict.
find(
""),
nullptr);
88 TEST(OrderedDictTest, SubscriptOperatorThrowsWhenPassedKeysThatAreNotPresent) {
90 ASSERT_EQ(dict[
"a"], 1);
91 ASSERT_EQ(dict[
"b"], 2);
96 SubscriptOperatorReturnsItemsPositionallyWhenPassedIntegers) {
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);
104 TEST(OrderedDictTest, SubscriptOperatorsThrowswhenPassedKeysThatAreNotPresent) {
106 ASSERT_THROWS_WITH(dict[
"foo"],
"Key 'foo' is not defined");
107 ASSERT_THROWS_WITH(dict[
""],
"Key '' is not defined");
110 TEST(OrderedDictTest, UpdateInsertsAllItemsFromAnotherOrderedDict) {
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);
120 TEST(OrderedDictTest, UpdateAlsoChecksForDuplicates) {
123 ASSERT_THROWS_WITH(dict2.
update(dict),
"Key 'a' already defined");
126 TEST(OrderedDictTest, CanIterateItems) {
128 auto iterator = dict.
begin();
129 ASSERT_NE(iterator, dict.
end());
130 ASSERT_EQ(iterator->key(),
"a");
131 ASSERT_EQ(iterator->value(), 1);
133 ASSERT_NE(iterator, dict.
end());
134 ASSERT_EQ(iterator->key(),
"b");
135 ASSERT_EQ(iterator->value(), 2);
137 ASSERT_EQ(iterator, dict.
end());
140 TEST(OrderedDictTest, ClearMakesTheDictEmpty) {
147 TEST(OrderedDictTest, CanCopyConstruct) {
150 ASSERT_EQ(copy.
size(), 2);
151 ASSERT_EQ(*copy[0], 1);
152 ASSERT_EQ(*copy[1], 2);
155 TEST(OrderedDictTest, CanCopyAssign) {
158 ASSERT_NE(copy.
find(
"c"),
nullptr);
160 ASSERT_EQ(copy.size(), 2);
161 ASSERT_EQ(*copy[0], 1);
162 ASSERT_EQ(*copy[1], 2);
163 ASSERT_EQ(copy.find(
"c"),
nullptr);
166 TEST(OrderedDictTest, CanMoveConstruct) {
169 ASSERT_EQ(copy.
size(), 2);
170 ASSERT_EQ(*copy[0], 1);
171 ASSERT_EQ(*copy[1], 2);
174 TEST(OrderedDictTest, CanMoveAssign) {
177 ASSERT_NE(copy.
find(
"c"),
nullptr);
178 copy = std::move(dict);
179 ASSERT_EQ(copy.size(), 2);
180 ASSERT_EQ(*copy[0], 1);
181 ASSERT_EQ(*copy[1], 2);
182 ASSERT_EQ(copy.find(
"c"),
nullptr);
185 TEST(OrderedDictTest, CanInsertWithBraces) {
189 ASSERT_EQ(dict[
"a"].first, 1);
190 ASSERT_EQ(dict[
"a"].second, 2);
193 TEST(OrderedDictTest, ErrorMessagesIncludeTheKeyDescription) {
198 ASSERT_THROWS_WITH(dict[
"b"],
"Penguin 'b' is not defined");
199 ASSERT_THROWS_WITH(dict.
insert(
"a", 1),
"Penguin 'a' already defined");
202 TEST(OrderedDictTest, KeysReturnsAllKeys) {
204 ASSERT_EQ(dict.
keys(), std::vector<std::string>({
"a",
"b"}));
207 TEST(OrderedDictTest, ValuesReturnsAllValues) {
209 ASSERT_EQ(dict.
values(), std::vector<int>({1, 2}));
212 TEST(OrderedDictTest, ItemsReturnsAllItems) {
214 std::vector<OrderedDict<int>::Item> items = dict.
items();
215 ASSERT_EQ(items.size(), 2);
216 ASSERT_EQ(items[0].key(),
"a");
217 ASSERT_EQ(items[0].value(), 1);
218 ASSERT_EQ(items[1].key(),
"b");
219 ASSERT_EQ(items[1].value(), 2);
::std::vector< Value > values() const
Returns a newly allocated vector and copies all values from this OrderedDict into the vector...
size_t size() const noexcept
Returns the number of items currently stored in the OrderedDict.
Iterator begin()
Returns an iterator to the first item in the OrderedDict.
Iterator end()
Returns an iterator one past the last item in the OrderedDict.
Value & insert(K &&key, V &&value)
Inserts a new (key, value) pair into the OrderedDict.
Value * find(const Key &key) noexcept
Returns a pointer to the value associated with the given key, or a nullptr if no such key is stored i...
bool is_empty() const noexcept
Returns true if the OrderedDict contains no elements.
void update(OrderedDict &&other)
Inserts all items from other into this OrderedDict.
const std::string & key_description() const noexcept
Returns the key description string the OrderedDict was constructed with.
Item & back()
Returns the very last item in the OrderedDict and throws an exception if it is empty.
const std::vector< Item > & items() const noexcept
Returns the items stored in the OrderedDict.
::std::vector< Key > keys() const
Returns a newly allocated vector and copies all keys from this OrderedDict into the vector...
void clear()
Removes all items from this OrderedDict.
An ordered dictionary implementation, akin to Python's OrderedDict.
Item & front()
Returns the very first item in the OrderedDict and throws an exception if it is empty.