aboutsummaryrefslogtreecommitdiff
path: root/test/bankmap/bank_map.cpp
diff options
context:
space:
mode:
authorVitaly Novichkov <admin@wohlnet.ru>2018-06-19 15:04:50 +0300
committerVitaly Novichkov <admin@wohlnet.ru>2018-06-19 15:04:50 +0300
commitc0873278718da57ff19194377a35601027926c5f (patch)
tree6fca461d067a1e615788cecf7a74c175f0033fc8 /test/bankmap/bank_map.cpp
parent5a194eb263125e5505cca3ec0256c7efa348eaa4 (diff)
parent1026ecd3fd8dc865fb3a85ab4a130d9d1b494fd9 (diff)
downloadlibADLMIDI-c0873278718da57ff19194377a35601027926c5f.tar.gz
libADLMIDI-c0873278718da57ff19194377a35601027926c5f.tar.bz2
libADLMIDI-c0873278718da57ff19194377a35601027926c5f.zip
Merge branch 'master' into stable
Diffstat (limited to 'test/bankmap/bank_map.cpp')
-rw-r--r--test/bankmap/bank_map.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/test/bankmap/bank_map.cpp b/test/bankmap/bank_map.cpp
new file mode 100644
index 0000000..613fc59
--- /dev/null
+++ b/test/bankmap/bank_map.cpp
@@ -0,0 +1,100 @@
+#include <catch.hpp>
+#include <random>
+#include "adlmidi_bankmap.h"
+
+typedef BasicBankMap<int> BankMap;
+typedef BankMap::value_type ValuePair;
+typedef BankMap::iterator Iterator;
+typedef std::pair<Iterator, bool> InsertResult;
+
+static std::mt19937 rng;
+
+static size_t iterated_size(const BankMap &map)
+{
+ size_t size = 0;
+ auto it = map.begin(), end = map.end();
+ while(it != end) {
+ ++it;
+ ++size;
+ }
+ return size;
+}
+
+static bool consistent_size(const BankMap &map)
+{
+ return map.size() == iterated_size(map);
+}
+
+TEST_CASE("[BankMap] Construction")
+{
+ BankMap map;
+
+ REQUIRE(map.capacity() == 0);
+ REQUIRE(map.size() == 0);
+ REQUIRE(iterated_size(map) == 0);
+}
+
+TEST_CASE("[BankMap] Insert, erase and find")
+{
+ BankMap map;
+
+ for(unsigned i = 0; i < 10; ++i) {
+ const uint16_t key = rng();
+ const int value = rng();
+
+ uint16_t another_key = rng();
+ while (another_key == key)
+ another_key = rng();
+
+ InsertResult ir = map.insert(ValuePair{key, value});
+ REQUIRE(ir.second);
+ REQUIRE(map.size() == 1);
+ REQUIRE(iterated_size(map) == 1);
+
+ Iterator it = map.find(key);
+ REQUIRE(it != map.end());
+ REQUIRE(it->first == key);
+ REQUIRE(it->second == value);
+ REQUIRE(map.find(another_key) == map.end());
+
+ map.erase(it);
+ REQUIRE(map.find(key) == map.end());
+ REQUIRE(map.size() == 0);
+ REQUIRE(iterated_size(map) == 0);
+ }
+}
+
+TEST_CASE("[BankMap] Insert without expanding")
+{
+ BankMap map;
+
+ InsertResult ir;
+ ir = map.insert(ValuePair{0, 0}, BankMap::do_not_expand_t());
+ REQUIRE(ir.first == map.end());
+ REQUIRE(!ir.second);
+
+ map.reserve(1);
+ REQUIRE(map.capacity() > 1);
+
+ while(map.size() < map.capacity()) {
+ const uint16_t key = rng();
+ const int value = rng();
+ if(map.find(key) != map.end())
+ continue;
+ ir = map.insert(ValuePair{key, value}, BankMap::do_not_expand_t());
+ REQUIRE(ir.first != map.end());
+ REQUIRE(ir.first->first == key);
+ REQUIRE(ir.first->second == value);
+ REQUIRE(ir.second);
+ REQUIRE(consistent_size(map));
+ }
+
+ ir = map.insert(ValuePair{0, 0}, BankMap::do_not_expand_t());
+ REQUIRE(ir.first == map.end());
+ REQUIRE(!ir.second);
+ REQUIRE(consistent_size(map));
+
+ map.clear();
+ REQUIRE(map.size() == 0);
+ REQUIRE(consistent_size(map));
+}