-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringMap.cpp
155 lines (101 loc) · 3.68 KB
/
StringMap.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "StringMap.hpp"
#define _UNIT_TEST false
namespace lio {
// ===== Exception Implementation =====
const char* const
StringMapException::exceptionMessages_[] = {
STRINGMAP_EXCEPTION_MESSAGES
};
#undef STRINGMAP_EXCEPTION_MESSAGES // undef helps reducing unnecessary preprocessing work.
StringMapException::StringMapException(StringMapExceptionType exceptionType) {
this->exceptionType_ = exceptionType;
}
const char*
StringMapException::what() const noexcept {
return this->exceptionMessages_[(int) this->exceptionType_];
}
const StringMapExceptionType
StringMapException::type() const noexcept {
return this->exceptionType_;
}
// ===== Exception Implementation End =====
}
#if _UNIT_TEST
#include <iostream>
#include <string>
#include <map>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lio;
using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::pair;
string testContent = "Hello this is the test content and this is such an awesome test!";
TEST(StringMapTest, InitTest) {
StringMap<string> strMap (&testContent);
const string* content = strMap.GetContent();
EXPECT_EQ(content == &testContent, true);
}
TEST(StringMapTest, AddKeyValue) {
unsigned int location = static_cast<unsigned int> (content->find("Hello"));
unsigned int length = static_cast<unsigned int>(sizeof("Hello") - 1);
DataBlock<string*> keyData (location, length);
strMap.AddKeyValue("A", keyData);
strMap.AddKeyValue();
EXPECT_EQ(content == &testContent, true);
}
int main (int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
/*
using namespace lio;
using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::pair;
int main() {
string testContent = "Hello this is the test content and this is such an awesome test!";
StringMap<string> strMap (&testContent);
const string* content = strMap.GetContent();
DEBUG_cout << "orgContentLength: " << content->length() << endl;
unsigned int location = static_cast<unsigned int> (content->find("Hello"));
unsigned int length = static_cast<unsigned int>(sizeof("Hello") - 1);
//string keyA = "A";
DataBlock keyData (location, length);
strMap.AddKeyValue("A", keyData);
location = static_cast<unsigned int> (content->find("content"));
length = static_cast<unsigned int>(sizeof("content") - 1);
//string keyB = "B";
DataBlock keyDataB (location, length);
strMap.AddKeyValue("B", keyDataB);
strMap.AddKeyValue("C", DataBlock(6, 0));
DataBlock dba (2,6);
DataBlock dbb (2,5);
const bool MEANT_TO_BE_FAILED = true;
UnitTest::Test<DataBlock>(dba, dbb, "DBAB MUST FAIL", MEANT_TO_BE_FAILED);
string value = strMap.GetValueByKey("A");
UnitTest::Test<string>(value, "Hello", "GetValueByKeyA");
value = strMap.GetValueByKey("B");
DataBlock returned = strMap.GetDataBlockByKey("C");
UnitTest::Test<string>(value, "content", "GetValueByKeyB");
UnitTest::Test<DataBlock>(returned, DataBlock(6, 0), "GetDataBlockByKeyC");
DEBUG_cout << "orgContentLength: " << content->length() << endl;
int tokenizedCount = strMap.Tokenize(" ");
DEBUG_cout << "Token: " << tokenizedCount << endl;
UnitTest::Test<int>(tokenizedCount, 13, "Tokenize");
DataBlock section1 = strMap.GetSectionByIndex(0);
DEBUG_cout << "SectionStr:" << content->substr(section1.relativePosition, section1.length) << "END" << endl;
UnitTest::Test<DataBlock>(section1, DataBlock((unsigned int) 0, 5), "GetSectionByIndex1");
string appendingStr = "dddddddd";
strMap.AppendContent(&appendingStr);
UnitTest::Test<char>(*content->rbegin(), 'd', "Append String Test");
UnitTest::ReportTestResult();
return 0;
}
*/
#endif
#undef _UNIT_TEST