OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / changeset / tst_changeset.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include <utils/changeset.h>
35
36 #include <QtTest/QtTest>
37
38 //TESTED_COMPONENT=src/utils/changeset
39
40 class tst_ChangeSet : public QObject
41 {
42     Q_OBJECT
43
44 private slots:
45     void singleReplace();
46     void singleMove();
47     void singleInsert();
48     void singleRemove();
49     void singleFlip();
50     void singleCopy();
51
52     void doubleInsert();
53     void conflicts();
54 };
55
56
57 void tst_ChangeSet::singleReplace()
58 {
59     {
60         Utils::ChangeSet cs;
61         QString test("abcdef");
62         QVERIFY(cs.replace(0, 2, "ghi"));
63         cs.apply(&test);
64         QCOMPARE(test, QLatin1String("ghicdef"));
65     }
66     {
67         Utils::ChangeSet cs;
68         QString test("abcdef");
69         QVERIFY(cs.replace(4, 6, "ghi"));
70         cs.apply(&test);
71         QCOMPARE(test, QLatin1String("abcdghi"));
72     }
73     {
74         Utils::ChangeSet cs;
75         QString test("abcdef");
76         QVERIFY(cs.replace(3, 3, "ghi"));
77         cs.apply(&test);
78         QCOMPARE(test, QLatin1String("abcghidef"));
79     }
80     {
81         Utils::ChangeSet cs;
82         QString test("abcdef");
83         QVERIFY(cs.replace(0, 6, ""));
84         cs.apply(&test);
85         QCOMPARE(test, QLatin1String(""));
86     }
87     {
88         Utils::ChangeSet cs;
89         QString test("abcdef");
90         QVERIFY(cs.replace(3, 13, "ghi"));
91         cs.apply(&test);
92         QCOMPARE(test, QLatin1String("abcghi"));
93     }
94 }
95
96 void tst_ChangeSet::singleMove()
97 {
98     {
99         Utils::ChangeSet cs;
100         QString test("abcdef");
101         QVERIFY(cs.move(0, 2, 4));
102         cs.apply(&test);
103         QCOMPARE(test, QLatin1String("cdabef"));
104     }
105     {
106         Utils::ChangeSet cs;
107         QString test("abcdef");
108         QVERIFY(cs.move(4, 6, 0));
109         cs.apply(&test);
110         QCOMPARE(test, QLatin1String("efabcd"));
111     }
112     {
113         Utils::ChangeSet cs;
114         QString test("abcdef");
115         QVERIFY(cs.move(3, 13, 0));
116         cs.apply(&test);
117         QCOMPARE(test, QLatin1String("defabc"));
118     }
119     {
120         Utils::ChangeSet cs;
121         QString test("abcdef");
122         QVERIFY(cs.move(3, 3, 0));
123         cs.apply(&test);
124         QCOMPARE(test, QLatin1String("abcdef"));
125     }
126     {
127         Utils::ChangeSet cs;
128         QString test("abcdef");
129         QVERIFY(cs.move(0, 1, 10));
130         cs.apply(&test);
131         // ### maybe this should expand the string or error?
132         QCOMPARE(test, QLatin1String("bcdef"));
133     }
134 }
135
136 void tst_ChangeSet::singleInsert()
137 {
138     {
139         Utils::ChangeSet cs;
140         QString test("abcdef");
141         QVERIFY(cs.insert(0, "ghi"));
142         cs.apply(&test);
143         QCOMPARE(test, QLatin1String("ghiabcdef"));
144     }
145     {
146         Utils::ChangeSet cs;
147         QString test("abcdef");
148         QVERIFY(cs.insert(6, "ghi"));
149         cs.apply(&test);
150         QCOMPARE(test, QLatin1String("abcdefghi"));
151     }
152     {
153         Utils::ChangeSet cs;
154         QString test("abcdef");
155         QVERIFY(cs.insert(3, ""));
156         cs.apply(&test);
157         QCOMPARE(test, QLatin1String("abcdef"));
158     }
159     {
160         Utils::ChangeSet cs;
161         QString test("abcdef");
162         QVERIFY(cs.insert(7, "g"));
163         cs.apply(&test);
164         // ### maybe this should expand the string or error?
165         QCOMPARE(test, QLatin1String("abcdef"));
166     }
167 }
168
169 void tst_ChangeSet::singleRemove()
170 {
171     {
172         Utils::ChangeSet cs;
173         QString test("abcdef");
174         QVERIFY(cs.remove(0, 1));
175         cs.apply(&test);
176         QCOMPARE(test, QLatin1String("bcdef"));
177     }
178     {
179         Utils::ChangeSet cs;
180         QString test("abcdef");
181         QVERIFY(cs.remove(3, 6));
182         cs.apply(&test);
183         QCOMPARE(test, QLatin1String("abc"));
184     }
185     {
186         Utils::ChangeSet cs;
187         QString test("abcdef");
188         QVERIFY(cs.remove(4, 14));
189         cs.apply(&test);
190         QCOMPARE(test, QLatin1String("abcd"));
191     }
192     {
193         Utils::ChangeSet cs;
194         QString test("abcdef");
195         QVERIFY(cs.remove(2, 2));
196         cs.apply(&test);
197         QCOMPARE(test, QLatin1String("abcdef"));
198     }
199     {
200         Utils::ChangeSet cs;
201         QString test("abcdef");
202         QVERIFY(cs.remove(7, 8));
203         cs.apply(&test);
204         QCOMPARE(test, QLatin1String("abcdef"));
205     }
206 }
207
208 void tst_ChangeSet::singleFlip()
209 {
210     {
211         Utils::ChangeSet cs;
212         QString test("abcdef");
213         QVERIFY(cs.flip(0, 2, 3, 6));
214         cs.apply(&test);
215         QCOMPARE(test, QLatin1String("defcab"));
216     }
217     {
218         Utils::ChangeSet cs;
219         QString test("abcdef");
220         QVERIFY(cs.flip(1, 3, 3, 4));
221         cs.apply(&test);
222         QCOMPARE(test, QLatin1String("adbcef"));
223     }
224     {
225         Utils::ChangeSet cs;
226         QString test("abcdef");
227         QVERIFY(cs.flip(3, 3, 4, 4));
228         cs.apply(&test);
229         QCOMPARE(test, QLatin1String("abcdef"));
230     }
231     {
232         Utils::ChangeSet cs;
233         QString test("abcdef");
234         QVERIFY(cs.flip(3, 3, 4, 5));
235         cs.apply(&test);
236         QCOMPARE(test, QLatin1String("abcedf"));
237     }
238     {
239         Utils::ChangeSet cs;
240         QString test("abcdef");
241         QVERIFY(cs.flip(0, 6, 6, 12));
242         cs.apply(&test);
243         QCOMPARE(test, QLatin1String("abcdef"));
244     }
245     {
246         Utils::ChangeSet cs;
247         QString test("abcdef");
248         QVERIFY(cs.flip(0, 6, 7, 10));
249         cs.apply(&test);
250         // ### maybe this should expand the string or error?
251         QCOMPARE(test, QLatin1String(""));
252     }
253     {
254         Utils::ChangeSet cs;
255         QCOMPARE(cs.flip(0, 3, 1, 4), false);
256     }
257     {
258         Utils::ChangeSet cs;
259         QCOMPARE(cs.flip(0, 3, 2, 5), false);
260     }
261     {
262         Utils::ChangeSet cs;
263         QVERIFY(cs.flip(0, 3, 0, 0));
264         QString test("abcdef");
265         cs.apply(&test);
266         QCOMPARE(test, QLatin1String("abcdef"));
267     }
268     {
269         Utils::ChangeSet cs;
270         QVERIFY(cs.flip(0, 0, 0, 3));
271         QString test("abcdef");
272         cs.apply(&test);
273         QCOMPARE(test, QLatin1String("abcdef"));
274     }
275     {
276         Utils::ChangeSet cs;
277         QVERIFY(cs.flip(0, 3, 3, 3));
278         QString test("abcdef");
279         cs.apply(&test);
280         QCOMPARE(test, QLatin1String("abcdef"));
281     }
282 }
283
284 void tst_ChangeSet::singleCopy()
285 {
286     {
287         Utils::ChangeSet cs;
288         QString test("abcdef");
289         QVERIFY(cs.copy(0, 2, 4));
290         cs.apply(&test);
291         QCOMPARE(test, QLatin1String("abcdabef"));
292     }
293     {
294         Utils::ChangeSet cs;
295         QString test("abcdef");
296         QVERIFY(cs.copy(1, 3, 3));
297         cs.apply(&test);
298         QCOMPARE(test, QLatin1String("abcbcdef"));
299     }
300     {
301         Utils::ChangeSet cs;
302         QString test("abcdef");
303         QVERIFY(cs.copy(3, 3, 4));
304         cs.apply(&test);
305         QCOMPARE(test, QLatin1String("abcdef"));
306     }
307     {
308         Utils::ChangeSet cs;
309         QString test("abcdef");
310         QVERIFY(cs.copy(0, 6, 6));
311         cs.apply(&test);
312         QCOMPARE(test, QLatin1String("abcdefabcdef"));
313     }
314     {
315         Utils::ChangeSet cs;
316         QString test("abcdef");
317         QVERIFY(cs.copy(0, 6, 7));
318         cs.apply(&test);
319         // ### maybe this should expand the string or error?
320         QCOMPARE(test, QLatin1String("abcdef"));
321     }
322     {
323         Utils::ChangeSet cs;
324         QCOMPARE(cs.copy(0, 3, 1), false);
325     }
326     {
327         Utils::ChangeSet cs;
328         QCOMPARE(cs.copy(0, 3, 2), false);
329     }
330     {
331         Utils::ChangeSet cs;
332         QVERIFY(cs.copy(0, 3, 0));
333         QString test("abcdef");
334         cs.apply(&test);
335         QCOMPARE(test, QLatin1String("abcabcdef"));
336     }
337     {
338         Utils::ChangeSet cs;
339         QVERIFY(cs.copy(0, 3, 3));
340         QString test("abcdef");
341         cs.apply(&test);
342         QCOMPARE(test, QLatin1String("abcabcdef"));
343     }
344 }
345
346 void tst_ChangeSet::doubleInsert()
347 {
348     {
349         Utils::ChangeSet cs;
350         QVERIFY(cs.insert(1, "01"));
351         QVERIFY(cs.insert(1, "234"));
352         QString test("abcdef");
353         cs.apply(&test);
354         QCOMPARE(test, QLatin1String("a01234bcdef"));
355     }
356     {
357         Utils::ChangeSet cs;
358         QVERIFY(cs.insert(1, "234"));
359         QVERIFY(cs.insert(1, "01"));
360         QString test("abcdef");
361         cs.apply(&test);
362         QCOMPARE(test, QLatin1String("a23401bcdef"));
363     }
364     {
365         Utils::ChangeSet cs;
366         QVERIFY(cs.insert(1, "01"));
367         QVERIFY(cs.remove(1, 2));
368         QVERIFY(cs.insert(2, "234"));
369         QString test("abcdef");
370         cs.apply(&test);
371         QCOMPARE(test, QLatin1String("a01234cdef"));
372     }
373 }
374
375 void tst_ChangeSet::conflicts()
376 {
377     {
378         Utils::ChangeSet cs;
379         QVERIFY(cs.move(1, 4, 5));
380         QCOMPARE(cs.replace(0, 2, "abc"), false);
381     }
382     {
383         Utils::ChangeSet cs;
384         QVERIFY(cs.move(1, 4, 5));
385         QCOMPARE(cs.replace(1, 4, "abc"), false);
386     }
387     {
388         Utils::ChangeSet cs;
389         QVERIFY(cs.move(1, 4, 5));
390         QCOMPARE(cs.replace(1, 2, "abc"), false);
391     }
392     {
393         Utils::ChangeSet cs;
394         QVERIFY(cs.move(1, 4, 5));
395         QCOMPARE(cs.replace(2, 2, "abc"), false);
396     }
397     {
398         Utils::ChangeSet cs;
399         QVERIFY(cs.move(1, 4, 5));
400         QCOMPARE(cs.replace(2, 3, "abc"), false);
401     }
402     {
403         Utils::ChangeSet cs;
404         QVERIFY(cs.move(1, 4, 5));
405         QCOMPARE(cs.replace(3, 3, "abc"), false);
406     }
407     {
408         Utils::ChangeSet cs;
409         QVERIFY(cs.move(1, 4, 5));
410         QCOMPARE(cs.replace(3, 4, "abc"), false);
411     }
412     {
413         Utils::ChangeSet cs;
414         QVERIFY(cs.move(1, 4, 5));
415         QCOMPARE(cs.replace(4, 6, "abc"), false);
416     }
417
418     {
419         Utils::ChangeSet cs;
420         QVERIFY(cs.move(1, 4, 5));
421         QVERIFY(cs.replace(0, 1, "bla"));
422         QString test("abcdef");
423         cs.apply(&test);
424         QCOMPARE(test, QLatin1String("blaebcdf"));
425     }
426     {
427         Utils::ChangeSet cs;
428         QVERIFY(cs.move(1, 4, 5));
429         QVERIFY(cs.replace(4, 5, "bla"));
430         QString test("abcdef");
431         cs.apply(&test);
432         QCOMPARE(test, QLatin1String("ablabcdf"));
433     }
434     {
435         Utils::ChangeSet cs;
436         QVERIFY(cs.move(1, 4, 5));
437         QVERIFY(cs.replace(5, 6, "bla"));
438         QString test("abcdef");
439         cs.apply(&test);
440         QCOMPARE(test, QLatin1String("aebcdbla"));
441     }
442 }
443
444 QTEST_MAIN(tst_ChangeSet)
445
446 #include "tst_changeset.moc"