~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to thirdparty/breakpad/processor/static_contained_range_map_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 23:40:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130307234027-1jc3yomfdliyapta
Tags: 0.6.0+dfsg1-0ubuntu1
Reduce dfsg stripping to fix build (source remains still DFSG compliant)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
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
 
13
// distribution.
 
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.
 
17
//
 
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.
 
29
 
 
30
// static_contained_range_map_unittest.cc: Unit tests for
 
31
// StaticContainedRangeMap.
 
32
//
 
33
// Author: Siyang Xie (lambxsy@google.com)
 
34
 
 
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"
 
40
 
 
41
#include "processor/scoped_ptr.h"
 
42
#include "processor/logging.h"
 
43
 
 
44
namespace {
 
45
 
 
46
typedef google_breakpad::ContainedRangeMap<unsigned int, int> CRMMap;
 
47
typedef google_breakpad::StaticContainedRangeMap<unsigned int, int> TestMap;
 
48
 
 
49
// Each element in test_data contains the expected result when calling
 
50
// RetrieveRange on an address.
 
51
const int test_data[] = {
 
52
  0,   // 0
 
53
  0,   // 1
 
54
  0,   // 2
 
55
  0,   // 3
 
56
  0,   // 4
 
57
  0,   // 5
 
58
  0,   // 6
 
59
  0,   // 7
 
60
  9,   // 8
 
61
  7,   // 9
 
62
  1,   // 10
 
63
  5,   // 11
 
64
  6,   // 12
 
65
  6,   // 13
 
66
  6,   // 14
 
67
  6,   // 15
 
68
  6,   // 16
 
69
  6,   // 17
 
70
  6,   // 18
 
71
  5,   // 19
 
72
  7,   // 20
 
73
  8,   // 21
 
74
  0,   // 22
 
75
  0,   // 23
 
76
  0,   // 24
 
77
  0,   // 25
 
78
  0,   // 26
 
79
  0,   // 27
 
80
  0,   // 28
 
81
  0,   // 29
 
82
  10,  // 30
 
83
  10,  // 31
 
84
  10,  // 32
 
85
  11,  // 33
 
86
  11,  // 34
 
87
  11,  // 35
 
88
  0,   // 36
 
89
  0,   // 37
 
90
  0,   // 38
 
91
  0,   // 39
 
92
  14,  // 40
 
93
  14,  // 41
 
94
  14,  // 42
 
95
  14,  // 43
 
96
  15,  // 44
 
97
  15,  // 45
 
98
  15,  // 46
 
99
  15,  // 47
 
100
  0,   // 48
 
101
  0,   // 49
 
102
  19,  // 50
 
103
  18,  // 51
 
104
  18,  // 52
 
105
  18,  // 53
 
106
  18,  // 54
 
107
  18,  // 55
 
108
  18,  // 56
 
109
  18,  // 57
 
110
  18,  // 58
 
111
  20,  // 59
 
112
  21,  // 60
 
113
  25,  // 61
 
114
  26,  // 62
 
115
  26,  // 63
 
116
  26,  // 64
 
117
  26,  // 65
 
118
  26,  // 66
 
119
  26,  // 67
 
120
  24,  // 68
 
121
  22,  // 69
 
122
  30,  // 70
 
123
  30,  // 71
 
124
  30,  // 72
 
125
  30,  // 73
 
126
  31,  // 74
 
127
  31,  // 75
 
128
  30,  // 76
 
129
  32,  // 77
 
130
  32,  // 78
 
131
  30,  // 79
 
132
  34,  // 80
 
133
  35,  // 81
 
134
  36,  // 82
 
135
  39,  // 83
 
136
  38,  // 84
 
137
  37,  // 85
 
138
  43,  // 86
 
139
  44,  // 87
 
140
  41,  // 88
 
141
  45,  // 89
 
142
  42,  // 90
 
143
  0,   // 91
 
144
  0,   // 92
 
145
  0,   // 93
 
146
  0,   // 94
 
147
  0,   // 95
 
148
  0,   // 96
 
149
  0,   // 97
 
150
  0,   // 98
 
151
  0    // 99
 
152
};
 
153
 
 
154
}  // namespace
 
155
 
 
156
namespace google_breakpad {
 
157
 
 
158
class TestStaticCRMMap : public ::testing::Test {
 
159
 protected:
 
160
  void SetUp();
 
161
 
 
162
  // A referrence map for testing StaticCRMMap.
 
163
  google_breakpad::ContainedRangeMap<unsigned int, int> crm_map_;
 
164
 
 
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_;
 
169
 
 
170
  google_breakpad::ContainedRangeMapSerializer<unsigned int, int> serializer_;
 
171
 
 
172
  scoped_array<char> serialized_data_;
 
173
};
 
174
 
 
175
void TestStaticCRMMap::SetUp() {
 
176
  // First, do the StoreRange tests.  This validates the containment
 
177
  // rules.
 
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));
 
227
 
 
228
  // Serialize crm_map to generate serialized data.
 
229
  unsigned int size;
 
230
  serialized_data_.reset(serializer_.Serialize(&crm_map_, &size));
 
231
  BPLOG(INFO) << "Serialized data size: " << size << " Bytes.";
 
232
 
 
233
  // Construct test_map_ from serialized data.
 
234
  test_map_ = TestMap(serialized_data_.get());
 
235
}
 
236
 
 
237
TEST_F(TestStaticCRMMap, TestEmptyMap) {
 
238
  CRMMap empty_crm_map;
 
239
 
 
240
  unsigned int size;
 
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()));
 
244
 
 
245
  const unsigned int kCorrectSizeForEmptyMap = 16;
 
246
  ASSERT_EQ(kCorrectSizeForEmptyMap, size);
 
247
 
 
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));
 
252
}
 
253
 
 
254
TEST_F(TestStaticCRMMap, TestSingleElementMap) {
 
255
  CRMMap crm_map;
 
256
  // Test on one element:
 
257
  int entry = 1;
 
258
  crm_map.StoreRange(10, 10,  entry);
 
259
 
 
260
  unsigned int size;
 
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()));
 
264
 
 
265
  const unsigned int kCorrectSizeForSingleElementMap = 40;
 
266
  ASSERT_EQ(kCorrectSizeForSingleElementMap, size);
 
267
 
 
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);
 
275
}
 
276
 
 
277
TEST_F(TestStaticCRMMap, RunTestData) {
 
278
  unsigned int test_high = sizeof(test_data) / sizeof(test_data[0]);
 
279
 
 
280
  // Now, do the RetrieveRange tests.  This further validates that the
 
281
  // objects were stored properly and that retrieval returns the correct
 
282
  // object.
 
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
 
289
 
 
290
  for (unsigned int address = 0; address < test_high; ++address) {
 
291
    const int *entryptr;
 
292
    int value = 0;
 
293
    if (test_map_.RetrieveRange(address, entryptr))
 
294
      value = *entryptr;
 
295
 
 
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 "
 
301
                                         << address;
 
302
#else  // !GENERATE_TEST_DATA
 
303
    printf("    %d%c%s  // %d\n", value,
 
304
                                  address == test_high - 1 ? ' ' : ',',
 
305
                                  value < 10 ? " " : "",
 
306
                                  address);
 
307
#endif  // !GENERATE_TEST_DATA
 
308
  }
 
309
 
 
310
#ifdef GENERATE_TEST_DATA
 
311
  printf("  };\n");
 
312
#endif  // GENERATE_TEST_DATA
 
313
}
 
314
 
 
315
}  // namespace google_breakpad
 
316
 
 
317
int main(int argc, char *argv[]) {
 
318
  ::testing::InitGoogleTest(&argc, argv);
 
319
 
 
320
  return RUN_ALL_TESTS();
 
321
}