12752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner// Copyright 2016 the V8 project authors. All rights reserved.
22752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner// Use of this source code is governed by a BSD-style license that can be
32752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner// found in the LICENSE file.
42752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner
52752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner#include "src/v8.h"
62752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner
7#include "src/interpreter/source-position-table.h"
8#include "test/unittests/test-utils.h"
9
10namespace v8 {
11namespace internal {
12namespace interpreter {
13
14class SourcePositionTableTest : public TestWithIsolateAndZone {
15 public:
16  SourcePositionTableTest() {}
17  ~SourcePositionTableTest() override {}
18};
19
20// Some random offsets, mostly at 'suspicious' bit boundaries.
21static int offsets[] = {0,   1,   2,    3,    4,     30,      31,  32,
22                        33,  62,  63,   64,   65,    126,     127, 128,
23                        129, 250, 1000, 9999, 12000, 31415926};
24
25TEST_F(SourcePositionTableTest, EncodeStatement) {
26  SourcePositionTableBuilder builder(isolate(), zone());
27  for (int i = 0; i < arraysize(offsets); i++) {
28    builder.AddPosition(offsets[i], offsets[i], true);
29  }
30
31  // To test correctness, we rely on the assertions in ToSourcePositionTable().
32  // (Also below.)
33  CHECK(!builder.ToSourcePositionTable().is_null());
34}
35
36TEST_F(SourcePositionTableTest, EncodeStatementDuplicates) {
37  SourcePositionTableBuilder builder(isolate(), zone());
38  for (int i = 0; i < arraysize(offsets); i++) {
39    builder.AddPosition(offsets[i], offsets[i], true);
40    builder.AddPosition(offsets[i], offsets[i] + 1, true);
41  }
42
43  // To test correctness, we rely on the assertions in ToSourcePositionTable().
44  // (Also below.)
45  CHECK(!builder.ToSourcePositionTable().is_null());
46}
47
48TEST_F(SourcePositionTableTest, EncodeExpression) {
49  SourcePositionTableBuilder builder(isolate(), zone());
50  for (int i = 0; i < arraysize(offsets); i++) {
51    builder.AddPosition(offsets[i], offsets[i], false);
52  }
53  CHECK(!builder.ToSourcePositionTable().is_null());
54}
55
56TEST_F(SourcePositionTableTest, EncodeAscending) {
57  SourcePositionTableBuilder builder(isolate(), zone());
58
59  int code_offset = 0;
60  int source_position = 0;
61  for (int i = 0; i < arraysize(offsets); i++) {
62    code_offset += offsets[i];
63    source_position += offsets[i];
64    if (i % 2) {
65      builder.AddPosition(code_offset, source_position, true);
66    } else {
67      builder.AddPosition(code_offset, source_position, false);
68    }
69  }
70
71  // Also test negative offsets for source positions:
72  for (int i = 0; i < arraysize(offsets); i++) {
73    code_offset += offsets[i];
74    source_position -= offsets[i];
75    if (i % 2) {
76      builder.AddPosition(code_offset, source_position, true);
77    } else {
78      builder.AddPosition(code_offset, source_position, false);
79    }
80  }
81
82  CHECK(!builder.ToSourcePositionTable().is_null());
83}
84
85}  // namespace interpreter
86}  // namespace internal
87}  // namespace v8
88