Commit d3d9265a authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

test: add fixed array/2d array tests

parent 38c5b675
/* Copyright (C) 2016-2018 INRA
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <bits/fixed-2darray.hpp>
#include <bits/fixed-array.hpp>
#include <bits/unit-test.hpp>
#include <functional>
#include <numeric>
static void
check_fixed_array()
{
bits::fixed_array<int> a(10);
Ensures(a.size() == 10);
std::iota(a.begin(), a.end(), 1);
Ensures(a[0] == 1);
Ensures(a[1] == 2);
Ensures(a[2] == 3);
Ensures(a[3] == 4);
Ensures(a[4] == 5);
Ensures(a[5] == 6);
Ensures(a[6] == 7);
Ensures(a[7] == 8);
Ensures(a[8] == 9);
Ensures(a[9] == 10);
{
bits::fixed_array<int> copy(a);
std::iota(copy.rbegin(), copy.rend(), 1);
Ensures(copy[9] == 1);
Ensures(copy[8] == 2);
Ensures(copy[7] == 3);
Ensures(copy[6] == 4);
Ensures(copy[5] == 5);
Ensures(copy[4] == 6);
Ensures(copy[3] == 7);
Ensures(copy[2] == 8);
Ensures(copy[1] == 9);
Ensures(copy[0] == 10);
}
bits::fixed_array<int> b(a);
Ensures(a.data() != b.data());
bits::fixed_array<int> c(std::move(a));
Ensures(a.data() == nullptr);
Ensures(b.data() != c.data());
bits::fixed_array<double> d(15, 3.0);
Ensures(d[0] == 3.0);
Ensures(d[7] == 3.0);
Ensures(d[14] == 3.0);
bits::fixed_array<double> e;
std::swap(d, e);
Ensures(not d);
Ensures(e[0] == 3.0);
Ensures(e[7] == 3.0);
Ensures(e[14] == 3.0);
bits::fixed_array<double> x(1000, 123.0);
if (x) {
for (int i{ 0 }; i < 1000; ++i)
Ensures(x[i] == 123.0);
auto it = std::find_if_not(
x.begin(),
x.end(),
std::bind(std::equal_to<double>(), 123.0, std::placeholders::_1));
Ensures(it == x.end());
}
}
static void
check_fixed_2darray()
{
bits::fixed_2darray<int> a(2, 10);
Ensures(a.size() == 20);
Ensures(a.rows() == 2);
Ensures(a.columns() == 10);
std::iota(a.begin(), a.end(), 0);
Ensures(a.data()[0] == 0);
Ensures(a.data()[19] == 19);
std::iota(a.rbegin(), a.rend(), 0);
Ensures(a.data()[0] == 19);
Ensures(a.data()[19] == 0);
Ensures(a(0, 0) == 19);
Ensures(a(1, 9) == 0);
a(0, 1) = 100;
a(1, 0) = 200;
Ensures(a.data()[1] == 100);
Ensures(a.data()[10] == 200);
}
int
main(int /* argc */, char* /* argv */ [])
{
check_fixed_array();
check_fixed_2darray();
return unit_test::report_errors();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment