2017-04-04 01:43:43 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2015-08-20 21:09:14 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
#include <catch/catch.hpp>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2016-08-10 12:24:00 -04:00
|
|
|
#include <gsl/gsl>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2016-05-09 07:02:27 -04:00
|
|
|
#include <initializer_list>
|
2017-04-20 10:51:37 -04:00
|
|
|
#include <vector>
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-04-04 01:43:43 -04:00
|
|
|
using gsl::fail_fast;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("static_array")
|
2015-08-20 21:09:14 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
int a[4] = {1, 2, 3, 4};
|
|
|
|
const int(&c_a)[4] = a;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
CHECK(&gsl::at(a, i) == &a[i]);
|
|
|
|
CHECK(&gsl::at(c_a, i) == &a[i]);
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("std_array")
|
|
|
|
{
|
|
|
|
std::array<int, 4> a = {1, 2, 3, 4};
|
|
|
|
const std::array<int, 4>& c_a = a;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
|
|
|
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("StdVector")
|
|
|
|
{
|
|
|
|
std::vector<int> a = {1, 2, 3, 4};
|
|
|
|
const std::vector<int>& c_a = a;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
|
|
|
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
2016-05-09 07:02:27 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
|
|
|
}
|
2016-05-09 07:02:27 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("InitializerList")
|
|
|
|
{
|
|
|
|
std::initializer_list<int> a = {1, 2, 3, 4};
|
2017-04-04 01:43:43 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
CHECK(gsl::at(a, i) == i + 1);
|
|
|
|
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
|
2017-04-04 01:43:43 -04:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
|
|
|
|
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast);
|
|
|
|
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast);
|
2017-04-04 01:43:43 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 15:26:38 -04:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
|
2017-04-04 01:43:43 -04:00
|
|
|
static constexpr bool test_constexpr()
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int a1[4] = {1, 2, 3, 4};
|
|
|
|
const int(&c_a1)[4] = a1;
|
|
|
|
std::array<int, 4> a2 = {1, 2, 3, 4};
|
2017-04-04 01:43:43 -04:00
|
|
|
const std::array<int, 4>& c_a2 = a2;
|
2016-05-09 07:02:27 -04:00
|
|
|
|
2017-04-04 01:43:43 -04:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (&gsl::at(a1, i) != &a1[i]) return false;
|
|
|
|
if (&gsl::at(c_a1, i) != &a1[i]) return false;
|
2017-04-17 15:26:38 -04:00
|
|
|
// requires C++17:
|
|
|
|
// if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false;
|
|
|
|
if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false;
|
2017-04-20 10:51:37 -04:00
|
|
|
if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false;
|
2016-05-09 07:02:27 -04:00
|
|
|
}
|
2017-04-04 01:43:43 -04:00
|
|
|
|
|
|
|
return true;
|
2015-08-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
|
2017-04-04 01:43:43 -04:00
|
|
|
static_assert(test_constexpr(), "FAIL");
|
|
|
|
#endif
|