1
// Copyright (c) 2010, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// static_contained_range_map_unittest.cc: Unit tests for
31
// StaticContainedRangeMap.
33
// Author: Siyang Xie (lambxsy@google.com)
35
#include "breakpad_googletest_includes.h"
36
#include "processor/contained_range_map-inl.h"
37
#include "processor/static_contained_range_map-inl.h"
38
#include "processor/simple_serializer-inl.h"
39
#include "processor/map_serializers-inl.h"
41
#include "processor/scoped_ptr.h"
42
#include "processor/logging.h"
46
typedef google_breakpad::ContainedRangeMap<unsigned int, int> CRMMap;
47
typedef google_breakpad::StaticContainedRangeMap<unsigned int, int> TestMap;
49
// Each element in test_data contains the expected result when calling
50
// RetrieveRange on an address.
51
const int test_data[] = {
156
namespace google_breakpad {
158
class TestStaticCRMMap : public ::testing::Test {
162
// A referrence map for testing StaticCRMMap.
163
google_breakpad::ContainedRangeMap<unsigned int, int> crm_map_;
165
// Static version of crm_map using serialized data of crm_map.
166
// The goal of testing is to make sure TestMap provides same results for
167
// lookup operation(s) as CRMMap does.
168
google_breakpad::StaticContainedRangeMap<unsigned int, int> test_map_;
170
google_breakpad::ContainedRangeMapSerializer<unsigned int, int> serializer_;
172
scoped_array<char> serialized_data_;
175
void TestStaticCRMMap::SetUp() {
176
// First, do the StoreRange tests. This validates the containment
178
// We confirm the referrence map correctly stores data during setup.
179
ASSERT_TRUE (crm_map_.StoreRange(10, 10, 1));
180
ASSERT_FALSE(crm_map_.StoreRange(10, 10, 2)); // exactly equal to 1
181
ASSERT_FALSE(crm_map_.StoreRange(11, 10, 3)); // begins inside 1 and extends up
182
ASSERT_FALSE(crm_map_.StoreRange( 9, 10, 4)); // begins below 1 and ends inside
183
ASSERT_TRUE (crm_map_.StoreRange(11, 9, 5)); // contained by existing
184
ASSERT_TRUE (crm_map_.StoreRange(12, 7, 6));
185
ASSERT_TRUE (crm_map_.StoreRange( 9, 12, 7)); // contains existing
186
ASSERT_TRUE (crm_map_.StoreRange( 9, 13, 8));
187
ASSERT_TRUE (crm_map_.StoreRange( 8, 14, 9));
188
ASSERT_TRUE (crm_map_.StoreRange(30, 3, 10));
189
ASSERT_TRUE (crm_map_.StoreRange(33, 3, 11));
190
ASSERT_TRUE (crm_map_.StoreRange(30, 6, 12)); // storable but totally masked
191
ASSERT_TRUE (crm_map_.StoreRange(40, 8, 13)); // will be totally masked
192
ASSERT_TRUE (crm_map_.StoreRange(40, 4, 14));
193
ASSERT_TRUE (crm_map_.StoreRange(44, 4, 15));
194
ASSERT_FALSE(crm_map_.StoreRange(32, 10, 16)); // begins in #10, ends in #14
195
ASSERT_FALSE(crm_map_.StoreRange(50, 0, 17)); // zero length
196
ASSERT_TRUE (crm_map_.StoreRange(50, 10, 18));
197
ASSERT_TRUE (crm_map_.StoreRange(50, 1, 19));
198
ASSERT_TRUE (crm_map_.StoreRange(59, 1, 20));
199
ASSERT_TRUE (crm_map_.StoreRange(60, 1, 21));
200
ASSERT_TRUE (crm_map_.StoreRange(69, 1, 22));
201
ASSERT_TRUE (crm_map_.StoreRange(60, 10, 23));
202
ASSERT_TRUE (crm_map_.StoreRange(68, 1, 24));
203
ASSERT_TRUE (crm_map_.StoreRange(61, 1, 25));
204
ASSERT_TRUE (crm_map_.StoreRange(61, 8, 26));
205
ASSERT_FALSE(crm_map_.StoreRange(59, 9, 27));
206
ASSERT_FALSE(crm_map_.StoreRange(59, 10, 28));
207
ASSERT_FALSE(crm_map_.StoreRange(59, 11, 29));
208
ASSERT_TRUE (crm_map_.StoreRange(70, 10, 30));
209
ASSERT_TRUE (crm_map_.StoreRange(74, 2, 31));
210
ASSERT_TRUE (crm_map_.StoreRange(77, 2, 32));
211
ASSERT_FALSE(crm_map_.StoreRange(72, 6, 33));
212
ASSERT_TRUE (crm_map_.StoreRange(80, 3, 34));
213
ASSERT_TRUE (crm_map_.StoreRange(81, 1, 35));
214
ASSERT_TRUE (crm_map_.StoreRange(82, 1, 36));
215
ASSERT_TRUE (crm_map_.StoreRange(83, 3, 37));
216
ASSERT_TRUE (crm_map_.StoreRange(84, 1, 38));
217
ASSERT_TRUE (crm_map_.StoreRange(83, 1, 39));
218
ASSERT_TRUE (crm_map_.StoreRange(86, 5, 40));
219
ASSERT_TRUE (crm_map_.StoreRange(88, 1, 41));
220
ASSERT_TRUE (crm_map_.StoreRange(90, 1, 42));
221
ASSERT_TRUE (crm_map_.StoreRange(86, 1, 43));
222
ASSERT_TRUE (crm_map_.StoreRange(87, 1, 44));
223
ASSERT_TRUE (crm_map_.StoreRange(89, 1, 45));
224
ASSERT_TRUE (crm_map_.StoreRange(87, 4, 46));
225
ASSERT_TRUE (crm_map_.StoreRange(87, 3, 47));
226
ASSERT_FALSE(crm_map_.StoreRange(86, 2, 48));
228
// Serialize crm_map to generate serialized data.
230
serialized_data_.reset(serializer_.Serialize(&crm_map_, &size));
231
BPLOG(INFO) << "Serialized data size: " << size << " Bytes.";
233
// Construct test_map_ from serialized data.
234
test_map_ = TestMap(serialized_data_.get());
237
TEST_F(TestStaticCRMMap, TestEmptyMap) {
238
CRMMap empty_crm_map;
241
scoped_array<char> serialized_data;
242
serialized_data.reset(serializer_.Serialize(&empty_crm_map, &size));
243
scoped_ptr<TestMap> test_map(new TestMap(serialized_data.get()));
245
const unsigned int kCorrectSizeForEmptyMap = 16;
246
ASSERT_EQ(kCorrectSizeForEmptyMap, size);
248
const int *entry_test;
249
ASSERT_FALSE(test_map->RetrieveRange(-1, entry_test));
250
ASSERT_FALSE(test_map->RetrieveRange(0, entry_test));
251
ASSERT_FALSE(test_map->RetrieveRange(10, entry_test));
254
TEST_F(TestStaticCRMMap, TestSingleElementMap) {
256
// Test on one element:
258
crm_map.StoreRange(10, 10, entry);
261
scoped_array<char> serialized_data;
262
serialized_data.reset(serializer_.Serialize(&crm_map, &size));
263
scoped_ptr<TestMap> test_map(new TestMap(serialized_data.get()));
265
const unsigned int kCorrectSizeForSingleElementMap = 40;
266
ASSERT_EQ(kCorrectSizeForSingleElementMap, size);
268
const int *entry_test;
269
ASSERT_FALSE(test_map->RetrieveRange(-1, entry_test));
270
ASSERT_FALSE(test_map->RetrieveRange(0, entry_test));
271
ASSERT_TRUE(test_map->RetrieveRange(10, entry_test));
272
ASSERT_EQ(*entry_test, entry);
273
ASSERT_TRUE(test_map->RetrieveRange(13, entry_test));
274
ASSERT_EQ(*entry_test, entry);
277
TEST_F(TestStaticCRMMap, RunTestData) {
278
unsigned int test_high = sizeof(test_data) / sizeof(test_data[0]);
280
// Now, do the RetrieveRange tests. This further validates that the
281
// objects were stored properly and that retrieval returns the correct
283
// If GENERATE_TEST_DATA is defined, instead of the retrieval tests, a
284
// new test_data array will be printed. Exercise caution when doing this.
285
// Be sure to verify the results manually!
286
#ifdef GENERATE_TEST_DATA
287
printf(" const int test_data[] = {\n");
288
#endif // GENERATE_TEST_DATA
290
for (unsigned int address = 0; address < test_high; ++address) {
293
if (test_map_.RetrieveRange(address, entryptr))
296
#ifndef GENERATE_TEST_DATA
297
// Don't use ASSERT inside the loop because it won't show the failed
298
// |address|, and the line number will always be the same. That makes
299
// it difficult to figure out which test failed.
300
EXPECT_EQ(value, test_data[address]) << "FAIL: retrieve address "
302
#else // !GENERATE_TEST_DATA
303
printf(" %d%c%s // %d\n", value,
304
address == test_high - 1 ? ' ' : ',',
305
value < 10 ? " " : "",
307
#endif // !GENERATE_TEST_DATA
310
#ifdef GENERATE_TEST_DATA
312
#endif // GENERATE_TEST_DATA
315
} // namespace google_breakpad
317
int main(int argc, char *argv[]) {
318
::testing::InitGoogleTest(&argc, argv);
320
return RUN_ALL_TESTS();