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