toolkit/crashreporter/google-breakpad/src/common/test_assembler_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/test_assembler_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1662 @@
     1.4 +// Copyright (c) 2010, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
    1.34 +
    1.35 +// test_assembler_unittest.cc: Unit tests for google_breakpad::TestAssembler.
    1.36 +
    1.37 +#include <string>
    1.38 +#include <string.h>
    1.39 +
    1.40 +#include "breakpad_googletest_includes.h"
    1.41 +#include "common/test_assembler.h"
    1.42 +#include "common/using_std_string.h"
    1.43 +
    1.44 +using google_breakpad::test_assembler::Label;
    1.45 +using google_breakpad::test_assembler::Section;
    1.46 +using google_breakpad::test_assembler::kBigEndian;
    1.47 +using google_breakpad::test_assembler::kLittleEndian;
    1.48 +using testing::Test;
    1.49 +
    1.50 +TEST(ConstructLabel, Simple) {
    1.51 +  Label l;
    1.52 +}
    1.53 +
    1.54 +TEST(ConstructLabel, Undefined) {
    1.55 +  Label l;
    1.56 +  EXPECT_FALSE(l.IsKnownConstant());
    1.57 +}
    1.58 +
    1.59 +TEST(ConstructLabelDeathTest, Undefined) {
    1.60 +  Label l;
    1.61 +  ASSERT_DEATH(l.Value(), "IsKnownConstant\\(&v\\)");
    1.62 +}
    1.63 +
    1.64 +TEST(ConstructLabel, Constant) {
    1.65 +  Label l(0x060b9f974eaf301eULL);
    1.66 +  uint64_t v;
    1.67 +  EXPECT_TRUE(l.IsKnownConstant(&v));
    1.68 +  EXPECT_EQ(v, 0x060b9f974eaf301eULL);
    1.69 +  EXPECT_EQ(l.Value(), 0x060b9f974eaf301eULL);
    1.70 +}
    1.71 +
    1.72 +TEST(ConstructLabel, Copy) {
    1.73 +  Label l;
    1.74 +  Label m(l);
    1.75 +  uint64_t v;
    1.76 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m, &v));
    1.77 +  EXPECT_EQ(0U, v);
    1.78 +}
    1.79 +
    1.80 +// The left-hand-side of a label assignment can be either
    1.81 +// unconstrained, related, or known. The right-hand-side can be any of
    1.82 +// those, or an integer.
    1.83 +TEST(Assignment, UnconstrainedToUnconstrained) {
    1.84 +  Label l, m;
    1.85 +  l = m;
    1.86 +  EXPECT_EQ(0U, l-m);
    1.87 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m));
    1.88 +  uint64_t d;
    1.89 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
    1.90 +  EXPECT_EQ(0U, d);
    1.91 +  EXPECT_FALSE(l.IsKnownConstant());
    1.92 +}
    1.93 +
    1.94 +TEST(Assignment, UnconstrainedToRelated) {
    1.95 +  Label l, m, n;
    1.96 +  l = n;
    1.97 +  l = m;
    1.98 +  EXPECT_EQ(0U, l-m);
    1.99 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m));
   1.100 +  uint64_t d;
   1.101 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
   1.102 +  EXPECT_EQ(0U, d);
   1.103 +  EXPECT_FALSE(l.IsKnownConstant());
   1.104 +}
   1.105 +
   1.106 +TEST(Assignment, UnconstrainedToKnown) {
   1.107 +  Label l, m;
   1.108 +  l = 0x8fd16e55b20a39c1ULL;
   1.109 +  l = m;
   1.110 +  EXPECT_EQ(0U, l-m);
   1.111 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m));
   1.112 +  uint64_t d;
   1.113 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m, &d));
   1.114 +  EXPECT_EQ(0U, d);
   1.115 +  EXPECT_TRUE(m.IsKnownConstant());
   1.116 +  EXPECT_EQ(0x8fd16e55b20a39c1ULL, m.Value());
   1.117 +}
   1.118 +
   1.119 +TEST(Assignment, RelatedToUnconstrained) {
   1.120 +  Label l, m, n;
   1.121 +  m = n;
   1.122 +  l = m;
   1.123 +  EXPECT_EQ(0U, l-n);
   1.124 +  EXPECT_TRUE(l.IsKnownOffsetFrom(n));
   1.125 +  uint64_t d;
   1.126 +  EXPECT_TRUE(l.IsKnownOffsetFrom(n, &d));
   1.127 +  EXPECT_EQ(0U, d);
   1.128 +  EXPECT_FALSE(l.IsKnownConstant());
   1.129 +}
   1.130 +
   1.131 +TEST(Assignment, RelatedToRelated) {
   1.132 +  Label l, m, n, o;
   1.133 +  l = n;
   1.134 +  m = o;
   1.135 +  l = m;
   1.136 +  EXPECT_EQ(0U, n-o);
   1.137 +  EXPECT_TRUE(n.IsKnownOffsetFrom(o));
   1.138 +  uint64_t d;
   1.139 +  EXPECT_TRUE(n.IsKnownOffsetFrom(o, &d));
   1.140 +  EXPECT_EQ(0U, d);
   1.141 +  EXPECT_FALSE(l.IsKnownConstant());
   1.142 +}
   1.143 +
   1.144 +TEST(Assignment, RelatedToKnown) {
   1.145 +  Label l, m, n;
   1.146 +  m = n;
   1.147 +  l = 0xd2011f8c82ad56f2ULL;
   1.148 +  l = m;
   1.149 +  EXPECT_TRUE(l.IsKnownConstant());
   1.150 +  EXPECT_EQ(0xd2011f8c82ad56f2ULL, l.Value());
   1.151 +  EXPECT_TRUE(m.IsKnownConstant());
   1.152 +  EXPECT_EQ(0xd2011f8c82ad56f2ULL, m.Value());
   1.153 +  EXPECT_TRUE(n.IsKnownConstant());
   1.154 +  EXPECT_EQ(0xd2011f8c82ad56f2ULL, n.Value());
   1.155 +}
   1.156 +
   1.157 +TEST(Assignment, KnownToUnconstrained) {
   1.158 +  Label l, m;
   1.159 +  m = 0x50b024c0d6073887ULL;
   1.160 +  l = m;
   1.161 +  EXPECT_TRUE(l.IsKnownConstant());
   1.162 +  EXPECT_EQ(0x50b024c0d6073887ULL, l.Value());
   1.163 +  EXPECT_TRUE(m.IsKnownConstant());
   1.164 +  EXPECT_EQ(0x50b024c0d6073887ULL, m.Value());
   1.165 +}
   1.166 +
   1.167 +TEST(Assignment, KnownToRelated) {
   1.168 +  Label l, m, n;
   1.169 +  l = n;
   1.170 +  m = 0x5348883655c727e5ULL;
   1.171 +  l = m;
   1.172 +  EXPECT_TRUE(l.IsKnownConstant());
   1.173 +  EXPECT_EQ(0x5348883655c727e5ULL, l.Value());
   1.174 +  EXPECT_TRUE(m.IsKnownConstant());
   1.175 +  EXPECT_EQ(0x5348883655c727e5ULL, m.Value());
   1.176 +  EXPECT_TRUE(n.IsKnownConstant());
   1.177 +  EXPECT_EQ(0x5348883655c727e5ULL, n.Value());
   1.178 +}
   1.179 +
   1.180 +TEST(Assignment, KnownToKnown) {
   1.181 +  Label l, m;
   1.182 +  l = 0x36c209c20987564eULL;
   1.183 +  m = 0x36c209c20987564eULL;
   1.184 +  l = m;
   1.185 +  EXPECT_TRUE(l.IsKnownConstant());
   1.186 +  EXPECT_EQ(0x36c209c20987564eULL, l.Value());
   1.187 +  EXPECT_TRUE(m.IsKnownConstant());
   1.188 +  EXPECT_EQ(0x36c209c20987564eULL, m.Value());
   1.189 +}
   1.190 +
   1.191 +TEST(Assignment, ConstantToUnconstrained) {
   1.192 +  Label l;
   1.193 +  l = 0xc02495f4d7f5a957ULL;
   1.194 +  EXPECT_TRUE(l.IsKnownConstant());
   1.195 +  EXPECT_EQ(0xc02495f4d7f5a957ULL, l.Value());
   1.196 +}
   1.197 +
   1.198 +TEST(Assignment, ConstantToRelated) {
   1.199 +  Label l, m;
   1.200 +  l = m;
   1.201 +  l = 0x4577901cf275488dULL;
   1.202 +  EXPECT_TRUE(l.IsKnownConstant());
   1.203 +  EXPECT_EQ(0x4577901cf275488dULL, l.Value());
   1.204 +  EXPECT_TRUE(m.IsKnownConstant());
   1.205 +  EXPECT_EQ(0x4577901cf275488dULL, m.Value());
   1.206 +}
   1.207 +
   1.208 +TEST(Assignment, ConstantToKnown) {
   1.209 +  Label l;
   1.210 +  l = 0xec0b9c369b7e8ea7ULL;
   1.211 +  l = 0xec0b9c369b7e8ea7ULL;
   1.212 +  EXPECT_TRUE(l.IsKnownConstant());
   1.213 +  EXPECT_EQ(0xec0b9c369b7e8ea7ULL, l.Value());
   1.214 +}
   1.215 +
   1.216 +TEST(AssignmentDeathTest, Self) {
   1.217 +  Label l;
   1.218 +  ASSERT_DEATH(l = l, "binding != this");
   1.219 +}
   1.220 +
   1.221 +TEST(AssignmentDeathTest, IndirectCycle) {
   1.222 +  Label l, m, n;
   1.223 +  l = m;
   1.224 +  m = n;
   1.225 +  ASSERT_DEATH(n = l, "binding != this");
   1.226 +}
   1.227 +
   1.228 +TEST(AssignmentDeathTest, Cycle) {
   1.229 +  Label l, m, n, o;
   1.230 +  l = m;
   1.231 +  m = n;
   1.232 +  o = n;
   1.233 +  ASSERT_DEATH(o = l, "binding != this");
   1.234 +}
   1.235 +
   1.236 +TEST(Addition, LabelConstant) {
   1.237 +  Label l, m;
   1.238 +  m = l + 0x5248d93e8bbe9497ULL;
   1.239 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.240 +  uint64_t d;
   1.241 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.242 +  EXPECT_EQ(0x5248d93e8bbe9497ULL, d);
   1.243 +  EXPECT_FALSE(m.IsKnownConstant());
   1.244 +}
   1.245 +
   1.246 +TEST(Addition, ConstantLabel) {
   1.247 +  Label l, m;
   1.248 +  m = 0xf51e94e00d6e3c84ULL + l;
   1.249 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.250 +  uint64_t d;
   1.251 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.252 +  EXPECT_EQ(0xf51e94e00d6e3c84ULL, d);
   1.253 +  EXPECT_FALSE(m.IsKnownConstant());
   1.254 +}
   1.255 +
   1.256 +TEST(Addition, KnownLabelConstant) {
   1.257 +  Label l, m;
   1.258 +  l = 0x16286307042ce0d8ULL;
   1.259 +  m = l + 0x3fdddd91306719d7ULL;
   1.260 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.261 +  uint64_t d;
   1.262 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.263 +  EXPECT_EQ(0x3fdddd91306719d7ULL, d);
   1.264 +  EXPECT_TRUE(m.IsKnownConstant());
   1.265 +  EXPECT_EQ(0x16286307042ce0d8ULL + 0x3fdddd91306719d7ULL, m.Value());
   1.266 +}
   1.267 +
   1.268 +TEST(Addition, ConstantKnownLabel) {
   1.269 +  Label l, m;
   1.270 +  l = 0x50f62d0cdd1031deULL;
   1.271 +  m = 0x1b13462d8577c538ULL + l;
   1.272 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.273 +  uint64_t d;
   1.274 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.275 +  EXPECT_EQ(0x1b13462d8577c538ULL, d);
   1.276 +  EXPECT_TRUE(m.IsKnownConstant());
   1.277 +  EXPECT_EQ(0x50f62d0cdd1031deULL + 0x1b13462d8577c538ULL, m.Value());
   1.278 +}
   1.279 +
   1.280 +TEST(Subtraction, LabelConstant) {
   1.281 +  Label l, m;
   1.282 +  m = l - 0x0620884d21d3138eULL;
   1.283 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.284 +  uint64_t d;
   1.285 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.286 +  EXPECT_EQ(-0x0620884d21d3138eULL, d);
   1.287 +  EXPECT_FALSE(m.IsKnownConstant());
   1.288 +}
   1.289 +
   1.290 +TEST(Subtraction, KnownLabelConstant) {
   1.291 +  Label l, m;
   1.292 +  l = 0x6237fbaf9ef7929eULL;
   1.293 +  m = l - 0x317730995d2ab6eeULL;
   1.294 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l));
   1.295 +  uint64_t d;
   1.296 +  EXPECT_TRUE(m.IsKnownOffsetFrom(l, &d));
   1.297 +  EXPECT_EQ(-0x317730995d2ab6eeULL, d);
   1.298 +  EXPECT_TRUE(m.IsKnownConstant());
   1.299 +  EXPECT_EQ(0x6237fbaf9ef7929eULL - 0x317730995d2ab6eeULL, m.Value());
   1.300 +}
   1.301 +
   1.302 +TEST(SubtractionDeathTest, LabelLabel) {
   1.303 +  Label l, m;
   1.304 +  ASSERT_DEATH(l - m, "IsKnownOffsetFrom\\(label, &offset\\)");
   1.305 +}
   1.306 +
   1.307 +TEST(Subtraction, LabelLabel) {
   1.308 +  Label l, m;
   1.309 +  l = m + 0x7fa77ec63e28a17aULL;
   1.310 +  EXPECT_EQ(0x7fa77ec63e28a17aULL, l - m);
   1.311 +  EXPECT_EQ(-0x7fa77ec63e28a17aULL, m - l);
   1.312 +}
   1.313 +
   1.314 +TEST(IsKnownConstant, Undefined) {
   1.315 +  Label l;
   1.316 +  EXPECT_FALSE(l.IsKnownConstant());
   1.317 +}
   1.318 +
   1.319 +TEST(IsKnownConstant, RelatedLabel) {
   1.320 +  Label l, m;
   1.321 +  l = m;
   1.322 +  EXPECT_FALSE(l.IsKnownConstant());
   1.323 +  EXPECT_FALSE(m.IsKnownConstant());
   1.324 +}
   1.325 +
   1.326 +TEST(IsKnownConstant, Constant) {
   1.327 +  Label l;
   1.328 +  l = 0xf374b1bdd6a22576ULL;
   1.329 +  EXPECT_TRUE(l.IsKnownConstant());
   1.330 +}
   1.331 +
   1.332 +TEST(IsKnownOffsetFrom, Unrelated) {
   1.333 +  Label l, m;
   1.334 +  EXPECT_FALSE(l.IsKnownOffsetFrom(m));
   1.335 +}
   1.336 +
   1.337 +TEST(IsKnownOffsetFrom, Related) {
   1.338 +  Label l, m;
   1.339 +  l = m;
   1.340 +  EXPECT_TRUE(l.IsKnownOffsetFrom(m));
   1.341 +}
   1.342 +
   1.343 +// Test the construction of chains of related labels, and the
   1.344 +// propagation of values through them.
   1.345 +//
   1.346 +// Although the relations between labels are supposed to behave
   1.347 +// symmetrically --- that is, 'a = b' should put a and b in
   1.348 +// indistinguishable states --- there's a distinction made internally
   1.349 +// between the target (a) and the source (b).
   1.350 +// 
   1.351 +// So there are five test axes to cover:
   1.352 +//
   1.353 +// - Do we construct the chain with assignment ("Assign") or with constructors
   1.354 +//   ("Construct")?
   1.355 +//
   1.356 +// - Do we set the value of the label at the start of the chain
   1.357 +//   ("Start") or the label at the end ("End")?
   1.358 +//
   1.359 +// - Are we testing the propagation of a relationship between variable
   1.360 +//   values ("Relation"), or the propagation of a known constant value
   1.361 +//   ("Value")?
   1.362 +//
   1.363 +// - Do we set the value before building the chain ("Before") or after
   1.364 +//   the chain has been built ("After")?
   1.365 +//
   1.366 +// - Do we add new relationships to the end of the existing chain
   1.367 +//   ("Forward") or to the beginning ("Backward")?
   1.368 +//
   1.369 +// Of course, "Construct" and "Backward" can't be combined, which
   1.370 +// eliminates eight combinations, and "Construct", "End", and "Before"
   1.371 +// can't be combined, which eliminates two more, so there are are 22
   1.372 +// combinations, not 32.
   1.373 +
   1.374 +TEST(LabelChain, AssignStartRelationBeforeForward) {
   1.375 +  Label a, b, c, d;
   1.376 +  Label x;
   1.377 +  a = x;
   1.378 +  b = a + 0x1;
   1.379 +  c = b + 0x10;
   1.380 +  d = c + 0x100;
   1.381 +  EXPECT_EQ(0x111U, d-x);
   1.382 +  EXPECT_EQ(0x11U,  c-x);
   1.383 +  EXPECT_EQ(0x1U,   b-x);
   1.384 +  EXPECT_EQ(0U,     a-x);
   1.385 +}
   1.386 +
   1.387 +TEST(LabelChain, AssignStartRelationBeforeBackward) {
   1.388 +  Label a, b, c, d;
   1.389 +  Label x;
   1.390 +  a = x;
   1.391 +  d = c + 0x100;
   1.392 +  c = b + 0x10;
   1.393 +  b = a + 0x1;
   1.394 +  EXPECT_EQ(0x111U, d-x);
   1.395 +  EXPECT_EQ(0x11U,  c-x);
   1.396 +  EXPECT_EQ(0x1U,   b-x);
   1.397 +  EXPECT_EQ(0U,     a-x);
   1.398 +}
   1.399 +
   1.400 +TEST(LabelChain, AssignStartRelationAfterForward) {
   1.401 +  Label a, b, c, d;
   1.402 +  Label x;
   1.403 +  b = a + 0x1;
   1.404 +  c = b + 0x10;
   1.405 +  d = c + 0x100;
   1.406 +  a = x;
   1.407 +  EXPECT_EQ(0x111U, d-x);
   1.408 +  EXPECT_EQ(0x11U,  c-x);
   1.409 +  EXPECT_EQ(0x1U,   b-x);
   1.410 +  EXPECT_EQ(0U,     a-x);
   1.411 +}
   1.412 +
   1.413 +TEST(LabelChain, AssignStartRelationAfterBackward) {
   1.414 +  Label a, b, c, d;
   1.415 +  Label x;
   1.416 +  d = c + 0x100;
   1.417 +  c = b + 0x10;
   1.418 +  b = a + 0x1;
   1.419 +  a = x;
   1.420 +  EXPECT_EQ(0x111U, d-x);
   1.421 +  EXPECT_EQ(0x11U,  c-x);
   1.422 +  EXPECT_EQ(0x1U,   b-x);
   1.423 +  EXPECT_EQ(0U,     a-x);
   1.424 +}
   1.425 +
   1.426 +TEST(LabelChain, AssignStartValueBeforeForward) {
   1.427 +  Label a, b, c, d;
   1.428 +  a = 0xa131200190546ac2ULL;
   1.429 +  b = a + 0x1;
   1.430 +  c = b + 0x10;
   1.431 +  d = c + 0x100;
   1.432 +  EXPECT_EQ(0xa131200190546ac2ULL + 0x111U, d.Value());
   1.433 +  EXPECT_EQ(0xa131200190546ac2ULL + 0x11U,  c.Value());
   1.434 +  EXPECT_EQ(0xa131200190546ac2ULL + 0x1U,   b.Value());
   1.435 +  EXPECT_EQ(0xa131200190546ac2ULL + 0U,     a.Value());
   1.436 +}
   1.437 +
   1.438 +TEST(LabelChain, AssignStartValueBeforeBackward) {
   1.439 +  Label a, b, c, d;
   1.440 +  a = 0x8da17e1670ad4fa2ULL;
   1.441 +  d = c + 0x100;
   1.442 +  c = b + 0x10;
   1.443 +  b = a + 0x1;
   1.444 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x111U, d.Value());
   1.445 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x11U,  c.Value());
   1.446 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0x1U,   b.Value());
   1.447 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL + 0U,     a.Value());
   1.448 +}
   1.449 +
   1.450 +TEST(LabelChain, AssignStartValueAfterForward) {
   1.451 +  Label a, b, c, d;
   1.452 +  b = a + 0x1;
   1.453 +  c = b + 0x10;
   1.454 +  d = c + 0x100;
   1.455 +  a = 0x99b8f51bafd41adaULL;
   1.456 +  EXPECT_EQ(0x99b8f51bafd41adaULL + 0x111U, d.Value());
   1.457 +  EXPECT_EQ(0x99b8f51bafd41adaULL + 0x11U,  c.Value());
   1.458 +  EXPECT_EQ(0x99b8f51bafd41adaULL + 0x1U,   b.Value());
   1.459 +  EXPECT_EQ(0x99b8f51bafd41adaULL + 0U,     a.Value());
   1.460 +}
   1.461 +
   1.462 +TEST(LabelChain, AssignStartValueAfterBackward) {
   1.463 +  Label a, b, c, d;
   1.464 +  d = c + 0x100;
   1.465 +  c = b + 0x10;
   1.466 +  b = a + 0x1;
   1.467 +  a = 0xc86ca1d97ab5df6eULL;
   1.468 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x111U, d.Value());
   1.469 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x11U,  c.Value());
   1.470 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0x1U,   b.Value());
   1.471 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL + 0U,     a.Value());
   1.472 +}
   1.473 +
   1.474 +TEST(LabelChain, AssignEndRelationBeforeForward) {
   1.475 +  Label a, b, c, d;
   1.476 +  Label x;
   1.477 +  x = d;
   1.478 +  b = a + 0x1;
   1.479 +  c = b + 0x10;
   1.480 +  d = c + 0x100;
   1.481 +  EXPECT_EQ(-(uint64_t)0x111U, a-x);
   1.482 +  EXPECT_EQ(-(uint64_t)0x110U, b-x);
   1.483 +  EXPECT_EQ(-(uint64_t)0x100U, c-x);
   1.484 +  EXPECT_EQ(-(uint64_t)0U,     d-x);
   1.485 +}
   1.486 +
   1.487 +TEST(LabelChain, AssignEndRelationBeforeBackward) {
   1.488 +  Label a, b, c, d;
   1.489 +  Label x;
   1.490 +  x = d;
   1.491 +  d = c + 0x100;
   1.492 +  c = b + 0x10;
   1.493 +  b = a + 0x1;
   1.494 +  EXPECT_EQ(-(uint64_t)0x111U, a-x);
   1.495 +  EXPECT_EQ(-(uint64_t)0x110U, b-x);
   1.496 +  EXPECT_EQ(-(uint64_t)0x100U, c-x);
   1.497 +  EXPECT_EQ(-(uint64_t)0U,     d-x);
   1.498 +}
   1.499 +
   1.500 +TEST(LabelChain, AssignEndRelationAfterForward) {
   1.501 +  Label a, b, c, d;
   1.502 +  Label x;
   1.503 +  b = a + 0x1;
   1.504 +  c = b + 0x10;
   1.505 +  d = c + 0x100;
   1.506 +  x = d;
   1.507 +  EXPECT_EQ(-(uint64_t)0x111U, a-x);
   1.508 +  EXPECT_EQ(-(uint64_t)0x110U, b-x);
   1.509 +  EXPECT_EQ(-(uint64_t)0x100U, c-x);
   1.510 +  EXPECT_EQ(-(uint64_t)0x000U, d-x);
   1.511 +}
   1.512 +
   1.513 +TEST(LabelChain, AssignEndRelationAfterBackward) {
   1.514 +  Label a, b, c, d;
   1.515 +  Label x;
   1.516 +  d = c + 0x100;
   1.517 +  c = b + 0x10;
   1.518 +  b = a + 0x1;
   1.519 +  x = d;
   1.520 +  EXPECT_EQ(-(uint64_t)0x111U, a-x);
   1.521 +  EXPECT_EQ(-(uint64_t)0x110U, b-x);
   1.522 +  EXPECT_EQ(-(uint64_t)0x100U, c-x);
   1.523 +  EXPECT_EQ(-(uint64_t)0x000U, d-x);
   1.524 +}
   1.525 +
   1.526 +TEST(LabelChain, AssignEndValueBeforeForward) {
   1.527 +  Label a, b, c, d;
   1.528 +  d = 0xa131200190546ac2ULL;
   1.529 +  b = a + 0x1;
   1.530 +  c = b + 0x10;
   1.531 +  d = c + 0x100;
   1.532 +  EXPECT_EQ(0xa131200190546ac2ULL - 0x111, a.Value());
   1.533 +  EXPECT_EQ(0xa131200190546ac2ULL - 0x110, b.Value());
   1.534 +  EXPECT_EQ(0xa131200190546ac2ULL - 0x100, c.Value());
   1.535 +  EXPECT_EQ(0xa131200190546ac2ULL - 0x000, d.Value());
   1.536 +}
   1.537 +
   1.538 +TEST(LabelChain, AssignEndValueBeforeBackward) {
   1.539 +  Label a, b, c, d;
   1.540 +  d = 0x8da17e1670ad4fa2ULL;
   1.541 +  d = c + 0x100;
   1.542 +  c = b + 0x10;
   1.543 +  b = a + 0x1;
   1.544 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x111, a.Value());
   1.545 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x110, b.Value());
   1.546 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x100, c.Value());
   1.547 +  EXPECT_EQ(0x8da17e1670ad4fa2ULL - 0x000, d.Value());
   1.548 +}
   1.549 +
   1.550 +TEST(LabelChain, AssignEndValueAfterForward) {
   1.551 +  Label a, b, c, d;
   1.552 +  b = a + 0x1;
   1.553 +  c = b + 0x10;
   1.554 +  d = c + 0x100;
   1.555 +  d = 0x99b8f51bafd41adaULL;
   1.556 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x111, a.Value());
   1.557 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x110, b.Value());
   1.558 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x100, c.Value());
   1.559 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x000, d.Value());
   1.560 +}
   1.561 +
   1.562 +TEST(LabelChain, AssignEndValueAfterBackward) {
   1.563 +  Label a, b, c, d;
   1.564 +  d = c + 0x100;
   1.565 +  c = b + 0x10;
   1.566 +  b = a + 0x1;
   1.567 +  d = 0xc86ca1d97ab5df6eULL;
   1.568 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x111, a.Value());
   1.569 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x110, b.Value());
   1.570 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x100, c.Value());
   1.571 +  EXPECT_EQ(0xc86ca1d97ab5df6eULL - 0x000, d.Value());
   1.572 +}
   1.573 +
   1.574 +TEST(LabelChain, ConstructStartRelationBeforeForward) {
   1.575 +  Label x;
   1.576 +  Label a(x);
   1.577 +  Label b(a + 0x1);
   1.578 +  Label c(b + 0x10);
   1.579 +  Label d(c + 0x100);
   1.580 +  EXPECT_EQ(0x111U, d-x);
   1.581 +  EXPECT_EQ(0x11U,  c-x);
   1.582 +  EXPECT_EQ(0x1U,   b-x);
   1.583 +  EXPECT_EQ(0U,     a-x);
   1.584 +}
   1.585 +
   1.586 +TEST(LabelChain, ConstructStartRelationAfterForward) {
   1.587 +  Label x;
   1.588 +  Label a;
   1.589 +  Label b(a + 0x1);
   1.590 +  Label c(b + 0x10);
   1.591 +  Label d(c + 0x100);
   1.592 +  a = x;
   1.593 +  EXPECT_EQ(0x111U, d-x);
   1.594 +  EXPECT_EQ(0x11U,  c-x);
   1.595 +  EXPECT_EQ(0x1U,   b-x);
   1.596 +  EXPECT_EQ(0U,     a-x);
   1.597 +}
   1.598 +
   1.599 +TEST(LabelChain, ConstructStartValueBeforeForward) {
   1.600 +  Label a(0x5d234d177d01ccc8ULL);
   1.601 +  Label b(a + 0x1);
   1.602 +  Label c(b + 0x10);
   1.603 +  Label d(c + 0x100);
   1.604 +  EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x111U, d.Value());
   1.605 +  EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x011U, c.Value());
   1.606 +  EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x001U, b.Value());
   1.607 +  EXPECT_EQ(0x5d234d177d01ccc8ULL + 0x000U, a.Value());
   1.608 +}
   1.609 +
   1.610 +TEST(LabelChain, ConstructStartValueAfterForward) {
   1.611 +  Label a;
   1.612 +  Label b(a + 0x1);
   1.613 +  Label c(b + 0x10);
   1.614 +  Label d(c + 0x100);
   1.615 +  a = 0xded85d54586e84fcULL;
   1.616 +  EXPECT_EQ(0xded85d54586e84fcULL + 0x111U, d.Value());
   1.617 +  EXPECT_EQ(0xded85d54586e84fcULL + 0x011U, c.Value());
   1.618 +  EXPECT_EQ(0xded85d54586e84fcULL + 0x001U, b.Value());
   1.619 +  EXPECT_EQ(0xded85d54586e84fcULL + 0x000U, a.Value());
   1.620 +}
   1.621 +
   1.622 +TEST(LabelChain, ConstructEndRelationAfterForward) {
   1.623 +  Label x;
   1.624 +  Label a;
   1.625 +  Label b(a + 0x1);
   1.626 +  Label c(b + 0x10);
   1.627 +  Label d(c + 0x100);
   1.628 +  x = d;
   1.629 +  EXPECT_EQ(-(uint64_t)0x111U, a-x);
   1.630 +  EXPECT_EQ(-(uint64_t)0x110U, b-x);
   1.631 +  EXPECT_EQ(-(uint64_t)0x100U, c-x);
   1.632 +  EXPECT_EQ(-(uint64_t)0x000U, d-x);
   1.633 +}
   1.634 +
   1.635 +TEST(LabelChain, ConstructEndValueAfterForward) {
   1.636 +  Label a;
   1.637 +  Label b(a + 0x1);
   1.638 +  Label c(b + 0x10);
   1.639 +  Label d(c + 0x100);
   1.640 +  d = 0x99b8f51bafd41adaULL;
   1.641 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x111, a.Value());
   1.642 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x110, b.Value());
   1.643 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x100, c.Value());
   1.644 +  EXPECT_EQ(0x99b8f51bafd41adaULL - 0x000, d.Value());
   1.645 +}
   1.646 +
   1.647 +TEST(LabelTree, KnownValue) {
   1.648 +  Label l, m, n, o, p;
   1.649 +  l = m;
   1.650 +  m = n;
   1.651 +  o = p;
   1.652 +  p = n;
   1.653 +  l = 0x536b5de3d468a1b5ULL;
   1.654 +  EXPECT_EQ(0x536b5de3d468a1b5ULL, o.Value());
   1.655 +}
   1.656 +
   1.657 +TEST(LabelTree, Related) {
   1.658 +  Label l, m, n, o, p;
   1.659 +  l = m - 1;
   1.660 +  m = n - 10;
   1.661 +  o = p + 100;
   1.662 +  p = n + 1000;
   1.663 +  EXPECT_EQ(1111U, o - l);
   1.664 +}
   1.665 +
   1.666 +TEST(EquationDeathTest, EqualConstants) {
   1.667 +  Label m = 0x0d3962f280f07d24ULL;
   1.668 +  Label n = 0x0d3962f280f07d24ULL;
   1.669 +  m = n; // no death expected
   1.670 +}
   1.671 +
   1.672 +TEST(EquationDeathTest, EqualIndirectConstants) {
   1.673 +  Label m = 0xa347f1e5238fe6a1ULL;
   1.674 +  Label n;
   1.675 +  Label o = n;
   1.676 +  n = 0xa347f1e5238fe6a1ULL;
   1.677 +  n = m; // no death expected
   1.678 +}
   1.679 +
   1.680 +TEST(EquationDeathTest, ConstantClash) {
   1.681 +  Label m = 0xd4cc0f4f630ec741ULL;
   1.682 +  Label n = 0x934cd2d8254fc3eaULL;
   1.683 +  ASSERT_DEATH(m = n, "addend_ == addend");
   1.684 +}
   1.685 +
   1.686 +TEST(EquationDeathTest, IndirectConstantClash) {
   1.687 +  Label m = 0xd4cc0f4f630ec741ULL;
   1.688 +  Label n, o;
   1.689 +  n = o;
   1.690 +  o = 0xcfbe3b83ac49ce86ULL;
   1.691 +  ASSERT_DEATH(m = n, "addend_ == addend");
   1.692 +}
   1.693 +
   1.694 +// Assigning to a related label may free the next Binding on its
   1.695 +// chain. This test always passes; it is interesting to memory
   1.696 +// checkers and coverage analysis.
   1.697 +TEST(LabelReferenceCount, AssignmentFree) {
   1.698 +  Label l;
   1.699 +  {
   1.700 +    Label m;
   1.701 +    l = m;
   1.702 +  }
   1.703 +  // This should free m's Binding.
   1.704 +  l = 0xca8bae92f0376d4fULL;
   1.705 +  ASSERT_EQ(0xca8bae92f0376d4fULL, l.Value());
   1.706 +}
   1.707 +
   1.708 +// Finding the value of a label may free the Binding it refers to. This test
   1.709 +// always passes; it is interesting to memory checkers and coverage analysis.
   1.710 +TEST(LabelReferenceCount, FindValueFree) {
   1.711 +  Label l;
   1.712 +  {
   1.713 +    Label m, n;
   1.714 +    l = m;
   1.715 +    m = n;
   1.716 +    n = 0x7a0b0c576672daafULL;
   1.717 +    // At this point, l's Binding refers to m's Binding, which refers
   1.718 +    // to n's binding.
   1.719 +  }
   1.720 +  // Now, l is the only reference keeping the three Bindings alive.
   1.721 +  // Resolving its value should free l's and m's original bindings.
   1.722 +  ASSERT_EQ(0x7a0b0c576672daafULL, l.Value());
   1.723 +}
   1.724 +
   1.725 +TEST(ConstructSection, Simple) {
   1.726 +  Section s;
   1.727 +}
   1.728 +
   1.729 +TEST(ConstructSection, WithEndian) {
   1.730 +  Section s(kBigEndian);
   1.731 +}
   1.732 +
   1.733 +// A fixture class for TestAssembler::Section tests.
   1.734 +class SectionFixture {
   1.735 + public:
   1.736 +  Section section;
   1.737 +  string contents;
   1.738 +  static const uint8_t data[];
   1.739 +  static const size_t data_size;
   1.740 +};
   1.741 +
   1.742 +const uint8_t SectionFixture::data[] = {
   1.743 +  0x87, 0x4f, 0x43, 0x67, 0x30, 0xd0, 0xd4, 0x0e
   1.744 +};
   1.745 +
   1.746 +#define I0()
   1.747 +#define I1(a) { a }
   1.748 +#define I2(a,b) { a,b }
   1.749 +#define I3(a,b,c) { a,b,c }
   1.750 +#define I4(a,b,c,d) { a,b,c,d }
   1.751 +#define I5(a,b,c,d,e) { a,b,c,d,e }
   1.752 +#define I6(a,b,c,d,e,f) { a,b,c,d,e,f }
   1.753 +#define I7(a,b,c,d,e,f,g) { a,b,c,d,e,f,g }
   1.754 +#define I8(a,b,c,d,e,f,g,h) { a,b,c,d,e,f,g,h }
   1.755 +#define I9(a,b,c,d,e,f,g,h,i) { a,b,c,d,e,f,g,h,i }
   1.756 +#define ASSERT_BYTES(s, b)                                              \
   1.757 +  do                                                                    \
   1.758 +    {                                                                   \
   1.759 +      static const uint8_t expected_bytes[] = b;                       \
   1.760 +      ASSERT_EQ(sizeof(expected_bytes), s.size());                      \
   1.761 +      ASSERT_TRUE(memcmp(s.data(), (const char *) expected_bytes,       \
   1.762 +                         sizeof(expected_bytes)) == 0);                 \
   1.763 +    }                                                                   \
   1.764 +  while(0)
   1.765 +
   1.766 +class Append: public SectionFixture, public Test { };
   1.767 +
   1.768 +TEST_F(Append, Bytes) {
   1.769 +  section.Append(data, sizeof(data));
   1.770 +  ASSERT_TRUE(section.GetContents(&contents));
   1.771 +  ASSERT_EQ(sizeof(data), contents.size());
   1.772 +  EXPECT_TRUE(0 == memcmp(contents.data(), (const char *) data, sizeof(data)));
   1.773 +}
   1.774 +
   1.775 +TEST_F(Append, BytesTwice) {
   1.776 +  section.Append(data, sizeof(data));
   1.777 +  section.Append(data, sizeof(data));
   1.778 +  ASSERT_TRUE(section.GetContents(&contents));
   1.779 +  ASSERT_EQ(2 * sizeof(data), contents.size());
   1.780 +  ASSERT_TRUE(0 == memcmp(contents.data(), (const char *) data, sizeof(data)));
   1.781 +  ASSERT_TRUE(0 == memcmp(contents.data() + sizeof(data),
   1.782 +                          (const char *) data, sizeof(data)));
   1.783 +}
   1.784 +
   1.785 +TEST_F(Append, String) {
   1.786 +  string s1 = "howdy ";
   1.787 +  string s2 = "there";
   1.788 +  section.Append(s1);
   1.789 +  section.Append(s2);
   1.790 +  ASSERT_TRUE(section.GetContents(&contents));
   1.791 +  ASSERT_STREQ(contents.c_str(), "howdy there");
   1.792 +}
   1.793 +
   1.794 +TEST_F(Append, CString) {
   1.795 +  section.AppendCString("howdy");
   1.796 +  section.AppendCString("");
   1.797 +  section.AppendCString("there");
   1.798 +  ASSERT_TRUE(section.GetContents(&contents));
   1.799 +  ASSERT_EQ(string("howdy\0\0there\0", 13), contents);
   1.800 +}
   1.801 +
   1.802 +TEST_F(Append, CStringSize) {
   1.803 +  section.AppendCString("howdy", 3);
   1.804 +  section.AppendCString("there", 5);
   1.805 +  section.AppendCString("fred", 6);
   1.806 +  section.AppendCString("natalie", 0);
   1.807 +  section.AppendCString("", 10);
   1.808 +  ASSERT_TRUE(section.GetContents(&contents));
   1.809 +  ASSERT_EQ(string("howtherefred\0\0\0\0\0\0\0\0\0\0\0\0", 24), contents);
   1.810 +}
   1.811 +
   1.812 +TEST_F(Append, RepeatedBytes) {
   1.813 +  section.Append((size_t) 10, '*');
   1.814 +  ASSERT_TRUE(section.GetContents(&contents));
   1.815 +  ASSERT_STREQ(contents.c_str(), "**********");
   1.816 +}
   1.817 +
   1.818 +TEST_F(Append, GeneralLE1) {
   1.819 +  section.Append(kLittleEndian, 1, 42);
   1.820 +  ASSERT_TRUE(section.GetContents(&contents));
   1.821 +  ASSERT_BYTES(contents, I1(42));
   1.822 +}
   1.823 +
   1.824 +TEST_F(Append, GeneralLE2) {
   1.825 +  section.Append(kLittleEndian, 2, 0x15a1);
   1.826 +  ASSERT_TRUE(section.GetContents(&contents));
   1.827 +  ASSERT_BYTES(contents, I2(0xa1, 0x15));
   1.828 +}
   1.829 +
   1.830 +TEST_F(Append, GeneralLE3) {
   1.831 +  section.Append(kLittleEndian, 3, 0x59ae8d);
   1.832 +  ASSERT_TRUE(section.GetContents(&contents));
   1.833 +  ASSERT_BYTES(contents, I3(0x8d, 0xae, 0x59));
   1.834 +}
   1.835 +
   1.836 +TEST_F(Append, GeneralLE4) {
   1.837 +  section.Append(kLittleEndian, 4, 0x51603c56);
   1.838 +  ASSERT_TRUE(section.GetContents(&contents));
   1.839 +  ASSERT_BYTES(contents, I4(0x56, 0x3c, 0x60, 0x51));
   1.840 +}
   1.841 +
   1.842 +TEST_F(Append, GeneralLE5) {
   1.843 +  section.Append(kLittleEndian, 5, 0x385e2803b4ULL);
   1.844 +  ASSERT_TRUE(section.GetContents(&contents));
   1.845 +  ASSERT_BYTES(contents, I5(0xb4, 0x03, 0x28, 0x5e, 0x38));
   1.846 +}
   1.847 +
   1.848 +TEST_F(Append, GeneralLE6) {
   1.849 +  section.Append(kLittleEndian, 6, 0xc7db9534dd1fULL);
   1.850 +  ASSERT_TRUE(section.GetContents(&contents));
   1.851 +  ASSERT_BYTES(contents, I6(0x1f, 0xdd, 0x34, 0x95, 0xdb, 0xc7));
   1.852 +}
   1.853 +
   1.854 +TEST_F(Append, GeneralLE7) {
   1.855 +  section.Append(kLittleEndian, 7, 0x1445c9f1b843e6ULL);
   1.856 +  ASSERT_TRUE(section.GetContents(&contents));
   1.857 +  ASSERT_BYTES(contents, I7(0xe6, 0x43, 0xb8, 0xf1, 0xc9, 0x45, 0x14));
   1.858 +}
   1.859 +
   1.860 +TEST_F(Append, GeneralLE8) {
   1.861 +  section.Append(kLittleEndian, 8, 0xaf48019dfe5c01e5ULL);
   1.862 +  ASSERT_TRUE(section.GetContents(&contents));
   1.863 +  ASSERT_BYTES(contents, I8(0xe5, 0x01, 0x5c, 0xfe, 0x9d, 0x01, 0x48, 0xaf));
   1.864 +}
   1.865 +
   1.866 +TEST_F(Append, GeneralBE1) {
   1.867 +  section.Append(kBigEndian, 1, 0xd0ULL);
   1.868 +  ASSERT_TRUE(section.GetContents(&contents));
   1.869 +  ASSERT_BYTES(contents, I1(0xd0));
   1.870 +}
   1.871 +
   1.872 +TEST_F(Append, GeneralBE2) {
   1.873 +  section.Append(kBigEndian, 2, 0x2e7eULL);
   1.874 +  ASSERT_TRUE(section.GetContents(&contents));
   1.875 +  ASSERT_BYTES(contents, I2(0x2e, 0x7e));
   1.876 +}
   1.877 +
   1.878 +TEST_F(Append, GeneralBE3) {
   1.879 +  section.Append(kBigEndian, 3, 0x37dad6ULL);
   1.880 +  ASSERT_TRUE(section.GetContents(&contents));
   1.881 +  ASSERT_BYTES(contents, I3(0x37, 0xda, 0xd6));
   1.882 +}
   1.883 +
   1.884 +TEST_F(Append, GeneralBE4) {
   1.885 +  section.Append(kBigEndian, 4, 0x715935c7ULL);
   1.886 +  ASSERT_TRUE(section.GetContents(&contents));
   1.887 +  ASSERT_BYTES(contents, I4(0x71, 0x59, 0x35, 0xc7));
   1.888 +}
   1.889 +
   1.890 +TEST_F(Append, GeneralBE5) {
   1.891 +  section.Append(kBigEndian, 5, 0x42baeb02b7ULL);
   1.892 +  ASSERT_TRUE(section.GetContents(&contents));
   1.893 +  ASSERT_BYTES(contents, I5(0x42, 0xba, 0xeb, 0x02, 0xb7));
   1.894 +}
   1.895 +
   1.896 +TEST_F(Append, GeneralBE6) {
   1.897 +  section.Append(kBigEndian, 6, 0xf1cdf10e7b18ULL);
   1.898 +  ASSERT_TRUE(section.GetContents(&contents));
   1.899 +  ASSERT_BYTES(contents, I6(0xf1, 0xcd, 0xf1, 0x0e, 0x7b, 0x18));
   1.900 +}
   1.901 +
   1.902 +TEST_F(Append, GeneralBE7) {
   1.903 +  section.Append(kBigEndian, 7, 0xf50a724f0b0d20ULL);
   1.904 +  ASSERT_TRUE(section.GetContents(&contents));
   1.905 +  ASSERT_BYTES(contents, I7(0xf5, 0x0a, 0x72, 0x4f, 0x0b, 0x0d, 0x20));
   1.906 +}
   1.907 +
   1.908 +TEST_F(Append, GeneralBE8) {
   1.909 +  section.Append(kBigEndian, 8, 0xa6b2cb5e98dc9c16ULL);
   1.910 +  ASSERT_TRUE(section.GetContents(&contents));
   1.911 +  ASSERT_BYTES(contents, I8(0xa6, 0xb2, 0xcb, 0x5e, 0x98, 0xdc, 0x9c, 0x16));
   1.912 +}
   1.913 +
   1.914 +TEST_F(Append, GeneralLE1Label) {
   1.915 +  Label l;
   1.916 +  section.Append(kLittleEndian, 1, l);
   1.917 +  l = 42;
   1.918 +  ASSERT_TRUE(section.GetContents(&contents));
   1.919 +  ASSERT_BYTES(contents, I1(42));
   1.920 +}
   1.921 +
   1.922 +TEST_F(Append, GeneralLE2Label) {
   1.923 +  Label l;
   1.924 +  section.Append(kLittleEndian, 2, l);
   1.925 +  l = 0x15a1;
   1.926 +  ASSERT_TRUE(section.GetContents(&contents));
   1.927 +  ASSERT_BYTES(contents, I2(0xa1, 0x15));
   1.928 +}
   1.929 +
   1.930 +TEST_F(Append, GeneralLE3Label) {
   1.931 +  Label l;
   1.932 +  section.Append(kLittleEndian, 3, l);
   1.933 +  l = 0x59ae8d;
   1.934 +  ASSERT_TRUE(section.GetContents(&contents));
   1.935 +  ASSERT_BYTES(contents, I3(0x8d, 0xae, 0x59));
   1.936 +}
   1.937 +
   1.938 +TEST_F(Append, GeneralLE4Label) {
   1.939 +  Label l;
   1.940 +  section.Append(kLittleEndian, 4, l);
   1.941 +  l = 0x51603c56;
   1.942 +  ASSERT_TRUE(section.GetContents(&contents));
   1.943 +  ASSERT_BYTES(contents, I4(0x56, 0x3c, 0x60, 0x51));
   1.944 +}
   1.945 +
   1.946 +TEST_F(Append, GeneralLE5Label) {
   1.947 +  Label l;
   1.948 +  section.Append(kLittleEndian, 5, l);
   1.949 +  l = 0x385e2803b4ULL;
   1.950 +  ASSERT_TRUE(section.GetContents(&contents));
   1.951 +  ASSERT_BYTES(contents, I5(0xb4, 0x03, 0x28, 0x5e, 0x38));
   1.952 +}
   1.953 +
   1.954 +TEST_F(Append, GeneralLE6Label) {
   1.955 +  Label l;
   1.956 +  section.Append(kLittleEndian, 6, l);
   1.957 +  l = 0xc7db9534dd1fULL;
   1.958 +  ASSERT_TRUE(section.GetContents(&contents));
   1.959 +  ASSERT_BYTES(contents, I6(0x1f, 0xdd, 0x34, 0x95, 0xdb, 0xc7));
   1.960 +}
   1.961 +
   1.962 +TEST_F(Append, GeneralLE7Label) {
   1.963 +  Label l;
   1.964 +  section.Append(kLittleEndian, 7, l);
   1.965 +  l = 0x1445c9f1b843e6ULL;
   1.966 +  ASSERT_TRUE(section.GetContents(&contents));
   1.967 +  ASSERT_BYTES(contents, I7(0xe6, 0x43, 0xb8, 0xf1, 0xc9, 0x45, 0x14));
   1.968 +}
   1.969 +
   1.970 +TEST_F(Append, GeneralLE8Label) {
   1.971 +  Label l;
   1.972 +  section.Append(kLittleEndian, 8, l);
   1.973 +  l = 0xaf48019dfe5c01e5ULL;
   1.974 +  ASSERT_TRUE(section.GetContents(&contents));
   1.975 +  ASSERT_BYTES(contents, I8(0xe5, 0x01, 0x5c, 0xfe, 0x9d, 0x01, 0x48, 0xaf));
   1.976 +}
   1.977 +
   1.978 +TEST_F(Append, GeneralBE1Label) {
   1.979 +  Label l;
   1.980 +  section.Append(kBigEndian, 1, l);
   1.981 +  l = 0xd0ULL;
   1.982 +  ASSERT_TRUE(section.GetContents(&contents));
   1.983 +  ASSERT_BYTES(contents, I1(0xd0));
   1.984 +}
   1.985 +
   1.986 +TEST_F(Append, GeneralBE2Label) {
   1.987 +  Label l;
   1.988 +  section.Append(kBigEndian, 2, l);
   1.989 +  l = 0x2e7eULL;
   1.990 +  ASSERT_TRUE(section.GetContents(&contents));
   1.991 +  ASSERT_BYTES(contents, I2(0x2e, 0x7e));
   1.992 +}
   1.993 +
   1.994 +TEST_F(Append, GeneralBE3Label) {
   1.995 +  Label l;
   1.996 +  section.Append(kBigEndian, 3, l);
   1.997 +  l = 0x37dad6ULL;
   1.998 +  ASSERT_TRUE(section.GetContents(&contents));
   1.999 +  ASSERT_BYTES(contents, I3(0x37, 0xda, 0xd6));
  1.1000 +}
  1.1001 +
  1.1002 +TEST_F(Append, GeneralBE4Label) {
  1.1003 +  Label l;
  1.1004 +  section.Append(kBigEndian, 4, l);
  1.1005 +  l = 0x715935c7ULL;
  1.1006 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1007 +  ASSERT_BYTES(contents, I4(0x71, 0x59, 0x35, 0xc7));
  1.1008 +}
  1.1009 +
  1.1010 +TEST_F(Append, GeneralBE5Label) {
  1.1011 +  Label l;
  1.1012 +  section.Append(kBigEndian, 5, l);
  1.1013 +  l = 0x42baeb02b7ULL;
  1.1014 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1015 +  ASSERT_BYTES(contents, I5(0x42, 0xba, 0xeb, 0x02, 0xb7));
  1.1016 +}
  1.1017 +
  1.1018 +TEST_F(Append, GeneralBE6Label) {
  1.1019 +  Label l;
  1.1020 +  section.Append(kBigEndian, 6, l);
  1.1021 +  l = 0xf1cdf10e7b18ULL;
  1.1022 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1023 +  ASSERT_BYTES(contents, I6(0xf1, 0xcd, 0xf1, 0x0e, 0x7b, 0x18));
  1.1024 +}
  1.1025 +
  1.1026 +TEST_F(Append, GeneralBE7Label) {
  1.1027 +  Label l;
  1.1028 +  section.Append(kBigEndian, 7, l);
  1.1029 +  l = 0xf50a724f0b0d20ULL;
  1.1030 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1031 +  ASSERT_BYTES(contents, I7(0xf5, 0x0a, 0x72, 0x4f, 0x0b, 0x0d, 0x20));
  1.1032 +}
  1.1033 +
  1.1034 +TEST_F(Append, GeneralBE8Label) {
  1.1035 +  Label l;
  1.1036 +  section.Append(kBigEndian, 8, l);
  1.1037 +  l = 0xa6b2cb5e98dc9c16ULL;
  1.1038 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1039 +  ASSERT_BYTES(contents, I8(0xa6, 0xb2, 0xcb, 0x5e, 0x98, 0xdc, 0x9c, 0x16));
  1.1040 +}
  1.1041 +
  1.1042 +TEST_F(Append, B8) {
  1.1043 +  section.Append(1, 0x2a);
  1.1044 +  section.B8(0xd3U);
  1.1045 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1046 +  ASSERT_BYTES(contents, I2(0x2a, 0xd3));
  1.1047 +}
  1.1048 +
  1.1049 +TEST_F(Append, B8Label) {
  1.1050 +  Label l;
  1.1051 +  section.Append(1, 0x2a);
  1.1052 +  section.B8(l);
  1.1053 +  l = 0x4bU;
  1.1054 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1055 +  ASSERT_BYTES(contents, I2(0x2a, 0x4b));
  1.1056 +}
  1.1057 +
  1.1058 +TEST_F(Append, B16) {
  1.1059 +  section.Append(1, 0x2a);
  1.1060 +  section.B16(0x472aU);
  1.1061 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1062 +  ASSERT_BYTES(contents, I3(0x2a, 0x47, 0x2a));
  1.1063 +}
  1.1064 +
  1.1065 +TEST_F(Append, B16Label) {
  1.1066 +  Label l;
  1.1067 +  section.Append(1, 0x2a);
  1.1068 +  section.B16(l);
  1.1069 +  l = 0x55e8U;
  1.1070 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1071 +  ASSERT_BYTES(contents, I3(0x2a, 0x55, 0xe8));
  1.1072 +}
  1.1073 +
  1.1074 +TEST_F(Append, B32) {
  1.1075 +  section.Append(1, 0x2a);
  1.1076 +  section.B32(0xbd412cbcU);
  1.1077 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1078 +  ASSERT_BYTES(contents, I5(0x2a, 0xbd, 0x41, 0x2c, 0xbc));
  1.1079 +}
  1.1080 +
  1.1081 +TEST_F(Append, B32Label) {
  1.1082 +  Label l;
  1.1083 +  section.Append(1, 0x2a);
  1.1084 +  section.B32(l);
  1.1085 +  l = 0x208e37d5U;
  1.1086 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1087 +  ASSERT_BYTES(contents, I5(0x2a, 0x20, 0x8e, 0x37, 0xd5));
  1.1088 +}
  1.1089 +
  1.1090 +TEST_F(Append, B64) {
  1.1091 +  section.Append(1, 0x2a);
  1.1092 +  section.B64(0x3402a013111e68adULL);
  1.1093 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1094 +  ASSERT_BYTES(contents, 
  1.1095 +               I9(0x2a, 0x34, 0x02, 0xa0, 0x13, 0x11, 0x1e, 0x68, 0xad));
  1.1096 +}
  1.1097 +
  1.1098 +TEST_F(Append, B64Label) {
  1.1099 +  Label l;
  1.1100 +  section.Append(1, 0x2a);
  1.1101 +  section.B64(l);
  1.1102 +  l = 0x355dbfbb4ac6d57fULL;
  1.1103 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1104 +  ASSERT_BYTES(contents, 
  1.1105 +               I9(0x2a, 0x35, 0x5d, 0xbf, 0xbb, 0x4a, 0xc6, 0xd5, 0x7f));
  1.1106 +}
  1.1107 +
  1.1108 +TEST_F(Append, L8) {
  1.1109 +  section.Append(1, 0x2a);
  1.1110 +  section.L8(0x26U);
  1.1111 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1112 +  ASSERT_BYTES(contents, I2(0x2a, 0x26));
  1.1113 +}
  1.1114 +
  1.1115 +TEST_F(Append, L8Label) {
  1.1116 +  Label l;
  1.1117 +  section.Append(1, 0x2a);
  1.1118 +  section.L8(l);
  1.1119 +  l = 0xa8U;
  1.1120 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1121 +  ASSERT_BYTES(contents, I2(0x2a, 0xa8));
  1.1122 +}
  1.1123 +
  1.1124 +TEST_F(Append, L16) {
  1.1125 +  section.Append(1, 0x2a);
  1.1126 +  section.L16(0xca6dU);
  1.1127 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1128 +  ASSERT_BYTES(contents, I3(0x2a, 0x6d, 0xca));
  1.1129 +}
  1.1130 +
  1.1131 +TEST_F(Append, L16Label) {
  1.1132 +  Label l;
  1.1133 +  section.Append(1, 0x2a);
  1.1134 +  section.L16(l);
  1.1135 +  l = 0xd21fU;
  1.1136 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1137 +  ASSERT_BYTES(contents, I3(0x2a, 0x1f, 0xd2));
  1.1138 +}
  1.1139 +
  1.1140 +TEST_F(Append, L32) {
  1.1141 +  section.Append(1, 0x2a);
  1.1142 +  section.L32(0x558f6181U);
  1.1143 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1144 +  ASSERT_BYTES(contents, I5(0x2a, 0x81, 0x61, 0x8f, 0x55));
  1.1145 +}
  1.1146 +
  1.1147 +TEST_F(Append, L32Label) {
  1.1148 +  Label l;
  1.1149 +  section.Append(1, 0x2a);
  1.1150 +  section.L32(l);
  1.1151 +  l = 0x4b810f82U;
  1.1152 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1153 +  ASSERT_BYTES(contents, I5(0x2a, 0x82, 0x0f, 0x81, 0x4b));
  1.1154 +}
  1.1155 +
  1.1156 +TEST_F(Append, L64) {
  1.1157 +  section.Append(1, 0x2a);
  1.1158 +  section.L64(0x564384f7579515bfULL);
  1.1159 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1160 +  ASSERT_BYTES(contents, 
  1.1161 +               I9(0x2a, 0xbf, 0x15, 0x95, 0x57, 0xf7, 0x84, 0x43, 0x56));
  1.1162 +}
  1.1163 +
  1.1164 +TEST_F(Append, L64Label) {
  1.1165 +  Label l;
  1.1166 +  section.Append(1, 0x2a);
  1.1167 +  section.L64(l);
  1.1168 +  l = 0x424b1d020667c8dbULL;
  1.1169 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1170 +  ASSERT_BYTES(contents, 
  1.1171 +               I9(0x2a, 0xdb, 0xc8, 0x67, 0x06, 0x02, 0x1d, 0x4b, 0x42));
  1.1172 +}
  1.1173 +
  1.1174 +TEST_F(Append, D8Big) {
  1.1175 +  section.set_endianness(kBigEndian);
  1.1176 +  section.Append(1, 0x2a);
  1.1177 +  section.D8(0xe6U);
  1.1178 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1179 +  ASSERT_BYTES(contents, I2(0x2a, 0xe6));
  1.1180 +}
  1.1181 +
  1.1182 +TEST_F(Append, D8BigLabel) {
  1.1183 +  Label l;
  1.1184 +  section.set_endianness(kBigEndian);
  1.1185 +  section.Append(1, 0x2a);
  1.1186 +  section.D8(l);
  1.1187 +  l = 0xeeU;
  1.1188 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1189 +  ASSERT_BYTES(contents, I2(0x2a, 0xee));
  1.1190 +}
  1.1191 +
  1.1192 +TEST_F(Append, D16Big) {
  1.1193 +  section.set_endianness(kBigEndian);
  1.1194 +  section.Append(1, 0x2a);
  1.1195 +  section.D16(0x83b1U);
  1.1196 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1197 +  ASSERT_BYTES(contents, I3(0x2a, 0x83, 0xb1));
  1.1198 +}
  1.1199 +
  1.1200 +TEST_F(Append, D16BigLabel) {
  1.1201 +  Label l;
  1.1202 +  section.set_endianness(kBigEndian);
  1.1203 +  section.Append(1, 0x2a);
  1.1204 +  section.D16(l);
  1.1205 +  l = 0x5b55U;
  1.1206 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1207 +  ASSERT_BYTES(contents, I3(0x2a, 0x5b, 0x55));
  1.1208 +}
  1.1209 +
  1.1210 +TEST_F(Append, D32Big) {
  1.1211 +  section.set_endianness(kBigEndian);
  1.1212 +  section.Append(1, 0x2a);
  1.1213 +  section.D32(0xd0b0e431U);
  1.1214 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1215 +  ASSERT_BYTES(contents, I5(0x2a, 0xd0, 0xb0, 0xe4, 0x31));
  1.1216 +}
  1.1217 +
  1.1218 +TEST_F(Append, D32BigLabel) {
  1.1219 +  Label l;
  1.1220 +  section.set_endianness(kBigEndian);
  1.1221 +  section.Append(1, 0x2a);
  1.1222 +  section.D32(l);
  1.1223 +  l = 0x312fb340U;
  1.1224 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1225 +  ASSERT_BYTES(contents, I5(0x2a, 0x31, 0x2f, 0xb3, 0x40));
  1.1226 +}
  1.1227 +
  1.1228 +TEST_F(Append, D64Big) {
  1.1229 +  section.set_endianness(kBigEndian);
  1.1230 +  section.Append(1, 0x2a);
  1.1231 +  section.D64(0xb109843500dbcb16ULL);
  1.1232 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1233 +  ASSERT_BYTES(contents, 
  1.1234 +               I9(0x2a, 0xb1, 0x09, 0x84, 0x35, 0x00, 0xdb, 0xcb, 0x16));
  1.1235 +}
  1.1236 +
  1.1237 +TEST_F(Append, D64BigLabel) {
  1.1238 +  Label l;
  1.1239 +  section.set_endianness(kBigEndian);
  1.1240 +  section.Append(1, 0x2a);
  1.1241 +  section.D64(l);
  1.1242 +  l = 0x9a0d61b70f671fd7ULL;
  1.1243 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1244 +  ASSERT_BYTES(contents, 
  1.1245 +               I9(0x2a, 0x9a, 0x0d, 0x61, 0xb7, 0x0f, 0x67, 0x1f, 0xd7));
  1.1246 +}
  1.1247 +
  1.1248 +TEST_F(Append, D8Little) {
  1.1249 +  section.set_endianness(kLittleEndian);
  1.1250 +  section.Append(1, 0x2a);
  1.1251 +  section.D8(0x42U);
  1.1252 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1253 +  ASSERT_BYTES(contents, I2(0x2a, 0x42));
  1.1254 +}
  1.1255 +
  1.1256 +TEST_F(Append, D8LittleLabel) {
  1.1257 +  Label l;
  1.1258 +  section.set_endianness(kLittleEndian);
  1.1259 +  section.Append(1, 0x2a);
  1.1260 +  section.D8(l);
  1.1261 +  l = 0x05U;
  1.1262 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1263 +  ASSERT_BYTES(contents, I2(0x2a, 0x05));
  1.1264 +}
  1.1265 +
  1.1266 +TEST_F(Append, D16Little) {
  1.1267 +  section.set_endianness(kLittleEndian);
  1.1268 +  section.Append(1, 0x2a);
  1.1269 +  section.D16(0xc5c5U);
  1.1270 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1271 +  ASSERT_BYTES(contents, I3(0x2a, 0xc5, 0xc5));
  1.1272 +}
  1.1273 +
  1.1274 +TEST_F(Append, D16LittleLabel) {
  1.1275 +  Label l;
  1.1276 +  section.set_endianness(kLittleEndian);
  1.1277 +  section.Append(1, 0x2a);
  1.1278 +  section.D16(l);
  1.1279 +  l = 0xb620U;
  1.1280 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1281 +  ASSERT_BYTES(contents, I3(0x2a, 0x20, 0xb6));
  1.1282 +}
  1.1283 +
  1.1284 +TEST_F(Append, D32Little) {
  1.1285 +  section.set_endianness(kLittleEndian);
  1.1286 +  section.Append(1, 0x2a);
  1.1287 +  section.D32(0x1a87d0feU);
  1.1288 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1289 +  ASSERT_BYTES(contents, I5(0x2a, 0xfe, 0xd0, 0x87, 0x1a));
  1.1290 +}
  1.1291 +
  1.1292 +TEST_F(Append, D32LittleLabel) {
  1.1293 +  Label l;
  1.1294 +  section.set_endianness(kLittleEndian);
  1.1295 +  section.Append(1, 0x2a);
  1.1296 +  section.D32(l);
  1.1297 +  l = 0xb8012d6bU;
  1.1298 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1299 +  ASSERT_BYTES(contents, I5(0x2a, 0x6b, 0x2d, 0x01, 0xb8));
  1.1300 +}
  1.1301 +
  1.1302 +TEST_F(Append, D64Little) {
  1.1303 +  section.set_endianness(kLittleEndian);
  1.1304 +  section.Append(1, 0x2a);
  1.1305 +  section.D64(0x42de75c61375a1deULL);
  1.1306 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1307 +  ASSERT_BYTES(contents, 
  1.1308 +               I9(0x2a, 0xde, 0xa1, 0x75, 0x13, 0xc6, 0x75, 0xde, 0x42));
  1.1309 +}
  1.1310 +
  1.1311 +TEST_F(Append, D64LittleLabel) {
  1.1312 +  Label l;
  1.1313 +  section.set_endianness(kLittleEndian);
  1.1314 +  section.Append(1, 0x2a);
  1.1315 +  section.D64(l);
  1.1316 +  l = 0x8b3bececf3fb5312ULL;
  1.1317 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1318 +  ASSERT_BYTES(contents, 
  1.1319 +               I9(0x2a, 0x12, 0x53, 0xfb, 0xf3, 0xec, 0xec, 0x3b, 0x8b));
  1.1320 +}
  1.1321 +
  1.1322 +TEST_F(Append, Variety) {
  1.1323 +  Label a, b, c, d, e, f, g, h;
  1.1324 +  section.Append(kBigEndian, 1, a)
  1.1325 +      .Append(kLittleEndian, 8, h)
  1.1326 +      .Append(kBigEndian, 1, 0x8bULL)
  1.1327 +      .Append(kLittleEndian, 8, 0x0ea56540448f4439ULL)
  1.1328 +      .Append(kBigEndian, 2, b)
  1.1329 +      .Append(kLittleEndian, 7, g)
  1.1330 +      .Append(kBigEndian, 2, 0xcf15ULL)
  1.1331 +      .Append(kLittleEndian, 7, 0x29694f04c5724aULL)
  1.1332 +      .Append(kBigEndian, 3, c)
  1.1333 +      .Append(kLittleEndian, 6, f)
  1.1334 +      .Append(kBigEndian, 3, 0x8c3ffdULL)
  1.1335 +      .Append(kLittleEndian, 6, 0x6f11ba80187aULL)
  1.1336 +      .Append(kBigEndian, 4, d)
  1.1337 +      .Append(kLittleEndian, 5, e)
  1.1338 +      .Append(kBigEndian, 4, 0x2fda2472ULL)
  1.1339 +      .Append(kLittleEndian, 5, 0x0aa02d423fULL)
  1.1340 +      .Append(kBigEndian, 5, e)
  1.1341 +      .Append(kLittleEndian, 4, d)
  1.1342 +      .Append(kBigEndian, 5, 0x53ba432138ULL)
  1.1343 +      .Append(kLittleEndian, 4, 0xf139ae60ULL)
  1.1344 +      .Append(kBigEndian, 6, f)
  1.1345 +      .Append(kLittleEndian, 3, c)
  1.1346 +      .Append(kBigEndian, 6, 0x168e436af716ULL)
  1.1347 +      .Append(kLittleEndian, 3, 0x3ef189ULL)
  1.1348 +      .Append(kBigEndian, 7, g)
  1.1349 +      .Append(kLittleEndian, 2, b)
  1.1350 +      .Append(kBigEndian, 7, 0xacd4ef233e47d9ULL)
  1.1351 +      .Append(kLittleEndian, 2, 0x5311ULL)
  1.1352 +      .Append(kBigEndian, 8, h)
  1.1353 +      .Append(kLittleEndian, 1, a)
  1.1354 +      .Append(kBigEndian, 8, 0x4668d5f1c93637a1ULL)
  1.1355 +      .Append(kLittleEndian, 1, 0x65ULL);
  1.1356 +  a = 0x79ac9bd8aa256b35ULL;
  1.1357 +  b = 0x22d13097ef86c91cULL;
  1.1358 +  c = 0xf204968b0a05862fULL;
  1.1359 +  d = 0x163177f15a0eb4ecULL;
  1.1360 +  e = 0xbd1b0f1d977f2246ULL;
  1.1361 +  f = 0x2b0842eee83c6461ULL;
  1.1362 +  g = 0x92f4b928a4bf875eULL;
  1.1363 +  h = 0x61a199a8f7286ba6ULL;
  1.1364 +  ASSERT_EQ(8 * 18U, section.Size());
  1.1365 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1366 +
  1.1367 +  static const uint8_t expected[] = {
  1.1368 +    0x35,    0xa6, 0x6b, 0x28, 0xf7, 0xa8, 0x99, 0xa1, 0x61,
  1.1369 +    0x8b,    0x39, 0x44, 0x8f, 0x44, 0x40, 0x65, 0xa5, 0x0e,
  1.1370 +    0xc9, 0x1c,    0x5e, 0x87, 0xbf, 0xa4, 0x28, 0xb9, 0xf4,
  1.1371 +    0xcf, 0x15,    0x4a, 0x72, 0xc5, 0x04, 0x4f, 0x69, 0x29,
  1.1372 +    0x05, 0x86, 0x2f,    0x61, 0x64, 0x3c, 0xe8, 0xee, 0x42,
  1.1373 +    0x8c, 0x3f, 0xfd,    0x7a, 0x18, 0x80, 0xba, 0x11, 0x6f,
  1.1374 +    0x5a, 0x0e, 0xb4, 0xec,    0x46, 0x22, 0x7f, 0x97, 0x1d,
  1.1375 +    0x2f, 0xda, 0x24, 0x72,    0x3f, 0x42, 0x2d, 0xa0, 0x0a,
  1.1376 +    0x1d, 0x97, 0x7f, 0x22, 0x46,    0xec, 0xb4, 0x0e, 0x5a,
  1.1377 +    0x53, 0xba, 0x43, 0x21, 0x38,    0x60, 0xae, 0x39, 0xf1,
  1.1378 +    0x42, 0xee, 0xe8, 0x3c, 0x64, 0x61,    0x2f, 0x86, 0x05,
  1.1379 +    0x16, 0x8e, 0x43, 0x6a, 0xf7, 0x16,    0x89, 0xf1, 0x3e,
  1.1380 +    0xf4, 0xb9, 0x28, 0xa4, 0xbf, 0x87, 0x5e,    0x1c, 0xc9,
  1.1381 +    0xac, 0xd4, 0xef, 0x23, 0x3e, 0x47, 0xd9,    0x11, 0x53,
  1.1382 +    0x61, 0xa1, 0x99, 0xa8, 0xf7, 0x28, 0x6b, 0xa6,    0x35,
  1.1383 +    0x46, 0x68, 0xd5, 0xf1, 0xc9, 0x36, 0x37, 0xa1,    0x65,
  1.1384 +  };
  1.1385 +
  1.1386 +  ASSERT_TRUE(0 == memcmp(contents.data(), expected, sizeof(expected)));
  1.1387 +}
  1.1388 +
  1.1389 +TEST_F(Append, Section) {
  1.1390 +  section.Append("murder");
  1.1391 +  {
  1.1392 +    Section middle;
  1.1393 +    middle.Append(" she");
  1.1394 +    section.Append(middle);
  1.1395 +  }
  1.1396 +  section.Append(" wrote");
  1.1397 +  EXPECT_EQ(16U, section.Size());
  1.1398 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1399 +  EXPECT_STREQ(contents.c_str(), "murder she wrote");
  1.1400 +}
  1.1401 +
  1.1402 +TEST_F(Append, SectionRefs) {
  1.1403 +  section.Append("sugar ");
  1.1404 +  Label l;
  1.1405 +  {
  1.1406 +    Section middle;
  1.1407 +    Label m;
  1.1408 +    middle.B32(m);
  1.1409 +    section.Append(middle);
  1.1410 +    m = 0x66726565;
  1.1411 +  }
  1.1412 +  section.Append(" jazz");
  1.1413 +  EXPECT_EQ(15U, section.Size());
  1.1414 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1415 +  EXPECT_STREQ(contents.c_str(), "sugar free jazz");
  1.1416 +}
  1.1417 +
  1.1418 +TEST_F(Append, LEB128_0) {
  1.1419 +  section.LEB128(0);
  1.1420 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1421 +  EXPECT_EQ(string("\0", 1), contents);
  1.1422 +}
  1.1423 +
  1.1424 +TEST_F(Append, LEB128_0x3f) {
  1.1425 +  section.LEB128(0x3f);
  1.1426 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1427 +  EXPECT_EQ(string("\x3f", 1), contents);
  1.1428 +}
  1.1429 +
  1.1430 +TEST_F(Append, LEB128_0x40) {
  1.1431 +  section.LEB128(0x40);
  1.1432 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1433 +  EXPECT_EQ(string("\xc0\x00", 2), contents);
  1.1434 +}
  1.1435 +
  1.1436 +TEST_F(Append, LEB128_0x7f) {
  1.1437 +  section.LEB128(0x7f);
  1.1438 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1439 +  EXPECT_EQ(string("\xff\x00", 2), contents);
  1.1440 +}
  1.1441 +
  1.1442 +TEST_F(Append, LEB128_0x80) {
  1.1443 +  section.LEB128(0x80);
  1.1444 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1445 +  EXPECT_EQ(string("\x80\x01", 2), contents);
  1.1446 +}
  1.1447 +
  1.1448 +TEST_F(Append, LEB128_0xff) {
  1.1449 +  section.LEB128(0xff);
  1.1450 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1451 +  EXPECT_EQ(string("\xff\x01", 2), contents);
  1.1452 +}
  1.1453 +
  1.1454 +TEST_F(Append, LEB128_0x1fff) {
  1.1455 +  section.LEB128(0x1fff);
  1.1456 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1457 +  EXPECT_EQ(string("\xff\x3f", 2), contents);
  1.1458 +}
  1.1459 +
  1.1460 +TEST_F(Append, LEB128_0x2000) {
  1.1461 +  section.LEB128(0x2000);
  1.1462 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1463 +  EXPECT_EQ(string("\x80\xc0\x00", 3), contents);
  1.1464 +}
  1.1465 +
  1.1466 +TEST_F(Append, LEB128_n1) {
  1.1467 +  section.LEB128(-1);
  1.1468 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1469 +  EXPECT_EQ(string("\x7f", 1), contents);
  1.1470 +}
  1.1471 +
  1.1472 +TEST_F(Append, LEB128_n0x40) {
  1.1473 +  section.LEB128(-0x40);
  1.1474 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1475 +  EXPECT_EQ(string("\x40", 1), contents);
  1.1476 +}
  1.1477 +
  1.1478 +TEST_F(Append, LEB128_n0x41) {
  1.1479 +  section.LEB128(-0x41);
  1.1480 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1481 +  EXPECT_EQ(string("\xbf\x7f", 2), contents);
  1.1482 +}
  1.1483 +
  1.1484 +TEST_F(Append, LEB128_n0x7f) {
  1.1485 +  section.LEB128(-0x7f);
  1.1486 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1487 +  EXPECT_EQ(string("\x81\x7f", 2), contents);
  1.1488 +}
  1.1489 +
  1.1490 +TEST_F(Append, LEB128_n0x80) {
  1.1491 +  section.LEB128(-0x80);
  1.1492 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1493 +  EXPECT_EQ(string("\x80\x7f", 2), contents);
  1.1494 +}
  1.1495 +
  1.1496 +TEST_F(Append, LEB128_n0x2000) {
  1.1497 +  section.LEB128(-0x2000);
  1.1498 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1499 +  EXPECT_EQ(string("\x80\x40", 2), contents);
  1.1500 +}
  1.1501 +
  1.1502 +TEST_F(Append, LEB128_n0x2001) {
  1.1503 +  section.LEB128(-0x2001);
  1.1504 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1505 +  EXPECT_EQ(string("\xff\xbf\x7f", 3), contents);
  1.1506 +}
  1.1507 +
  1.1508 +TEST_F(Append,ULEB128_0) {
  1.1509 +  section.ULEB128(0);
  1.1510 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1511 +  EXPECT_EQ(string("\0", 1), contents);
  1.1512 +}
  1.1513 +
  1.1514 +TEST_F(Append,ULEB128_1) {
  1.1515 +  section.ULEB128(1);
  1.1516 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1517 +  EXPECT_EQ(string("\x01", 1), contents);
  1.1518 +}
  1.1519 +
  1.1520 +TEST_F(Append,ULEB128_0x3f) {
  1.1521 +  section.ULEB128(0x3f);
  1.1522 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1523 +  EXPECT_EQ(string("\x3f", 1), contents);
  1.1524 +}
  1.1525 +
  1.1526 +TEST_F(Append,ULEB128_0x40) {
  1.1527 +  section.ULEB128(0x40);
  1.1528 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1529 +  EXPECT_EQ(string("\x40", 1), contents);
  1.1530 +}
  1.1531 +
  1.1532 +TEST_F(Append,ULEB128_0x7f) {
  1.1533 +  section.ULEB128(0x7f);
  1.1534 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1535 +  EXPECT_EQ(string("\x7f", 1), contents);
  1.1536 +}
  1.1537 +
  1.1538 +TEST_F(Append,ULEB128_0x80) {
  1.1539 +  section.ULEB128(0x80);
  1.1540 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1541 +  EXPECT_EQ(string("\x80\x01", 2), contents);
  1.1542 +}
  1.1543 +
  1.1544 +TEST_F(Append,ULEB128_0xff) {
  1.1545 +  section.ULEB128(0xff);
  1.1546 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1547 +  EXPECT_EQ(string("\xff\x01", 2), contents);
  1.1548 +}
  1.1549 +
  1.1550 +TEST_F(Append,ULEB128_0x100) {
  1.1551 +  section.ULEB128(0x100);
  1.1552 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1553 +  EXPECT_EQ(string("\x80\x02", 2), contents);
  1.1554 +}
  1.1555 +
  1.1556 +TEST_F(Append,ULEB128_0x1fff) {
  1.1557 +  section.ULEB128(0x1fff);
  1.1558 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1559 +  EXPECT_EQ(string("\xff\x3f", 2), contents);
  1.1560 +}
  1.1561 +
  1.1562 +TEST_F(Append,ULEB128_0x2000) {
  1.1563 +  section.ULEB128(0x2000);
  1.1564 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1565 +  EXPECT_EQ(string("\x80\x40", 2), contents);
  1.1566 +}
  1.1567 +
  1.1568 +TEST_F(Append,ULEB128_0x3fff) {
  1.1569 +  section.ULEB128(0x3fff);
  1.1570 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1571 +  EXPECT_EQ(string("\xff\x7f", 2), contents);
  1.1572 +}
  1.1573 +
  1.1574 +TEST_F(Append,ULEB128_0x4000) {
  1.1575 +  section.ULEB128(0x4000);
  1.1576 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1577 +  EXPECT_EQ(string("\x80\x80\x01", 3), contents);
  1.1578 +}
  1.1579 +
  1.1580 +TEST_F(Append,ULEB128_12857) {
  1.1581 +  section.ULEB128(12857);
  1.1582 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1583 +  EXPECT_EQ(string("\xb9\x64", 2), contents);
  1.1584 +}
  1.1585 +
  1.1586 +TEST_F(Append, LEBChain) {
  1.1587 +  section.LEB128(-0x80).ULEB128(12857).Append("*");
  1.1588 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1589 +  EXPECT_EQ(string("\x80\x7f\xb9\x64*", 5), contents);
  1.1590 +}
  1.1591 +
  1.1592 +
  1.1593 +class GetContents: public SectionFixture, public Test { };
  1.1594 +
  1.1595 +TEST_F(GetContents, Undefined) {
  1.1596 +  Label l;
  1.1597 +  section.Append(kLittleEndian, 8, l);
  1.1598 +  ASSERT_FALSE(section.GetContents(&contents));
  1.1599 +}
  1.1600 +
  1.1601 +TEST_F(GetContents, ClearsContents) {
  1.1602 +  section.Append((size_t) 10, '*');
  1.1603 +  EXPECT_EQ(10U, section.Size());
  1.1604 +  EXPECT_TRUE(section.GetContents(&contents));
  1.1605 +  EXPECT_EQ(0U, section.Size());
  1.1606 +}
  1.1607 +
  1.1608 +TEST_F(GetContents, ClearsReferences) {
  1.1609 +  Label l;
  1.1610 +  section.Append(kBigEndian, 1, l);
  1.1611 +  l = 42;
  1.1612 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1613 +  ASSERT_BYTES(contents, I1(42));
  1.1614 +  ASSERT_TRUE(section.GetContents(&contents)); // should not die
  1.1615 +}
  1.1616 +
  1.1617 +class Miscellanea: public SectionFixture, public Test { };
  1.1618 +
  1.1619 +TEST_F(Miscellanea, Clear) {
  1.1620 +  section.Append("howdy");
  1.1621 +  Label l;
  1.1622 +  section.L32(l);
  1.1623 +  EXPECT_EQ(9U, section.Size());
  1.1624 +  section.Clear();
  1.1625 +  EXPECT_EQ(0U, section.Size());
  1.1626 +  l = 0x8d231bf0U;
  1.1627 +  ASSERT_TRUE(section.GetContents(&contents)); // should not die
  1.1628 +}
  1.1629 +
  1.1630 +TEST_F(Miscellanea, Align) {
  1.1631 +  section.Append("*");
  1.1632 +  EXPECT_EQ(1U, section.Size());
  1.1633 +  section.Align(4).Append("*");
  1.1634 +  EXPECT_EQ(5U, section.Size());
  1.1635 +  section.Append("*").Align(2);
  1.1636 +  EXPECT_EQ(6U, section.Size());
  1.1637 +}
  1.1638 +
  1.1639 +TEST_F(Miscellanea, AlignPad) {
  1.1640 +  section.Append("*");
  1.1641 +  EXPECT_EQ(1U, section.Size());
  1.1642 +  section.Align(4, ' ').Append("*");
  1.1643 +  EXPECT_EQ(5U, section.Size());
  1.1644 +  section.Append("*").Align(2, ' ');
  1.1645 +  EXPECT_EQ(6U, section.Size());
  1.1646 +  ASSERT_TRUE(section.GetContents(&contents));
  1.1647 +  ASSERT_EQ(string("*   **"), contents);
  1.1648 +}
  1.1649 +
  1.1650 +TEST_F(Miscellanea, StartHereMark) {
  1.1651 +  Label m;
  1.1652 +  section.Append(42, ' ').Mark(&m).Append(13, '+');
  1.1653 +  EXPECT_EQ(42U, m - section.start());
  1.1654 +  EXPECT_EQ(42U + 13U, section.Here() - section.start());
  1.1655 +  EXPECT_FALSE(section.start().IsKnownConstant());
  1.1656 +  EXPECT_FALSE(m.IsKnownConstant());
  1.1657 +  EXPECT_FALSE(section.Here().IsKnownConstant());
  1.1658 +}
  1.1659 +
  1.1660 +TEST_F(Miscellanea, Endianness) {
  1.1661 +  section.set_endianness(kBigEndian);
  1.1662 +  EXPECT_EQ(kBigEndian, section.endianness());
  1.1663 +  section.set_endianness(kLittleEndian);
  1.1664 +  EXPECT_EQ(kLittleEndian, section.endianness());
  1.1665 +}

mercurial