blob: 266d2d4998ebe333336782c6e6e1ab50df79b423 [file] [log] [blame]
Daniel Dunbar9fde9c42010-06-29 16:52:24 +00001// RUN: %clangxx -emit-llvm -c -o - %s
Jyotsna Verma8cfa0ba2013-04-04 20:56:42 +00002// XFAIL: hexagon
Sebastian Redle47590e2009-05-29 16:43:59 +00003#include <stddef.h>
Douglas Gregor2f1735c2009-05-26 21:27:04 +00004#include <stdlib.h>
5#include <assert.h>
6
Sebastian Redle47590e2009-05-29 16:43:59 +00007// Placement new requires <new> to be included, but we don't support that yet.
8void* operator new(size_t, void* ptr) throw() {
9 return ptr;
10}
11void operator delete(void*, void*) throw() {
12}
13
Douglas Gregor2f1735c2009-05-26 21:27:04 +000014template<typename T>
15class dynarray {
Douglas Gregorc177aa22009-05-28 16:41:44 +000016public:
Douglas Gregor2f1735c2009-05-26 21:27:04 +000017 dynarray() { Start = Last = End = 0; }
18
19 dynarray(const dynarray &other) {
20 Start = (T*)malloc(sizeof(T) * other.size());
21 Last = End = Start + other.size();
22
Douglas Gregor2f1735c2009-05-26 21:27:04 +000023 for (unsigned I = 0, N = other.size(); I != N; ++I)
Sebastian Redle47590e2009-05-29 16:43:59 +000024 new (Start + I) T(other[I]);
Douglas Gregor2f1735c2009-05-26 21:27:04 +000025 }
26
27 ~dynarray() {
Douglas Gregor4fe95f92009-09-04 19:04:08 +000028 for (unsigned I = 0, N = size(); I != N; ++I)
29 Start[I].~T();
30
Douglas Gregor2f1735c2009-05-26 21:27:04 +000031 free(Start);
32 }
33
34 dynarray &operator=(const dynarray &other) {
35 T* NewStart = (T*)malloc(sizeof(T) * other.size());
36
Douglas Gregor2f1735c2009-05-26 21:27:04 +000037 for (unsigned I = 0, N = other.size(); I != N; ++I)
Anders Carlssona53f93b2009-06-01 00:40:08 +000038 new (NewStart + I) T(other[I]);
Sebastian Redle47590e2009-05-29 16:43:59 +000039
Douglas Gregor4fe95f92009-09-04 19:04:08 +000040 for (unsigned I = 0, N = size(); I != N; ++I)
41 Start[I].~T();
42
Douglas Gregor2f1735c2009-05-26 21:27:04 +000043 free(Start);
44 Start = NewStart;
45 Last = End = NewStart + other.size();
46 return *this;
47 }
48
49 unsigned size() const { return Last - Start; }
50 unsigned capacity() const { return End - Start; }
51
Douglas Gregor5c7e2812009-07-22 15:45:39 +000052 void push_back(const T& value);
53
Douglas Gregor2f1735c2009-05-26 21:27:04 +000054 void pop_back() {
Douglas Gregor2f1735c2009-05-26 21:27:04 +000055 --Last;
Douglas Gregor4fe95f92009-09-04 19:04:08 +000056 Last->~T();
Douglas Gregor2f1735c2009-05-26 21:27:04 +000057 }
58
59 T& operator[](unsigned Idx) {
60 return Start[Idx];
61 }
62
63 const T& operator[](unsigned Idx) const {
64 return Start[Idx];
65 }
66
Douglas Gregor2f1735c2009-05-26 21:27:04 +000067 typedef T* iterator;
68 typedef const T* const_iterator;
69
Douglas Gregor815215d2009-05-27 05:35:12 +000070 iterator begin() { return Start; }
71 const_iterator begin() const { return Start; }
Douglas Gregor2f1735c2009-05-26 21:27:04 +000072
Douglas Gregor815215d2009-05-27 05:35:12 +000073 iterator end() { return Last; }
74 const_iterator end() const { return Last; }
Douglas Gregor2f1735c2009-05-26 21:27:04 +000075
Douglas Gregord7f37bf2009-06-22 23:06:13 +000076 bool operator==(const dynarray &other) const {
77 if (size() != other.size())
78 return false;
79
80 for (unsigned I = 0, N = size(); I != N; ++I)
81 if ((*this)[I] != other[I])
82 return false;
83
84 return true;
85 }
86
87 bool operator!=(const dynarray &other) const {
88 return !(*this == other);
89 }
90
Douglas Gregor2f1735c2009-05-26 21:27:04 +000091public:
92 T* Start, *Last, *End;
93};
94
Douglas Gregor5c7e2812009-07-22 15:45:39 +000095template<typename T>
96void dynarray<T>::push_back(const T& value) {
97 if (Last == End) {
98 unsigned NewCapacity = capacity() * 2;
99 if (NewCapacity == 0)
100 NewCapacity = 4;
101
102 T* NewStart = (T*)malloc(sizeof(T) * NewCapacity);
103
104 unsigned Size = size();
105 for (unsigned I = 0; I != Size; ++I)
106 new (NewStart + I) T(Start[I]);
107
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000108 for (unsigned I = 0, N = size(); I != N; ++I)
109 Start[I].~T();
Douglas Gregor5c7e2812009-07-22 15:45:39 +0000110 free(Start);
111
112 Start = NewStart;
113 Last = Start + Size;
114 End = Start + NewCapacity;
115 }
116
117 new (Last) T(value);
118 ++Last;
119}
120
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000121struct Point {
122 Point() { x = y = z = 0.0; }
123 Point(const Point& other) : x(other.x), y(other.y), z(other.z) { }
124
125 float x, y, z;
126};
127
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000128int main() {
129 dynarray<int> di;
130 di.push_back(0);
131 di.push_back(1);
132 di.push_back(2);
133 di.push_back(3);
134 di.push_back(4);
135 assert(di.size() == 5);
136 for (dynarray<int>::iterator I = di.begin(), IEnd = di.end(); I != IEnd; ++I)
137 assert(*I == I - di.begin());
138
Douglas Gregor815215d2009-05-27 05:35:12 +0000139 for (int I = 0, N = di.size(); I != N; ++I)
140 assert(di[I] == I);
141
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000142 di.pop_back();
143 assert(di.size() == 4);
144 di.push_back(4);
145
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000146 dynarray<int> di2 = di;
147 assert(di2.size() == 5);
148 assert(di.begin() != di2.begin());
149 for (dynarray<int>::iterator I = di2.begin(), IEnd = di2.end();
150 I != IEnd; ++I)
151 assert(*I == I - di2.begin());
152
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000153 dynarray<int> di3(di);
154 assert(di3.size() == 5);
155 assert(di.begin() != di3.begin());
156 for (dynarray<int>::iterator I = di3.begin(), IEnd = di3.end();
157 I != IEnd; ++I)
158 assert(*I == I - di3.begin());
159
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000160 dynarray<int> di4;
161 assert(di4.size() == 0);
162 di4 = di;
163 assert(di4.size() == 5);
164 assert(di.begin() != di4.begin());
165 for (dynarray<int>::iterator I = di4.begin(), IEnd = di4.end();
166 I != IEnd; ++I)
167 assert(*I == I - di4.begin());
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000168
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000169 assert(di4 == di);
170 di4[3] = 17;
171 assert(di4 != di);
172
173 dynarray<Point> dp;
174 dp.push_back(Point());
175 assert(dp.size() == 1);
176
Douglas Gregor2f1735c2009-05-26 21:27:04 +0000177 return 0;
178}