From 408a15ba4e76879a512ebaf80d93d44145a7a27f Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 29 Aug 2022 08:23:48 -0700 Subject: Fix SmallVector's resize() method (#4979) A resize from a large amount to a small amount would sometimes not clear the flexible storage, if we used it before but not after. --- test/example/small_vector.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'test') diff --git a/test/example/small_vector.cpp b/test/example/small_vector.cpp index 29d964997..a1e394e23 100644 --- a/test/example/small_vector.cpp +++ b/test/example/small_vector.cpp @@ -66,12 +66,47 @@ template void test(size_t N) { t.reserve(t.capacity() + 100); assert(t.capacity() >= N + 100); } + { + // Test resizing. + T t; + + assert(t.empty()); + t.resize(1); + assert(t.size() == 1); + t.resize(2); + assert(t.size() == 2); + t.resize(3); + assert(t.size() == 3); + t.resize(6); + assert(t.size() == 6); + + // Now go in reverse. + t.resize(6); + assert(t.size() == 6); + t.resize(3); + assert(t.size() == 3); + t.resize(2); + assert(t.size() == 2); + t.resize(1); + assert(t.size() == 1); + + // Test a big leap from nothing (rather than gradual increase as before). + t.clear(); + assert(t.empty()); + t.resize(6); + assert(t.size() == 6); + t.resize(2); + assert(t.size() == 2); + t.clear(); + assert(t.empty()); + } } int main() { test>(0); test>(1); test>(2); + test>(3); test>(10); std::cout << "ok.\n"; } -- cgit v1.2.3