1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // Original author: Jim Blandy <[email protected]> <[email protected]>
30
31 // test_assembler_unittest.cc: Unit tests for google_breakpad::TestAssembler.
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h> // Must come first
35 #endif
36
37 #include <string>
38 #include <string.h>
39
40 #include "breakpad_googletest_includes.h"
41 #include "common/test_assembler.h"
42 #include "common/using_std_string.h"
43
44 using google_breakpad::test_assembler::Label;
45 using google_breakpad::test_assembler::Section;
46 using google_breakpad::test_assembler::kBigEndian;
47 using google_breakpad::test_assembler::kLittleEndian;
48 using testing::Test;
49
TEST(ConstructLabel,Simple)50 TEST(ConstructLabel, Simple) {
51 Label l;
52 }
53
TEST(ConstructLabel,Undefined)54 TEST(ConstructLabel, Undefined) {
55 Label l;
56 EXPECT_FALSE(l.IsKnownConstant());
57 }
58
TEST(ConstructLabelDeathTest,Undefined)59 TEST(ConstructLabelDeathTest, Undefined) {
60 Label l;
61 ASSERT_DEATH(l.Value(), "IsKnownConstant\\(&v\\)");
62 }
63
TEST(ConstructLabel,Constant)64 TEST(ConstructLabel, Constant) {
65 Label l(0x060b9f974eaf301eULL);
66 uint64_t v;
67 EXPECT_TRUE(l.IsKnownConstant(&v));
68 EXPECT_EQ(v, 0x060b9f974eaf301eULL);
69 EXPECT_EQ(l.Value(), 0x060b9f974eaf301eULL);
70 }
71
TEST(ConstructLabel,Copy)72 TEST(ConstructLabel, Copy) {
73 Label l;
74 Label m(l);
75 uint64_t v;
76 EXPECT_TRUE(l.IsKnownOffsetFrom(m, &v));
77 EXPECT_EQ(0U, v);
78 }
79
80 // The left-hand-side of a label assignment can be either
81 // unconstrained, related, or known. The right-hand-side can be any of
82 // those, or an integer.
TEST(Assignment,UnconstrainedToUnconstrained)83 TEST(Assignment, UnconstrainedToUnconstrained) {
84 Label l, m;
85 l = m;
86 EXPECT_EQ(0U, l-m);
87 EXPECT_TRUE(l.IsKnownOffsetFrom(m));
88 uint64_t d;
89 EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
90 EXPECT_EQ(0U, d);
91 EXPECT_FALSE(l.IsKnownConstant());
92 }
93
TEST(Assignment,UnconstrainedToRelated)94 TEST(Assignment, UnconstrainedToRelated) {
95 Label l, m, n;
96 l = n;
97 l = m;
98 EXPECT_EQ(0U, l-m);
99 EXPECT_TRUE(l.IsKnownOffsetFrom(m));
100 uint64_t d;
101 EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
102 EXPECT_EQ(0U, d);
103 EXPECT_FALSE(l.IsKnownConstant());
104 }
105
TEST(Assignment,UnconstrainedToKnown)106 TEST(Assignment, UnconstrainedToKnown) {
107 Label l, m;
108 l = 0x8fd16e55b20a39c1ULL;
109 l = m;
110 EXPECT_EQ(0U, l-m);
111 EXPECT_TRUE(l.IsKnownOffsetFrom(m));
112 uint64_t d;
113 EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
114 EXPECT_EQ(0U, d);
115 EXPECT_TRUE(m.IsKnownConstant());
116 EXPECT_EQ(0x8fd16e55b20a39c1ULL, m.Value());
117 }
118
TEST(Assignment,RelatedToUnconstrained)119 TEST(Assignment, RelatedToUnconstrained) {
120 Label l, m, n;
121 m = n;
122 l = m;
123 EXPECT_EQ(0U, l-n);
124 EXPECT_TRUE(l.IsKnownOffsetFrom(n));
125 uint64_t d;
126 EXPECT_TRUE(l.IsKnownOffsetFrom(n, &d));
127 EXPECT_EQ(0U, d);
128 EXPECT_FALSE(l.IsKnownConstant());
129 }
130
TEST(Assignment,RelatedToRelated)131 TEST(Assignment, RelatedToRelated) {
132 Label l, m, n, o;
133 l = n;
134 m = o;
135 l = m;
136 EXPECT_EQ(0U, n-o);
137 EXPECT_TRUE(n.IsKnownOffsetFrom(o));
138 uint64_t d;
139 EXPECT_TRUE(n.IsKnownOffsetFrom(o, &d));
140 EXPECT_EQ(0U, d);
141 EXPECT_FALSE(l.IsKnownConstant());
142 }
143
TEST(Assignment,RelatedToKnown)144 TEST(Assignment, RelatedToKnown) {
145 Label l, m, n;
146 m = n;
147 l = 0xd2011f8c82ad56f2ULL;
148 l = m;
149 EXPECT_TRUE(l.IsKnownConstant());
150 EXPECT_EQ(0xd2011f8c82ad56f2ULL, l.Value());
151 EXPECT_TRUE(m.IsKnownConstant());
152 EXPECT_EQ(0xd2011f8c82ad56f2ULL, m.Value());
153 EXPECT_TRUE(n.IsKnownConstant());
154 EXPECT_EQ(0xd2011f8c82ad56f2ULL, n.Value());
155 }
156
TEST(Assignment,KnownToUnconstrained)157 TEST(Assignment, KnownToUnconstrained) {
158 Label l, m;
159 m = 0x50b024c0d6073887ULL;
160 l = m;
161 EXPECT_TRUE(l.IsKnownConstant());
162 EXPECT_EQ(0x50b024c0d6073887ULL, l.Value());
163 EXPECT_TRUE(m.IsKnownConstant());
164 EXPECT_EQ(0x50b024c0d6073887ULL, m.Value());
165 }
166
TEST(Assignment,KnownToRelated)167 TEST(Assignment, KnownToRelated) {
168 Label l, m, n;
169 l = n;
170 m = 0x5348883655c727e5ULL;
171 l = m;
172 EXPECT_TRUE(l.IsKnownConstant());
173 EXPECT_EQ(0x5348883655c727e5ULL, l.Value());
174 EXPECT_TRUE(m.IsKnownConstant());
175 EXPECT_EQ(0x5348883655c727e5ULL, m.Value());
176 EXPECT_TRUE(n.IsKnownConstant());
177 EXPECT_EQ(0x5348883655c727e5ULL, n.Value());
178 }
179
TEST(Assignment,KnownToKnown)180 TEST(Assignment, KnownToKnown) {
181 Label l, m;
182 l = 0x36c209c20987564eULL;
183 m = 0x36c209c20987564eULL;
184 l = m;
185 EXPECT_TRUE(l.IsKnownConstant());
186 EXPECT_EQ(0x36c209c20987564eULL, l.Value());
187 EXPECT_TRUE(m.IsKnownConstant());
188 EXPECT_EQ(0x36c209c20987564eULL, m.Value());
189 }
190
TEST(Assignment,ConstantToUnconstrained)191 TEST(Assignment, ConstantToUnconstrained) {
192 Label l;
193 l = 0xc02495f4d7f5a957ULL;
194 EXPECT_TRUE(l.IsKnownConstant());
195 EXPECT_EQ(0xc02495f4d7f5a957ULL, l.Value());
196 }
197
TEST(Assignment,ConstantToRelated)198 TEST(Assignment, ConstantToRelated) {
199 Label l, m;
200 l = m;
201 l = 0x4577901cf275488dULL;
202 EXPECT_TRUE(l.IsKnownConstant());
203 EXPECT_EQ(0x4577901cf275488dULL, l.Value());
204 EXPECT_TRUE(m.IsKnownConstant());
205 EXPECT_EQ(0x4577901cf275488dULL, m.Value());
206 }
207
TEST(Assignment,ConstantToKnown)208 TEST(Assignment, ConstantToKnown) {
209 Label l;
210 l = 0xec0b9c369b7e8ea7ULL;
211 l = 0xec0b9c369b7e8ea7ULL;
212 EXPECT_TRUE(l.IsKnownConstant());
213 EXPECT_EQ(0xec0b9c369b7e8ea7ULL, l.Value());
214 }
215
TEST(AssignmentDeathTest,Self)216 TEST(AssignmentDeathTest, Self) {
217 Label l;
218 ASSERT_DEATH(l = l, "binding != this");
219 }
220
TEST(AssignmentDeathTest,IndirectCycle)221 TEST(AssignmentDeathTest, IndirectCycle) {
222 Label l, m, n;
223 l = m;
224 m = n;
225 ASSERT_DEATH(n = l, "binding != this");
226 }
227
TEST(AssignmentDeathTest,Cycle)228 TEST(AssignmentDeathTest, Cycle) {
229 Label l, m, n, o;
230 l = m;
231 m = n;
232 o = n;
233 ASSERT_DEATH(o = l, "binding != this");
234 }
235
TEST(Addition,LabelConstant)236 TEST(Addition, LabelConstant) {
237 Label l, m;
238 m = l + 0x5248d93e8bbe9497ULL;
239 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
240 uint64_t d;
241 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
242 EXPECT_EQ(0x5248d93e8bbe9497ULL, d);
243 EXPECT_FALSE(m.IsKnownConstant());
244 }
245
TEST(Addition,ConstantLabel)246 TEST(Addition, ConstantLabel) {
247 Label l, m;
248 m = 0xf51e94e00d6e3c84ULL + l;
249 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
250 uint64_t d;
251 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
252 EXPECT_EQ(0xf51e94e00d6e3c84ULL, d);
253 EXPECT_FALSE(m.IsKnownConstant());
254 }
255
TEST(Addition,KnownLabelConstant)256 TEST(Addition, KnownLabelConstant) {
257 Label l, m;
258 l = 0x16286307042ce0d8ULL;
259 m = l + 0x3fdddd91306719d7ULL;
260 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
261 uint64_t d;
262 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
263 EXPECT_EQ(0x3fdddd91306719d7ULL, d);
264 EXPECT_TRUE(m.IsKnownConstant());
265 EXPECT_EQ(0x16286307042ce0d8ULL + 0x3fdddd91306719d7ULL, m.Value());
266 }
267
TEST(Addition,ConstantKnownLabel)268 TEST(Addition, ConstantKnownLabel) {
269 Label l, m;
270 l = 0x50f62d0cdd1031deULL;
271 m = 0x1b13462d8577c538ULL + l;
272 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
273 uint64_t d;
274 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
275 EXPECT_EQ(0x1b13462d8577c538ULL, d);
276 EXPECT_TRUE(m.IsKnownConstant());
277 EXPECT_EQ(0x50f62d0cdd1031deULL + 0x1b13462d8577c538ULL, m.Value());
278 }
279
TEST(Subtraction,LabelConstant)280 TEST(Subtraction, LabelConstant) {
281 Label l, m;
282 m = l - 0x0620884d21d3138eULL;
283 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
284 uint64_t d;
285 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
286 EXPECT_EQ(-0x0620884d21d3138eULL, d);
287 EXPECT_FALSE(m.IsKnownConstant());
288 }
289
TEST(Subtraction,KnownLabelConstant)290 TEST(Subtraction, KnownLabelConstant) {
291 Label l, m;
292 l = 0x6237fbaf9ef7929eULL;
293 m = l - 0x317730995d2ab6eeULL;
294 EXPECT_TRUE(m.IsKnownOffsetFrom(l));
295 uint64_t d;
296 EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
297 EXPECT_EQ(-0x317730995d2ab6eeULL, d);
298 EXPECT_TRUE(m.IsKnownConstant());
299 EXPECT_EQ(0x6237fbaf9ef7929eULL - 0x317730995d2ab6eeULL, m.Value());
300 }
301
TEST(SubtractionDeathTest,LabelLabel)302 TEST(SubtractionDeathTest, LabelLabel) {
303 Label l, m;
304 ASSERT_DEATH(l - m, "IsKnownOffsetFrom\\(label, &offset\\)");
305 }
306
TEST(Subtraction,LabelLabel)307 TEST(Subtraction, LabelLabel) {
308 Label l, m;
309 l = m + 0x7fa77ec63e28a17aULL;
310 EXPECT_EQ(0x7fa77ec63e28a17aULL, l - m);
311 EXPECT_EQ(-0x7fa77ec63e28a17aULL, m - l);
312 }
313
TEST(IsKnownConstant,Undefined)314 TEST(IsKnownConstant, Undefined) {
315 Label l;
316 EXPECT_FALSE(l.IsKnownConstant());
317 }
318
TEST(IsKnownConstant,RelatedLabel)319 TEST(IsKnownConstant, RelatedLabel) {
320 Label l, m;
321 l = m;
322 EXPECT_FALSE(l.IsKnownConstant());
323 EXPECT_FALSE(m.IsKnownConstant());
324 }
325
TEST(IsKnownConstant,Constant)326 TEST(IsKnownConstant, Constant) {
327 Label l;
328 l = 0xf374b1bdd6a22576ULL;
329 EXPECT_TRUE(l.IsKnownConstant());
330 }
331
TEST(IsKnownOffsetFrom,Unrelated)332 TEST(IsKnownOffsetFrom, Unrelated) {
333 Label l, m;
334 EXPECT_FALSE(l.IsKnownOffsetFrom(m));
335 }
336
TEST(IsKnownOffsetFrom,Related)337 TEST(IsKnownOffsetFrom, Related) {
338 Label l, m;
339 l = m;
340 EXPECT_TRUE(l.IsKnownOffsetFrom(m));
341 }
342
343 // Test the construction of chains of related labels, and the
344 // propagation of values through them.
345 //
346 // Although the relations between labels are supposed to behave
347 // symmetrically --- that is, 'a = b' should put a and b in
348 // indistinguishable states --- there's a distinction made internally
349 // between the target (a) and the source (b).
350 //
351 // So there are five test axes to cover:
352 //
353 // - Do we construct the chain with assignment ("Assign") or with constructors
354 // ("Construct")?
355 //
356 // - Do we set the value of the label at the start of the chain
357 // ("Start") or the label at the end ("End")?
358 //
359 // - Are we testing the propagation of a relationship between variable
360 // values ("Relation"), or the propagation of a known constant value
361 // ("Value")?
362 //
363 // - Do we set the value before building the chain ("Before") or after
364 // the chain has been built ("After")?
365 //
366 // - Do we add new relationships to the end of the existing chain
367 // ("Forward") or to the beginning ("Backward")?
368 //
369 // Of course, "Construct" and "Backward" can't be combined, which
370 // eliminates eight combinations, and "Construct", "End", and "Before"
371 // can't be combined, which eliminates two more, so there are are 22
372 // combinations, not 32.
373
TEST(LabelChain,AssignStartRelationBeforeForward)374 TEST(LabelChain, AssignStartRelationBeforeForward) {
375 Label a, b, c, d;
376 Label x;
377 a = x;
378 b = a + 0x1;
379 c = b + 0x10;
380 d = c + 0x100;
381 EXPECT_EQ(0x111U, d-x);
382 EXPECT_EQ(0x11U, c-x);
383 EXPECT_EQ(0x1U, b-x);
384 EXPECT_EQ(0U, a-x);
385 }
386
TEST(LabelChain,AssignStartRelationBeforeBackward)387 TEST(LabelChain, AssignStartRelationBeforeBackward) {
388 Label a, b, c, d;
389 Label x;
390 a = x;
391 d = c + 0x100;
392 c = b + 0x10;
393 b = a + 0x1;
394 EXPECT_EQ(0x111U, d-x);
395 EXPECT_EQ(0x11U, c-x);
396 EXPECT_EQ(0x1U, b-x);
397 EXPECT_EQ(0U, a-x);
398 }
399
TEST(LabelChain,AssignStartRelationAfterForward)400 TEST(LabelChain, AssignStartRelationAfterForward) {
401 Label a, b, c, d;
402 Label x;
403 b = a + 0x1;
404 c = b + 0x10;
405 d = c + 0x100;
406 a = x;
407 EXPECT_EQ(0x111U, d-x);
408 EXPECT_EQ(0x11U, c-x);
409 EXPECT_EQ(0x1U, b-x);
410 EXPECT_EQ(0U, a-x);
411 }
412
TEST(LabelChain,AssignStartRelationAfterBackward)413 TEST(LabelChain, AssignStartRelationAfterBackward) {
414 Label a, b, c, d;
415 Label x;
416 d = c + 0x100;
417 c = b + 0x10;
418 b = a + 0x1;
419 a = x;
420 EXPECT_EQ(0x111U, d-x);
421 EXPECT_EQ(0x11U, c-x);
422 EXPECT_EQ(0x1U, b-x);
423 EXPECT_EQ(0U, a-x);
424 }
425
TEST(LabelChain,AssignStartValueBeforeForward)426 TEST(LabelChain, AssignStartValueBeforeForward) {
427 Label a, b, c, d;
428 a = 0xa131200190546ac2ULL;
429 b = a + 0x1;
430 c = b + 0x10;
431 d = c + 0x100;
432 EXPECT_EQ(0xa131200190546ac2ULL + 0x111U, d.Value());
433 EXPECT_EQ(0xa131200190546ac2ULL + 0x11U, c.Value());
434 EXPECT_EQ(0xa131200190546ac2ULL + 0x1U, b.Value());
435 EXPECT_EQ(0xa131200190546ac2ULL + 0U, a.Value());
436 }
437
TEST(LabelChain,AssignStartValueBeforeBackward)438 TEST(LabelChain, AssignStartValueBeforeBackward) {
439 Label a, b, c, d;
440 a = 0x8da17e1670ad4fa2ULL;
441 d = c + 0x100;
442 c = b + 0x10;
443 b = a + 0x1;
444 EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x111U, d.Value());
445 EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x11U, c.Value());
446 EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x1U, b.Value());
447 EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0U, a.Value());
448 }
449
TEST(LabelChain,AssignStartValueAfterForward)450 TEST(LabelChain, AssignStartValueAfterForward) {
451 Label a, b, c, d;
452 b = a + 0x1;
453 c = b + 0x10;
454 d = c + 0x100;
455 a = 0x99b8f51bafd41adaULL;
456 EXPECT_EQ(0x99b8f51bafd41adaULL + 0x111U, d.Value());
457 EXPECT_EQ(0x99b8f51bafd41adaULL + 0x11U, c.Value());
458 EXPECT_EQ(0x99b8f51bafd41adaULL + 0x1U, b.Value());
459 EXPECT_EQ(0x99b8f51bafd41adaULL + 0U, a.Value());
460 }
461
TEST(LabelChain,AssignStartValueAfterBackward)462 TEST(LabelChain, AssignStartValueAfterBackward) {
463 Label a, b, c, d;
464 d = c + 0x100;
465 c = b + 0x10;
466 b = a + 0x1;
467 a = 0xc86ca1d97ab5df6eULL;
468 EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x111U, d.Value());
469 EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x11U, c.Value());
470 EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x1U, b.Value());
471 EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0U, a.Value());
472 }
473
TEST(LabelChain,AssignEndRelationBeforeForward)474 TEST(LabelChain, AssignEndRelationBeforeForward) {
475 Label a, b, c, d;
476 Label x;
477 x = d;
478 b = a + 0x1;
479 c = b + 0x10;
480 d = c + 0x100;
481 EXPECT_EQ(-(uint64_t)0x111U, a-x);
482 EXPECT_EQ(-(uint64_t)0x110U, b-x);
483 EXPECT_EQ(-(uint64_t)0x100U, c-x);
484 EXPECT_EQ(-(uint64_t)0U, d-x);
485 }
486
TEST(LabelChain,AssignEndRelationBeforeBackward)487 TEST(LabelChain, AssignEndRelationBeforeBackward) {
488 Label a, b, c, d;
489 Label x;
490 x = d;
491 d = c + 0x100;
492 c = b + 0x10;
493 b = a + 0x1;
494 EXPECT_EQ(-(uint64_t)0x111U, a-x);
495 EXPECT_EQ(-(uint64_t)0x110U, b-x);
496 EXPECT_EQ(-(uint64_t)0x100U, c-x);
497 EXPECT_EQ(-(uint64_t)0U, d-x);
498 }
499
TEST(LabelChain,AssignEndRelationAfterForward)500 TEST(LabelChain, AssignEndRelationAfterForward) {
501 Label a, b, c, d;
502 Label x;
503 b = a + 0x1;
504 c = b + 0x10;
505 d = c + 0x100;
506 x = d;
507 EXPECT_EQ(-(uint64_t)0x111U, a-x);
508 EXPECT_EQ(-(uint64_t)0x110U, b-x);
509 EXPECT_EQ(-(uint64_t)0x100U, c-x);
510 EXPECT_EQ(-(uint64_t)0x000U, d-x);
511 }
512
TEST(LabelChain,AssignEndRelationAfterBackward)513 TEST(LabelChain, AssignEndRelationAfterBackward) {
514 Label a, b, c, d;
515 Label x;
516 d = c + 0x100;
517 c = b + 0x10;
518 b = a + 0x1;
519 x = d;
520 EXPECT_EQ(-(uint64_t)0x111U, a-x);
521 EXPECT_EQ(-(uint64_t)0x110U, b-x);
522 EXPECT_EQ(-(uint64_t)0x100U, c-x);
523 EXPECT_EQ(-(uint64_t)0x000U, d-x);
524 }
525
TEST(LabelChain,AssignEndValueBeforeForward)526 TEST(LabelChain, AssignEndValueBeforeForward) {
527 Label a, b, c, d;
528 d = 0xa131200190546ac2ULL;
529 b = a + 0x1;
530 c = b + 0x10;
531 d = c + 0x100;
532 EXPECT_EQ(0xa131200190546ac2ULL - 0x111, a.Value());
533 EXPECT_EQ(0xa131200190546ac2ULL - 0x110, b.Value());
534 EXPECT_EQ(0xa131200190546ac2ULL - 0x100, c.Value());
535 EXPECT_EQ(0xa131200190546ac2ULL - 0x000, d.Value());
536 }
537
TEST(LabelChain,AssignEndValueBeforeBackward)538 TEST(LabelChain, AssignEndValueBeforeBackward) {
539 Label a, b, c, d;
540 d = 0x8da17e1670ad4fa2ULL;
541 d = c + 0x100;
542 c = b + 0x10;
543 b = a + 0x1;
544 EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x111, a.Value());
545 EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x110, b.Value());
546 EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x100, c.Value());
547 EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x000, d.Value());
548 }
549
TEST(LabelChain,AssignEndValueAfterForward)550 TEST(LabelChain, AssignEndValueAfterForward) {
551 Label a, b, c, d;
552 b = a + 0x1;
553 c = b + 0x10;
554 d = c + 0x100;
555 d = 0x99b8f51bafd41adaULL;
556 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x111, a.Value());
557 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x110, b.Value());
558 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x100, c.Value());
559 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x000, d.Value());
560 }
561
TEST(LabelChain,AssignEndValueAfterBackward)562 TEST(LabelChain, AssignEndValueAfterBackward) {
563 Label a, b, c, d;
564 d = c + 0x100;
565 c = b + 0x10;
566 b = a + 0x1;
567 d = 0xc86ca1d97ab5df6eULL;
568 EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x111, a.Value());
569 EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x110, b.Value());
570 EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x100, c.Value());
571 EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x000, d.Value());
572 }
573
TEST(LabelChain,ConstructStartRelationBeforeForward)574 TEST(LabelChain, ConstructStartRelationBeforeForward) {
575 Label x;
576 Label a(x);
577 Label b(a + 0x1);
578 Label c(b + 0x10);
579 Label d(c + 0x100);
580 EXPECT_EQ(0x111U, d-x);
581 EXPECT_EQ(0x11U, c-x);
582 EXPECT_EQ(0x1U, b-x);
583 EXPECT_EQ(0U, a-x);
584 }
585
TEST(LabelChain,ConstructStartRelationAfterForward)586 TEST(LabelChain, ConstructStartRelationAfterForward) {
587 Label x;
588 Label a;
589 Label b(a + 0x1);
590 Label c(b + 0x10);
591 Label d(c + 0x100);
592 a = x;
593 EXPECT_EQ(0x111U, d-x);
594 EXPECT_EQ(0x11U, c-x);
595 EXPECT_EQ(0x1U, b-x);
596 EXPECT_EQ(0U, a-x);
597 }
598
TEST(LabelChain,ConstructStartValueBeforeForward)599 TEST(LabelChain, ConstructStartValueBeforeForward) {
600 Label a(0x5d234d177d01ccc8ULL);
601 Label b(a + 0x1);
602 Label c(b + 0x10);
603 Label d(c + 0x100);
604 EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x111U, d.Value());
605 EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x011U, c.Value());
606 EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x001U, b.Value());
607 EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x000U, a.Value());
608 }
609
TEST(LabelChain,ConstructStartValueAfterForward)610 TEST(LabelChain, ConstructStartValueAfterForward) {
611 Label a;
612 Label b(a + 0x1);
613 Label c(b + 0x10);
614 Label d(c + 0x100);
615 a = 0xded85d54586e84fcULL;
616 EXPECT_EQ(0xded85d54586e84fcULL + 0x111U, d.Value());
617 EXPECT_EQ(0xded85d54586e84fcULL + 0x011U, c.Value());
618 EXPECT_EQ(0xded85d54586e84fcULL + 0x001U, b.Value());
619 EXPECT_EQ(0xded85d54586e84fcULL + 0x000U, a.Value());
620 }
621
TEST(LabelChain,ConstructEndRelationAfterForward)622 TEST(LabelChain, ConstructEndRelationAfterForward) {
623 Label x;
624 Label a;
625 Label b(a + 0x1);
626 Label c(b + 0x10);
627 Label d(c + 0x100);
628 x = d;
629 EXPECT_EQ(-(uint64_t)0x111U, a-x);
630 EXPECT_EQ(-(uint64_t)0x110U, b-x);
631 EXPECT_EQ(-(uint64_t)0x100U, c-x);
632 EXPECT_EQ(-(uint64_t)0x000U, d-x);
633 }
634
TEST(LabelChain,ConstructEndValueAfterForward)635 TEST(LabelChain, ConstructEndValueAfterForward) {
636 Label a;
637 Label b(a + 0x1);
638 Label c(b + 0x10);
639 Label d(c + 0x100);
640 d = 0x99b8f51bafd41adaULL;
641 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x111, a.Value());
642 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x110, b.Value());
643 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x100, c.Value());
644 EXPECT_EQ(0x99b8f51bafd41adaULL - 0x000, d.Value());
645 }
646
TEST(LabelTree,KnownValue)647 TEST(LabelTree, KnownValue) {
648 Label l, m, n, o, p;
649 l = m;
650 m = n;
651 o = p;
652 p = n;
653 l = 0x536b5de3d468a1b5ULL;
654 EXPECT_EQ(0x536b5de3d468a1b5ULL, o.Value());
655 }
656
TEST(LabelTree,Related)657 TEST(LabelTree, Related) {
658 Label l, m, n, o, p;
659 l = m - 1;
660 m = n - 10;
661 o = p + 100;
662 p = n + 1000;
663 EXPECT_EQ(1111U, o - l);
664 }
665
TEST(EquationDeathTest,EqualConstants)666 TEST(EquationDeathTest, EqualConstants) {
667 Label m = 0x0d3962f280f07d24ULL;
668 Label n = 0x0d3962f280f07d24ULL;
669 m = n; // no death expected
670 }
671
TEST(EquationDeathTest,EqualIndirectConstants)672 TEST(EquationDeathTest, EqualIndirectConstants) {
673 Label m = 0xa347f1e5238fe6a1ULL;
674 Label n;
675 Label o = n;
676 n = 0xa347f1e5238fe6a1ULL;
677 n = m; // no death expected
678 }
679
TEST(EquationDeathTest,ConstantClash)680 TEST(EquationDeathTest, ConstantClash) {
681 Label m = 0xd4cc0f4f630ec741ULL;
682 Label n = 0x934cd2d8254fc3eaULL;
683 ASSERT_DEATH(m = n, "addend_ == addend");
684 }
685
TEST(EquationDeathTest,IndirectConstantClash)686 TEST(EquationDeathTest, IndirectConstantClash) {
687 Label m = 0xd4cc0f4f630ec741ULL;
688 Label n, o;
689 n = o;
690 o = 0xcfbe3b83ac49ce86ULL;
691 ASSERT_DEATH(m = n, "addend_ == addend");
692 }
693
694 // Assigning to a related label may free the next Binding on its
695 // chain. This test always passes; it is interesting to memory
696 // checkers and coverage analysis.
TEST(LabelReferenceCount,AssignmentFree)697 TEST(LabelReferenceCount, AssignmentFree) {
698 Label l;
699 {
700 Label m;
701 l = m;
702 }
703 // This should free m's Binding.
704 l = 0xca8bae92f0376d4fULL;
705 ASSERT_EQ(0xca8bae92f0376d4fULL, l.Value());
706 }
707
708 // Finding the value of a label may free the Binding it refers to. This test
709 // always passes; it is interesting to memory checkers and coverage analysis.
TEST(LabelReferenceCount,FindValueFree)710 TEST(LabelReferenceCount, FindValueFree) {
711 Label l;
712 {
713 Label m, n;
714 l = m;
715 m = n;
716 n = 0x7a0b0c576672daafULL;
717 // At this point, l's Binding refers to m's Binding, which refers
718 // to n's binding.
719 }
720 // Now, l is the only reference keeping the three Bindings alive.
721 // Resolving its value should free l's and m's original bindings.
722 ASSERT_EQ(0x7a0b0c576672daafULL, l.Value());
723 }
724
TEST(ConstructSection,Simple)725 TEST(ConstructSection, Simple) {
726 Section s;
727 }
728
TEST(ConstructSection,WithEndian)729 TEST(ConstructSection, WithEndian) {
730 Section s(kBigEndian);
731 }
732
733 // A fixture class for TestAssembler::Section tests.
734 class SectionFixture {
735 public:
736 Section section;
737 string contents;
738 static const uint8_t data[];
739 static const size_t data_size;
740 };
741
742 const uint8_t SectionFixture::data[] = {
743 0x87, 0x4f, 0x43, 0x67, 0x30, 0xd0, 0xd4, 0x0e
744 };
745
746 #define I0()
747 #define I1(a) { a }
748 #define I2(a,b) { a,b }
749 #define I3(a,b,c) { a,b,c }
750 #define I4(a,b,c,d) { a,b,c,d }
751 #define I5(a,b,c,d,e) { a,b,c,d,e }
752 #define I6(a,b,c,d,e,f) { a,b,c,d,e,f }
753 #define I7(a,b,c,d,e,f,g) { a,b,c,d,e,f,g }
754 #define I8(a,b,c,d,e,f,g,h) { a,b,c,d,e,f,g,h }
755 #define I9(a,b,c,d,e,f,g,h,i) { a,b,c,d,e,f,g,h,i }
756 #define ASSERT_BYTES(s, b) \
757 do \
758 { \
759 static const uint8_t expected_bytes[] = b; \
760 ASSERT_EQ(sizeof(expected_bytes), s.size()); \
761 ASSERT_TRUE(memcmp(s.data(), (const char*) expected_bytes, \
762 sizeof(expected_bytes)) == 0); \
763 } \
764 while(0)
765
766 class Append: public SectionFixture, public Test { };
767
TEST_F(Append,Bytes)768 TEST_F(Append, Bytes) {
769 section.Append(data, sizeof(data));
770 ASSERT_TRUE(section.GetContents(&contents));
771 ASSERT_EQ(sizeof(data), contents.size());
772 EXPECT_TRUE(0 == memcmp(contents.data(), (const char*) data, sizeof(data)));
773 }
774
TEST_F(Append,BytesTwice)775 TEST_F(Append, BytesTwice) {
776 section.Append(data, sizeof(data));
777 section.Append(data, sizeof(data));
778 ASSERT_TRUE(section.GetContents(&contents));
779 ASSERT_EQ(2 * sizeof(data), contents.size());
780 ASSERT_TRUE(0 == memcmp(contents.data(), (const char*) data, sizeof(data)));
781 ASSERT_TRUE(0 == memcmp(contents.data() + sizeof(data),
782 (const char*) data, sizeof(data)));
783 }
784
TEST_F(Append,String)785 TEST_F(Append, String) {
786 string s1 = "howdy ";
787 string s2 = "there";
788 section.Append(s1);
789 section.Append(s2);
790 ASSERT_TRUE(section.GetContents(&contents));
791 ASSERT_STREQ(contents.c_str(), "howdy there");
792 }
793
TEST_F(Append,CString)794 TEST_F(Append, CString) {
795 section.AppendCString("howdy");
796 section.AppendCString("");
797 section.AppendCString("there");
798 ASSERT_TRUE(section.GetContents(&contents));
799 ASSERT_EQ(string("howdy\0\0there\0", 13), contents);
800 }
801
TEST_F(Append,CStringSize)802 TEST_F(Append, CStringSize) {
803 section.AppendCString("howdy", 3);
804 section.AppendCString("there", 5);
805 section.AppendCString("fred", 6);
806 section.AppendCString("natalie", 0);
807 section.AppendCString("", 10);
808 ASSERT_TRUE(section.GetContents(&contents));
809 ASSERT_EQ(string("howtherefred\0\0\0\0\0\0\0\0\0\0\0\0", 24), contents);
810 }
811
TEST_F(Append,RepeatedBytes)812 TEST_F(Append, RepeatedBytes) {
813 section.Append((size_t) 10, '*');
814 ASSERT_TRUE(section.GetContents(&contents));
815 ASSERT_STREQ(contents.c_str(), "**********");
816 }
817
TEST_F(Append,GeneralLE1)818 TEST_F(Append, GeneralLE1) {
819 section.Append(kLittleEndian, 1, 42);
820 ASSERT_TRUE(section.GetContents(&contents));
821 ASSERT_BYTES(contents, I1(42));
822 }
823
TEST_F(Append,GeneralLE2)824 TEST_F(Append, GeneralLE2) {
825 section.Append(kLittleEndian, 2, 0x15a1);
826 ASSERT_TRUE(section.GetContents(&contents));
827 ASSERT_BYTES(contents, I2(0xa1, 0x15));
828 }
829
TEST_F(Append,GeneralLE3)830 TEST_F(Append, GeneralLE3) {
831 section.Append(kLittleEndian, 3, 0x59ae8d);
832 ASSERT_TRUE(section.GetContents(&contents));
833 ASSERT_BYTES(contents, I3(0x8d, 0xae, 0x59));
834 }
835
TEST_F(Append,GeneralLE4)836 TEST_F(Append, GeneralLE4) {
837 section.Append(kLittleEndian, 4, 0x51603c56);
838 ASSERT_TRUE(section.GetContents(&contents));
839 ASSERT_BYTES(contents, I4(0x56, 0x3c, 0x60, 0x51));
840 }
841
TEST_F(Append,GeneralLE5)842 TEST_F(Append, GeneralLE5) {
843 section.Append(kLittleEndian, 5, 0x385e2803b4ULL);
844 ASSERT_TRUE(section.GetContents(&contents));
845 ASSERT_BYTES(contents, I5(0xb4, 0x03, 0x28, 0x5e, 0x38));
846 }
847
TEST_F(Append,GeneralLE6)848 TEST_F(Append, GeneralLE6) {
849 section.Append(kLittleEndian, 6, 0xc7db9534dd1fULL);
850 ASSERT_TRUE(section.GetContents(&contents));
851 ASSERT_BYTES(contents, I6(0x1f, 0xdd, 0x34, 0x95, 0xdb, 0xc7));
852 }
853
TEST_F(Append,GeneralLE7)854 TEST_F(Append, GeneralLE7) {
855 section.Append(kLittleEndian, 7, 0x1445c9f1b843e6ULL);
856 ASSERT_TRUE(section.GetContents(&contents));
857 ASSERT_BYTES(contents, I7(0xe6, 0x43, 0xb8, 0xf1, 0xc9, 0x45, 0x14));
858 }
859
TEST_F(Append,GeneralLE8)860 TEST_F(Append, GeneralLE8) {
861 section.Append(kLittleEndian, 8, 0xaf48019dfe5c01e5ULL);
862 ASSERT_TRUE(section.GetContents(&contents));
863 ASSERT_BYTES(contents, I8(0xe5, 0x01, 0x5c, 0xfe, 0x9d, 0x01, 0x48, 0xaf));
864 }
865
TEST_F(Append,GeneralBE1)866 TEST_F(Append, GeneralBE1) {
867 section.Append(kBigEndian, 1, 0xd0ULL);
868 ASSERT_TRUE(section.GetContents(&contents));
869 ASSERT_BYTES(contents, I1(0xd0));
870 }
871
TEST_F(Append,GeneralBE2)872 TEST_F(Append, GeneralBE2) {
873 section.Append(kBigEndian, 2, 0x2e7eULL);
874 ASSERT_TRUE(section.GetContents(&contents));
875 ASSERT_BYTES(contents, I2(0x2e, 0x7e));
876 }
877
TEST_F(Append,GeneralBE3)878 TEST_F(Append, GeneralBE3) {
879 section.Append(kBigEndian, 3, 0x37dad6ULL);
880 ASSERT_TRUE(section.GetContents(&contents));
881 ASSERT_BYTES(contents, I3(0x37, 0xda, 0xd6));
882 }
883
TEST_F(Append,GeneralBE4)884 TEST_F(Append, GeneralBE4) {
885 section.Append(kBigEndian, 4, 0x715935c7ULL);
886 ASSERT_TRUE(section.GetContents(&contents));
887 ASSERT_BYTES(contents, I4(0x71, 0x59, 0x35, 0xc7));
888 }
889
TEST_F(Append,GeneralBE5)890 TEST_F(Append, GeneralBE5) {
891 section.Append(kBigEndian, 5, 0x42baeb02b7ULL);
892 ASSERT_TRUE(section.GetContents(&contents));
893 ASSERT_BYTES(contents, I5(0x42, 0xba, 0xeb, 0x02, 0xb7));
894 }
895
TEST_F(Append,GeneralBE6)896 TEST_F(Append, GeneralBE6) {
897 section.Append(kBigEndian, 6, 0xf1cdf10e7b18ULL);
898 ASSERT_TRUE(section.GetContents(&contents));
899 ASSERT_BYTES(contents, I6(0xf1, 0xcd, 0xf1, 0x0e, 0x7b, 0x18));
900 }
901
TEST_F(Append,GeneralBE7)902 TEST_F(Append, GeneralBE7) {
903 section.Append(kBigEndian, 7, 0xf50a724f0b0d20ULL);
904 ASSERT_TRUE(section.GetContents(&contents));
905 ASSERT_BYTES(contents, I7(0xf5, 0x0a, 0x72, 0x4f, 0x0b, 0x0d, 0x20));
906 }
907
TEST_F(Append,GeneralBE8)908 TEST_F(Append, GeneralBE8) {
909 section.Append(kBigEndian, 8, 0xa6b2cb5e98dc9c16ULL);
910 ASSERT_TRUE(section.GetContents(&contents));
911 ASSERT_BYTES(contents, I8(0xa6, 0xb2, 0xcb, 0x5e, 0x98, 0xdc, 0x9c, 0x16));
912 }
913
TEST_F(Append,GeneralLE1Label)914 TEST_F(Append, GeneralLE1Label) {
915 Label l;
916 section.Append(kLittleEndian, 1, l);
917 l = 42;
918 ASSERT_TRUE(section.GetContents(&contents));
919 ASSERT_BYTES(contents, I1(42));
920 }
921
TEST_F(Append,GeneralLE2Label)922 TEST_F(Append, GeneralLE2Label) {
923 Label l;
924 section.Append(kLittleEndian, 2, l);
925 l = 0x15a1;
926 ASSERT_TRUE(section.GetContents(&contents));
927 ASSERT_BYTES(contents, I2(0xa1, 0x15));
928 }
929
TEST_F(Append,GeneralLE3Label)930 TEST_F(Append, GeneralLE3Label) {
931 Label l;
932 section.Append(kLittleEndian, 3, l);
933 l = 0x59ae8d;
934 ASSERT_TRUE(section.GetContents(&contents));
935 ASSERT_BYTES(contents, I3(0x8d, 0xae, 0x59));
936 }
937
TEST_F(Append,GeneralLE4Label)938 TEST_F(Append, GeneralLE4Label) {
939 Label l;
940 section.Append(kLittleEndian, 4, l);
941 l = 0x51603c56;
942 ASSERT_TRUE(section.GetContents(&contents));
943 ASSERT_BYTES(contents, I4(0x56, 0x3c, 0x60, 0x51));
944 }
945
TEST_F(Append,GeneralLE5Label)946 TEST_F(Append, GeneralLE5Label) {
947 Label l;
948 section.Append(kLittleEndian, 5, l);
949 l = 0x385e2803b4ULL;
950 ASSERT_TRUE(section.GetContents(&contents));
951 ASSERT_BYTES(contents, I5(0xb4, 0x03, 0x28, 0x5e, 0x38));
952 }
953
TEST_F(Append,GeneralLE6Label)954 TEST_F(Append, GeneralLE6Label) {
955 Label l;
956 section.Append(kLittleEndian, 6, l);
957 l = 0xc7db9534dd1fULL;
958 ASSERT_TRUE(section.GetContents(&contents));
959 ASSERT_BYTES(contents, I6(0x1f, 0xdd, 0x34, 0x95, 0xdb, 0xc7));
960 }
961
TEST_F(Append,GeneralLE7Label)962 TEST_F(Append, GeneralLE7Label) {
963 Label l;
964 section.Append(kLittleEndian, 7, l);
965 l = 0x1445c9f1b843e6ULL;
966 ASSERT_TRUE(section.GetContents(&contents));
967 ASSERT_BYTES(contents, I7(0xe6, 0x43, 0xb8, 0xf1, 0xc9, 0x45, 0x14));
968 }
969
TEST_F(Append,GeneralLE8Label)970 TEST_F(Append, GeneralLE8Label) {
971 Label l;
972 section.Append(kLittleEndian, 8, l);
973 l = 0xaf48019dfe5c01e5ULL;
974 ASSERT_TRUE(section.GetContents(&contents));
975 ASSERT_BYTES(contents, I8(0xe5, 0x01, 0x5c, 0xfe, 0x9d, 0x01, 0x48, 0xaf));
976 }
977
TEST_F(Append,GeneralBE1Label)978 TEST_F(Append, GeneralBE1Label) {
979 Label l;
980 section.Append(kBigEndian, 1, l);
981 l = 0xd0ULL;
982 ASSERT_TRUE(section.GetContents(&contents));
983 ASSERT_BYTES(contents, I1(0xd0));
984 }
985
TEST_F(Append,GeneralBE2Label)986 TEST_F(Append, GeneralBE2Label) {
987 Label l;
988 section.Append(kBigEndian, 2, l);
989 l = 0x2e7eULL;
990 ASSERT_TRUE(section.GetContents(&contents));
991 ASSERT_BYTES(contents, I2(0x2e, 0x7e));
992 }
993
TEST_F(Append,GeneralBE3Label)994 TEST_F(Append, GeneralBE3Label) {
995 Label l;
996 section.Append(kBigEndian, 3, l);
997 l = 0x37dad6ULL;
998 ASSERT_TRUE(section.GetContents(&contents));
999 ASSERT_BYTES(contents, I3(0x37, 0xda, 0xd6));
1000 }
1001
TEST_F(Append,GeneralBE4Label)1002 TEST_F(Append, GeneralBE4Label) {
1003 Label l;
1004 section.Append(kBigEndian, 4, l);
1005 l = 0x715935c7ULL;
1006 ASSERT_TRUE(section.GetContents(&contents));
1007 ASSERT_BYTES(contents, I4(0x71, 0x59, 0x35, 0xc7));
1008 }
1009
TEST_F(Append,GeneralBE5Label)1010 TEST_F(Append, GeneralBE5Label) {
1011 Label l;
1012 section.Append(kBigEndian, 5, l);
1013 l = 0x42baeb02b7ULL;
1014 ASSERT_TRUE(section.GetContents(&contents));
1015 ASSERT_BYTES(contents, I5(0x42, 0xba, 0xeb, 0x02, 0xb7));
1016 }
1017
TEST_F(Append,GeneralBE6Label)1018 TEST_F(Append, GeneralBE6Label) {
1019 Label l;
1020 section.Append(kBigEndian, 6, l);
1021 l = 0xf1cdf10e7b18ULL;
1022 ASSERT_TRUE(section.GetContents(&contents));
1023 ASSERT_BYTES(contents, I6(0xf1, 0xcd, 0xf1, 0x0e, 0x7b, 0x18));
1024 }
1025
TEST_F(Append,GeneralBE7Label)1026 TEST_F(Append, GeneralBE7Label) {
1027 Label l;
1028 section.Append(kBigEndian, 7, l);
1029 l = 0xf50a724f0b0d20ULL;
1030 ASSERT_TRUE(section.GetContents(&contents));
1031 ASSERT_BYTES(contents, I7(0xf5, 0x0a, 0x72, 0x4f, 0x0b, 0x0d, 0x20));
1032 }
1033
TEST_F(Append,GeneralBE8Label)1034 TEST_F(Append, GeneralBE8Label) {
1035 Label l;
1036 section.Append(kBigEndian, 8, l);
1037 l = 0xa6b2cb5e98dc9c16ULL;
1038 ASSERT_TRUE(section.GetContents(&contents));
1039 ASSERT_BYTES(contents, I8(0xa6, 0xb2, 0xcb, 0x5e, 0x98, 0xdc, 0x9c, 0x16));
1040 }
1041
TEST_F(Append,B8)1042 TEST_F(Append, B8) {
1043 section.Append(1, 0x2a);
1044 section.B8(0xd3U);
1045 ASSERT_TRUE(section.GetContents(&contents));
1046 ASSERT_BYTES(contents, I2(0x2a, 0xd3));
1047 }
1048
TEST_F(Append,B8Label)1049 TEST_F(Append, B8Label) {
1050 Label l;
1051 section.Append(1, 0x2a);
1052 section.B8(l);
1053 l = 0x4bU;
1054 ASSERT_TRUE(section.GetContents(&contents));
1055 ASSERT_BYTES(contents, I2(0x2a, 0x4b));
1056 }
1057
TEST_F(Append,B16)1058 TEST_F(Append, B16) {
1059 section.Append(1, 0x2a);
1060 section.B16(0x472aU);
1061 ASSERT_TRUE(section.GetContents(&contents));
1062 ASSERT_BYTES(contents, I3(0x2a, 0x47, 0x2a));
1063 }
1064
TEST_F(Append,B16Label)1065 TEST_F(Append, B16Label) {
1066 Label l;
1067 section.Append(1, 0x2a);
1068 section.B16(l);
1069 l = 0x55e8U;
1070 ASSERT_TRUE(section.GetContents(&contents));
1071 ASSERT_BYTES(contents, I3(0x2a, 0x55, 0xe8));
1072 }
1073
TEST_F(Append,B32)1074 TEST_F(Append, B32) {
1075 section.Append(1, 0x2a);
1076 section.B32(0xbd412cbcU);
1077 ASSERT_TRUE(section.GetContents(&contents));
1078 ASSERT_BYTES(contents, I5(0x2a, 0xbd, 0x41, 0x2c, 0xbc));
1079 }
1080
TEST_F(Append,B32Label)1081 TEST_F(Append, B32Label) {
1082 Label l;
1083 section.Append(1, 0x2a);
1084 section.B32(l);
1085 l = 0x208e37d5U;
1086 ASSERT_TRUE(section.GetContents(&contents));
1087 ASSERT_BYTES(contents, I5(0x2a, 0x20, 0x8e, 0x37, 0xd5));
1088 }
1089
TEST_F(Append,B64)1090 TEST_F(Append, B64) {
1091 section.Append(1, 0x2a);
1092 section.B64(0x3402a013111e68adULL);
1093 ASSERT_TRUE(section.GetContents(&contents));
1094 ASSERT_BYTES(contents,
1095 I9(0x2a, 0x34, 0x02, 0xa0, 0x13, 0x11, 0x1e, 0x68, 0xad));
1096 }
1097
TEST_F(Append,B64Label)1098 TEST_F(Append, B64Label) {
1099 Label l;
1100 section.Append(1, 0x2a);
1101 section.B64(l);
1102 l = 0x355dbfbb4ac6d57fULL;
1103 ASSERT_TRUE(section.GetContents(&contents));
1104 ASSERT_BYTES(contents,
1105 I9(0x2a, 0x35, 0x5d, 0xbf, 0xbb, 0x4a, 0xc6, 0xd5, 0x7f));
1106 }
1107
TEST_F(Append,L8)1108 TEST_F(Append, L8) {
1109 section.Append(1, 0x2a);
1110 section.L8(0x26U);
1111 ASSERT_TRUE(section.GetContents(&contents));
1112 ASSERT_BYTES(contents, I2(0x2a, 0x26));
1113 }
1114
TEST_F(Append,L8Label)1115 TEST_F(Append, L8Label) {
1116 Label l;
1117 section.Append(1, 0x2a);
1118 section.L8(l);
1119 l = 0xa8U;
1120 ASSERT_TRUE(section.GetContents(&contents));
1121 ASSERT_BYTES(contents, I2(0x2a, 0xa8));
1122 }
1123
TEST_F(Append,L16)1124 TEST_F(Append, L16) {
1125 section.Append(1, 0x2a);
1126 section.L16(0xca6dU);
1127 ASSERT_TRUE(section.GetContents(&contents));
1128 ASSERT_BYTES(contents, I3(0x2a, 0x6d, 0xca));
1129 }
1130
TEST_F(Append,L16Label)1131 TEST_F(Append, L16Label) {
1132 Label l;
1133 section.Append(1, 0x2a);
1134 section.L16(l);
1135 l = 0xd21fU;
1136 ASSERT_TRUE(section.GetContents(&contents));
1137 ASSERT_BYTES(contents, I3(0x2a, 0x1f, 0xd2));
1138 }
1139
TEST_F(Append,L32)1140 TEST_F(Append, L32) {
1141 section.Append(1, 0x2a);
1142 section.L32(0x558f6181U);
1143 ASSERT_TRUE(section.GetContents(&contents));
1144 ASSERT_BYTES(contents, I5(0x2a, 0x81, 0x61, 0x8f, 0x55));
1145 }
1146
TEST_F(Append,L32Label)1147 TEST_F(Append, L32Label) {
1148 Label l;
1149 section.Append(1, 0x2a);
1150 section.L32(l);
1151 l = 0x4b810f82U;
1152 ASSERT_TRUE(section.GetContents(&contents));
1153 ASSERT_BYTES(contents, I5(0x2a, 0x82, 0x0f, 0x81, 0x4b));
1154 }
1155
TEST_F(Append,L64)1156 TEST_F(Append, L64) {
1157 section.Append(1, 0x2a);
1158 section.L64(0x564384f7579515bfULL);
1159 ASSERT_TRUE(section.GetContents(&contents));
1160 ASSERT_BYTES(contents,
1161 I9(0x2a, 0xbf, 0x15, 0x95, 0x57, 0xf7, 0x84, 0x43, 0x56));
1162 }
1163
TEST_F(Append,L64Label)1164 TEST_F(Append, L64Label) {
1165 Label l;
1166 section.Append(1, 0x2a);
1167 section.L64(l);
1168 l = 0x424b1d020667c8dbULL;
1169 ASSERT_TRUE(section.GetContents(&contents));
1170 ASSERT_BYTES(contents,
1171 I9(0x2a, 0xdb, 0xc8, 0x67, 0x06, 0x02, 0x1d, 0x4b, 0x42));
1172 }
1173
TEST_F(Append,D8Big)1174 TEST_F(Append, D8Big) {
1175 section.set_endianness(kBigEndian);
1176 section.Append(1, 0x2a);
1177 section.D8(0xe6U);
1178 ASSERT_TRUE(section.GetContents(&contents));
1179 ASSERT_BYTES(contents, I2(0x2a, 0xe6));
1180 }
1181
TEST_F(Append,D8BigLabel)1182 TEST_F(Append, D8BigLabel) {
1183 Label l;
1184 section.set_endianness(kBigEndian);
1185 section.Append(1, 0x2a);
1186 section.D8(l);
1187 l = 0xeeU;
1188 ASSERT_TRUE(section.GetContents(&contents));
1189 ASSERT_BYTES(contents, I2(0x2a, 0xee));
1190 }
1191
TEST_F(Append,D16Big)1192 TEST_F(Append, D16Big) {
1193 section.set_endianness(kBigEndian);
1194 section.Append(1, 0x2a);
1195 section.D16(0x83b1U);
1196 ASSERT_TRUE(section.GetContents(&contents));
1197 ASSERT_BYTES(contents, I3(0x2a, 0x83, 0xb1));
1198 }
1199
TEST_F(Append,D16BigLabel)1200 TEST_F(Append, D16BigLabel) {
1201 Label l;
1202 section.set_endianness(kBigEndian);
1203 section.Append(1, 0x2a);
1204 section.D16(l);
1205 l = 0x5b55U;
1206 ASSERT_TRUE(section.GetContents(&contents));
1207 ASSERT_BYTES(contents, I3(0x2a, 0x5b, 0x55));
1208 }
1209
TEST_F(Append,D32Big)1210 TEST_F(Append, D32Big) {
1211 section.set_endianness(kBigEndian);
1212 section.Append(1, 0x2a);
1213 section.D32(0xd0b0e431U);
1214 ASSERT_TRUE(section.GetContents(&contents));
1215 ASSERT_BYTES(contents, I5(0x2a, 0xd0, 0xb0, 0xe4, 0x31));
1216 }
1217
TEST_F(Append,D32BigLabel)1218 TEST_F(Append, D32BigLabel) {
1219 Label l;
1220 section.set_endianness(kBigEndian);
1221 section.Append(1, 0x2a);
1222 section.D32(l);
1223 l = 0x312fb340U;
1224 ASSERT_TRUE(section.GetContents(&contents));
1225 ASSERT_BYTES(contents, I5(0x2a, 0x31, 0x2f, 0xb3, 0x40));
1226 }
1227
TEST_F(Append,D64Big)1228 TEST_F(Append, D64Big) {
1229 section.set_endianness(kBigEndian);
1230 section.Append(1, 0x2a);
1231 section.D64(0xb109843500dbcb16ULL);
1232 ASSERT_TRUE(section.GetContents(&contents));
1233 ASSERT_BYTES(contents,
1234 I9(0x2a, 0xb1, 0x09, 0x84, 0x35, 0x00, 0xdb, 0xcb, 0x16));
1235 }
1236
TEST_F(Append,D64BigLabel)1237 TEST_F(Append, D64BigLabel) {
1238 Label l;
1239 section.set_endianness(kBigEndian);
1240 section.Append(1, 0x2a);
1241 section.D64(l);
1242 l = 0x9a0d61b70f671fd7ULL;
1243 ASSERT_TRUE(section.GetContents(&contents));
1244 ASSERT_BYTES(contents,
1245 I9(0x2a, 0x9a, 0x0d, 0x61, 0xb7, 0x0f, 0x67, 0x1f, 0xd7));
1246 }
1247
TEST_F(Append,D8Little)1248 TEST_F(Append, D8Little) {
1249 section.set_endianness(kLittleEndian);
1250 section.Append(1, 0x2a);
1251 section.D8(0x42U);
1252 ASSERT_TRUE(section.GetContents(&contents));
1253 ASSERT_BYTES(contents, I2(0x2a, 0x42));
1254 }
1255
TEST_F(Append,D8LittleLabel)1256 TEST_F(Append, D8LittleLabel) {
1257 Label l;
1258 section.set_endianness(kLittleEndian);
1259 section.Append(1, 0x2a);
1260 section.D8(l);
1261 l = 0x05U;
1262 ASSERT_TRUE(section.GetContents(&contents));
1263 ASSERT_BYTES(contents, I2(0x2a, 0x05));
1264 }
1265
TEST_F(Append,D16Little)1266 TEST_F(Append, D16Little) {
1267 section.set_endianness(kLittleEndian);
1268 section.Append(1, 0x2a);
1269 section.D16(0xc5c5U);
1270 ASSERT_TRUE(section.GetContents(&contents));
1271 ASSERT_BYTES(contents, I3(0x2a, 0xc5, 0xc5));
1272 }
1273
TEST_F(Append,D16LittleLabel)1274 TEST_F(Append, D16LittleLabel) {
1275 Label l;
1276 section.set_endianness(kLittleEndian);
1277 section.Append(1, 0x2a);
1278 section.D16(l);
1279 l = 0xb620U;
1280 ASSERT_TRUE(section.GetContents(&contents));
1281 ASSERT_BYTES(contents, I3(0x2a, 0x20, 0xb6));
1282 }
1283
TEST_F(Append,D32Little)1284 TEST_F(Append, D32Little) {
1285 section.set_endianness(kLittleEndian);
1286 section.Append(1, 0x2a);
1287 section.D32(0x1a87d0feU);
1288 ASSERT_TRUE(section.GetContents(&contents));
1289 ASSERT_BYTES(contents, I5(0x2a, 0xfe, 0xd0, 0x87, 0x1a));
1290 }
1291
TEST_F(Append,D32LittleLabel)1292 TEST_F(Append, D32LittleLabel) {
1293 Label l;
1294 section.set_endianness(kLittleEndian);
1295 section.Append(1, 0x2a);
1296 section.D32(l);
1297 l = 0xb8012d6bU;
1298 ASSERT_TRUE(section.GetContents(&contents));
1299 ASSERT_BYTES(contents, I5(0x2a, 0x6b, 0x2d, 0x01, 0xb8));
1300 }
1301
TEST_F(Append,D64Little)1302 TEST_F(Append, D64Little) {
1303 section.set_endianness(kLittleEndian);
1304 section.Append(1, 0x2a);
1305 section.D64(0x42de75c61375a1deULL);
1306 ASSERT_TRUE(section.GetContents(&contents));
1307 ASSERT_BYTES(contents,
1308 I9(0x2a, 0xde, 0xa1, 0x75, 0x13, 0xc6, 0x75, 0xde, 0x42));
1309 }
1310
TEST_F(Append,D64LittleLabel)1311 TEST_F(Append, D64LittleLabel) {
1312 Label l;
1313 section.set_endianness(kLittleEndian);
1314 section.Append(1, 0x2a);
1315 section.D64(l);
1316 l = 0x8b3bececf3fb5312ULL;
1317 ASSERT_TRUE(section.GetContents(&contents));
1318 ASSERT_BYTES(contents,
1319 I9(0x2a, 0x12, 0x53, 0xfb, 0xf3, 0xec, 0xec, 0x3b, 0x8b));
1320 }
1321
TEST_F(Append,Variety)1322 TEST_F(Append, Variety) {
1323 Label a, b, c, d, e, f, g, h;
1324 section.Append(kBigEndian, 1, a)
1325 .Append(kLittleEndian, 8, h)
1326 .Append(kBigEndian, 1, 0x8bULL)
1327 .Append(kLittleEndian, 8, 0x0ea56540448f4439ULL)
1328 .Append(kBigEndian, 2, b)
1329 .Append(kLittleEndian, 7, g)
1330 .Append(kBigEndian, 2, 0xcf15ULL)
1331 .Append(kLittleEndian, 7, 0x29694f04c5724aULL)
1332 .Append(kBigEndian, 3, c)
1333 .Append(kLittleEndian, 6, f)
1334 .Append(kBigEndian, 3, 0x8c3ffdULL)
1335 .Append(kLittleEndian, 6, 0x6f11ba80187aULL)
1336 .Append(kBigEndian, 4, d)
1337 .Append(kLittleEndian, 5, e)
1338 .Append(kBigEndian, 4, 0x2fda2472ULL)
1339 .Append(kLittleEndian, 5, 0x0aa02d423fULL)
1340 .Append(kBigEndian, 5, e)
1341 .Append(kLittleEndian, 4, d)
1342 .Append(kBigEndian, 5, 0x53ba432138ULL)
1343 .Append(kLittleEndian, 4, 0xf139ae60ULL)
1344 .Append(kBigEndian, 6, f)
1345 .Append(kLittleEndian, 3, c)
1346 .Append(kBigEndian, 6, 0x168e436af716ULL)
1347 .Append(kLittleEndian, 3, 0x3ef189ULL)
1348 .Append(kBigEndian, 7, g)
1349 .Append(kLittleEndian, 2, b)
1350 .Append(kBigEndian, 7, 0xacd4ef233e47d9ULL)
1351 .Append(kLittleEndian, 2, 0x5311ULL)
1352 .Append(kBigEndian, 8, h)
1353 .Append(kLittleEndian, 1, a)
1354 .Append(kBigEndian, 8, 0x4668d5f1c93637a1ULL)
1355 .Append(kLittleEndian, 1, 0x65ULL);
1356 a = 0x79ac9bd8aa256b35ULL;
1357 b = 0x22d13097ef86c91cULL;
1358 c = 0xf204968b0a05862fULL;
1359 d = 0x163177f15a0eb4ecULL;
1360 e = 0xbd1b0f1d977f2246ULL;
1361 f = 0x2b0842eee83c6461ULL;
1362 g = 0x92f4b928a4bf875eULL;
1363 h = 0x61a199a8f7286ba6ULL;
1364 ASSERT_EQ(8 * 18U, section.Size());
1365 ASSERT_TRUE(section.GetContents(&contents));
1366
1367 static const uint8_t expected[] = {
1368 0x35, 0xa6, 0x6b, 0x28, 0xf7, 0xa8, 0x99, 0xa1, 0x61,
1369 0x8b, 0x39, 0x44, 0x8f, 0x44, 0x40, 0x65, 0xa5, 0x0e,
1370 0xc9, 0x1c, 0x5e, 0x87, 0xbf, 0xa4, 0x28, 0xb9, 0xf4,
1371 0xcf, 0x15, 0x4a, 0x72, 0xc5, 0x04, 0x4f, 0x69, 0x29,
1372 0x05, 0x86, 0x2f, 0x61, 0x64, 0x3c, 0xe8, 0xee, 0x42,
1373 0x8c, 0x3f, 0xfd, 0x7a, 0x18, 0x80, 0xba, 0x11, 0x6f,
1374 0x5a, 0x0e, 0xb4, 0xec, 0x46, 0x22, 0x7f, 0x97, 0x1d,
1375 0x2f, 0xda, 0x24, 0x72, 0x3f, 0x42, 0x2d, 0xa0, 0x0a,
1376 0x1d, 0x97, 0x7f, 0x22, 0x46, 0xec, 0xb4, 0x0e, 0x5a,
1377 0x53, 0xba, 0x43, 0x21, 0x38, 0x60, 0xae, 0x39, 0xf1,
1378 0x42, 0xee, 0xe8, 0x3c, 0x64, 0x61, 0x2f, 0x86, 0x05,
1379 0x16, 0x8e, 0x43, 0x6a, 0xf7, 0x16, 0x89, 0xf1, 0x3e,
1380 0xf4, 0xb9, 0x28, 0xa4, 0xbf, 0x87, 0x5e, 0x1c, 0xc9,
1381 0xac, 0xd4, 0xef, 0x23, 0x3e, 0x47, 0xd9, 0x11, 0x53,
1382 0x61, 0xa1, 0x99, 0xa8, 0xf7, 0x28, 0x6b, 0xa6, 0x35,
1383 0x46, 0x68, 0xd5, 0xf1, 0xc9, 0x36, 0x37, 0xa1, 0x65,
1384 };
1385
1386 ASSERT_TRUE(0 == memcmp(contents.data(), expected, sizeof(expected)));
1387 }
1388
TEST_F(Append,Section)1389 TEST_F(Append, Section) {
1390 section.Append("murder");
1391 {
1392 Section middle;
1393 middle.Append(" she");
1394 section.Append(middle);
1395 }
1396 section.Append(" wrote");
1397 EXPECT_EQ(16U, section.Size());
1398 EXPECT_TRUE(section.GetContents(&contents));
1399 EXPECT_STREQ(contents.c_str(), "murder she wrote");
1400 }
1401
TEST_F(Append,SectionRefs)1402 TEST_F(Append, SectionRefs) {
1403 section.Append("sugar ");
1404 Label l;
1405 {
1406 Section middle;
1407 Label m;
1408 middle.B32(m);
1409 section.Append(middle);
1410 m = 0x66726565;
1411 }
1412 section.Append(" jazz");
1413 EXPECT_EQ(15U, section.Size());
1414 EXPECT_TRUE(section.GetContents(&contents));
1415 EXPECT_STREQ(contents.c_str(), "sugar free jazz");
1416 }
1417
TEST_F(Append,LEB128_0)1418 TEST_F(Append, LEB128_0) {
1419 section.LEB128(0);
1420 EXPECT_TRUE(section.GetContents(&contents));
1421 EXPECT_EQ(string("\0", 1), contents);
1422 }
1423
TEST_F(Append,LEB128_0x3f)1424 TEST_F(Append, LEB128_0x3f) {
1425 section.LEB128(0x3f);
1426 EXPECT_TRUE(section.GetContents(&contents));
1427 EXPECT_EQ(string("\x3f", 1), contents);
1428 }
1429
TEST_F(Append,LEB128_0x40)1430 TEST_F(Append, LEB128_0x40) {
1431 section.LEB128(0x40);
1432 EXPECT_TRUE(section.GetContents(&contents));
1433 EXPECT_EQ(string("\xc0\x00", 2), contents);
1434 }
1435
TEST_F(Append,LEB128_0x7f)1436 TEST_F(Append, LEB128_0x7f) {
1437 section.LEB128(0x7f);
1438 EXPECT_TRUE(section.GetContents(&contents));
1439 EXPECT_EQ(string("\xff\x00", 2), contents);
1440 }
1441
TEST_F(Append,LEB128_0x80)1442 TEST_F(Append, LEB128_0x80) {
1443 section.LEB128(0x80);
1444 EXPECT_TRUE(section.GetContents(&contents));
1445 EXPECT_EQ(string("\x80\x01", 2), contents);
1446 }
1447
TEST_F(Append,LEB128_0xff)1448 TEST_F(Append, LEB128_0xff) {
1449 section.LEB128(0xff);
1450 EXPECT_TRUE(section.GetContents(&contents));
1451 EXPECT_EQ(string("\xff\x01", 2), contents);
1452 }
1453
TEST_F(Append,LEB128_0x1fff)1454 TEST_F(Append, LEB128_0x1fff) {
1455 section.LEB128(0x1fff);
1456 EXPECT_TRUE(section.GetContents(&contents));
1457 EXPECT_EQ(string("\xff\x3f", 2), contents);
1458 }
1459
TEST_F(Append,LEB128_0x2000)1460 TEST_F(Append, LEB128_0x2000) {
1461 section.LEB128(0x2000);
1462 EXPECT_TRUE(section.GetContents(&contents));
1463 EXPECT_EQ(string("\x80\xc0\x00", 3), contents);
1464 }
1465
TEST_F(Append,LEB128_n1)1466 TEST_F(Append, LEB128_n1) {
1467 section.LEB128(-1);
1468 EXPECT_TRUE(section.GetContents(&contents));
1469 EXPECT_EQ(string("\x7f", 1), contents);
1470 }
1471
TEST_F(Append,LEB128_n0x40)1472 TEST_F(Append, LEB128_n0x40) {
1473 section.LEB128(-0x40);
1474 EXPECT_TRUE(section.GetContents(&contents));
1475 EXPECT_EQ(string("\x40", 1), contents);
1476 }
1477
TEST_F(Append,LEB128_n0x41)1478 TEST_F(Append, LEB128_n0x41) {
1479 section.LEB128(-0x41);
1480 EXPECT_TRUE(section.GetContents(&contents));
1481 EXPECT_EQ(string("\xbf\x7f", 2), contents);
1482 }
1483
TEST_F(Append,LEB128_n0x7f)1484 TEST_F(Append, LEB128_n0x7f) {
1485 section.LEB128(-0x7f);
1486 EXPECT_TRUE(section.GetContents(&contents));
1487 EXPECT_EQ(string("\x81\x7f", 2), contents);
1488 }
1489
TEST_F(Append,LEB128_n0x80)1490 TEST_F(Append, LEB128_n0x80) {
1491 section.LEB128(-0x80);
1492 EXPECT_TRUE(section.GetContents(&contents));
1493 EXPECT_EQ(string("\x80\x7f", 2), contents);
1494 }
1495
TEST_F(Append,LEB128_n0x2000)1496 TEST_F(Append, LEB128_n0x2000) {
1497 section.LEB128(-0x2000);
1498 EXPECT_TRUE(section.GetContents(&contents));
1499 EXPECT_EQ(string("\x80\x40", 2), contents);
1500 }
1501
TEST_F(Append,LEB128_n0x2001)1502 TEST_F(Append, LEB128_n0x2001) {
1503 section.LEB128(-0x2001);
1504 EXPECT_TRUE(section.GetContents(&contents));
1505 EXPECT_EQ(string("\xff\xbf\x7f", 3), contents);
1506 }
1507
TEST_F(Append,ULEB128_0)1508 TEST_F(Append,ULEB128_0) {
1509 section.ULEB128(0);
1510 EXPECT_TRUE(section.GetContents(&contents));
1511 EXPECT_EQ(string("\0", 1), contents);
1512 }
1513
TEST_F(Append,ULEB128_1)1514 TEST_F(Append,ULEB128_1) {
1515 section.ULEB128(1);
1516 EXPECT_TRUE(section.GetContents(&contents));
1517 EXPECT_EQ(string("\x01", 1), contents);
1518 }
1519
TEST_F(Append,ULEB128_0x3f)1520 TEST_F(Append,ULEB128_0x3f) {
1521 section.ULEB128(0x3f);
1522 EXPECT_TRUE(section.GetContents(&contents));
1523 EXPECT_EQ(string("\x3f", 1), contents);
1524 }
1525
TEST_F(Append,ULEB128_0x40)1526 TEST_F(Append,ULEB128_0x40) {
1527 section.ULEB128(0x40);
1528 EXPECT_TRUE(section.GetContents(&contents));
1529 EXPECT_EQ(string("\x40", 1), contents);
1530 }
1531
TEST_F(Append,ULEB128_0x7f)1532 TEST_F(Append,ULEB128_0x7f) {
1533 section.ULEB128(0x7f);
1534 EXPECT_TRUE(section.GetContents(&contents));
1535 EXPECT_EQ(string("\x7f", 1), contents);
1536 }
1537
TEST_F(Append,ULEB128_0x80)1538 TEST_F(Append,ULEB128_0x80) {
1539 section.ULEB128(0x80);
1540 EXPECT_TRUE(section.GetContents(&contents));
1541 EXPECT_EQ(string("\x80\x01", 2), contents);
1542 }
1543
TEST_F(Append,ULEB128_0xff)1544 TEST_F(Append,ULEB128_0xff) {
1545 section.ULEB128(0xff);
1546 EXPECT_TRUE(section.GetContents(&contents));
1547 EXPECT_EQ(string("\xff\x01", 2), contents);
1548 }
1549
TEST_F(Append,ULEB128_0x100)1550 TEST_F(Append,ULEB128_0x100) {
1551 section.ULEB128(0x100);
1552 EXPECT_TRUE(section.GetContents(&contents));
1553 EXPECT_EQ(string("\x80\x02", 2), contents);
1554 }
1555
TEST_F(Append,ULEB128_0x1fff)1556 TEST_F(Append,ULEB128_0x1fff) {
1557 section.ULEB128(0x1fff);
1558 EXPECT_TRUE(section.GetContents(&contents));
1559 EXPECT_EQ(string("\xff\x3f", 2), contents);
1560 }
1561
TEST_F(Append,ULEB128_0x2000)1562 TEST_F(Append,ULEB128_0x2000) {
1563 section.ULEB128(0x2000);
1564 EXPECT_TRUE(section.GetContents(&contents));
1565 EXPECT_EQ(string("\x80\x40", 2), contents);
1566 }
1567
TEST_F(Append,ULEB128_0x3fff)1568 TEST_F(Append,ULEB128_0x3fff) {
1569 section.ULEB128(0x3fff);
1570 EXPECT_TRUE(section.GetContents(&contents));
1571 EXPECT_EQ(string("\xff\x7f", 2), contents);
1572 }
1573
TEST_F(Append,ULEB128_0x4000)1574 TEST_F(Append,ULEB128_0x4000) {
1575 section.ULEB128(0x4000);
1576 EXPECT_TRUE(section.GetContents(&contents));
1577 EXPECT_EQ(string("\x80\x80\x01", 3), contents);
1578 }
1579
TEST_F(Append,ULEB128_12857)1580 TEST_F(Append,ULEB128_12857) {
1581 section.ULEB128(12857);
1582 EXPECT_TRUE(section.GetContents(&contents));
1583 EXPECT_EQ(string("\xb9\x64", 2), contents);
1584 }
1585
TEST_F(Append,LEBChain)1586 TEST_F(Append, LEBChain) {
1587 section.LEB128(-0x80).ULEB128(12857).Append("*");
1588 EXPECT_TRUE(section.GetContents(&contents));
1589 EXPECT_EQ(string("\x80\x7f\xb9\x64*", 5), contents);
1590 }
1591
1592
1593 class GetContents: public SectionFixture, public Test { };
1594
TEST_F(GetContents,Undefined)1595 TEST_F(GetContents, Undefined) {
1596 Label l;
1597 section.Append(kLittleEndian, 8, l);
1598 ASSERT_FALSE(section.GetContents(&contents));
1599 }
1600
TEST_F(GetContents,ClearsContents)1601 TEST_F(GetContents, ClearsContents) {
1602 section.Append((size_t) 10, '*');
1603 EXPECT_EQ(10U, section.Size());
1604 EXPECT_TRUE(section.GetContents(&contents));
1605 EXPECT_EQ(0U, section.Size());
1606 }
1607
TEST_F(GetContents,ClearsReferences)1608 TEST_F(GetContents, ClearsReferences) {
1609 Label l;
1610 section.Append(kBigEndian, 1, l);
1611 l = 42;
1612 ASSERT_TRUE(section.GetContents(&contents));
1613 ASSERT_BYTES(contents, I1(42));
1614 ASSERT_TRUE(section.GetContents(&contents)); // should not die
1615 }
1616
1617 class Miscellanea: public SectionFixture, public Test { };
1618
TEST_F(Miscellanea,Clear)1619 TEST_F(Miscellanea, Clear) {
1620 section.Append("howdy");
1621 Label l;
1622 section.L32(l);
1623 EXPECT_EQ(9U, section.Size());
1624 section.Clear();
1625 EXPECT_EQ(0U, section.Size());
1626 l = 0x8d231bf0U;
1627 ASSERT_TRUE(section.GetContents(&contents)); // should not die
1628 }
1629
TEST_F(Miscellanea,Align)1630 TEST_F(Miscellanea, Align) {
1631 section.Append("*");
1632 EXPECT_EQ(1U, section.Size());
1633 section.Align(4).Append("*");
1634 EXPECT_EQ(5U, section.Size());
1635 section.Append("*").Align(2);
1636 EXPECT_EQ(6U, section.Size());
1637 }
1638
TEST_F(Miscellanea,AlignPad)1639 TEST_F(Miscellanea, AlignPad) {
1640 section.Append("*");
1641 EXPECT_EQ(1U, section.Size());
1642 section.Align(4, ' ').Append("*");
1643 EXPECT_EQ(5U, section.Size());
1644 section.Append("*").Align(2, ' ');
1645 EXPECT_EQ(6U, section.Size());
1646 ASSERT_TRUE(section.GetContents(&contents));
1647 ASSERT_EQ(string("* **"), contents);
1648 }
1649
TEST_F(Miscellanea,StartHereMark)1650 TEST_F(Miscellanea, StartHereMark) {
1651 Label m;
1652 section.Append(42, ' ').Mark(&m).Append(13, '+');
1653 EXPECT_EQ(42U, m - section.start());
1654 EXPECT_EQ(42U + 13U, section.Here() - section.start());
1655 EXPECT_FALSE(section.start().IsKnownConstant());
1656 EXPECT_FALSE(m.IsKnownConstant());
1657 EXPECT_FALSE(section.Here().IsKnownConstant());
1658 }
1659
TEST_F(Miscellanea,Endianness)1660 TEST_F(Miscellanea, Endianness) {
1661 section.set_endianness(kBigEndian);
1662 EXPECT_EQ(kBigEndian, section.endianness());
1663 section.set_endianness(kLittleEndian);
1664 EXPECT_EQ(kLittleEndian, section.endianness());
1665 }
1666