-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_mlog.cpp
230 lines (181 loc) · 5.38 KB
/
test_mlog.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "gtest/gtest.h"
#define MLOG_OPT
#define MLOG_TAG MlogTest
#include "mlog_base.h"
#include "mlog_integration.h"
// Best practice: put tests into a namespace
namespace testing {
namespace mlog {
/////////////////////////////////////////////////////////////////////
// Google Test.
//
enum SEARCH_MODE {
SEARCH_MODE_FORWARD,
SEARCH_MODE_BACKWARD
};
inline mlog_uint8_t* toText(mlog_uint32_t adr) {
return reinterpret_cast<mlog_uint8_t*>(adr);
}
static const mlog_uint8_t* getFirstLogEntry();
static bool getNextLogEntry(bool reset, mlog_uint32_t* address, bool* hasValue, mlog_value_t* value);
static bool advancedCharSearch(const char* text, char ch, SEARCH_MODE searchMode);
TEST(MlogTest, Simple) {
mlog_init();
mlog_log(toText(0x00123456));
const mlog_uint8_t* logBuffer = getFirstLogEntry();
EXPECT_EQ(0x00, logBuffer[0]);
EXPECT_EQ(0x12, logBuffer[1]);
EXPECT_EQ(0x34, logBuffer[2]);
EXPECT_EQ(0x56, logBuffer[3]);
MLOG_INFO(Hello_there);
}
TEST(MLogTest, SuccessiveTexts) {
mlog_init();
const mlog_uint8_t* buf = mlog_log_buffer();
mlog_log(toText(0x00778899));
mlog_log(toText(0x00ABCDEF));
mlog_uint32_t adr;
mlog_value_t val;
bool hasVal;
bool success;
success = getNextLogEntry(true, &adr, &hasVal, &val);
EXPECT_TRUE(success);
EXPECT_EQ(0x00778899, adr);
EXPECT_FALSE(hasVal);
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_TRUE(success);
EXPECT_EQ(0x00ABCDEF, adr);
EXPECT_FALSE(hasVal);
// Attempt to read beyond buffer.
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_FALSE(success);
}
TEST(MLogTest, SuccessiveTextsAndValues) {
mlog_init();
mlog_log(toText(0x00778899));
mlog_log_value(toText(0x00ABCDEF), 12345);
mlog_log(toText(0x00457799));
mlog_log_value(toText(0x00B0B1B2), 987654321);
mlog_uint32_t adr;
mlog_value_t val;
bool hasVal;
bool success;
success = getNextLogEntry(true, &adr, &hasVal, &val);
EXPECT_EQ(0x00778899, adr);
EXPECT_FALSE(hasVal);
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_EQ(0x00ABCDEF, adr);
EXPECT_EQ(12345, val);
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_EQ(0x00457799, adr);
EXPECT_FALSE(hasVal);
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_EQ(0x00B0B1B2, adr);
EXPECT_EQ(987654321, val);
}
TEST(MLogTest, ValuesOtherThanUnit32) {
mlog_init();
// 1. No value passed at al.
MLOG_INFO(without_value);
// 2. Pass an int.
MLOG_INFO_VALUE(with_value, 1234);
// 3. Pass an enum.
enum colors_e { red = 42, green = 43, blue = 44 };
colors_e current_color = green;
MLOG_INFO_VALUE(Current_color, current_color);
// 4. Pass double.
MLOG_INFO_VALUE(pi, 3.1415);
mlog_uint32_t adr;
mlog_value_t val;
bool hasVal;
bool success;
// 1.
success = getNextLogEntry(true, &adr, &hasVal, &val);
EXPECT_FALSE(hasVal);
// 2.
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_TRUE(hasVal);
EXPECT_EQ(1234, val);
// 3.
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_TRUE(hasVal);
EXPECT_EQ(43, val);
// 4.
success = getNextLogEntry(false, &adr, &hasVal, &val);
EXPECT_TRUE(hasVal);
EXPECT_EQ(3, val);
}
TEST(MLogTest, AdvancedCharSearch) {
EXPECT_TRUE(advancedCharSearch("Foobar", 'o', SEARCH_MODE_FORWARD));
}
static const mlog_uint8_t* getFirstLogEntry() {
return mlog_log_buffer() + sizeof(MLOG_MAGIC_HEADER);
}
static bool getNextLogEntry(bool reset, mlog_uint32_t* address, bool* hasValue, mlog_value_t* value) {
static const mlog_uint8_t* s_current;
if (reset) {
s_current = getFirstLogEntry();
}
// If end of buffer reached.
if (s_current + sizeof(mlog_uint32_t) > mlog_log_buffer_head()) {
return false;
}
// Get address.
mlog_uint32_t adr = MLOG_PEEK_MLOG_UINT32_T_BIG(s_current);
s_current += sizeof(adr);
if (adr & MLOG_VALUE_PRESENT_UINT32_MASK) {
adr &= ~MLOG_VALUE_PRESENT_UINT32_MASK;
mlog_value_t val = MLOG_PEEK_MLOG_UINT32_T_BIG(s_current);
s_current += sizeof(val);
*value = val;
*hasValue = true;
} else {
*hasValue = false;
}
*address = adr;
return true;
}
static bool advancedCharSearch(const char* text, char ch, SEARCH_MODE searchMode) {
bool found = false;
MLOG_INFO(Enter_AdvancedCharSearch);
MLOG_ENSURE(text != 0, text_must_not_be_null);
if (ch == '\0') {
MLOG_WARN(search_char_should_not_be_null);
}
int delta;
const char* begin;
const char* end;
switch (searchMode) {
case SEARCH_MODE_FORWARD:
begin = text;
end = text + strlen(text) - 1;
delta = 1;
break;
case SEARCH_MODE_BACKWARD:
begin = text + strlen(text) - 1;
end = text;
delta = -1;
default:
MLOG_ERROR_VALUE(invalid_search_mode, searchMode);
return false;
}
for (;;) {
if (*begin == ch) {
found = true;
break;
}
if (begin == end) {
break;
}
begin += delta;
end += delta;
}
MLOG_INFO(Exit_AdvancedCharSearch);
return found;
}
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}