tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 29 b72c6db6890b
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 
    42 
    43 #include <QtTest/QtTest>
    43 #include <QtTest/QtTest>
    44 #include <qvarlengtharray.h>
    44 #include <qvarlengtharray.h>
       
    45 #include <qvariant.h>
    45 
    46 
    46 const int N = 1;
    47 const int N = 1;
    47 
    48 
    48 //TESTED_CLASS=
    49 //TESTED_CLASS=
    49 //TESTED_FILES=
    50 //TESTED_FILES=
    59 private slots:
    60 private slots:
    60     void append();
    61     void append();
    61     void removeLast();
    62     void removeLast();
    62     void oldTests();
    63     void oldTests();
    63     void task214223();
    64     void task214223();
       
    65     void QTBUG6718_resize();
    64 };
    66 };
    65 
    67 
    66 int fooCtor = 0;
    68 int fooCtor = 0;
    67 int fooDtor = 0;
    69 int fooDtor = 0;
    68 
    70 
    69 struct Foo
    71 struct Foo
    70 {
    72 {
    71     int *p;
    73     int *p;
    72 
    74 
    73     Foo() { p = new int; ++fooCtor; }
    75     Foo() { p = new int; ++fooCtor; }
    74     Foo(const Foo &other) { p = new int; ++fooCtor; }
    76     Foo(const Foo &/*other*/) { p = new int; ++fooCtor; }
    75 
    77 
    76     void operator=(const Foo & /* other */) { }
    78     void operator=(const Foo & /* other */) { }
    77 
    79 
    78     ~Foo() { delete p; ++fooDtor; }
    80     ~Foo() { delete p; ++fooDtor; }
    79 };
    81 };
   242 {
   244 {
   243     //creating a QVarLengthArray of the same size as the prealloc size
   245     //creating a QVarLengthArray of the same size as the prealloc size
   244     // will make the next call to append(const T&) corrupt the memory
   246     // will make the next call to append(const T&) corrupt the memory
   245     // you should get a segfault pretty soon after that :-)
   247     // you should get a segfault pretty soon after that :-)
   246     QVarLengthArray<float, 1> d(1);
   248     QVarLengthArray<float, 1> d(1);
   247     for (int i=0; i<30; i++) 
   249     for (int i=0; i<30; i++)
   248         d.append(i);
   250         d.append(i);
       
   251 }
       
   252 
       
   253 void tst_QVarLengthArray::QTBUG6718_resize()
       
   254 {
       
   255     //MOVABLE
       
   256     {
       
   257         QVarLengthArray<QVariant,1> values(1);
       
   258         QCOMPARE(values.size(), 1);
       
   259         values[0] = 1;
       
   260         values.resize(2);
       
   261         QCOMPARE(values[1], QVariant());
       
   262         QCOMPARE(values[0], QVariant(1));
       
   263         values[1] = 2;
       
   264         QCOMPARE(values[1], QVariant(2));
       
   265         QCOMPARE(values.size(), 2);
       
   266     }
       
   267 
       
   268     //POD
       
   269     {
       
   270         QVarLengthArray<int,1> values(1);
       
   271         QCOMPARE(values.size(), 1);
       
   272         values[0] = 1;
       
   273         values.resize(2);
       
   274         QCOMPARE(values[0], 1);
       
   275         values[1] = 2;
       
   276         QCOMPARE(values[1], 2);
       
   277         QCOMPARE(values.size(), 2);
       
   278     }
       
   279 
       
   280     //COMPLEX
       
   281     {
       
   282         QVarLengthArray<QVarLengthArray<QString, 15>,1> values(1);
       
   283         QCOMPARE(values.size(), 1);
       
   284         values[0].resize(10);
       
   285         values.resize(2);
       
   286         QCOMPARE(values[1].size(), 0);
       
   287         QCOMPARE(values[0].size(), 10);
       
   288         values[1].resize(20);
       
   289         QCOMPARE(values[1].size(), 20);
       
   290         QCOMPARE(values.size(), 2);
       
   291     }
   249 }
   292 }
   250 
   293 
   251 QTEST_APPLESS_MAIN(tst_QVarLengthArray)
   294 QTEST_APPLESS_MAIN(tst_QVarLengthArray)
   252 #include "tst_qvarlengtharray.moc"
   295 #include "tst_qvarlengtharray.moc"