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