1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <catch.hpp>
#include <algorithm>
#include <chrono>
#include <stdexcept>
#include <string>
#include <thread>
#include <type_traits>
#include <utility>
#include "unit_test_util.hh"
#include <libcuckoo/cuckoohash_map.hh>
TEST_CASE("iterator types", "[iterator]") {
using Ltbl = IntIntTable::locked_table;
using It = Ltbl::iterator;
using ConstIt = Ltbl::const_iterator;
const bool it_difference_type =
std::is_same<Ltbl::difference_type, It::difference_type>::value;
const bool it_value_type =
std::is_same<Ltbl::value_type, It::value_type>::value;
const bool it_pointer = std::is_same<Ltbl::pointer, It::pointer>::value;
const bool it_reference = std::is_same<Ltbl::reference, It::reference>::value;
const bool it_iterator_category =
std::is_same<std::bidirectional_iterator_tag,
It::iterator_category>::value;
const bool const_it_difference_type =
std::is_same<Ltbl::difference_type, ConstIt::difference_type>::value;
const bool const_it_value_type =
std::is_same<Ltbl::value_type, ConstIt::value_type>::value;
const bool const_it_reference =
std::is_same<Ltbl::const_reference, ConstIt::reference>::value;
const bool const_it_pointer =
std::is_same<Ltbl::const_pointer, ConstIt::pointer>::value;
const bool const_it_iterator_category =
std::is_same<std::bidirectional_iterator_tag,
ConstIt::iterator_category>::value;
REQUIRE(it_difference_type);
REQUIRE(it_value_type);
REQUIRE(it_pointer);
REQUIRE(it_reference);
REQUIRE(it_iterator_category);
REQUIRE(const_it_difference_type);
REQUIRE(const_it_value_type);
REQUIRE(const_it_pointer);
REQUIRE(const_it_reference);
REQUIRE(const_it_iterator_category);
}
TEST_CASE("empty table iteration", "[iterator]") {
IntIntTable table;
{
auto lt = table.lock_table();
REQUIRE(lt.begin() == lt.begin());
REQUIRE(lt.begin() == lt.end());
REQUIRE(lt.cbegin() == lt.begin());
REQUIRE(lt.begin() == lt.end());
REQUIRE(lt.cbegin() == lt.begin());
REQUIRE(lt.cend() == lt.end());
}
}
TEST_CASE("iterator walkthrough", "[iterator]") {
IntIntTable table;
for (int i = 0; i < 10; ++i) {
table.insert(i, i);
}
SECTION("forward postfix walkthrough") {
auto lt = table.lock_table();
auto it = lt.cbegin();
for (size_t i = 0; i < table.size(); ++i) {
REQUIRE((*it).first == (*it).second);
REQUIRE(it->first == it->second);
auto old_it = it;
REQUIRE(old_it == it++);
}
REQUIRE(it == lt.end());
}
SECTION("forward prefix walkthrough") {
auto lt = table.lock_table();
auto it = lt.cbegin();
for (size_t i = 0; i < table.size(); ++i) {
REQUIRE((*it).first == (*it).second);
REQUIRE(it->first == it->second);
++it;
}
REQUIRE(it == lt.end());
}
SECTION("backwards postfix walkthrough") {
auto lt = table.lock_table();
auto it = lt.cend();
for (size_t i = 0; i < table.size(); ++i) {
auto old_it = it;
REQUIRE(old_it == it--);
REQUIRE((*it).first == (*it).second);
REQUIRE(it->first == it->second);
}
REQUIRE(it == lt.begin());
}
SECTION("backwards prefix walkthrough") {
auto lt = table.lock_table();
auto it = lt.cend();
for (size_t i = 0; i < table.size(); ++i) {
--it;
REQUIRE((*it).first == (*it).second);
REQUIRE(it->first == it->second);
}
REQUIRE(it == lt.begin());
}
SECTION("walkthrough works after move") {
auto lt = table.lock_table();
auto it = lt.cend();
auto lt2 = std::move(lt);
for (size_t i = 0; i < table.size(); ++i) {
--it;
REQUIRE((*it).first == (*it).second);
REQUIRE(it->first == it->second);
}
REQUIRE(it == lt2.begin());
}
}
TEST_CASE("iterator modification", "[iterator]") {
IntIntTable table;
for (int i = 0; i < 10; ++i) {
table.insert(i, i);
}
auto lt = table.lock_table();
for (auto it = lt.begin(); it != lt.end(); ++it) {
it->second = it->second + 1;
}
auto it = lt.cbegin();
for (size_t i = 0; i < table.size(); ++i) {
REQUIRE(it->first == it->second - 1);
++it;
}
REQUIRE(it == lt.end());
}
TEST_CASE("lock table blocks inserts", "[iterator]") {
IntIntTable table;
auto lt = table.lock_table();
std::thread thread([&table]() {
for (int i = 0; i < 10; ++i) {
table.insert(i, i);
}
});
std::this_thread::sleep_for(std::chrono::milliseconds(100));
REQUIRE(table.size() == 0);
lt.unlock();
thread.join();
REQUIRE(table.size() == 10);
}
TEST_CASE("Cast iterator to const iterator", "[iterator]") {
IntIntTable table;
for (int i = 0; i < 10; ++i) {
table.insert(i, i);
}
auto lt = table.lock_table();
for (IntIntTable::locked_table::iterator it = lt.begin(); it != lt.end();
++it) {
REQUIRE(it->first == it->second);
it->second++;
IntIntTable::locked_table::const_iterator const_it = it;
REQUIRE(it->first + 1 == it->second);
}
}
|