From 78eb61b38eb7232beb5ffdb63adbd2aaaff16c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dziepak?= Date: Mon, 31 Aug 2015 09:38:53 +0200 Subject: [PATCH] tests: add test for managed_vector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Dziepak --- configure.py | 3 +- tests/managed_vector_test.cc | 142 +++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 tests/managed_vector_test.cc diff --git a/configure.py b/configure.py index b2240342df..47711a403d 100755 --- a/configure.py +++ b/configure.py @@ -152,6 +152,7 @@ urchin_tests = [ 'tests/murmur_hash_test', 'tests/allocation_strategy_test', 'tests/logalloc_test', + 'tests/managed_vector_test', 'tests/crc_test', ] @@ -401,7 +402,7 @@ deps = { for t in urchin_tests: deps[t] = urchin_tests_dependencies + [t + '.cc'] - if 'types_test' not in t and 'keys_test' not in t and 'partitioner_test' not in t and 'map_difference_test' not in t and 'frozen_mutation_test' not in t and 'perf_mutation' not in t and 'cartesian_product_test' not in t and 'perf_hash' not in t and 'perf_cql_parser' not in t and 'message' not in t and 'perf_simple_query' not in t and 'serialization' not in t and t != 'tests/gossip' and 'compound_test' not in t and 'range_test' not in t and 'crc_test' not in t and 'perf_sstable' not in t: + if 'types_test' not in t and 'keys_test' not in t and 'partitioner_test' not in t and 'map_difference_test' not in t and 'frozen_mutation_test' not in t and 'perf_mutation' not in t and 'cartesian_product_test' not in t and 'perf_hash' not in t and 'perf_cql_parser' not in t and 'message' not in t and 'perf_simple_query' not in t and 'serialization' not in t and t != 'tests/gossip' and 'compound_test' not in t and 'range_test' not in t and 'crc_test' not in t and 'perf_sstable' not in t and 'managed_vector_test' not in t: deps[t] += urchin_tests_seastar_deps deps['tests/sstable_test'] += ['tests/sstable_datafile_test.cc'] diff --git a/tests/managed_vector_test.cc b/tests/managed_vector_test.cc new file mode 100644 index 0000000000..69efc8b1f8 --- /dev/null +++ b/tests/managed_vector_test.cc @@ -0,0 +1,142 @@ +/* + * Copyright 2015 Cloudius Systems + */ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE core + +#include + +#include "utils/managed_vector.hh" +#include "utils/logalloc.hh" + +static constexpr unsigned count = 125; + +template +void fill(Vector& vec) { + BOOST_CHECK(vec.empty()); + for (unsigned i = 0; i < count; i++) { + vec.emplace_back(i); + } + BOOST_CHECK(!vec.empty()); + BOOST_CHECK_EQUAL(vec.size(), count); + BOOST_CHECK_GE(vec.capacity(), vec.size()); +} + +template +void verify_filled(const Vector& vec) { + unsigned idx = 0; + for (auto&& v : vec) { + BOOST_CHECK_EQUAL(v, idx); + BOOST_CHECK_EQUAL(vec[idx], idx); + idx++; + } + BOOST_CHECK_EQUAL(idx, count); +} + +template +void verify_empty(const Vector& vec) { + BOOST_CHECK_EQUAL(vec.size(), 0); + BOOST_CHECK(vec.empty()); + for (auto&& v : vec) { + (void)v; + BOOST_FAIL("vec should be empty"); + } +} + +BOOST_AUTO_TEST_CASE(test_emplace) { + managed_vector vec; + fill(vec); + verify_filled(vec); + + vec.clear(); + verify_empty(vec); +} + +BOOST_AUTO_TEST_CASE(test_copy) { + managed_vector vec; + + managed_vector vec2(vec); + verify_empty(vec2); + + fill(vec); + + managed_vector vec3(vec); + verify_filled(vec); + verify_filled(vec3); +} + +BOOST_AUTO_TEST_CASE(test_move) { + managed_vector vec; + + managed_vector vec2(std::move(vec)); + verify_empty(vec2); + + fill(vec); + + managed_vector vec3(std::move(vec)); + verify_empty(vec); + verify_filled(vec3); +} + +BOOST_AUTO_TEST_CASE(test_erase) { + managed_vector vec; + fill(vec); + + vec.erase(vec.begin()); + + unsigned idx = 1; + for (auto&& v : vec) { + BOOST_CHECK_EQUAL(v, idx); + BOOST_CHECK_EQUAL(vec[idx - 1], idx); + idx++; + } + BOOST_CHECK_EQUAL(idx, count); +} + +BOOST_AUTO_TEST_CASE(test_resize_up) { + managed_vector vec; + fill(vec); + vec.resize(count + 5); + + unsigned idx = 0; + for (auto&& v : vec) { + if (idx < count) { + BOOST_CHECK_EQUAL(v, idx); + BOOST_CHECK_EQUAL(vec[idx], idx); + } else { + BOOST_CHECK_EQUAL(v, 0); + BOOST_CHECK_EQUAL(vec[idx], 0); + } + idx++; + } + BOOST_CHECK_EQUAL(idx, count + 5); +} + +BOOST_AUTO_TEST_CASE(test_resize_down) { + managed_vector vec; + fill(vec); + vec.resize(5); + + unsigned idx = 0; + for (auto&& v : vec) { + BOOST_CHECK_EQUAL(v, idx); + BOOST_CHECK_EQUAL(vec[idx], idx); + idx++; + } + BOOST_CHECK_EQUAL(idx, 5); +} + +BOOST_AUTO_TEST_CASE(test_compaction) { + logalloc::region reg; + with_allocator(reg.allocator(), [&] { + managed_vector vec; + fill(vec); + + verify_filled(vec); + + reg.full_compaction(); + + verify_filled(vec); + }); +}