Flutter Engine
The Flutter Engine
growable_array_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 <utility>
6
7#include "platform/assert.h"
8#include "vm/growable_array.h"
9#include "vm/symbols.h"
10#include "vm/unit_test.h"
11
12namespace dart {
13
14template <class GrowableArrayInt, class GrowableArrayInt64>
16 GrowableArrayInt g;
17 EXPECT_EQ(0, g.length());
18 EXPECT(g.is_empty());
19 g.Add(5);
20 EXPECT_EQ(5, g[0]);
21 EXPECT_EQ(1, g.length());
22 EXPECT(!g.is_empty());
23 g.Add(3);
24 const GrowableArrayInt& temp = g;
25 EXPECT_EQ(5, temp[0]);
26 EXPECT_EQ(3, temp[1]);
27 for (int i = 0; i < 10000; i++) {
28 g.Add(i);
29 }
30 EXPECT_EQ(10002, g.length());
31 EXPECT_EQ(10000 - 1, g.Last());
32
33 GrowableArrayInt64 f(10);
34 EXPECT_EQ(0, f.length());
35 f.Add(-1LL);
36 f.Add(15LL);
37 EXPECT_EQ(2, f.length());
38 for (int64_t l = 0; l < 100; l++) {
39 f.Add(l);
40 }
41 EXPECT_EQ(102, f.length());
42 EXPECT_EQ(100 - 1, f.Last());
43 EXPECT_EQ(-1LL, f[0]);
44
45 GrowableArrayInt h;
46 EXPECT_EQ(0, h.length());
47 h.Add(101);
48 h.Add(102);
49 h.Add(103);
50 EXPECT_EQ(3, h.length());
51 EXPECT_EQ(103, h.Last());
52 h.RemoveLast();
53 EXPECT_EQ(2, h.length());
54 EXPECT_EQ(102, h.Last());
55 h.RemoveLast();
56 EXPECT_EQ(1, h.length());
57 EXPECT_EQ(101, h.Last());
58 h.RemoveLast();
59 EXPECT_EQ(0, h.length());
60 EXPECT(h.is_empty());
61 h.Add(-8899);
62 h.Add(7908);
63 EXPECT(!h.is_empty());
64 h.Clear();
65 EXPECT(h.is_empty());
66}
67
69 TestGrowableArray<GrowableArray<int>, GrowableArray<int64_t> >();
70}
71
73 TestGrowableArray<MallocGrowableArray<int>, MallocGrowableArray<int64_t> >();
74}
75
76static int greatestFirst(const int* a, const int* b) {
77 if (*a > *b) {
78 return -1;
79 } else if (*a < *b) {
80 return 1;
81 } else {
82 return 0;
83 }
84}
85
86TEST_CASE(GrowableArraySort) {
88 g.Add(12);
89 g.Add(4);
90 g.Add(64);
91 g.Add(8);
93 EXPECT_EQ(64, g[0]);
94 EXPECT_EQ(4, g.Last());
95}
96
97ISOLATE_UNIT_TEST_CASE(GrowableHandlePtr) {
98 Zone* zone = Thread::Current()->zone();
100 EXPECT_EQ(0, test1.length());
101 test1.Add(Symbols::Int());
102 EXPECT(test1[0].ptr() == Symbols::Int().ptr());
103 EXPECT_EQ(1, test1.length());
104
107 test2->Add(Symbols::GetterPrefix());
108 EXPECT((*test2)[0].ptr() == Symbols::GetterPrefix().ptr());
109 EXPECT_EQ(1, test2->length());
110}
111
112TEST_CASE(GrowableArrayMoveCtor) {
114 a.Add(4);
115 a.Add(5);
116 int* a_data = a.data();
117
118 GrowableArray<int> b(std::move(a));
119
120 EXPECT_EQ(0, a.length());
121 EXPECT_EQ((int*)nullptr, a.data());
122 EXPECT_EQ(2, b.length());
123 EXPECT_EQ(a_data, b.data());
124}
125
126TEST_CASE(GrowableArrayMoveAssign) {
128 a.Add(1);
129 a.Add(2);
130 a.Add(3);
131 b.Add(7);
132 int* a_data = a.data();
133 int* b_data = b.data();
134
135 a = std::move(b);
136
137 EXPECT_EQ(1, a.length());
138 EXPECT_EQ(b_data, a.data());
139 EXPECT_EQ(3, b.length());
140 EXPECT_EQ(a_data, b.data());
141}
142
143} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
static void test1(skiatest::Reporter *reporter, SkWriter32 *writer)
void Add(const T &value)
void Sort(int compare(const T *, const T *))
Zone * zone() const
Definition: thread_state.h:37
static Thread * Current()
Definition: thread.h:362
static bool b
struct MyStruct a[10]
Definition: dart_vm.cc:33
void TestGrowableArray()
static int greatestFirst(const int *a, const int *b)
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
TEST_CASE(DirectoryCurrent)
SkScalar h