Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [diff] [blame] | 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> // for AssertionHandler, StringRef, CHECK_THROW... |
| 18 | |
| 19 | #include <gsl/gsl_util> // for at |
| 20 | |
| 21 | #include <array> // for array |
| 22 | #include <cstddef> // for size_t |
| 23 | #include <initializer_list> // for initializer_list |
| 24 | #include <vector> // for vector |
| 25 | |
| 26 | namespace gsl { |
| 27 | struct fail_fast; |
| 28 | } // namespace gsl |
| 29 | |
| 30 | using gsl::fail_fast; |
| 31 | |
| 32 | TEST_CASE("static_array") |
| 33 | { |
| 34 | int a[4] = {1, 2, 3, 4}; |
| 35 | const int(&c_a)[4] = a; |
| 36 | |
| 37 | for (int i = 0; i < 4; ++i) { |
| 38 | CHECK(&gsl::at(a, i) == &a[i]); |
| 39 | CHECK(&gsl::at(c_a, i) == &a[i]); |
| 40 | } |
| 41 | |
| 42 | CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); |
| 43 | CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); |
| 44 | CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); |
| 45 | CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); |
| 46 | } |
| 47 | |
| 48 | TEST_CASE("std_array") |
| 49 | { |
| 50 | std::array<int, 4> a = {1, 2, 3, 4}; |
| 51 | const std::array<int, 4>& c_a = a; |
| 52 | |
| 53 | for (int i = 0; i < 4; ++i) { |
| 54 | CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]); |
| 55 | CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); |
| 56 | } |
| 57 | |
| 58 | CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); |
| 59 | CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); |
| 60 | CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); |
| 61 | CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); |
| 62 | } |
| 63 | |
| 64 | TEST_CASE("StdVector") |
| 65 | { |
| 66 | std::vector<int> a = {1, 2, 3, 4}; |
| 67 | const std::vector<int>& c_a = a; |
| 68 | |
| 69 | for (int i = 0; i < 4; ++i) { |
| 70 | CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]); |
| 71 | CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); |
| 72 | } |
| 73 | |
| 74 | CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); |
| 75 | CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); |
| 76 | CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast); |
| 77 | CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast); |
| 78 | } |
| 79 | |
| 80 | TEST_CASE("InitializerList") |
| 81 | { |
| 82 | std::initializer_list<int> a = {1, 2, 3, 4}; |
| 83 | |
| 84 | for (int i = 0; i < 4; ++i) { |
| 85 | CHECK(gsl::at(a, i) == i + 1); |
| 86 | CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1); |
| 87 | } |
| 88 | |
| 89 | CHECK_THROWS_AS(gsl::at(a, -1), fail_fast); |
| 90 | CHECK_THROWS_AS(gsl::at(a, 4), fail_fast); |
| 91 | CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast); |
| 92 | CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast); |
| 93 | } |
| 94 | |
| 95 | #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910 |
| 96 | static constexpr bool test_constexpr() |
| 97 | { |
| 98 | int a1[4] = {1, 2, 3, 4}; |
| 99 | const int(&c_a1)[4] = a1; |
| 100 | std::array<int, 4> a2 = {1, 2, 3, 4}; |
| 101 | const std::array<int, 4>& c_a2 = a2; |
| 102 | |
| 103 | for (int i = 0; i < 4; ++i) { |
| 104 | if (&gsl::at(a1, i) != &a1[i]) return false; |
| 105 | if (&gsl::at(c_a1, i) != &a1[i]) return false; |
| 106 | // requires C++17: |
| 107 | // if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false; |
| 108 | if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false; |
| 109 | if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false; |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | static_assert(test_constexpr(), "FAIL"); |
| 116 | #endif |