1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved. 4 // 5 // This code is licensed under the MIT License (MIT). 6 // 7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 13 // THE SOFTWARE. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 17 #include <catch/catch.hpp> 18 19 #include <gsl/gsl> 20 21 #include <initializer_list> 22 #include <vector> 23 24 using gsl::fail_fast; 25 26 TEST_CASE("static_array") 27 { 28 int a[4] = {1, 2, 3, 4}; 29 const int(&c_a)[4] = a; 30 31 for (int i = 0; i < 4; ++i) { 32 CHECK(&gsl::at(a, i) == &a[i]); 33 CHECK(&gsl::at(c_a, i) == &a[i]); 34 } 35 36 CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); 37 CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); 38 CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); 39 CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); 40 } 41 42 TEST_CASE("std_array") 43 { 44 std::array<int, 4> a = {1, 2, 3, 4}; 45 const std::array<int, 4>& c_a = a; 46 47 for (int i = 0; i < 4; ++i) { 48 CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]); 49 CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); 50 } 51 52 CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); 53 CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); 54 CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); 55 CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); 56 } 57 58 TEST_CASE("StdVector") 59 { 60 std::vector<int> a = {1, 2, 3, 4}; 61 const std::vector<int>& c_a = a; 62 63 for (int i = 0; i < 4; ++i) { 64 CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]); 65 CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); 66 } 67 68 CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); 69 CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); 70 CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); 71 CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); 72 } 73 74 TEST_CASE("InitializerList") 75 { 76 std::initializer_list<int> a = {1, 2, 3, 4}; 77 78 for (int i = 0; i < 4; ++i) { 79 CHECK(gsl::at(a, i) == i + 1); 80 CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1); 81 } 82 83 CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); 84 CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); 85 CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast); 86 CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast); 87 } 88 89 #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910 90 static constexpr bool test_constexpr() 91 { 92 int a1[4] = {1, 2, 3, 4}; 93 const int(&c_a1)[4] = a1; 94 std::array<int, 4> a2 = {1, 2, 3, 4}; 95 const std::array<int, 4>& c_a2 = a2; 96 97 for (int i = 0; i < 4; ++i) { 98 if (&gsl::at(a1, i) != &a1[i]) return false; 99 if (&gsl::at(c_a1, i) != &a1[i]) return false; 100 // requires C++17: 101 // if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false; 102 if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false; 103 if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false; 104 } 105 106 return true; 107 } 108 109 static_assert(test_constexpr(), "FAIL"); 110 #endif 111