summaryrefslogtreecommitdiffstats
path: root/tests/auto/collections/tst_collections.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/collections/tst_collections.cpp')
-rw-r--r--tests/auto/collections/tst_collections.cpp212
1 files changed, 212 insertions, 0 deletions
diff --git a/tests/auto/collections/tst_collections.cpp b/tests/auto/collections/tst_collections.cpp
index 82ec0fa07d..8a0ca3eca2 100644
--- a/tests/auto/collections/tst_collections.cpp
+++ b/tests/auto/collections/tst_collections.cpp
@@ -166,6 +166,9 @@ private slots:
void forwardDeclared();
void alignment();
void QTBUG13079_collectionInsideCollection();
+
+ void foreach_2();
+ void insert_remove_loop();
};
struct LargeStatic {
@@ -3707,5 +3710,214 @@ void tst_Collections::QTBUG13079_collectionInsideCollection()
#endif
}
+template<class Container> void foreach_test_arrays(const Container &container)
+{
+ typedef typename Container::value_type T;
+ int i = 0;
+ QSet <T> set;
+ foreach(const T & val, container) {
+ QVERIFY( val == container[i] );
+ set << val;
+ i++;
+ }
+ QCOMPARE(set.count(), container.count());
+
+ //modify the container while iterating.
+ Container c2 = container;
+ Container c3;
+ i = 0;
+ foreach (T val, c2) {
+ c3 << val;
+ c2.insert((i * 89) % c2.size(), T() );
+ QVERIFY( val == container.at(i) );
+ val = T();
+ i++;
+ }
+ QVERIFY(c3 == container);
+}
+
+
+void tst_Collections::foreach_2()
+{
+ QStringList strlist = QString::fromLatin1("a,b,c,d,e,f,g,h,ih,kl,mn,op,qr,st,uvw,xyz").split(",");
+ foreach_test_arrays(strlist);
+ foreach_test_arrays(QList<QString>(strlist));
+ foreach_test_arrays(strlist.toVector());
+
+ QList<int> intlist;
+ intlist << 1 << 2 << 3 << 4 <<5 << 6 << 7 << 8 << 9;
+ foreach_test_arrays(intlist);
+ foreach_test_arrays(intlist.toVector());
+
+ QVarLengthArray<int> varl1;
+ QVarLengthArray<int, 3> varl2;
+ QVarLengthArray<int, 10> varl3;
+ foreach(int i, intlist) {
+ varl1 << i;
+ varl2 << i;
+ varl3 << i;
+ }
+ QCOMPARE(varl1.count(), intlist.count());
+ QCOMPARE(varl2.count(), intlist.count());
+ QCOMPARE(varl3.count(), intlist.count());
+ foreach_test_arrays(varl1);
+ foreach_test_arrays(varl2);
+ foreach_test_arrays(varl3);
+
+ QVarLengthArray<QString> varl4;
+ QVarLengthArray<QString, 3> varl5;
+ QVarLengthArray<QString, 18> varl6;
+ foreach(const QString &str, strlist) {
+ varl4 << str;
+ varl5 << str;
+ varl6 << str;
+ }
+ QCOMPARE(varl4.count(), strlist.count());
+ QCOMPARE(varl5.count(), strlist.count());
+ QCOMPARE(varl6.count(), strlist.count());
+ foreach_test_arrays(varl4);
+ foreach_test_arrays(varl5);
+ foreach_test_arrays(varl6);
+}
+
+struct IntOrString
+{
+ int val;
+ IntOrString(int v) : val(v) { }
+ IntOrString(const QString &v) : val(v.toInt()) { }
+ operator int() { return val; }
+ operator QString() { return QString::number(val); }
+#ifndef QT_NO_STL
+ operator std::string() { return QString::number(val).toStdString(); }
+ IntOrString(const std::string &v) : val(QString::fromStdString(v).toInt()) { }
+#endif
+};
+
+template<class Container> void insert_remove_loop_impl()
+{
+ typedef typename Container::value_type T;
+ Container t;
+ t.append(T(IntOrString(1)));
+ t << (T(IntOrString(2)));
+ t += (T(IntOrString(3)));
+ t.prepend(T(IntOrString(4)));
+ t.insert(2, 3 , T(IntOrString(5)));
+ t.insert(4, T(IntOrString(6)));
+ t.insert(t.begin() + 2, T(IntOrString(7)));
+ t.insert(t.begin() + 5, 3, T(IntOrString(8)));
+ int expect1[] = { 4 , 1 , 7, 5 , 5 , 8, 8, 8, 6, 5, 2 , 3 };
+ QCOMPARE(size_t(t.count()), sizeof(expect1)/sizeof(int));
+ for (int i = 0; i < t.count(); i++) {
+ QCOMPARE(t[i], T(IntOrString(expect1[i])));
+ }
+
+ Container compare_test1 = t;
+ t.replace(5, T(IntOrString(9)));
+ Container compare_test2 = t;
+ QVERIFY(!(compare_test1 == t));
+ QVERIFY( (compare_test1 != t));
+ QVERIFY( (compare_test2 == t));
+ QVERIFY(!(compare_test2 != t));
+ t.remove(7);
+ t.remove(2, 3);
+ int expect2[] = { 4 , 1 , 9, 8, 6, 5, 2 , 3 };
+ QCOMPARE(size_t(t.count()), sizeof(expect2)/sizeof(int));
+ for (int i = 0; i < t.count(); i++) {
+ QCOMPARE(t[i], T(IntOrString(expect2[i])));
+ }
+
+ for (typename Container::iterator it = t.begin(); it != t.end(); ) {
+ if ( int(IntOrString(*it)) % 2 )
+ ++it;
+ else
+ it = t.erase(it);
+ }
+
+ int expect3[] = { 1 , 9, 5, 3 };
+ QCOMPARE(size_t(t.count()), sizeof(expect3)/sizeof(int));
+ for (int i = 0; i < t.count(); i++) {
+ QCOMPARE(t[i], T(IntOrString(expect3[i])));
+ }
+
+ t.erase(t.begin() + 1, t.end() - 1);
+
+ int expect4[] = { 1 , 3 };
+ QCOMPARE(size_t(t.count()), sizeof(expect4)/sizeof(int));
+ for (int i = 0; i < t.count(); i++) {
+ QCOMPARE(t[i], T(IntOrString(expect4[i])));
+ }
+
+ t << T(IntOrString(10)) << T(IntOrString(11)) << T(IntOrString(12)) << T(IntOrString(13));
+ t << T(IntOrString(14)) << T(IntOrString(15)) << T(IntOrString(16)) << T(IntOrString(17));
+ t << T(IntOrString(18)) << T(IntOrString(19)) << T(IntOrString(20)) << T(IntOrString(21));
+ for (typename Container::iterator it = t.begin(); it != t.end(); ++it) {
+ int iv = int(IntOrString(*it));
+ if ( iv % 2 ) {
+ it = t.insert(it, T(IntOrString(iv * iv)));
+ it = t.insert(it + 2, T(IntOrString(iv * iv + 1)));
+ }
+ }
+
+ int expect5[] = { 1, 1, 2, 3*3, 3, 3*3+1, 10, 11*11, 11, 11*11+1, 12 , 13*13, 13, 13*13+1, 14,
+ 15*15, 15, 15*15+1, 16 , 17*17, 17, 17*17+1 ,18 , 19*19, 19, 19*19+1, 20, 21*21, 21, 21*21+1 };
+ QCOMPARE(size_t(t.count()), sizeof(expect5)/sizeof(int));
+ for (int i = 0; i < t.count(); i++) {
+ QCOMPARE(t[i], T(IntOrString(expect5[i])));
+ }
+}
+
+
+//Add insert(int, int, T) so it has the same interface as QVector and QVarLengthArray for the test.
+template<typename T>
+struct ExtList : QList<T> {
+ using QList<T>::insert;
+ void insert(int before, int n, const T&x) {
+ while (n--) {
+ this->insert(before, x );
+ }
+ }
+ void insert(typename QList<T>::iterator before, int n, const T&x) {
+ while (n--) {
+ before = this->insert(before, x);
+ }
+ }
+
+ void remove(int i) {
+ this->removeAt(i);
+ }
+ void remove(int i, int n) {
+ while (n--) {
+ this->removeAt(i);
+ }
+ }
+};
+
+void tst_Collections::insert_remove_loop()
+{
+ insert_remove_loop_impl<ExtList<int> >();
+ insert_remove_loop_impl<ExtList<QString> >();
+ insert_remove_loop_impl<QVector<int> >();
+ insert_remove_loop_impl<QVector<QString> >();
+ insert_remove_loop_impl<QVarLengthArray<int> >();
+ insert_remove_loop_impl<QVarLengthArray<QString> >();
+ insert_remove_loop_impl<QVarLengthArray<int, 10> >();
+ insert_remove_loop_impl<QVarLengthArray<QString, 10> >();
+ insert_remove_loop_impl<QVarLengthArray<int, 3> >();
+ insert_remove_loop_impl<QVarLengthArray<QString, 3> >();
+ insert_remove_loop_impl<QVarLengthArray<int, 15> >();
+ insert_remove_loop_impl<QVarLengthArray<QString, 15> >();
+
+#ifndef QT_NO_STL
+ insert_remove_loop_impl<ExtList<std::string> >();
+ insert_remove_loop_impl<QVector<std::string> >();
+ insert_remove_loop_impl<QVarLengthArray<std::string> >();
+ insert_remove_loop_impl<QVarLengthArray<std::string, 10> >();
+ insert_remove_loop_impl<QVarLengthArray<std::string, 3> >();
+ insert_remove_loop_impl<QVarLengthArray<std::string, 15> >();
+#endif
+}
+
+
+
QTEST_APPLESS_MAIN(tst_Collections)
#include "tst_collections.moc"