Commit 752c16cf authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud
Browse files

Add debug operator and add template alias when preallocation size is

zero (dtkArrayDynamic). This alias is only valid using c++11.
parent 3e3481ef
......@@ -327,6 +327,20 @@ private:
bool isValidIterator(const iterator &i) const;
};
// ///////////////////////////////////////////////////////////////////
// Helpers
// ///////////////////////////////////////////////////////////////////
template <typename T, qlonglong PreallocSize> QDebug &operator<<(QDebug debug, const dtkArray<T, PreallocSize> &array);
// ///////////////////////////////////////////////////////////////////
// Alias when c++1 is available
// ///////////////////////////////////////////////////////////////////
#ifdef Q_COMPILER_TEMPLATE_ALIAS
template<typename T> using dtkArrayDynamic = dtkArray<T, 0>;
#endif
// ///////////////////////////////////////////////////////////////////
// Implementation
// ///////////////////////////////////////////////////////////////////
......
......@@ -244,7 +244,7 @@ template <typename T, qlonglong PreallocSize> inline dtkArray<T, PreallocSize>::
template <typename T, qlonglong PreallocSize> inline dtkArray<T, PreallocSize>::dtkArray(const T *values, qlonglong size) : isRawData(false)
{
Q_STATIC_ASSERT_X(PreallocSize > 0, "dtkArray PreallocSize must be greater than 0.");
Q_STATIC_ASSERT_X(PreallocSize >= 0, "dtkArray PreallocSize must be greater than 0.");
Q_ASSERT_X(size >= 0, "dtkArray::dtkArray", "Size must be greater than or equal to 0.");
if (size > PreallocSize) {
d = Data::allocate(size);
......@@ -1174,5 +1174,22 @@ template<typename T, qlonglong PreallocSize> inline QDataStream& operator>>(QDat
return s;
}
// ///////////////////////////////////////////////////////////////////
template <typename T, qlonglong PreallocSize> inline QDebug &operator<<(QDebug debug, const dtkArray<T, PreallocSize> &array)
{
const bool oldSetting = debug.autoInsertSpaces();
debug.nospace() << "dtkArray";
debug.nospace() << '(';
for (typename dtkArray<T, PreallocSize>::size_type i = 0; i < array.size(); ++i) {
if (i)
debug << ", ";
debug << array.at(i);
}
debug << ')';
debug.setAutoInsertSpaces(oldSetting);
return debug.maybeSpace();
}
//
// dtkArray.tpp ends here
......@@ -1526,7 +1526,7 @@ void dtkArrayTestCase::testSetRawData(void)
QCOMPARE(array.at(index), contents[index]);
}
QVERIFY(array.constData() == contents);
QString strings[] = {QLatin1String("foo"), QLatin1String("bar")};
dtkArray<QString> array2;
......@@ -1556,7 +1556,7 @@ void dtkArrayTestCase::testSetRawData(void)
QVERIFY(array2.constData() == strings);
for (int index = 0; index < 2; ++index) {
QCOMPARE(array2.at(index), strings[index]);
}
}
}
void dtkArrayTestCase::testFromRawData(void)
......@@ -1839,12 +1839,12 @@ void dtkArrayTestCase::testDataStream(void)
// QDataStream stream2(ba);
// stream2 >> vvar;
// }
// dtkArray<QString *> array;
// for (int index = 0; index < 10; ++index)
// array.append(new QString(QString::number(index)));
// QByteArray data;
// {
// QDataStream stream(&data, QIODevice::WriteOnly);
......@@ -1861,7 +1861,79 @@ void dtkArrayTestCase::testDataStream(void)
// QCOMPARE(*(array2.at(index)), *(array.at(index)));
// }
// }
#endif
}
// Test is performed when using c++11
void dtkArrayTestCase::testDynamicArray(void)
{
#ifdef Q_COMPILER_TEMPLATE_ALIAS
dtkArrayDynamic<double> array;
// Check the basic properties.
QVERIFY(array.isEmpty());
QVERIFY(!array.usePreallocation());
QCOMPARE(array.count(), 0LL);
QCOMPARE(array.size(), 0LL);
QCOMPARE(array.capacity(), 0LL);
QCOMPARE(array.preallocatedCapacity(), 0LL);
QVERIFY(array.constData() == 0);
QVERIFY(array.data() == 0);
// Add one element and check the basic properties again.
array.append(0.0);
QVERIFY(!array.isEmpty());
QVERIFY(!array.usePreallocation());
QCOMPARE(array.count(), 1LL);
QCOMPARE(array.size(), 1LL);
QVERIFY(array.capacity() > 1);
QVERIFY(array.constData() != 0);
QVERIFY(array.data() == array.constData());
//
array.append(1.0, 2.0);
array.append(3.0, 4.0, 5.0);
array.append(6.0, 7.0, 8.0, 9.0);
QVERIFY(array.size() == 10LL);
for (int i = 0; i < 10; ++i)
QCOMPARE(double(i), array.at(i));
dtkArray<double> array1;
array1 = array;
QVERIFY(array.isSharedWith(array1));
for (int i = 0; i < 10; ++i)
QCOMPARE(double(i), array1.at(i));
array[5] = 11.0;
QVERIFY(array.isDetached());
QVERIFY(array != array1);
array.resize(100);
QVERIFY(array.size() == 100LL);
double contents[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
7.0, 8.0, 9.0, 10.0, 11.0, 12.0};
array.setRawData(contents, 12);
QVERIFY(!array.isDetached());
array[10] = 1./10.;
QVERIFY(array.isDetached());
QVERIFY(array[10] != contents[10]);
array.setWritableRawData(contents, 12);
QVERIFY(array.isDetached());
array[10] = 1./10.;
QCOMPARE(array[10], contents[10]);
//
dtkArrayDynamic<double> array2(contents, 12);
QVERIFY(!array.isEmpty());
QCOMPARE(array.count(), 12LL);
array2 = array1;
#endif
}
......
......@@ -56,6 +56,7 @@ private slots:
void testFill(void);
void testZeroPrealloc(void);
void testDataStream(void);
void testDynamicArray(void);
private slots:
void cleanupTestCase(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment