Skip to content

Commit

Permalink
Add more tests and few fixes here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
asl authored and fruffy committed Jun 12, 2024
1 parent 440391a commit 7ba600c
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 47 deletions.
53 changes: 23 additions & 30 deletions lib/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace P4C {
/// A header-only implementation of a memory-efficient flat_map.
/// TODO: Replace this map with std::flat_map once available in C++23:
/// https://en.cppreference.com/w/cpp/container/flat_map
template <typename K, typename V, typename Compare = std::less<K>,
template <typename K, typename V, typename Compare = std::less<>,
typename Container = std::vector<std::pair<K, V>>>
struct flat_map {
using key_type = K;
Expand Down Expand Up @@ -97,9 +97,18 @@ struct flat_map {
return lower->second;
}

mapped_type &at(const key_type &key) { return lower_bound(key)->second; }

const mapped_type &at(const key_type &key) const { return lower_bound(key)->second; }
template <class Key>
mapped_type &at(const Key &key) {
auto found = lower_bound(key);
if (found == end()) throw std::out_of_range("key is out of range");
return found->second;
}
template <class Key>
const mapped_type &at(const Key &key) const {
auto found = lower_bound(key);
if (found == end()) throw std::out_of_range("key is out of range");
return found->second;
}

std::pair<iterator, bool> insert(value_type &&value) { return emplace(std::move(value)); }

Expand Down Expand Up @@ -269,33 +278,17 @@ struct flat_map {
iterator iterator_const_cast(const_iterator it) { return begin() + (it - cbegin()); }

struct KeyOrValueCompare {
bool operator()(const key_type &lhs, const key_type &rhs) const {
return key_compare()(lhs, rhs);
}
bool operator()(const key_type &lhs, const value_type &rhs) const {
return key_compare()(lhs, rhs.first);
}
template <typename T>
bool operator()(const key_type &lhs, const T &rhs) const {
return key_compare()(lhs, rhs);
}
template <typename T>
bool operator()(const T &lhs, const key_type &rhs) const {
return key_compare()(lhs, rhs);
}
bool operator()(const value_type &lhs, const key_type &rhs) const {
return key_compare()(lhs.first, rhs);
template <typename T, typename U>
bool operator()(const T &lhs, const U &rhs) const {
return key_compare()(extract(lhs), extract(rhs));
}
bool operator()(const value_type &lhs, const value_type &rhs) const {
return key_compare()(lhs.first, rhs.first);
}
template <typename T>
bool operator()(const value_type &lhs, const T &rhs) const {
return key_compare()(lhs.first, rhs);
}
template <typename T>
bool operator()(const T &lhs, const value_type &rhs) const {
return key_compare()(lhs, rhs.first);

private:
const key_type &extract(const value_type &v) const { return v.first; }

template <typename Key>
const Key &extract(const Key &k) const {
return k;
}
};

Expand Down
139 changes: 122 additions & 17 deletions test/gtest/flat_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,121 @@ limitations under the License.

#include "lib/flat_map.h"

#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>

#include <stdexcept>

#include "lib/map.h"

using ::testing::ElementsAre;
using ::testing::ElementsAreArray;

namespace Test {

TEST(FlatMap, RangeConstructor) {
P4C::flat_map<unsigned, unsigned>::value_type myMap[] = {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2},
{2, 3}, {3, 1}, {3, 2}, {3, 3}};

P4C::flat_map<unsigned, unsigned> first(std::begin(myMap), std::end(myMap));
EXPECT_THAT(first,
ElementsAre(std::make_pair(1, 1), std::make_pair(2, 1), std::make_pair(3, 1)));
}

TEST(FlatMap, InitializerListConstructor) {
P4C::flat_map<unsigned, unsigned> myMap(
{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {1, 2}, {10, 10}, {8, 8}});
EXPECT_THAT(myMap, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2), std::make_pair(3, 3),
std::make_pair(4, 4), std::make_pair(5, 5), std::make_pair(8, 8),
std::make_pair(10, 10)));
}

TEST(FlatMap, InitializerListAssignment) {
P4C::flat_map<unsigned, unsigned> myMap;
myMap = {{1, 1}, {2, 2}};
EXPECT_THAT(myMap, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
}

TEST(FlatMap, InsertFindSize) {
P4C::flat_map<unsigned, unsigned> s;
s.insert(std::make_pair(1, 1));
s.insert(std::make_pair(1, 1));
s.insert(std::make_pair(2, 2));

EXPECT_EQ(2u, s.size());
EXPECT_EQ(std::make_pair(1u, 1u), *s.find(1));
EXPECT_EQ(std::make_pair(2u, 2u), *s.find(2));
EXPECT_EQ(s.end(), s.find(7));
}

TEST(FlatMap, CopySwap) {
P4C::flat_map<unsigned, unsigned> original;
original.insert({1, 1});
original.insert({2, 2});
EXPECT_THAT(original, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));

P4C::flat_map<unsigned, unsigned> copy(original);
EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));

copy.erase(copy.begin());
copy.insert({10, 10});
EXPECT_THAT(copy, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));

original.swap(copy);
EXPECT_THAT(original, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
}

// operator[](const Key&)
TEST(FlatMap, SubscriptConstKey) {
P4C::flat_map<std::string, unsigned> m;

// Default construct elements that don't exist yet.
unsigned &s = m["a"];
EXPECT_EQ(0, s);
EXPECT_EQ(1u, m.size());

// The returned mapped reference should refer unsignedo the map.
s = 22;
EXPECT_EQ(22, m["a"]);

// Overwrite existing elements.
m["a"] = 44;
EXPECT_EQ(44, m["a"]);
}

// mapped_type& at(const Key&)
// const mapped_type& at(const Key&) const
TEST(FlatMap, At) {
P4C::flat_map<unsigned, std::string> m = {{1, "a"}, {2, "b"}};

// basic usage.
EXPECT_EQ("a", m.at(1));
EXPECT_EQ("b", m.at(2));

// const reference works.
const std::string &const_ref = std::as_const(m).at(1);
EXPECT_EQ("a", const_ref);

// reference works, can operate on the string.
m.at(1)[0] = 'x';
EXPECT_EQ("x", m.at(1));

// out-of-bounds will throw.
EXPECT_THROW(m.at(-1), std::out_of_range);
EXPECT_THROW({ m.at(-1)[0] = 'z'; }, std::out_of_range);

// heterogeneous look-up works.
P4C::flat_map<std::string, unsigned> m2 = {{"a", 1}, {"b", 2}};
EXPECT_EQ(1, m2.at(std::string_view("a")));
EXPECT_EQ(2, std::as_const(m2).at(std::string_view("b")));
}

TEST(FlatMap, MapEqual) {
P4C::flat_map<unsigned, unsigned> a;
P4C::flat_map<unsigned, unsigned> b;

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a[1] = 111;
a[2] = 222;
Expand All @@ -38,24 +142,24 @@ TEST(FlatMap, MapEqual) {
b[3] = 333;
b[4] = 444;

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a.erase(2);
b.erase(2);

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a.clear();
b.clear();

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);
}

TEST(FlatMap, MapNotEqual) {
P4C::flat_map<unsigned, unsigned> a;
P4C::flat_map<unsigned, unsigned> b;

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a[1] = 111;
a[2] = 222;
Expand All @@ -67,12 +171,12 @@ TEST(FlatMap, MapNotEqual) {
b[2] = 222;
b[1] = 111;

EXPECT_TRUE(a != b);
EXPECT_EQ(a, b);

a.clear();
b.clear();

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a[1] = 111;
a[2] = 222;
Expand All @@ -81,12 +185,12 @@ TEST(FlatMap, MapNotEqual) {
b[2] = 222;
b[3] = 333;

EXPECT_TRUE(a != b);
EXPECT_NE(a, b);

a.clear();
b.clear();

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a[1] = 111;
a[2] = 222;
Expand All @@ -98,12 +202,12 @@ TEST(FlatMap, MapNotEqual) {
b[2] = 333;
b[1] = 444;

EXPECT_TRUE(a != b);
EXPECT_NE(a, b);

a.clear();
b.clear();

EXPECT_TRUE(a == b);
EXPECT_EQ(a, b);

a[1] = 111;
a[2] = 222;
Expand All @@ -115,7 +219,7 @@ TEST(FlatMap, MapNotEqual) {
b[3] = 111;
b[4] = 111;

EXPECT_TRUE(a != b);
EXPECT_NE(a, b);
}

TEST(FlatMap, InsertEmplaceErase) {
Expand All @@ -141,26 +245,27 @@ TEST(FlatMap, InsertEmplaceErase) {
}
}

EXPECT_TRUE(std::equal(om.begin(), om.end(), sm.begin(), sm.end()));
EXPECT_THAT(om, ElementsAreArray(sm.begin(), sm.end()));

it = std::next(om.begin(), 2);
om.erase(it);
sm.erase(std::next(sm.begin(), 2));

EXPECT_TRUE(om.size() == sm.size());
EXPECT_TRUE(std::equal(om.begin(), om.end(), sm.begin(), sm.end()));
EXPECT_EQ(om.size(), sm.size());

EXPECT_THAT(om, ElementsAreArray(sm.begin(), sm.end()));
}

TEST(FlatMap, ExistingKey) {
P4C::flat_map<int, std::string> myMap{{1, "One"}, {2, "Two"}, {3, "Three"}};
P4C::flat_map<unsigned, std::string> myMap{{1, "One"}, {2, "Two"}, {3, "Three"}};

EXPECT_EQ(get(myMap, 1), "One");
EXPECT_EQ(get(myMap, 2), "Two");
EXPECT_EQ(get(myMap, 3), "Three");
}

TEST(FlatMap, NonExistingKey) {
P4C::flat_map<int, std::string> myMap{{1, "One"}, {2, "Two"}, {3, "Three"}};
P4C::flat_map<unsigned, std::string> myMap{{1, "One"}, {2, "Two"}, {3, "Three"}};

EXPECT_EQ(get(myMap, 4), "");
}
Expand Down

0 comments on commit 7ba600c

Please sign in to comment.