Flutter Engine
The Flutter Engine
kernel_test.cc
Go to the documentation of this file.
1// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include <type_traits>
6
7#include "platform/globals.h"
8
9#include "vm/kernel.h"
10#include "vm/object.h"
11#include "vm/unit_test.h"
12
13namespace dart {
14
16 const intptr_t raw_line_starts_data[] = {
17 0, 8, 12, 17, 18, 20, 23, 30, 31, 33,
18 };
19 const intptr_t length = std::extent<decltype(raw_line_starts_data)>::value;
20 ASSERT(length > 0);
21 const TypedData& line_starts_data = TypedData::Handle(
22 TypedData::New(kTypedDataUint16ArrayCid, length, Heap::kOld));
23 for (intptr_t i = 0; i < length; ++i) {
24 line_starts_data.SetUint16(i << 1,
25 static_cast<int16_t>(raw_line_starts_data[i]));
26 }
27 return line_starts_data;
28}
29
30ISOLATE_UNIT_TEST_CASE(KernelLineStartsReader_MaxPosition) {
32 EXPECT_EQ(33u, reader.MaxPosition());
33}
34
36 intptr_t position,
37 intptr_t expected_line,
38 intptr_t expected_col) {
39 intptr_t line;
40 intptr_t col;
41 EXPECT_EQ(true, reader.LocationForPosition(position, &line, &col));
42 EXPECT_EQ(expected_line, line);
43 EXPECT_EQ(expected_col, col);
44}
45
46ISOLATE_UNIT_TEST_CASE(KernelLineStartsReader_LocationForPosition) {
48 ExpectLocationForPosition(reader, 0, 1, 1);
49 ExpectLocationForPosition(reader, 4, 1, 5);
50 ExpectLocationForPosition(reader, 8, 2, 1);
51 ExpectLocationForPosition(reader, 14, 3, 3);
52 ExpectLocationForPosition(reader, 17, 4, 1);
53 ExpectLocationForPosition(reader, 19, 5, 2);
54 ExpectLocationForPosition(reader, 22, 6, 3);
55 ExpectLocationForPosition(reader, 29, 7, 7);
56 ExpectLocationForPosition(reader, 30, 8, 1);
57 ExpectLocationForPosition(reader, 32, 9, 2);
58 ExpectLocationForPosition(reader, 33, 10, 1);
59
60 intptr_t line;
61 intptr_t col;
62 EXPECT_EQ(false, reader.LocationForPosition(-1, &line, &col));
63 EXPECT_EQ(false, reader.LocationForPosition(34, &line, &col));
64}
65
67 intptr_t line,
68 intptr_t expected_first_token,
69 intptr_t expected_last_token) {
72 EXPECT_EQ(true, reader.TokenRangeAtLine(line, &first_token, &last_token));
73 EXPECT_EQ(expected_first_token, first_token.Serialize());
74 EXPECT_EQ(expected_last_token, last_token.Serialize());
75}
76
77ISOLATE_UNIT_TEST_CASE(KernelLineStartsReader_TokenRangeAtLine) {
79 ExpectTokenRangeAtLine(reader, 1, 0, 7);
80 ExpectTokenRangeAtLine(reader, 2, 8, 11);
81 ExpectTokenRangeAtLine(reader, 3, 12, 16);
82 ExpectTokenRangeAtLine(reader, 4, 17, 17);
83 ExpectTokenRangeAtLine(reader, 5, 18, 19);
84 ExpectTokenRangeAtLine(reader, 6, 20, 22);
85 ExpectTokenRangeAtLine(reader, 7, 23, 29);
86 ExpectTokenRangeAtLine(reader, 8, 30, 30);
87 ExpectTokenRangeAtLine(reader, 9, 31, 32);
88 ExpectTokenRangeAtLine(reader, 10, 33, 33);
89
92 EXPECT_EQ(false, reader.TokenRangeAtLine(0, &first_token, &last_token));
93 EXPECT_EQ(false, reader.TokenRangeAtLine(11, &first_token, &last_token));
94}
95
96} // namespace dart
@ kOld
Definition: heap.h:39
static Object & Handle()
Definition: object.h:407
static TokenPosition Synthetic(intptr_t value)
int32_t Serialize() const
static TypedDataPtr New(intptr_t class_id, intptr_t len, Heap::Space space=Heap::kNew)
Definition: object.cc:25587
DART_WARN_UNUSED_RESULT bool LocationForPosition(intptr_t position, intptr_t *line, intptr_t *col=nullptr) const
Definition: kernel.cc:43
DART_WARN_UNUSED_RESULT bool TokenRangeAtLine(intptr_t line_number, dart::TokenPosition *first_token_index, dart::TokenPosition *last_token_index) const
Definition: kernel.cc:71
#define ASSERT(E)
uint8_t value
size_t length
Definition: dart_vm.cc:33
void ExpectTokenRangeAtLine(const kernel::KernelLineStartsReader &reader, intptr_t line, intptr_t expected_first_token, intptr_t expected_last_token)
Definition: kernel_test.cc:66
const dart::TypedData & CreateLineStartsData()
Definition: kernel_test.cc:15
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
void ExpectLocationForPosition(const kernel::KernelLineStartsReader &reader, intptr_t position, intptr_t expected_line, intptr_t expected_col)
Definition: kernel_test.cc:35