1
// Copyright (c) 2006, 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
// contained_range_map_unittest.cc: Unit tests for ContainedRangeMap
32
// Author: Mark Mentovai
36
#include "processor/contained_range_map-inl.h"
38
#include "processor/logging.h"
41
#define ASSERT_TRUE(condition) \
43
fprintf(stderr, "FAIL: %s @ %s:%d\n", #condition, __FILE__, __LINE__); \
47
#define ASSERT_FALSE(condition) ASSERT_TRUE(!(condition))
53
using google_breakpad::ContainedRangeMap;
56
static bool RunTests() {
57
ContainedRangeMap<unsigned int, int> crm;
59
// First, do the StoreRange tests. This validates the containment
61
ASSERT_TRUE (crm.StoreRange(10, 10, 1));
62
ASSERT_FALSE(crm.StoreRange(10, 10, 2)); // exactly equal to 1
63
ASSERT_FALSE(crm.StoreRange(11, 10, 3)); // begins inside 1 and extends up
64
ASSERT_FALSE(crm.StoreRange( 9, 10, 4)); // begins below 1 and ends inside
65
ASSERT_TRUE (crm.StoreRange(11, 9, 5)); // contained by existing
66
ASSERT_TRUE (crm.StoreRange(12, 7, 6));
67
ASSERT_TRUE (crm.StoreRange( 9, 12, 7)); // contains existing
68
ASSERT_TRUE (crm.StoreRange( 9, 13, 8));
69
ASSERT_TRUE (crm.StoreRange( 8, 14, 9));
70
ASSERT_TRUE (crm.StoreRange(30, 3, 10));
71
ASSERT_TRUE (crm.StoreRange(33, 3, 11));
72
ASSERT_TRUE (crm.StoreRange(30, 6, 12)); // storable but totally masked
73
ASSERT_TRUE (crm.StoreRange(40, 8, 13)); // will be totally masked
74
ASSERT_TRUE (crm.StoreRange(40, 4, 14));
75
ASSERT_TRUE (crm.StoreRange(44, 4, 15));
76
ASSERT_FALSE(crm.StoreRange(32, 10, 16)); // begins in #10, ends in #14
77
ASSERT_FALSE(crm.StoreRange(50, 0, 17)); // zero length
78
ASSERT_TRUE (crm.StoreRange(50, 10, 18));
79
ASSERT_TRUE (crm.StoreRange(50, 1, 19));
80
ASSERT_TRUE (crm.StoreRange(59, 1, 20));
81
ASSERT_TRUE (crm.StoreRange(60, 1, 21));
82
ASSERT_TRUE (crm.StoreRange(69, 1, 22));
83
ASSERT_TRUE (crm.StoreRange(60, 10, 23));
84
ASSERT_TRUE (crm.StoreRange(68, 1, 24));
85
ASSERT_TRUE (crm.StoreRange(61, 1, 25));
86
ASSERT_TRUE (crm.StoreRange(61, 8, 26));
87
ASSERT_FALSE(crm.StoreRange(59, 9, 27));
88
ASSERT_FALSE(crm.StoreRange(59, 10, 28));
89
ASSERT_FALSE(crm.StoreRange(59, 11, 29));
90
ASSERT_TRUE (crm.StoreRange(70, 10, 30));
91
ASSERT_TRUE (crm.StoreRange(74, 2, 31));
92
ASSERT_TRUE (crm.StoreRange(77, 2, 32));
93
ASSERT_FALSE(crm.StoreRange(72, 6, 33));
94
ASSERT_TRUE (crm.StoreRange(80, 3, 34));
95
ASSERT_TRUE (crm.StoreRange(81, 1, 35));
96
ASSERT_TRUE (crm.StoreRange(82, 1, 36));
97
ASSERT_TRUE (crm.StoreRange(83, 3, 37));
98
ASSERT_TRUE (crm.StoreRange(84, 1, 38));
99
ASSERT_TRUE (crm.StoreRange(83, 1, 39));
100
ASSERT_TRUE (crm.StoreRange(86, 5, 40));
101
ASSERT_TRUE (crm.StoreRange(88, 1, 41));
102
ASSERT_TRUE (crm.StoreRange(90, 1, 42));
103
ASSERT_TRUE (crm.StoreRange(86, 1, 43));
104
ASSERT_TRUE (crm.StoreRange(87, 1, 44));
105
ASSERT_TRUE (crm.StoreRange(89, 1, 45));
106
ASSERT_TRUE (crm.StoreRange(87, 4, 46));
107
ASSERT_TRUE (crm.StoreRange(87, 3, 47));
108
ASSERT_FALSE(crm.StoreRange(86, 2, 48));
110
// Each element in test_data contains the expected result when calling
111
// RetrieveRange on an address.
112
const int test_data[] = {
214
unsigned int test_high = sizeof(test_data) / sizeof(int);
216
// Now, do the RetrieveRange tests. This further validates that the
217
// objects were stored properly and that retrieval returns the correct
219
// If GENERATE_TEST_DATA is defined, instead of the retrieval tests, a
220
// new test_data array will be printed. Exercise caution when doing this.
221
// Be sure to verify the results manually!
222
#ifdef GENERATE_TEST_DATA
223
printf(" const int test_data[] = {\n");
224
#endif // GENERATE_TEST_DATA
226
for (unsigned int address = 0; address < test_high; ++address) {
228
if (!crm.RetrieveRange(address, &value))
231
#ifndef GENERATE_TEST_DATA
232
// Don't use ASSERT inside the loop because it won't show the failed
233
// |address|, and the line number will always be the same. That makes
234
// it difficult to figure out which test failed.
235
if (value != test_data[address]) {
236
fprintf(stderr, "FAIL: retrieve %d expected %d observed %d @ %s:%d\n",
237
address, test_data[address], value, __FILE__, __LINE__);
240
#else // !GENERATE_TEST_DATA
241
printf(" %d%c%s // %d\n", value,
242
address == test_high - 1 ? ' ' : ',',
243
value < 10 ? " " : "",
245
#endif // !GENERATE_TEST_DATA
248
#ifdef GENERATE_TEST_DATA
250
#endif // GENERATE_TEST_DATA
259
int main(int argc, char **argv) {
260
BPLOG_INIT(&argc, &argv);
262
return RunTests() ? 0 : 1;