libcruft-util/test/iterator.cpp

54 lines
1.6 KiB
C++
Raw Normal View History

#include "../tap.hpp"
#include "../iterator.hpp"
#include <vector>
#include <array>
///////////////////////////////////////////////////////////////////////////////
int
main (int, char**)
{
util::TAP::logger tap;
2018-03-27 15:49:47 +11:00
// test that iteration across disparate types works as expected.
//
// the char array is an important element because it tends to decay to a
// pointer type unless we're paying careful attention.
{
std::vector<int> v_int { 1, 2, 3 };
std::array<float,3> a_float { 1.1f, 2.2f, 3.3f };
char c_char[] = { '\0', 'b', 'c' };
bool success = true;
2018-07-24 15:46:28 +10:00
for (auto const &[i, v, a, c]: util::izip (v_int, a_float, c_char)) {
2018-03-27 15:49:47 +11:00
success = success &&
v_int[i] == v &&
util::equal (a_float[i], a) &&
c_char[i] == c;
}
tap.expect (success, "izip containers of int, float, and char and an initialiser_list");
}
// test that the obvious structured binding syntax gives references to
// the underlying values rather than silently supplying lvalues.
//
// we deliberately do not use any type of references in the range loop so
// that we check the syntax a user is likely to employ actually works.
// references may not be an obvious consideration.
{
const std::array<unsigned,3> src { 0, 1, 2 };
std::array<unsigned,3> dst { 2, 0, 1 };
for (auto [a,b]: util::zip (src, dst))
b = a;
tap.expect_eq (src, dst, "copy using structured bindings");
}
return tap.status ();
}