275 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			275 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*!
 | |
|  * \file span.cpp
 | |
|  * \brief
 | |
|  *      Unit tests for span
 | |
|  *
 | |
|  * \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
 | |
|  *
 | |
|  * <dl class=\"section copyright\"><dt>License</dt><dd>
 | |
|  * 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.
 | |
|  * </dd></dl>
 | |
|  *
 | |
|  */
 | |
| #include <cont/span.h>
 | |
| #include <gtest/gtest.h>
 | |
| 
 | |
| #include <cont/deque.h>
 | |
| #include <vector>
 | |
| #include <type_traits>
 | |
| 
 | |
| // 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<span<int>>::value));
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_default_constructible<span<int, 0>>::value));
 | |
|        EXPECT_EQ (true, (!std::is_nothrow_default_constructible<span<int, 42>>::value));
 | |
| 
 | |
|        constexpr span<int> s1{};
 | |
| 
 | |
|        EXPECT_EQ (0UL, s1.size());
 | |
|        EXPECT_EQ (nullptr, s1.data());
 | |
|        EXPECT_EQ (s1.begin(), s1.end());
 | |
| 
 | |
|        constexpr span<int, 0> 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<span<int>, int*, int>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<const int>, int*, int>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<const int>, const int*, int>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<int, 42>, int*, int>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<const int, 42>, int*, int>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<const int, 42>, const int*, int>::value));
 | |
| 
 | |
|        int arr[] = {1, 2, 3};
 | |
| 
 | |
|        // dynamic size
 | |
|        span<int> 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<int, 3> 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<span<int>, int*, int*>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<float>, float*, float*>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<int, 42>, int*, int*>::value));
 | |
|        EXPECT_EQ (true, (std::is_constructible<span<float, 42>, float*, float*>::value));
 | |
| 
 | |
|        // dynamic size
 | |
|        span<int> 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<int, 3> 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<span<int>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int>, float_array_t>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int>, float_array_t>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, float_array_t&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, float_array_t>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, float_array_t&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, float_array_t&>::value));
 | |
| 
 | |
|        int arr[] = {1, 2, 3};
 | |
| 
 | |
|        // non-const dynamic size
 | |
|        span<int> 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<const int> 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<int, 3> 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<const int, 3> 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<int, 3>;
 | |
|        using float_array_t = std::array<float, 3>;
 | |
|        using zero_array_t = std::array<int, 0>;
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int>, float_array_t>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int>, float_array_t const&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, float_array_t>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, float_array_t const&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, float_array_t const&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t const&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, float_array_t&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<int>, zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int>, const zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<const int>, zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<const int>, const zero_array_t&>::value));
 | |
| 
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<int, 0>, zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, (!std::is_constructible<span<int, 0>, const zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<const int, 0>, zero_array_t&>::value));
 | |
|        EXPECT_EQ (true, ( std::is_constructible<span<const int, 0>, const zero_array_t&>::value));
 | |
| 
 | |
| 
 | |
|        int_array_t arr = {1, 2, 3};
 | |
| 
 | |
|        // non-const, dynamic size
 | |
|        span<int> 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<int const> 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<int, 3> 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<int const, 3> 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<int, 3>;
 | |
| //
 | |
| //       EXPECT_EQ (true, ( std::is_constructible<span<int>, container_t&>::value));
 | |
| //       EXPECT_EQ (true, (!std::is_constructible<span<int>, const container_t&>::value));
 | |
| //
 | |
| //       EXPECT_EQ (true, ( std::is_constructible<span<const int>, container_t&>::value));
 | |
| //       EXPECT_EQ (true, ( std::is_constructible<span<const int>, const container_t&>::value));
 | |
| //
 | |
| //       EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, container_t&>::value));
 | |
| //       EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, const container_t&>::value));
 | |
| //
 | |
| //       EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, container_t&>::value));
 | |
| //       EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, const container_t&>::value));
 | |
| //
 | |
| //       container_t cont = {1, 2, 3};
 | |
| //       const container_t ccont = {1, 2, 3};
 | |
| //
 | |
| //       // non-const, dynamic size
 | |
| //       span<int> 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<int const> 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);
 | |
| //
 | |
| //   }
 | |
| 
 | |
| }
 |