Line data Source code
1 : /**
2 : * @file BDictionaryTests.cpp
3 : * @copyright (c) 2014 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : * @license BSD, see the @c LICENSE file for more details
5 : * @brief Tests for the BDictionary class.
6 : */
7 :
8 : #include <gtest/gtest.h>
9 :
10 : #include "BDictionary.h"
11 : #include "BInteger.h"
12 : #include "BString.h"
13 :
14 : namespace bencoding {
15 : namespace tests {
16 :
17 : using namespace testing;
18 :
19 22 : class BDictionaryTests: public Test {};
20 :
21 5 : TEST_F(BDictionaryTests,
22 : DictionaryIsEmptyAfterCreation) {
23 2 : auto d = BDictionary::create();
24 :
25 1 : EXPECT_TRUE(d->empty());
26 1 : }
27 :
28 5 : TEST_F(BDictionaryTests,
29 : DictionaryIsEmptyAfterCreationFromEmptySequenceOfItems) {
30 2 : auto d = BDictionary::create({});
31 :
32 1 : EXPECT_TRUE(d->empty());
33 1 : }
34 :
35 5 : TEST_F(BDictionaryTests,
36 : DictionaryIsNotEmptyAfterItemIsAddedToEmptyDictionary) {
37 2 : auto d = BDictionary::create();
38 1 : (*d)[BString::create("test")] = BInteger::create(1);
39 :
40 1 : EXPECT_FALSE(d->empty());
41 1 : }
42 :
43 5 : TEST_F(BDictionaryTests,
44 : SizeCorrespondsToNumberOfItemsInsertedIntoDictionary) {
45 2 : auto d = BDictionary::create();
46 :
47 1 : ASSERT_EQ(0, d->size());
48 1 : (*d)[BString::create("test")] = BInteger::create(1);
49 1 : ASSERT_EQ(1, d->size());
50 1 : (*d)[BString::create("test2")] = BInteger::create(2);
51 1 : ASSERT_EQ(2, d->size());
52 : }
53 :
54 5 : TEST_F(BDictionaryTests,
55 : DictionaryCreatedFromNonEmptySequenceOfItemsContainsTheItems) {
56 2 : std::shared_ptr<BString> firstKey = BString::create("test1");
57 2 : std::shared_ptr<BItem> firstValue = BInteger::create(1);
58 2 : std::shared_ptr<BString> secondKey = BString::create("test2");
59 2 : std::shared_ptr<BItem> secondValue = BInteger::create(2);
60 : auto d = BDictionary::create({
61 : {firstKey, firstValue},
62 : {secondKey, secondValue}
63 2 : });
64 :
65 1 : EXPECT_EQ(2, d->size());
66 :
67 1 : auto i = d->begin();
68 1 : EXPECT_EQ(firstKey, i->first);
69 1 : EXPECT_EQ(firstValue, i->second);
70 :
71 1 : ++i;
72 1 : EXPECT_EQ(secondKey, i->first);
73 1 : EXPECT_EQ(secondValue, i->second);
74 1 : }
75 :
76 5 : TEST_F(BDictionaryTests,
77 : ValueIsChangedAfterInsertingAnotherValueForIdenticalKey) {
78 2 : auto d = BDictionary::create();
79 :
80 2 : std::shared_ptr<BString> key = BString::create("test");
81 1 : (*d)[key] = BInteger::create(1);
82 2 : std::shared_ptr<BItem> newValue = BInteger::create(1);
83 1 : (*d)[key] = newValue;
84 1 : ASSERT_EQ(newValue, (*d)[key]);
85 : }
86 :
87 5 : TEST_F(BDictionaryTests,
88 : AccessingNonExistingKeyReturnsNullPointer) {
89 2 : auto d = BDictionary::create();
90 :
91 1 : ASSERT_EQ(std::shared_ptr<BItem>(), (*d)[BString::create("test")]);
92 : }
93 :
94 5 : TEST_F(BDictionaryTests,
95 : IterationWorksCorrectlyOverEmptyDictionary) {
96 2 : auto d = BDictionary::create();
97 :
98 1 : EXPECT_EQ(d->begin(), d->end());
99 1 : }
100 :
101 5 : TEST_F(BDictionaryTests,
102 : IterationWorksCorrectlyOverEmptyConstantDictionary) {
103 2 : std::unique_ptr<const BDictionary> d = BDictionary::create();
104 :
105 1 : EXPECT_EQ(d->begin(), d->end());
106 1 : }
107 :
108 5 : TEST_F(BDictionaryTests,
109 : IterationWorksCorrectlyOverEmptyConstantDictionaryUsingCPrefixedMethods) {
110 2 : std::unique_ptr<const BDictionary> d = BDictionary::create();
111 :
112 1 : EXPECT_EQ(d->cbegin(), d->cend());
113 1 : }
114 :
115 5 : TEST_F(BDictionaryTests,
116 : IterationWorksCorrectlyOverDictionaryWithTwoItems) {
117 2 : auto d = BDictionary::create();
118 : // According to the specification, the values in the dictionary should
119 : // appear in a sorted order by the key values rather than addresses. To
120 : // test this, we first create the second key, which should have a lower
121 : // memory address than the first key, which is created after it. In this
122 : // way, we can test that the returned iterators iterate over the values in
123 : // the dictionary in a sorted order by the key values rather than
124 : // addresses.
125 2 : std::shared_ptr<BString> secondKey = BString::create("b");
126 2 : std::shared_ptr<BItem> secondValue = BInteger::create(2);
127 1 : (*d)[secondKey] = secondValue;
128 2 : std::shared_ptr<BString> firstKey = BString::create("a");
129 2 : std::shared_ptr<BItem> firstValue = BInteger::create(1);
130 1 : (*d)[firstKey] = firstValue;
131 :
132 1 : auto i = d->begin();
133 1 : ASSERT_EQ(firstKey, i->first);
134 1 : ASSERT_EQ(firstValue, i->second);
135 :
136 1 : ++i;
137 1 : ASSERT_EQ(secondKey, i->first);
138 1 : ASSERT_EQ(secondValue, i->second);
139 :
140 1 : ++i;
141 1 : ASSERT_EQ(d->end(), i);
142 : }
143 :
144 : } // namespace tests
145 3 : } // namespace bencoding
|