diff options
author | Dennis Oberst <dennis.oberst@qt.io> | 2023-06-01 13:45:34 +0200 |
---|---|---|
committer | Dennis Oberst <dennis.oberst@qt.io> | 2023-06-01 18:39:25 +0000 |
commit | 60f739f51f28e602a3a89448d489fc13babccf35 (patch) | |
tree | 2fc22b9ca41d9e59484cd6f67728b446d48cd94b | |
parent | 63a0f4dc3b9cf5d091877c361e096ff176c52138 (diff) |
tst_ContainerApiSymmetry: make assign_impl() more robust
Refactor the 'CHECK' macro to eliminate the capacity check and
explicitly verify that no reallocation occurred. The previous
implementation had to pass constants to suppress the issue arising
from differing growth rates between implementations.
Additionally, improve the 'std::stringstream' versions of the test
by incorporating the correct values. In the previous implementation,
the usage of:
auto tData = V(9);
~~~
std::stringstream ss("9 9 ");
had several issues. Firstly, it used the wrong test data since the
container's value_type of '(char) 9' resulted in a tab character '\t',
which was not accurately reflected in the stringstream assignment.
Secondly, this value caused problems in how stringstreams interprets it.
To address these issues, let's make the following improvements:
1. Use a default test value of 65 instead of (char) 9. This value, which
represents the character 'A', is less likely to cause errors and is more
intuitive.
2. Use the tData variable for the assignments in the stringstream. This
ensures that the correct data from the container is used.
3. Change the test value between the assign() calls to verify that the
container's contents are successfully overwritten.
These changes ensure, that the test cases are more accurate and
reliable.
Amends: 3b0536bbe8d6782f79e1bcc2b4f1925547c14c0b.
Change-Id: I9441c4818106bf93e93a1a5d2d2d54c89d80e7b0
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
-rw-r--r-- | tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp index 4c6c0e0671..c0fab0cc91 100644 --- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp +++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp @@ -772,9 +772,8 @@ void tst_ContainerApiSymmetry::resize_impl() const template <typename Container> void tst_ContainerApiSymmetry::assign_impl() const { -#define CHECK(Arr, ComparisonData, Sz_n, Sz_e, Cap_n, Cap_e) \ +#define CHECK(Arr, ComparisonData, Sz_n, Sz_e) \ QCOMPARE(Sz_n, Sz_e); \ - QCOMPARE(Cap_n, Cap_e); \ for (const auto &e : Arr) \ QCOMPARE(e, ComparisonData) \ /*end*/ @@ -792,56 +791,64 @@ void tst_ContainerApiSymmetry::assign_impl() const /* end */ using V = typename Container::value_type; using S = typename Container::size_type; - auto tData = V(9); + auto tData = V(65); { // fill version auto c = make<Container>(4); const S oldCapacity = c.capacity(); RET_CHECK(c.assign(4, tData)); - CHECK(c, tData, c.size(), S(4), c.capacity(), oldCapacity); + CHECK(c, tData, c.size(), S(4)); + QCOMPARE_EQ(c.capacity(), oldCapacity); - c.assign(8, tData); - CHECK(c, tData, c.size(), S(8), c.capacity(), std::max(oldCapacity, S(8))); + tData = V(66); + c.assign(8, tData); // may reallocate + CHECK(c, tData, c.size(), S(8)); + const S grownCapacity = c.capacity(); c.assign(0, tData); - CHECK(c, tData, c.size(), S(0), c.capacity(), std::max(oldCapacity, S(8))); + CHECK(c, tData, c.size(), S(0)); + QCOMPARE_EQ(c.capacity(), grownCapacity); } { // range version for non input iterator auto c = make<Container>(4); - const S oldCapacity = c.capacity(); auto iter = make<Container>(1); iter.assign(8, tData); - RET_CHECK(c.assign(iter.begin(), iter.end())); - CHECK(c, tData, c.size(), S(8), c.capacity(), std::max(oldCapacity, S(8))); + RET_CHECK(c.assign(iter.begin(), iter.end())); // may reallocate + CHECK(c, tData, c.size(), S(8)); + const S oldCapacity = c.capacity(); c.assign(iter.begin(), iter.begin()); - QCOMPARE_EQ(c.size(), S(0)); - QCOMPARE_EQ(c.capacity(), std::max(oldCapacity, S(8))); + CHECK(c, tData, c.size(), S(0)); + QCOMPARE_EQ(c.capacity(), oldCapacity); } { // range version for input iterator auto c = make<Container>(4); const S oldCapacity = c.capacity(); - std::stringstream ss("9 9 "); + std::stringstream ss; + ss << tData << ' ' << tData << ' '; RET_CHECK(c.assign(std::istream_iterator<V>{ss}, std::istream_iterator<V>{})); - CHECK(c, tData, c.size(), S(2), c.capacity(), oldCapacity); + CHECK(c, tData, c.size(), S(2)); + QCOMPARE_EQ(c.capacity(), oldCapacity); ss.str(""); ss.clear(); - ss << "9 9 9 9 "; + tData = V(66); + ss << tData << ' ' << tData << ' ' << tData << ' ' << tData << ' '; c.assign(std::istream_iterator<V>{ss}, std::istream_iterator<V>{}); - CHECK(c, tData, c.size(), S(4), c.capacity(), oldCapacity); + CHECK(c, tData, c.size(), S(4)); + QCOMPARE_EQ(c.capacity(), oldCapacity); ss.str(""); ss.clear(); - ss << "9 9 9 9 9 9 9 "; - c.assign(std::istream_iterator<V>{ss}, std::istream_iterator<V>{}); - // We cannot check the capacity here because growth rates differ between implementations. - // Pass a constant: - CHECK(c, tData, c.size(), S(7), /*c.capacity()*/S(8), S(8)); + tData = V(67); + ss << tData << ' ' << tData << ' ' << tData << ' ' << tData << ' ' + << tData << ' ' << tData << ' ' << tData << ' '; + c.assign(std::istream_iterator<V>{ss}, std::istream_iterator<V>{}); // may reallocate + CHECK(c, tData, c.size(), S(7)); } { // initializer-list version @@ -849,7 +856,8 @@ void tst_ContainerApiSymmetry::assign_impl() const const S oldCapacity = c.capacity(); std::initializer_list<V> list = {tData, tData, tData}; RET_CHECK(c.assign(list)); - CHECK(c, tData, c.size(), S(3), c.capacity(), oldCapacity); + CHECK(c, tData, c.size(), S(3)); + QCOMPARE_EQ(c.capacity(), oldCapacity); } #undef RET_CHECK |