OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 #include "vm/growable_array.h" |
5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
6 #include "vm/growable_array.h" | |
7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 template <class GrowableArrayInt, class GrowableArrayInt64> | 11 template <class GrowableArrayInt, class GrowableArrayInt64> |
12 void TestGrowableArray() { | 12 void TestGrowableArray() { |
13 GrowableArrayInt g; | 13 GrowableArrayInt g; |
14 EXPECT_EQ(0, g.length()); | 14 EXPECT_EQ(0, g.length()); |
15 EXPECT(g.is_empty()); | 15 EXPECT(g.is_empty()); |
16 g.Add(5); | 16 g.Add(5); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 h.RemoveLast(); | 55 h.RemoveLast(); |
56 EXPECT_EQ(0, h.length()); | 56 EXPECT_EQ(0, h.length()); |
57 EXPECT(h.is_empty()); | 57 EXPECT(h.is_empty()); |
58 h.Add(-8899); | 58 h.Add(-8899); |
59 h.Add(7908); | 59 h.Add(7908); |
60 EXPECT(!h.is_empty()); | 60 EXPECT(!h.is_empty()); |
61 h.Clear(); | 61 h.Clear(); |
62 EXPECT(h.is_empty()); | 62 EXPECT(h.is_empty()); |
63 } | 63 } |
64 | 64 |
65 | |
66 TEST_CASE(GrowableArray) { | 65 TEST_CASE(GrowableArray) { |
67 TestGrowableArray<GrowableArray<int>, GrowableArray<int64_t> >(); | 66 TestGrowableArray<GrowableArray<int>, GrowableArray<int64_t> >(); |
68 } | 67 } |
69 | 68 |
70 | |
71 TEST_CASE(MallocGrowableArray) { | 69 TEST_CASE(MallocGrowableArray) { |
72 TestGrowableArray<MallocGrowableArray<int>, MallocGrowableArray<int64_t> >(); | 70 TestGrowableArray<MallocGrowableArray<int>, MallocGrowableArray<int64_t> >(); |
73 } | 71 } |
74 | 72 |
75 | |
76 static int greatestFirst(const int* a, const int* b) { | 73 static int greatestFirst(const int* a, const int* b) { |
77 if (*a > *b) { | 74 if (*a > *b) { |
78 return -1; | 75 return -1; |
79 } else if (*a < *b) { | 76 } else if (*a < *b) { |
80 return 1; | 77 return 1; |
81 } else { | 78 } else { |
82 return 0; | 79 return 0; |
83 } | 80 } |
84 } | 81 } |
85 | 82 |
86 TEST_CASE(GrowableArraySort) { | 83 TEST_CASE(GrowableArraySort) { |
87 GrowableArray<int> g; | 84 GrowableArray<int> g; |
88 g.Add(12); | 85 g.Add(12); |
89 g.Add(4); | 86 g.Add(4); |
90 g.Add(64); | 87 g.Add(64); |
91 g.Add(8); | 88 g.Add(8); |
92 g.Sort(greatestFirst); | 89 g.Sort(greatestFirst); |
93 EXPECT_EQ(64, g[0]); | 90 EXPECT_EQ(64, g[0]); |
94 EXPECT_EQ(4, g.Last()); | 91 EXPECT_EQ(4, g.Last()); |
95 } | 92 } |
96 | 93 |
97 | |
98 TEST_CASE(GrowableHandlePtr) { | 94 TEST_CASE(GrowableHandlePtr) { |
99 Zone* zone = Thread::Current()->zone(); | 95 Zone* zone = Thread::Current()->zone(); |
100 GrowableHandlePtrArray<const String> test1(zone, 1); | 96 GrowableHandlePtrArray<const String> test1(zone, 1); |
101 EXPECT_EQ(0, test1.length()); | 97 EXPECT_EQ(0, test1.length()); |
102 test1.Add(Symbols::Int()); | 98 test1.Add(Symbols::Int()); |
103 EXPECT(test1[0].raw() == Symbols::Int().raw()); | 99 EXPECT(test1[0].raw() == Symbols::Int().raw()); |
104 EXPECT_EQ(1, test1.length()); | 100 EXPECT_EQ(1, test1.length()); |
105 | 101 |
106 ZoneGrowableHandlePtrArray<const String>* test2 = | 102 ZoneGrowableHandlePtrArray<const String>* test2 = |
107 new ZoneGrowableHandlePtrArray<const String>(zone, 1); | 103 new ZoneGrowableHandlePtrArray<const String>(zone, 1); |
108 test2->Add(Symbols::GetterPrefix()); | 104 test2->Add(Symbols::GetterPrefix()); |
109 EXPECT((*test2)[0].raw() == Symbols::GetterPrefix().raw()); | 105 EXPECT((*test2)[0].raw() == Symbols::GetterPrefix().raw()); |
110 EXPECT_EQ(1, test2->length()); | 106 EXPECT_EQ(1, test2->length()); |
111 } | 107 } |
112 | 108 |
113 } // namespace dart | 109 } // namespace dart |
OLD | NEW |