source-position-table-unittest.cc 2.84 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/v8.h"

7
#include "src/objects.h"
8
#include "src/source-position-table.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "test/unittests/test-utils.h"

namespace v8 {
namespace internal {
namespace interpreter {

class SourcePositionTableTest : public TestWithIsolateAndZone {
 public:
  SourcePositionTableTest() {}
  ~SourcePositionTableTest() override {}
};

// Some random offsets, mostly at 'suspicious' bit boundaries.
static int offsets[] = {0,   1,   2,    3,    4,     30,      31,  32,
                        33,  62,  63,   64,   65,    126,     127, 128,
                        129, 250, 1000, 9999, 12000, 31415926};

TEST_F(SourcePositionTableTest, EncodeStatement) {
27
  SourcePositionTableBuilder builder(zone());
28
  for (int i = 0; i < arraysize(offsets); i++) {
29
    builder.AddPosition(offsets[i], offsets[i], true);
30 31 32 33
  }

  // To test correctness, we rely on the assertions in ToSourcePositionTable().
  // (Also below.)
34 35
  CHECK(!builder.ToSourcePositionTable(isolate(), Handle<AbstractCode>())
             .is_null());
36 37
}

38
TEST_F(SourcePositionTableTest, EncodeStatementDuplicates) {
39
  SourcePositionTableBuilder builder(zone());
40
  for (int i = 0; i < arraysize(offsets); i++) {
41 42
    builder.AddPosition(offsets[i], offsets[i], true);
    builder.AddPosition(offsets[i], offsets[i] + 1, true);
43 44 45 46
  }

  // To test correctness, we rely on the assertions in ToSourcePositionTable().
  // (Also below.)
47 48
  CHECK(!builder.ToSourcePositionTable(isolate(), Handle<AbstractCode>())
             .is_null());
49 50
}

51
TEST_F(SourcePositionTableTest, EncodeExpression) {
52
  SourcePositionTableBuilder builder(zone());
53
  for (int i = 0; i < arraysize(offsets); i++) {
54
    builder.AddPosition(offsets[i], offsets[i], false);
55
  }
56 57
  CHECK(!builder.ToSourcePositionTable(isolate(), Handle<AbstractCode>())
             .is_null());
58 59 60
}

TEST_F(SourcePositionTableTest, EncodeAscending) {
61
  SourcePositionTableBuilder builder(zone());
62

63 64
  int code_offset = 0;
  int source_position = 0;
65
  for (int i = 0; i < arraysize(offsets); i++) {
66 67
    code_offset += offsets[i];
    source_position += offsets[i];
68
    if (i % 2) {
69
      builder.AddPosition(code_offset, source_position, true);
70
    } else {
71
      builder.AddPosition(code_offset, source_position, false);
72 73 74
    }
  }

75
  // Also test negative offsets for source positions:
76
  for (int i = 0; i < arraysize(offsets); i++) {
77 78
    code_offset += offsets[i];
    source_position -= offsets[i];
79
    if (i % 2) {
80
      builder.AddPosition(code_offset, source_position, true);
81
    } else {
82
      builder.AddPosition(code_offset, source_position, false);
83 84 85
    }
  }

86 87
  CHECK(!builder.ToSourcePositionTable(isolate(), Handle<AbstractCode>())
             .is_null());
88 89 90 91 92
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8