-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZmeyaTest07.cpp
131 lines (108 loc) · 5.71 KB
/
ZmeyaTest07.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "TestHelper.h"
#include "Zmeya.h"
#include "gtest/gtest.h"
struct HashMapTestRoot
{
zm::HashMap<int32_t, float> hashMap1;
zm::HashMap<int32_t, float> hashMap2;
zm::HashMap<int32_t, float> hashMap3;
zm::HashMap<zm::String, float> strHashMap1;
zm::HashMap<zm::String, float> strHashMap2;
zm::HashMap<int32_t, zm::String> strHashMap3;
zm::HashMap<int32_t, zm::String> strHashMap4;
zm::HashMap<zm::String, zm::String> strHashMap5;
zm::HashMap<zm::String, zm::String> strHashMap6;
};
static void validate(const HashMapTestRoot* root)
{
EXPECT_EQ(root->hashMap1.size(), std::size_t(5));
EXPECT_FLOAT_EQ(root->hashMap1.find(3, -1.0f), 7.0f);
EXPECT_FLOAT_EQ(root->hashMap1.find(4, -1.0f), 17.0f);
EXPECT_FLOAT_EQ(root->hashMap1.find(9, -1.0f), 79.0f);
EXPECT_FLOAT_EQ(root->hashMap1.find(11, -1.0f), 13.0f);
EXPECT_FLOAT_EQ(root->hashMap1.find(12, -1.0f), -1.0f);
EXPECT_FLOAT_EQ(root->hashMap1.find(77, 13.0f), 13.0f);
EXPECT_EQ(root->hashMap1.find(99), nullptr);
EXPECT_NE(root->hashMap1.find(3), nullptr);
EXPECT_EQ(root->hashMap1.contains(99), false);
EXPECT_EQ(root->hashMap1.contains(3), true);
EXPECT_EQ(root->hashMap2.size(), std::size_t(3));
EXPECT_FLOAT_EQ(root->hashMap2.find(1, 0.0f), -1.0f);
EXPECT_FLOAT_EQ(root->hashMap2.find(2, 0.0f), -2.0f);
EXPECT_FLOAT_EQ(root->hashMap2.find(3, 0.0f), -3.0f);
EXPECT_FLOAT_EQ(root->hashMap2.find(4, 0.0f), 0.0f);
EXPECT_FLOAT_EQ(root->hashMap2.find(5, 0.0f), 0.0f);
EXPECT_FLOAT_EQ(root->hashMap2.find(6, 0.0f), 0.0f);
EXPECT_EQ(root->hashMap3.size(), std::size_t(0));
EXPECT_FLOAT_EQ(root->hashMap3.find(1, 0.0f), 0.0f);
EXPECT_FLOAT_EQ(root->hashMap3.find(2, 0.0f), 0.0f);
EXPECT_FLOAT_EQ(root->hashMap3.find(3, 0.0f), 0.0f);
EXPECT_FALSE(root->hashMap3.contains(4));
EXPECT_FALSE(root->hashMap3.contains(5));
EXPECT_FALSE(root->hashMap3.contains(6));
EXPECT_EQ(root->strHashMap1.size(), std::size_t(3));
EXPECT_FLOAT_EQ(root->strHashMap1.find("one", 0.0f), 1.0f);
EXPECT_FLOAT_EQ(root->strHashMap1.find("two", 0.0f), 2.0f);
EXPECT_FLOAT_EQ(root->strHashMap1.find("three", 0.0f), 3.0f);
EXPECT_FLOAT_EQ(root->strHashMap1.find("five", 0.0f), 0.0f);
EXPECT_EQ(root->strHashMap2.size(), std::size_t(2));
EXPECT_FLOAT_EQ(root->strHashMap2.find("five", 0.0f), -5.0f);
EXPECT_FLOAT_EQ(root->strHashMap2.find("six", 0.0f), -6.0f);
EXPECT_FLOAT_EQ(root->strHashMap2.find("seven", 0.0f), 0.0f);
EXPECT_EQ(root->strHashMap3.size(), std::size_t(5));
EXPECT_STREQ(root->strHashMap3.find(1, ""), "one");
EXPECT_STREQ(root->strHashMap3.find(2, ""), "two");
EXPECT_STREQ(root->strHashMap3.find(3, ""), "three");
EXPECT_STREQ(root->strHashMap3.find(5, ""), "five");
EXPECT_STREQ(root->strHashMap3.find(10, ""), "ten");
EXPECT_STREQ(root->strHashMap3.find(13, nullptr), nullptr);
EXPECT_EQ(root->strHashMap4.size(), std::size_t(2));
EXPECT_STREQ(root->strHashMap4.find(5, ""), "five");
EXPECT_STREQ(root->strHashMap4.find(7, ""), "seven");
EXPECT_STREQ(root->strHashMap4.find(6, nullptr), nullptr);
EXPECT_EQ(root->strHashMap5.size(), std::size_t(5));
EXPECT_STREQ(root->strHashMap5.find("1", ""), "one");
EXPECT_STREQ(root->strHashMap5.find("2", ""), "two");
EXPECT_STREQ(root->strHashMap5.find("3", ""), "three");
EXPECT_STREQ(root->strHashMap5.find("5", ""), "five");
EXPECT_STREQ(root->strHashMap5.find("10", ""), "ten");
EXPECT_STREQ(root->strHashMap5.find("13", nullptr), nullptr);
EXPECT_EQ(root->strHashMap6.size(), std::size_t(2));
EXPECT_STREQ(root->strHashMap6.find("5", ""), "five");
EXPECT_STREQ(root->strHashMap6.find("7", ""), "seven");
EXPECT_STREQ(root->strHashMap6.find("6", nullptr), nullptr);
}
TEST(ZmeyaTestSuite, HashMapTest)
{
std::vector<char> bytesCopy;
{
std::shared_ptr<zm::BlobBuilder> blobBuilder = zm::BlobBuilder::create(1);
zm::BlobPtr<HashMapTestRoot> root = blobBuilder->allocate<HashMapTestRoot>();
// assign from std::unordered_map
std::unordered_map<int, float> testMap = {{3, 7.0f}, {4, 17.0f}, {9, 79.0f}, {11, 13.0f}, {77, 13.0f}};
blobBuilder->copyTo(root->hashMap1, testMap);
// assign from std::initializer_list
blobBuilder->copyTo(root->hashMap2, {{1, -1.0f}, {2, -2.0f}, {3, -3.0f}});
// assign from std::unordered_map (string key)
std::unordered_map<std::string, float> strMap1 = {{"one", 1.0f}, {"two", 2.0f}, {"three", 3.0f}};
blobBuilder->copyTo(root->strHashMap1, strMap1);
// assign from std::initializer_list (string key)
blobBuilder->copyTo(root->strHashMap2, {{"five", -5.0f}, {"six", -6.0f}});
// assign from std::unordered_map (string value)
std::unordered_map<int, std::string> strMap3 = {{1, "one"}, {2, "two"}, {3, "three"}, {5, "five"}, {10, "ten"}};
blobBuilder->copyTo(root->strHashMap3, strMap3);
// assign from std::initializer_list (string value)
blobBuilder->copyTo(root->strHashMap4, {{5, "five"}, {7, "seven"}});
// assign from std::unordered_map (string key+value)
std::unordered_map<std::string, std::string> strMap5 = {{"1", "one"}, {"2", "two"}, {"3", "three"}, {"5", "five"}, {"10", "ten"}};
blobBuilder->copyTo(root->strHashMap5, strMap5);
// assign from std::initializer_list (string key+value)
blobBuilder->copyTo(root->strHashMap6, {{"5", "five"}, {"7", "seven"}});
validate(root.get());
zm::Span<char> bytes = blobBuilder->finalize();
bytesCopy = utils::copyBytes(bytes);
std::memset(bytes.data, 0xFF, bytes.size);
}
const HashMapTestRoot* rootCopy = (const HashMapTestRoot*)(bytesCopy.data());
validate(rootCopy);
}