/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // 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. // /////////////////////////////////////////////////////////////////////////////// #ifdef _MSC_VER // blanket turn off warnings from CppCoreCheck from catch // so people aren't annoyed by them when running the tool. #pragma warning(disable : 26440 26426) // from catch #endif #include #include // for copy #include // for span #include // for array #include // for size_t namespace gsl { struct fail_fast; } // namespace gsl using namespace std; using namespace gsl; TEST(algorithm_tests, same_type) { // dynamic source and destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // static source and dynamic destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // dynamic source and static destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // static source and destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } } TEST(algorithm_tests, compatible_type) { // dynamic source and destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // static source and dynamic destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // dynamic source and static destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } // static source and destination span { std::array src{1, 2, 3, 4, 5}; std::array dst{}; const span src_span(src); const span dst_span(dst); copy(src_span, dst_span); copy(src_span, dst_span.subspan(src_span.size())); for (std::size_t i = 0; i < src.size(); ++i) { EXPECT_EQ(dst[i], src[i]); EXPECT_EQ(dst[i + src.size()], src[i]); } } } #ifdef CONFIRM_COMPILATION_ERRORS TEST(algorithm_tests, incompatible_type) { std::array src{1, 2, 3, 4}; std::array dst{}; span src_span_dyn(src); span src_span_static(src); span dst_span_dyn(dst); span dst_span_static(dst); // every line should produce a compilation error copy(src_span_dyn, dst_span_dyn); copy(src_span_dyn, dst_span_static); copy(src_span_static, dst_span_dyn); copy(src_span_static, dst_span_static); } #endif TEST(algorithm_tests, small_destination_span) { std::array src{1, 2, 3, 4}; std::array dst{}; const span src_span_dyn(src); const span src_span_static(src); const span dst_span_dyn(dst); const span dst_span_static(dst); EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), ".*"); EXPECT_DEATH(copy(src_span_dyn, dst_span_static), ".*"); EXPECT_DEATH(copy(src_span_static, dst_span_dyn), ".*"); #ifdef CONFIRM_COMPILATION_ERRORS copy(src_span_static, dst_span_static); #endif }