|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qvarlengtharray.h> |
|
45 |
|
46 const int N = 1; |
|
47 |
|
48 //TESTED_CLASS= |
|
49 //TESTED_FILES= |
|
50 |
|
51 class tst_QVarLengthArray : public QObject |
|
52 { |
|
53 Q_OBJECT |
|
54 |
|
55 public: |
|
56 tst_QVarLengthArray() {} |
|
57 virtual ~tst_QVarLengthArray() {} |
|
58 |
|
59 private slots: |
|
60 void append(); |
|
61 void removeLast(); |
|
62 void oldTests(); |
|
63 void task214223(); |
|
64 }; |
|
65 |
|
66 int fooCtor = 0; |
|
67 int fooDtor = 0; |
|
68 |
|
69 struct Foo |
|
70 { |
|
71 int *p; |
|
72 |
|
73 Foo() { p = new int; ++fooCtor; } |
|
74 Foo(const Foo &other) { p = new int; ++fooCtor; } |
|
75 |
|
76 void operator=(const Foo & /* other */) { } |
|
77 |
|
78 ~Foo() { delete p; ++fooDtor; } |
|
79 }; |
|
80 |
|
81 void tst_QVarLengthArray::append() |
|
82 { |
|
83 QVarLengthArray<QString> v; |
|
84 v.append(QString("hello")); |
|
85 |
|
86 QVarLengthArray<int> v2; // rocket! |
|
87 v2.append(5); |
|
88 } |
|
89 |
|
90 void tst_QVarLengthArray::removeLast() |
|
91 { |
|
92 { |
|
93 QVarLengthArray<char, 2> v; |
|
94 v.append(0); |
|
95 v.append(1); |
|
96 QCOMPARE(v.size(), 2); |
|
97 v.append(2); |
|
98 v.append(3); |
|
99 QCOMPARE(v.size(), 4); |
|
100 v.removeLast(); |
|
101 QCOMPARE(v.size(), 3); |
|
102 v.removeLast(); |
|
103 QCOMPARE(v.size(), 2); |
|
104 } |
|
105 |
|
106 { |
|
107 QVarLengthArray<QString, 2> v; |
|
108 v.append("0"); |
|
109 v.append("1"); |
|
110 QCOMPARE(v.size(), 2); |
|
111 v.append("2"); |
|
112 v.append("3"); |
|
113 QCOMPARE(v.size(), 4); |
|
114 v.removeLast(); |
|
115 QCOMPARE(v.size(), 3); |
|
116 v.removeLast(); |
|
117 QCOMPARE(v.size(), 2); |
|
118 } |
|
119 } |
|
120 |
|
121 void tst_QVarLengthArray::oldTests() |
|
122 { |
|
123 { |
|
124 QVarLengthArray<int, 256> sa(128); |
|
125 QVERIFY(sa.data() == &sa[0]); |
|
126 sa[0] = 0xfee; |
|
127 sa[10] = 0xff; |
|
128 QVERIFY(sa[0] == 0xfee); |
|
129 QVERIFY(sa[10] == 0xff); |
|
130 sa.resize(512); |
|
131 QVERIFY(sa.data() == &sa[0]); |
|
132 QVERIFY(sa[0] == 0xfee); |
|
133 QVERIFY(sa[10] == 0xff); |
|
134 QVERIFY(sa.size() == 512); |
|
135 sa.reserve(1024); |
|
136 QVERIFY(sa.capacity() == 1024); |
|
137 QVERIFY(sa.size() == 512); |
|
138 } |
|
139 { |
|
140 QVarLengthArray<QString> sa(10); |
|
141 sa[0] = "Hello"; |
|
142 sa[9] = "World"; |
|
143 QVERIFY(*sa.data() == "Hello"); |
|
144 QVERIFY(sa[9] == "World"); |
|
145 sa.reserve(512); |
|
146 QVERIFY(*sa.data() == "Hello"); |
|
147 QVERIFY(sa[9] == "World"); |
|
148 sa.resize(512); |
|
149 QVERIFY(*sa.data() == "Hello"); |
|
150 QVERIFY(sa[9] == "World"); |
|
151 } |
|
152 { |
|
153 int arr[2] = {1, 2}; |
|
154 QVarLengthArray<int> sa(10); |
|
155 QCOMPARE(sa.size(), 10); |
|
156 sa.append(arr, 2); |
|
157 QCOMPARE(sa.size(), 12); |
|
158 QCOMPARE(sa[10], 1); |
|
159 QCOMPARE(sa[11], 2); |
|
160 } |
|
161 { |
|
162 QString arr[2] = { QString("hello"), QString("world") }; |
|
163 QVarLengthArray<QString> sa(10); |
|
164 QCOMPARE(sa.size(), 10); |
|
165 sa.append(arr, 2); |
|
166 QCOMPARE(sa.size(), 12); |
|
167 QCOMPARE(sa[10], QString("hello")); |
|
168 QCOMPARE(sa[11], QString("world")); |
|
169 |
|
170 sa.append(arr, 1); |
|
171 QCOMPARE(sa.size(), 13); |
|
172 QCOMPARE(sa[12], QString("hello")); |
|
173 |
|
174 sa.append(arr, 0); |
|
175 QCOMPARE(sa.size(), 13); |
|
176 } |
|
177 { |
|
178 // assignment operator and copy constructor |
|
179 |
|
180 QVarLengthArray<int> sa(10); |
|
181 sa[5] = 5; |
|
182 |
|
183 QVarLengthArray<int> sa2(10); |
|
184 sa2[5] = 6; |
|
185 sa2 = sa; |
|
186 QCOMPARE(sa2[5], 5); |
|
187 |
|
188 QVarLengthArray<int> sa3(sa); |
|
189 QCOMPARE(sa3[5], 5); |
|
190 } |
|
191 |
|
192 QSKIP("This test causes the machine to crash when allocating too much memory.", SkipSingle); |
|
193 { |
|
194 QVarLengthArray<Foo> a; |
|
195 const int N = 0x7fffffff / sizeof(Foo); |
|
196 const int Prealloc = a.capacity(); |
|
197 const Foo *data0 = a.constData(); |
|
198 |
|
199 a.resize(N); |
|
200 if (a.size() == N) { |
|
201 QVERIFY(a.capacity() >= N); |
|
202 QCOMPARE(fooCtor, N); |
|
203 QCOMPARE(fooDtor, 0); |
|
204 |
|
205 for (int i = 0; i < N; i += 35000) |
|
206 a[i] = Foo(); |
|
207 } else { |
|
208 // this is the case we're actually testing |
|
209 QCOMPARE(a.size(), 0); |
|
210 QCOMPARE(a.capacity(), Prealloc); |
|
211 QCOMPARE(a.constData(), data0); |
|
212 QCOMPARE(fooCtor, 0); |
|
213 QCOMPARE(fooDtor, 0); |
|
214 |
|
215 a.resize(5); |
|
216 QCOMPARE(a.size(), 5); |
|
217 QCOMPARE(a.capacity(), Prealloc); |
|
218 QCOMPARE(a.constData(), data0); |
|
219 QCOMPARE(fooCtor, 5); |
|
220 QCOMPARE(fooDtor, 0); |
|
221 |
|
222 a.resize(Prealloc + 1); |
|
223 QCOMPARE(a.size(), Prealloc + 1); |
|
224 QVERIFY(a.capacity() >= Prealloc + 1); |
|
225 QVERIFY(a.constData() != data0); |
|
226 QCOMPARE(fooCtor, Prealloc + 6); |
|
227 QCOMPARE(fooDtor, 5); |
|
228 |
|
229 const Foo *data1 = a.constData(); |
|
230 |
|
231 a.resize(0x10000000); |
|
232 QCOMPARE(a.size(), 0); |
|
233 QVERIFY(a.capacity() >= Prealloc + 1); |
|
234 QVERIFY(a.constData() == data1); |
|
235 QCOMPARE(fooCtor, Prealloc + 6); |
|
236 QCOMPARE(fooDtor, Prealloc + 6); |
|
237 } |
|
238 } |
|
239 } |
|
240 |
|
241 void tst_QVarLengthArray::task214223() |
|
242 { |
|
243 //creating a QVarLengthArray of the same size as the prealloc size |
|
244 // will make the next call to append(const T&) corrupt the memory |
|
245 // you should get a segfault pretty soon after that :-) |
|
246 QVarLengthArray<float, 1> d(1); |
|
247 for (int i=0; i<30; i++) |
|
248 d.append(i); |
|
249 } |
|
250 |
|
251 QTEST_APPLESS_MAIN(tst_QVarLengthArray) |
|
252 #include "tst_qvarlengtharray.moc" |