Flutter Engine
The Flutter Engine
bitmap_test.cc
Go to the documentation of this file.
1// Copyright (c) 2012, 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 "vm/bitmap.h"
6
7#include "platform/assert.h"
9#include "vm/object.h"
10#include "vm/unit_test.h"
11
12namespace dart {
13
14// 0x4 is just a placeholder PC offset because no entry of a CSM should
15// have a PC offset of 0, otherwise internal assumptions break.
16static constexpr uint32_t kTestPcOffset = 0x4;
17static constexpr intptr_t kTestSpillSlotBitCount = 0;
18
19static CompressedStackMapsPtr MapsFromBuilder(Zone* zone, BitmapBuilder* bmap) {
22 return builder.Finalize();
23}
24
26 // Test basic bit map builder operations.
27 BitmapBuilder* builder1 = new BitmapBuilder();
28 EXPECT_EQ(0, builder1->Length());
29
30 bool value = true;
31 for (int32_t i = 0; i < 128; i++) {
32 builder1->Set(i, value);
33 value = !value;
34 }
35 EXPECT_EQ(128, builder1->Length());
36 value = true;
37 for (int32_t i = 0; i < 128; i++) {
38 EXPECT_EQ(value, builder1->Get(i));
39 value = !value;
40 }
41 value = true;
42 for (int32_t i = 0; i < 1024; i++) {
43 builder1->Set(i, value);
44 value = !value;
45 }
46 EXPECT_EQ(1024, builder1->Length());
47 value = true;
48 for (int32_t i = 0; i < 1024; i++) {
49 EXPECT_EQ(value, builder1->Get(i));
50 value = !value;
51 }
52
53 // Create a CompressedStackMaps object and verify its contents.
54 const auto& maps1 = CompressedStackMaps::Handle(
55 thread->zone(), MapsFromBuilder(thread->zone(), builder1));
56 auto it1 = maps1.iterator(thread);
57 EXPECT(it1.MoveNext());
58
59 EXPECT_EQ(kTestPcOffset, it1.pc_offset());
60 EXPECT_EQ(kTestSpillSlotBitCount, it1.SpillSlotBitCount());
61 EXPECT_EQ(1024, it1.Length());
62 value = true;
63 for (int32_t i = 0; i < 1024; i++) {
64 EXPECT_EQ(value, it1.IsObject(i));
65 value = !value;
66 }
67
68 EXPECT(!it1.MoveNext());
69
70 // Test the SetRange function in the builder.
71 builder1->SetRange(0, 256, false);
72 EXPECT_EQ(1024, builder1->Length());
73 builder1->SetRange(257, 1024, true);
74 EXPECT_EQ(1025, builder1->Length());
75 builder1->SetRange(1025, 2048, false);
76 EXPECT_EQ(2049, builder1->Length());
77 for (int32_t i = 0; i <= 256; i++) {
78 EXPECT(!builder1->Get(i));
79 }
80 for (int32_t i = 257; i <= 1024; i++) {
81 EXPECT(builder1->Get(i));
82 }
83 for (int32_t i = 1025; i <= 2048; i++) {
84 EXPECT(!builder1->Get(i));
85 }
86
87 const auto& maps2 = CompressedStackMaps::Handle(
88 thread->zone(), MapsFromBuilder(thread->zone(), builder1));
89 auto it2 = maps2.iterator(thread);
90 EXPECT(it2.MoveNext());
91
92 EXPECT_EQ(kTestPcOffset, it2.pc_offset());
93 EXPECT_EQ(kTestSpillSlotBitCount, it2.SpillSlotBitCount());
94 EXPECT_EQ(2049, it2.Length());
95 for (int32_t i = 0; i <= 256; i++) {
96 EXPECT(!it2.IsObject(i));
97 }
98 for (int32_t i = 257; i <= 1024; i++) {
99 EXPECT(it2.IsObject(i));
100 }
101 for (int32_t i = 1025; i <= 2048; i++) {
102 EXPECT(!it2.IsObject(i));
103 }
104
105 EXPECT(!it2.MoveNext());
106
107 // Test using SetLength to shorten the builder, followed by lengthening.
108 builder1->SetLength(747);
109 EXPECT_EQ(747, builder1->Length());
110 for (int32_t i = 257; i < 747; ++i) {
111 EXPECT(builder1->Get(i));
112 }
113
114 builder1->Set(800, false);
115 EXPECT_EQ(801, builder1->Length());
116 for (int32_t i = 257; i < 747; ++i) {
117 EXPECT(builder1->Get(i));
118 }
119 for (int32_t i = 747; i < 801; ++i) {
120 EXPECT(!builder1->Get(i));
121 }
122
123 builder1->Set(900, true);
124 EXPECT_EQ(901, builder1->Length());
125 for (int32_t i = 257; i < 747; ++i) {
126 EXPECT(builder1->Get(i));
127 }
128 for (int32_t i = 747; i < 900; ++i) {
129 EXPECT(!builder1->Get(i));
130 }
131 EXPECT(builder1->Get(900));
132}
133
134ISOLATE_UNIT_TEST_CASE(BitmapBuilder_Regress44946) {
135 BitmapBuilder* builder1 = new BitmapBuilder();
136 EXPECT_EQ(0, builder1->Length());
137
138 builder1->Set(10000, false);
139 EXPECT_EQ(10001, builder1->Length());
140 builder1->Set(9999, true);
141 EXPECT(builder1->Get(9999));
142}
143
144} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
bool Get(intptr_t bit_offset) const
Definition: bitmap.cc:35
void Set(intptr_t bit_offset, bool value)
Definition: bitmap.cc:44
intptr_t Length() const
Definition: bitmap.h:36
void SetRange(intptr_t min, intptr_t max, bool value)
Definition: bitmap.cc:75
void SetLength(intptr_t length)
Definition: bitmap.cc:13
static Object & Handle()
Definition: object.h:407
uint8_t value
Definition: dart_vm.cc:33
static constexpr uint32_t kTestPcOffset
Definition: bitmap_test.cc:16
static constexpr intptr_t kTestSpillSlotBitCount
Definition: bitmap_test.cc:17
static CompressedStackMapsPtr MapsFromBuilder(Zone *zone, BitmapBuilder *bmap)
Definition: bitmap_test.cc:19
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)