tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    54 Q_DECLARE_METATYPE(QVector<QString>);
    54 Q_DECLARE_METATYPE(QVector<QString>);
    55 Q_DECLARE_METATYPE(QList<int>);
    55 Q_DECLARE_METATYPE(QList<int>);
    56 Q_DECLARE_METATYPE(QList<double>);
    56 Q_DECLARE_METATYPE(QList<double>);
    57 Q_DECLARE_METATYPE(QList<QString>);
    57 Q_DECLARE_METATYPE(QList<QString>);
    58 
    58 
    59 class tst_map: public QObject
    59 class tst_QtConcurrentMap: public QObject
    60 {
    60 {
    61     Q_OBJECT
    61     Q_OBJECT
    62 private slots:
    62 private slots:
    63     void map();
    63     void map();
    64     void blocking_map();
    64     void blocking_map();
   112     }
   112     }
   113 };
   113 };
   114 
   114 
   115 Q_DECLARE_METATYPE(QList<Number>);
   115 Q_DECLARE_METATYPE(QList<Number>);
   116 
   116 
   117 void tst_map::map()
   117 void tst_QtConcurrentMap::map()
   118 {
   118 {
   119     // functors take arguments by reference, modifying the sequence in place
   119     // functors take arguments by reference, modifying the sequence in place
   120     {
   120     {
   121         QList<int> list;
   121         QList<int> list;
   122         list << 1 << 2 << 3;
   122         list << 1 << 2 << 3;
   244         QtConcurrent::map(numberList, &Number::multiplyBy2);
   244         QtConcurrent::map(numberList, &Number::multiplyBy2);
   245     }
   245     }
   246 #endif
   246 #endif
   247 }
   247 }
   248 
   248 
   249 void tst_map::blocking_map()
   249 void tst_QtConcurrentMap::blocking_map()
   250 {
   250 {
   251     // functors take arguments by reference, modifying the sequence in place
   251     // functors take arguments by reference, modifying the sequence in place
   252     {
   252     {
   253         QList<int> list;
   253         QList<int> list;
   254         list << 1 << 2 << 3;
   254         list << 1 << 2 << 3;
   426     {
   426     {
   427         return string.toInt();
   427         return string.toInt();
   428     }
   428     }
   429 };
   429 };
   430 
   430 
   431 void tst_map::mapped()
   431 void tst_QtConcurrentMap::mapped()
   432 {
   432 {
   433     QList<int> list;
   433     QList<int> list;
   434     list << 1 << 2 << 3;
   434     list << 1 << 2 << 3;
   435     QLinkedList<int> linkedList;
   435     QLinkedList<int> linkedList;
   436     linkedList << 1 << 2 << 3;
   436     linkedList << 1 << 2 << 3;
   788                            .results();
   788                            .results();
   789         QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
   789         QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
   790     }
   790     }
   791 }
   791 }
   792 
   792 
   793 void tst_map::blocking_mapped()
   793 void tst_QtConcurrentMap::blocking_mapped()
   794 {
   794 {
   795     QList<int> list;
   795     QList<int> list;
   796     list << 1 << 2 << 3;
   796     list << 1 << 2 << 3;
   797     QLinkedList<int> linkedList;
   797     QLinkedList<int> linkedList;
   798     linkedList << 1 << 2 << 3;
   798     linkedList << 1 << 2 << 3;
  1242     {
  1242     {
  1243         return x * x;
  1243         return x * x;
  1244     }
  1244     }
  1245 };
  1245 };
  1246 
  1246 
  1247 void tst_map::mappedReduced()
  1247 void tst_QtConcurrentMap::mappedReduced()
  1248 {
  1248 {
  1249     QList<int> list;
  1249     QList<int> list;
  1250     list << 1 << 2 << 3;
  1250     list << 1 << 2 << 3;
  1251     QLinkedList<int> linkedList;
  1251     QLinkedList<int> linkedList;
  1252     linkedList << 1 << 2 << 3;
  1252     linkedList << 1 << 2 << 3;
  1623     }
  1623     }
  1624 
  1624 
  1625     // ### the same as above, with an initial result value
  1625     // ### the same as above, with an initial result value
  1626 }
  1626 }
  1627 
  1627 
  1628 void tst_map::blocking_mappedReduced()
  1628 void tst_QtConcurrentMap::blocking_mappedReduced()
  1629 {
  1629 {
  1630     QList<int> list;
  1630     QList<int> list;
  1631     list << 1 << 2 << 3;
  1631     list << 1 << 2 << 3;
  1632     QLinkedList<int> linkedList;
  1632     QLinkedList<int> linkedList;
  1633     linkedList << 1 << 2 << 3;
  1633     linkedList << 1 << 2 << 3;
  2008 {
  2008 {
  2009     QTest::qSleep(100);
  2009     QTest::qSleep(100);
  2010     return val;
  2010     return val;
  2011 }
  2011 }
  2012 
  2012 
  2013 void tst_map::assignResult()
  2013 void tst_QtConcurrentMap::assignResult()
  2014 {
  2014 {
  2015     const QList<int> startList = QList<int>() << 0 << 1 << 2;
  2015     const QList<int> startList = QList<int>() << 0 << 1 << 2;
  2016     QList<int> list = QtConcurrent::blockingMapped(startList, sleeper);
  2016     QList<int> list = QtConcurrent::blockingMapped(startList, sleeper);
  2017     QCOMPARE(list.at(0), 0);
  2017     QCOMPARE(list.at(0), 0);
  2018     QCOMPARE(list.at(1), 1);
  2018     QCOMPARE(list.at(1), 1);
  2075 };
  2075 };
  2076 
  2076 
  2077 Q_DECLARE_METATYPE(QVector<MemFnTester>);
  2077 Q_DECLARE_METATYPE(QVector<MemFnTester>);
  2078 Q_DECLARE_METATYPE(QList<MemFnTester>);
  2078 Q_DECLARE_METATYPE(QList<MemFnTester>);
  2079 
  2079 
  2080 void tst_map::functionOverloads()
  2080 void tst_QtConcurrentMap::functionOverloads()
  2081 {
  2081 {
  2082     QList<int> intList;
  2082     QList<int> intList;
  2083     const QList<int> constIntList;
  2083     const QList<int> constIntList;
  2084     QList<MemFnTester> classList;
  2084     QList<MemFnTester> classList;
  2085     const QList<MemFnTester> constMemFnTesterList;
  2085     const QList<MemFnTester> constMemFnTesterList;
  2157 void fastReduce(int &result, const InstanceCounter&)
  2157 void fastReduce(int &result, const InstanceCounter&)
  2158 {
  2158 {
  2159     ++result;
  2159     ++result;
  2160 }
  2160 }
  2161 
  2161 
  2162 void tst_map::throttling()
  2162 void tst_QtConcurrentMap::throttling()
  2163 {
  2163 {
  2164     const int itemcount = 100;
  2164     const int itemcount = 100;
  2165     const int allowedTemporaries = QThread::idealThreadCount() * 40;
  2165     const int allowedTemporaries = QThread::idealThreadCount() * 40;
  2166 
  2166 
  2167     {
  2167     {
  2206 {
  2206 {
  2207     Q_UNUSED(e);
  2207     Q_UNUSED(e);
  2208     throw QtConcurrent::Exception();
  2208     throw QtConcurrent::Exception();
  2209 }
  2209 }
  2210 
  2210 
  2211 void tst_map::exceptions()
  2211 void tst_QtConcurrentMap::exceptions()
  2212 {
  2212 {
  2213     bool caught = false;
  2213     bool caught = false;
  2214     try  {
  2214     try  {
  2215         QList<int> list = QList<int>() << 1 << 2 << 3;
  2215         QList<int> list = QList<int>() << 1 << 2 << 3;
  2216         QtConcurrent::map(list, throwMapper).waitForFinished();
  2216         QtConcurrent::map(list, throwMapper).waitForFinished();
  2226 {
  2226 {
  2227     QTest::qWait(1);
  2227     QTest::qWait(1);
  2228     return i;
  2228     return i;
  2229 }
  2229 }
  2230 
  2230 
  2231 void tst_map::incrementalResults()
  2231 void tst_QtConcurrentMap::incrementalResults()
  2232 {
  2232 {
  2233     const int count = 200;
  2233     const int count = 200;
  2234     QList<int> ints; 
  2234     QList<int> ints; 
  2235     for (int i=0; i < count; ++i)
  2235     for (int i=0; i < count; ++i)
  2236         ints << i;
  2236         ints << i;
  2254 
  2254 
  2255 /*
  2255 /*
  2256     Test that mapped does not cause deep copies when holding
  2256     Test that mapped does not cause deep copies when holding
  2257     references to Qt containers.
  2257     references to Qt containers.
  2258 */
  2258 */
  2259 void tst_map::noDetatch()
  2259 void tst_QtConcurrentMap::noDetatch()
  2260 {
  2260 {
  2261     {
  2261     {
  2262         QList<int> l = QList<int>() << 1;
  2262         QList<int> l = QList<int>() << 1;
  2263         QVERIFY(l.isDetached());
  2263         QVERIFY(l.isDetached());
  2264 
  2264 
  2297         QVERIFY(ll.isDetached() == false);
  2297         QVERIFY(ll.isDetached() == false);
  2298     }
  2298     }
  2299 
  2299 
  2300 }
  2300 }
  2301 
  2301 
  2302 void tst_map::stlContainers()
  2302 void tst_QtConcurrentMap::stlContainers()
  2303 {
  2303 {
  2304 #ifdef QT_NO_STL
  2304 #ifdef QT_NO_STL
  2305     QSKIP("Qt compiled without STL support", SkipAll);
  2305     QSKIP("Qt compiled without STL support", SkipAll);
  2306 #else
  2306 #else
  2307     std::vector<int> vector;
  2307     std::vector<int> vector;
  2329     return InstanceCounter(ic);
  2329     return InstanceCounter(ic);
  2330 };
  2330 };
  2331 
  2331 
  2332 // Verify that held results are deleted when a future is
  2332 // Verify that held results are deleted when a future is
  2333 // assigned over with operator ==
  2333 // assigned over with operator ==
  2334 void tst_map::qFutureAssignmentLeak()
  2334 void tst_QtConcurrentMap::qFutureAssignmentLeak()
  2335 {
  2335 {
  2336     currentInstanceCount = 0;
  2336     currentInstanceCount = 0;
  2337     peakInstanceCount = 0;
  2337     peakInstanceCount = 0;
  2338     QFuture<InstanceCounter> future;
  2338     QFuture<InstanceCounter> future;
  2339     {
  2339     {
  2368 void add(int &result, const int &sum)
  2368 void add(int &result, const int &sum)
  2369 {
  2369 {
  2370     result += sum;
  2370     result += sum;
  2371 }
  2371 }
  2372 
  2372 
  2373 void tst_map::stressTest()
  2373 void tst_QtConcurrentMap::stressTest()
  2374 {
  2374 {
  2375     const int listSize = 1000;
  2375     const int listSize = 1000;
  2376     const int sum = (listSize - 1) * (listSize / 2);
  2376     const int sum = (listSize - 1) * (listSize / 2);
  2377     QList<int> list;
  2377     QList<int> list;
  2378 
  2378 
  2397         for (int j = 0; j < listSize; ++j)
  2397         for (int j = 0; j < listSize; ++j)
  2398             QCOMPARE(list.at(j), i + j + 1);
  2398             QCOMPARE(list.at(j), i + j + 1);
  2399     }
  2399     }
  2400 }
  2400 }
  2401 
  2401 
  2402 QTEST_MAIN(tst_map)
  2402 QTEST_MAIN(tst_QtConcurrentMap)
  2403 
  2403 
  2404 #else
  2404 #else
  2405 
  2405 
  2406 void tst_map::map() {}
  2406 void tst_QtConcurrentMap::map() {}
  2407 void tst_map::blocking_map() {}
  2407 void tst_QtConcurrentMap::blocking_map() {}
  2408 void tst_map::mapped() {}
  2408 void tst_QtConcurrentMap::mapped() {}
  2409 void tst_map::blocking_mapped() {}
  2409 void tst_QtConcurrentMap::blocking_mapped() {}
  2410 void tst_map::mappedReduced() {}
  2410 void tst_QtConcurrentMap::mappedReduced() {}
  2411 void tst_map::blocking_mappedReduced() {}
  2411 void tst_QtConcurrentMap::blocking_mappedReduced() {}
  2412 void tst_map::assignResult() {}
  2412 void tst_QtConcurrentMap::assignResult() {}
  2413 void tst_map::functionOverloads() {}
  2413 void tst_QtConcurrentMap::functionOverloads() {}
  2414 #ifndef QT_NO_EXCEPTIONS
  2414 #ifndef QT_NO_EXCEPTIONS
  2415 void tst_map::exceptions() {}
  2415 void tst_QtConcurrentMap::exceptions() {}
  2416 #endif
  2416 #endif
  2417 void tst_map::incrementalResults() {}
  2417 void tst_QtConcurrentMap::incrementalResults() {}
  2418 void tst_map::stressTest() {}
  2418 void tst_QtConcurrentMap::stressTest() {}
  2419 void tst_map::throttling() {}
  2419 void tst_QtConcurrentMap::throttling() {}
  2420 void tst_map::stlContainers() {}
  2420 void tst_QtConcurrentMap::stlContainers() {}
  2421 void tst_map::noDetatch() {}
  2421 void tst_QtConcurrentMap::noDetatch() {}
  2422 
  2422 
  2423 QTEST_NOOP_MAIN
  2423 QTEST_NOOP_MAIN
  2424 
  2424 
  2425 #endif
  2425 #endif
  2426 
  2426