libcruft-util/test/map/multi_fixed.cpp

103 lines
3.3 KiB
C++
Raw Normal View History

#include <cruft/util/map/multi_fixed.hpp>
#include <cruft/util/tap.hpp>
#include <cruft/util/random.hpp>
static void
test_sorted_iterators (cruft::TAP::logger &tap)
{
std::array<int, 64> keys;
// Attempt to generate a list of unsorted keys.
//
// This _might_ fail because shuffle _might_ return the original ordering,
// but I feel the number of keys we're using here should sufficiently
// mitigate this risk.
std::iota (std::begin (keys), std::end (keys), 0);
std::shuffle (std::begin (keys), std::end (keys), cruft::random::generator ());
if (std::is_sorted (std::begin (keys), std::end (keys)))
throw std::logic_error ("Keys should not be sorted before insertion");
cruft::map::multi_fixed<64,int,int> obj;
for (auto const k: keys)
obj.insert ({k, k});
tap.expect (
std::is_sorted (
obj.begin (),
obj.end (),
[] (auto const &a, auto const &b) { return a.first < b.first; }
),
"iterator range is provided in sorted order"
);
}
static void
test_bad_alloc (cruft::TAP::logger &tap)
{
tap.expect_throw<std::bad_alloc> ([] (void) {
cruft::map::multi_fixed<1,int,int> obj;
obj.insert ({0,0});
obj.insert ({1,1});
}, "overcommitting storage results in std::bad_alloc");
}
int main ()
{
cruft::TAP::logger tap;
{
cruft::map::multi_fixed<8,int,int> a, b;
tap.expect_eq (a, b, "default construction equality");
a.insert ({ 0, 1 });
tap.expect_eq (a.size (), 1u, "unit size after first insertion");
tap.expect_eq (a.find (0)->second, 1, "value retrieval after insertion");
tap.expect_neq (a, b, "inequality after insertion");
}
{
cruft::map::multi_fixed<4,int,int> const a {{ {0,0}, {3,3}, }};
cruft::map::multi_fixed<4,int,int> const b {{ {2,2}, {0,0}, }};
cruft::map::multi_fixed<4,int,int> const expected {{ {0,0}, {0,0}, {2,2}, {3,3}, }};
tap.expect_eq (a + b, expected, "summation of two sets");
}
{
cruft::map::multi_fixed<4,int,int> const a {{ {0,0}, {2,2}, {3,3}, }};
cruft::map::multi_fixed<4,int,int> const b {{ {0,0}, {1,1}, {2,2}, {4,4}, }};
cruft::map::multi_fixed<4,int,int> const expected {{ {3, 3} }};
tap.expect_eq (a - b, expected, "difference of two sets");
}
{
cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }};
init.erase (init.find (2));
cruft::map::multi_fixed<4, int, int> expected {{ {0,0}, {1,1}, {3,3} }};
tap.expect_eq (init, expected, "erase of inner element");
}
{
cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }};
init.erase (init.find (3));
cruft::map::multi_fixed<4, int, int> expected {{ {0,0}, {1,1}, {2,2} }};
tap.expect_eq (init, expected, "erase of last element");
}
2021-02-08 10:38:17 +11:00
{
cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }};
static constexpr int keys[] = { 1, 3 };
init.erase_keys (std::begin (keys), std::end (keys));
cruft::map::multi_fixed<4, int, int> expected {{ {0,0}, {2,2} }};
tap.expect_eq (init, expected, "erase_keys");
}
test_sorted_iterators (tap);
test_bad_alloc (tap);
return tap.status ();
}