tests/auto/qvector/tst_qvector.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 <qvector.h>
       
    45 
       
    46 //TESTED_CLASS=
       
    47 //TESTED_FILES=corelib/tools/qregexp.h corelib/tools/qregexp.cpp
       
    48 
       
    49 class tst_QVector : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_QVector() {}
       
    55     virtual ~tst_QVector() {}
       
    56 
       
    57 private slots:
       
    58     void outOfMemory();
       
    59 };
       
    60 
       
    61 int fooCtor;
       
    62 int fooDtor;
       
    63 
       
    64 struct Foo
       
    65 {
       
    66     int *p;
       
    67 
       
    68     Foo() { p = new int; ++fooCtor; }
       
    69     Foo(const Foo &other) { Q_UNUSED(other); p = new int; ++fooCtor; }
       
    70 
       
    71     void operator=(const Foo & /* other */) { }
       
    72 
       
    73     ~Foo() { delete p; ++fooDtor; }
       
    74 };
       
    75 
       
    76 void tst_QVector::outOfMemory()
       
    77 {
       
    78     fooCtor = 0;
       
    79     fooDtor = 0;
       
    80 
       
    81     const int N = 0x7fffffff / sizeof(Foo);
       
    82 
       
    83     {
       
    84         QVector<Foo> a;
       
    85 
       
    86         QSKIP("This test crashes on many of our machines.", SkipSingle);
       
    87         a.resize(N);
       
    88         if (a.size() == N) {
       
    89             QVERIFY(a.capacity() >= N);
       
    90             QCOMPARE(fooCtor, N);
       
    91             QCOMPARE(fooDtor, 0);
       
    92 
       
    93             for (int i = 0; i < N; i += 35000)
       
    94                 a[i] = Foo();
       
    95         } else {
       
    96             // this is the case we're actually testing
       
    97             QCOMPARE(a.size(), 0);
       
    98             QCOMPARE(a.capacity(), 0);
       
    99             QCOMPARE(fooCtor, 0);
       
   100             QCOMPARE(fooDtor, 0);
       
   101 
       
   102             a.resize(5);
       
   103             QCOMPARE(a.size(), 5);
       
   104             QVERIFY(a.capacity() >= 5);
       
   105             QCOMPARE(fooCtor, 5);
       
   106             QCOMPARE(fooDtor, 0);
       
   107 
       
   108             const int Prealloc = a.capacity();
       
   109             a.resize(Prealloc + 1);
       
   110             QCOMPARE(a.size(), Prealloc + 1);
       
   111             QVERIFY(a.capacity() >= Prealloc + 1);
       
   112             QCOMPARE(fooCtor, Prealloc + 6);
       
   113             QCOMPARE(fooDtor, 5);
       
   114 
       
   115             a.resize(0x10000000);
       
   116             QCOMPARE(a.size(), 0);
       
   117             QCOMPARE(a.capacity(), 0);
       
   118             QCOMPARE(fooCtor, Prealloc + 6);
       
   119             QCOMPARE(fooDtor, Prealloc + 6);
       
   120         }
       
   121     }
       
   122 
       
   123     fooCtor = 0;
       
   124     fooDtor = 0;
       
   125 
       
   126     {
       
   127         QVector<Foo> a(N);
       
   128         if (a.size() == N) {
       
   129             QVERIFY(a.capacity() >= N);
       
   130             QCOMPARE(fooCtor, N);
       
   131             QCOMPARE(fooDtor, 0);
       
   132 
       
   133             for (int i = 0; i < N; i += 35000)
       
   134                 a[i] = Foo();
       
   135         } else {
       
   136             // this is the case we're actually testing
       
   137             QCOMPARE(a.size(), 0);
       
   138             QCOMPARE(a.capacity(), 0);
       
   139             QCOMPARE(fooCtor, 0);
       
   140             QCOMPARE(fooDtor, 0);
       
   141         }
       
   142     }
       
   143 
       
   144     Foo foo;
       
   145 
       
   146     fooCtor = 0;
       
   147     fooDtor = 0;
       
   148 
       
   149     {
       
   150         QVector<Foo> a(N, foo);
       
   151         if (a.size() == N) {
       
   152             QVERIFY(a.capacity() >= N);
       
   153             QCOMPARE(fooCtor, N);
       
   154             QCOMPARE(fooDtor, 0);
       
   155 
       
   156             for (int i = 0; i < N; i += 35000)
       
   157                 a[i] = Foo();
       
   158         } else {
       
   159             // this is the case we're actually testing
       
   160             QCOMPARE(a.size(), 0);
       
   161             QCOMPARE(a.capacity(), 0);
       
   162             QCOMPARE(fooCtor, 0);
       
   163             QCOMPARE(fooDtor, 0);
       
   164         }
       
   165     }
       
   166 
       
   167     fooCtor = 0;
       
   168     fooDtor = 0;
       
   169 
       
   170     {
       
   171         QVector<Foo> a;
       
   172         a.resize(10);
       
   173         QCOMPARE(fooCtor, 10);
       
   174         QCOMPARE(fooDtor, 0);
       
   175 
       
   176         QVector<Foo> b(a);
       
   177         QCOMPARE(fooCtor, 10);
       
   178         QCOMPARE(fooDtor, 0);
       
   179 
       
   180         a.resize(N);
       
   181         if (a.size() == N) {
       
   182             QCOMPARE(fooCtor, N + 10);
       
   183         } else {
       
   184             QCOMPARE(a.size(), 0);
       
   185             QCOMPARE(a.capacity(), 0);
       
   186             QCOMPARE(fooCtor, 10);
       
   187             QCOMPARE(fooDtor, 0);
       
   188 
       
   189             QCOMPARE(b.size(), 10);
       
   190             QVERIFY(b.capacity() >= 10);
       
   191         }
       
   192     }
       
   193 
       
   194     {
       
   195         QVector<int> a;
       
   196         a.resize(10);
       
   197 
       
   198         QVector<int> b(a);
       
   199 
       
   200         a.resize(N);
       
   201         if (a.size() == N) {
       
   202             for (int i = 0; i < N; i += 60000)
       
   203                 a[i] = i;
       
   204         } else {
       
   205             QCOMPARE(a.size(), 0);
       
   206             QCOMPARE(a.capacity(), 0);
       
   207 
       
   208             QCOMPARE(b.size(), 10);
       
   209             QVERIFY(b.capacity() >= 10);
       
   210         }
       
   211 
       
   212         b.resize(N - 1);
       
   213         if (b.size() == N - 1) {
       
   214             for (int i = 0; i < N - 1; i += 60000)
       
   215                 b[i] = i;
       
   216         } else {
       
   217             QCOMPARE(b.size(), 0);
       
   218             QCOMPARE(b.capacity(), 0);
       
   219         }
       
   220     }
       
   221 }
       
   222 
       
   223 QTEST_APPLESS_MAIN(tst_QVector)
       
   224 #include "tst_qvector.moc"