summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-08-29 08:23:48 -0700
committerGitHub <noreply@github.com>2022-08-29 08:23:48 -0700
commit408a15ba4e76879a512ebaf80d93d44145a7a27f (patch)
tree5e9a934de373b5ce82cba63012de0a92277243e6
parent7fb44812fa307a5924e5ada58b4015de6736cce3 (diff)
downloadbinaryen-408a15ba4e76879a512ebaf80d93d44145a7a27f.tar.gz
binaryen-408a15ba4e76879a512ebaf80d93d44145a7a27f.tar.bz2
binaryen-408a15ba4e76879a512ebaf80d93d44145a7a27f.zip
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.
-rw-r--r--src/support/small_vector.h2
-rw-r--r--test/example/small_vector.cpp35
2 files changed, 37 insertions, 0 deletions
diff --git a/src/support/small_vector.h b/src/support/small_vector.h
index e2c865b8f..7c03ae02b 100644
--- a/src/support/small_vector.h
+++ b/src/support/small_vector.h
@@ -116,6 +116,8 @@ public:
usedFixed = std::min(N, newSize);
if (newSize > N) {
flexible.resize(newSize - N);
+ } else {
+ flexible.clear();
}
}
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<typename T> 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<SmallVector<int, 0>>(0);
test<SmallVector<int, 1>>(1);
test<SmallVector<int, 2>>(2);
+ test<SmallVector<int, 3>>(3);
test<SmallVector<int, 10>>(10);
std::cout << "ok.\n";
}