/*! * \file span.cpp * \brief * Unit tests for span * * \copyright Copyright (C) 2020 Christos Choutouridis * *
License
* The MIT License (MIT) * * 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 #include #include #include #include // tests from https://github.com/tcbrindle/span/blob/master/test/test_span.cpp namespace test_span { using namespace tbx; TEST (Tspan, default_constructors) { EXPECT_EQ (true, ( std::is_nothrow_default_constructible>::value)); EXPECT_EQ (true, ( std::is_nothrow_default_constructible>::value)); EXPECT_EQ (true, (!std::is_nothrow_default_constructible>::value)); constexpr span s1{}; EXPECT_EQ (0UL, s1.size()); EXPECT_EQ (nullptr, s1.data()); EXPECT_EQ (s1.begin(), s1.end()); constexpr span s2{}; EXPECT_EQ (0UL, s2.size()); EXPECT_EQ (nullptr, s2.data()); EXPECT_EQ (s2.begin(), s2.end()); } TEST (Tspan, pointer_constructors) { // pointer length EXPECT_EQ (true, (std::is_constructible, int*, int>::value)); EXPECT_EQ (true, (std::is_constructible, int*, int>::value)); EXPECT_EQ (true, (std::is_constructible, const int*, int>::value)); EXPECT_EQ (true, (std::is_constructible, int*, int>::value)); EXPECT_EQ (true, (std::is_constructible, int*, int>::value)); EXPECT_EQ (true, (std::is_constructible, const int*, int>::value)); int arr[] = {1, 2, 3}; // dynamic size span s1(arr, 3); EXPECT_EQ (3UL, s1.size()); EXPECT_EQ (arr, s1.data()); EXPECT_EQ (std::begin(arr), s1.begin()); EXPECT_EQ (std::end(arr), s1.end()); // fixed size span s2(arr, 3); EXPECT_EQ (3UL, s2.size()); EXPECT_EQ (arr, s2.data()); EXPECT_EQ (std::begin(arr), s2.begin()); EXPECT_EQ (std::end(arr), s2.end()); // pointer pointer EXPECT_EQ (true, (std::is_constructible, int*, int*>::value)); EXPECT_EQ (true, (std::is_constructible, float*, float*>::value)); EXPECT_EQ (true, (std::is_constructible, int*, int*>::value)); EXPECT_EQ (true, (std::is_constructible, float*, float*>::value)); // dynamic size span s3(arr, arr + 3); EXPECT_EQ (3UL, s3.size()); EXPECT_EQ (arr, s3.data()); EXPECT_EQ (std::begin(arr), s3.begin()); EXPECT_EQ (std::end(arr), s3.end()); // fixed size span s4(arr, arr + 3); EXPECT_EQ (3UL, s4.size()); EXPECT_EQ (arr, s4.data()); EXPECT_EQ (std::begin(arr), s4.begin()); EXPECT_EQ (std::end(arr), s4.end()); } TEST (Tspan, C_array_constructors) { using int_array_t = int[3]; using float_array_t = float[3]; EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t&>::value)); int arr[] = {1, 2, 3}; // non-const dynamic size span s1{arr}; EXPECT_EQ (s1.size(), 3UL); EXPECT_EQ (s1.data(), arr); EXPECT_EQ (s1.begin(), std::begin(arr)); EXPECT_EQ (s1.end(), std::end(arr)); // non-const dynamic size span s2{arr}; EXPECT_EQ (s2.size(), 3UL); EXPECT_EQ (s2.data(), arr); EXPECT_EQ (s2.begin(), std::begin(arr)); EXPECT_EQ (s2.end(), std::end(arr)); // non-const fixed size span s3{arr}; EXPECT_EQ (s3.size(), 3UL); EXPECT_EQ (s3.data(), arr); EXPECT_EQ (s3.begin(), std::begin(arr)); EXPECT_EQ (s3.end(), std::end(arr)); // non-const fixed size span s4{arr}; EXPECT_EQ (s4.size(), 3UL); EXPECT_EQ (s4.data(), arr); EXPECT_EQ (s4.begin(), std::begin(arr)); EXPECT_EQ (s4.end(), std::end(arr)); } TEST (Tspan, array_constructors) { using int_array_t = std::array; using float_array_t = std::array; using zero_array_t = std::array; EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t const&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t&>::value)); EXPECT_EQ (true, ( std::is_nothrow_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, int_array_t const&>::value)); EXPECT_EQ (true, (!std::is_constructible, float_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, zero_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, const zero_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, zero_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, const zero_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, zero_array_t&>::value)); EXPECT_EQ (true, (!std::is_constructible, const zero_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, zero_array_t&>::value)); EXPECT_EQ (true, ( std::is_constructible, const zero_array_t&>::value)); int_array_t arr = {1, 2, 3}; // non-const, dynamic size span s1{arr}; EXPECT_EQ(s1.size(), 3UL); EXPECT_EQ(s1.data(), arr.data()); EXPECT_EQ(s1.begin(), arr.data()); EXPECT_EQ(s1.end(), arr.data() + 3); //const, dynamic size span s2{arr}; EXPECT_EQ(s2.size(), 3UL); EXPECT_EQ(s2.data(), arr.data()); EXPECT_EQ(s2.begin(), arr.data()); EXPECT_EQ(s2.end(), arr.data() + 3); // non-const, static size span s3{arr}; EXPECT_EQ(s3.size(), 3UL); EXPECT_EQ(s3.data(), arr.data()); EXPECT_EQ(s3.begin(), arr.data()); EXPECT_EQ(s3.end(), arr.data() + 3); // const, dynamic size span s4{arr}; EXPECT_EQ(s4.size(), 3UL); EXPECT_EQ(s4.data(), arr.data()); EXPECT_EQ(s4.begin(), arr.data()); EXPECT_EQ(s4.end(), arr.data() + 3); } // TEST (Tspan, containter_constructors) { // using container_t = tbx::deque; // // EXPECT_EQ (true, ( std::is_constructible, container_t&>::value)); // EXPECT_EQ (true, (!std::is_constructible, const container_t&>::value)); // // EXPECT_EQ (true, ( std::is_constructible, container_t&>::value)); // EXPECT_EQ (true, ( std::is_constructible, const container_t&>::value)); // // EXPECT_EQ (true, (!std::is_constructible, container_t&>::value)); // EXPECT_EQ (true, (!std::is_constructible, const container_t&>::value)); // // EXPECT_EQ (true, (!std::is_constructible, container_t&>::value)); // EXPECT_EQ (true, (!std::is_constructible, const container_t&>::value)); // // container_t cont = {1, 2, 3}; // const container_t ccont = {1, 2, 3}; // // // non-const, dynamic size // span s1(cont); // EXPECT_EQ(s1.size(), 3UL); // EXPECT_EQ(s1.data(), cont.data()); // EXPECT_EQ(s1.begin(), cont.data()); // EXPECT_EQ(s1.end(), cont.data() + 3); // // // //const, dynamic size // span s2(cont); // EXPECT_EQ(s2.size(), 3UL); // EXPECT_EQ(s2.data(), cont.data()); // EXPECT_EQ(s2.begin(), cont.data()); // EXPECT_EQ(s2.end(), cont.data() + 3); // // } }