Replace UnitTest-cpp with Catch

This PR replaces UnitTest-cpp with Catch. Catch is downloaded
using cmake's ExternalProject_Add and installed into the
include directory as it's a header only library.

Signed-off-by: Rian Quinn <“rianquinn@gmail.com”>
This commit is contained in:
Rian Quinn 2017-04-28 06:45:49 -06:00
parent 8b320e3f5d
commit 845a59425c
23 changed files with 4236 additions and 4251 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
CMakeFiles CMakeFiles
build
include/catch
tests/CMakeFiles tests/CMakeFiles
tests/Debug tests/Debug
*.opensdf *.opensdf

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "tests/unittest-cpp"]
path = tests/unittest-cpp
url = https://github.com/unittest-cpp/unittest-cpp.git

View File

@ -10,7 +10,7 @@ cache:
- ${TRAVIS_BUILD_DIR}/deps/llvm-3.6.2/install - ${TRAVIS_BUILD_DIR}/deps/llvm-3.6.2/install
- ${TRAVIS_BUILD_DIR}/deps/llvm-3.7.1/install - ${TRAVIS_BUILD_DIR}/deps/llvm-3.7.1/install
- ${TRAVIS_BUILD_DIR}/deps/llvm-3.8.1/install - ${TRAVIS_BUILD_DIR}/deps/llvm-3.8.1/install
- ${TRAVIS_BUILD_DIR}/deps/llvm-3.9.0/install - ${TRAVIS_BUILD_DIR}/deps/llvm-3.9.1/install
matrix: matrix:
include: include:
@ -61,6 +61,19 @@ matrix:
- env: CLANG_VERSION=3.8 BUILD_TYPE=Release - env: CLANG_VERSION=3.8 BUILD_TYPE=Release
os: linux os: linux
addons: *clang38 addons: *clang38
- env: CLANG_VERSION=3.9 BUILD_TYPE=Debug
os: linux
addons: &clang39
apt:
packages:
- clang-3.9
- g++-5
sources: &sources
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.9
- env: CLANG_VERSION=3.9 BUILD_TYPE=Release
os: linux
addons: *clang39
- env: GCC_VERSION=5 BUILD_TYPE=Debug - env: GCC_VERSION=5 BUILD_TYPE=Debug
os: linux os: linux
addons: &gcc5 addons: &gcc5
@ -110,7 +123,7 @@ install:
if [[ "$CLANG_VERSION" == "3.6" ]]; then LLVM_VERSION="3.6.2"; fi if [[ "$CLANG_VERSION" == "3.6" ]]; then LLVM_VERSION="3.6.2"; fi
if [[ "$CLANG_VERSION" == "3.7" ]]; then LLVM_VERSION="3.7.1"; fi if [[ "$CLANG_VERSION" == "3.7" ]]; then LLVM_VERSION="3.7.1"; fi
if [[ "$CLANG_VERSION" == "3.8" ]]; then LLVM_VERSION="3.8.1"; fi if [[ "$CLANG_VERSION" == "3.8" ]]; then LLVM_VERSION="3.8.1"; fi
if [[ "$CLANG_VERSION" == "3.9" ]]; then LLVM_VERSION="3.9.0"; fi if [[ "$CLANG_VERSION" == "3.9" ]]; then LLVM_VERSION="3.9.1"; fi
LLVM_ROOT="${DEPS_DIR}/llvm-${LLVM_VERSION}" LLVM_ROOT="${DEPS_DIR}/llvm-${LLVM_VERSION}"
LLVM_URL="http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz" LLVM_URL="http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz"
LIBCXX_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxx-${LLVM_VERSION}.src.tar.xz" LIBCXX_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxx-${LLVM_VERSION}.src.tar.xz"

View File

@ -2,12 +2,15 @@ cmake_minimum_required(VERSION 2.8.7)
project(GSL CXX) project(GSL CXX)
include(ExternalProject)
find_package(Git REQUIRED)
# creates a library GSL which is an interface (header files only) # creates a library GSL which is an interface (header files only)
add_library(GSL INTERFACE) add_library(GSL INTERFACE)
# when minimum version required is 3.8.0 remove if below # when minimum version required is 3.8.0 remove if below
# both branches do exactly the same thing # both branches do exactly the same thing
if ( CMAKE_MAJOR_VERSION VERSION_LESS 3.7.9) if (CMAKE_MAJOR_VERSION VERSION_LESS 3.7.9)
if (NOT MSVC) if (NOT MSVC)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)

View File

@ -40,8 +40,6 @@ contributing any changes that were necessary back to this project to benefit the
To build the tests, you will require the following: To build the tests, you will require the following:
* [CMake](http://cmake.org), version 2.8.7 or later to be installed and in your PATH. * [CMake](http://cmake.org), version 2.8.7 or later to be installed and in your PATH.
* [UnitTest-cpp](https://github.com/Microsoft/unittest-cpp), to be cloned under the [tests/unittest-cpp](./tests/unittest-cpp) directory
of your GSL source.
These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`. These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`.

View File

@ -13,16 +13,15 @@ image:
- Visual Studio 2017 - Visual Studio 2017
cache: cache:
- C:\cmake-3.7.2-win32-x86 - C:\cmake-3.8.0-win32-x86
install: install:
- git clone --quiet --depth=1 https://github.com/Microsoft/unittest-cpp.git tests/unittest-cpp
- ps: | - ps: |
if (![IO.File]::Exists("C:\cmake-3.7.2-win32-x86\bin\cmake.exe")) { if (![IO.File]::Exists("C:\cmake-3.8.0-win32-x86\bin\cmake.exe")) {
Start-FileDownload 'https://cmake.org/files/v3.7/cmake-3.7.2-win32-x86.zip' Start-FileDownload 'https://cmake.org/files/v3.8/cmake-3.8.0-win32-x86.zip'
7z x -y cmake-3.7.2-win32-x86.zip -oC:\ 7z x -y cmake-3.8.0-win32-x86.zip -oC:\
} }
$env:PATH="C:\cmake-3.7.2-win32-x86\bin;$env:PATH" $env:PATH="C:\cmake-3.8.0-win32-x86\bin;$env:PATH"
before_build: before_build:
- ps: | - ps: |

View File

@ -41,6 +41,7 @@
// turn off some warnings that are noisy about our Expects statements // turn off some warnings that are noisy about our Expects statements
#pragma warning(push) #pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant #pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
#if _MSC_VER < 1910 #if _MSC_VER < 1910
#pragma push_macro("constexpr") #pragma push_macro("constexpr")

View File

@ -37,6 +37,7 @@
// turn off some warnings that are noisy about our Expects statements // turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant #pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
// blanket turn off warnings from CppCoreCheck for now // blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool. // so people aren't annoyed by them when running the tool.

View File

@ -5,15 +5,21 @@ project(GSLTests CXX)
# will make visual studio generated project group files # will make visual studio generated project group files
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/unittest-cpp/CMakeLists.txt) list(APPEND CATCH_CMAKE_ARGS
find_package(Git) "-DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}"
execute_process( "-DNO_SELFTEST=true"
COMMAND ${GIT_EXECUTABLE} submodule update --init )
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
endif()
add_subdirectory(unittest-cpp) # add catch
ExternalProject_Add(
catch
PREFIX ${CMAKE_BINARY_DIR}/catch
GIT_REPOSITORY https://github.com/philsquared/Catch.git
# GIT_TAG v1.9.4 uncomment once v1.9.4 is available
CMAKE_ARGS ${CATCH_CMAKE_ARGS}
LOG_DOWNLOAD 1
UPDATE_DISCONNECTED 1
)
# this interface adds compile options to how the tests are run # this interface adds compile options to how the tests are run
# please try to keep entries ordered =) # please try to keep entries ordered =)
@ -42,24 +48,29 @@ target_compile_options(gsl_tests_config INTERFACE
> >
) )
# set test to include the unittest-cpp headers
# this shiuld be removed when UnitTest++ has the proper headers
target_include_directories(gsl_tests_config INTERFACE
./unittest-cpp
)
# set definitions for tests # set definitions for tests
target_compile_definitions(gsl_tests_config INTERFACE target_compile_definitions(gsl_tests_config INTERFACE
GSL_THROW_ON_CONTRACT_VIOLATION GSL_THROW_ON_CONTRACT_VIOLATION
) )
# create the main executable for each test. this reduces the compile time
# of each test by pre-compiling catch.
add_library(test_catch STATIC test.cpp)
target_link_libraries(test_catch
GSL
gsl_tests_config
)
add_dependencies(test_catch catch)
set_property(TARGET test_catch PROPERTY FOLDER "GSL_tests")
function(add_gsl_test name) function(add_gsl_test name)
add_executable(${name} ${name}.cpp) add_executable(${name} ${name}.cpp)
target_link_libraries(${name} target_link_libraries(${name}
UnitTest++
GSL GSL
test_catch
gsl_tests_config gsl_tests_config
) )
add_dependencies(${name} catch)
add_test( add_test(
${name} ${name}
${name} ${name}

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl_algorithm> #include <gsl/gsl_algorithm>
@ -23,11 +23,8 @@
using namespace std; using namespace std;
using namespace gsl; using namespace gsl;
SUITE(copy_tests) TEST_CASE("same_type")
{ {
TEST(same_type)
{
// dynamic source and destination span // dynamic source and destination span
{ {
std::array<int, 5> src{1, 2, 3, 4, 5}; std::array<int, 5> src{1, 2, 3, 4, 5};
@ -95,10 +92,10 @@ SUITE(copy_tests)
CHECK(dst[i + src.size()] == src[i]); CHECK(dst[i + src.size()] == src[i]);
} }
} }
} }
TEST(compatible_type) TEST_CASE("compatible_type")
{ {
// dynamic source and destination span // dynamic source and destination span
{ {
std::array<short, 5> src{1, 2, 3, 4, 5}; std::array<short, 5> src{1, 2, 3, 4, 5};
@ -166,11 +163,11 @@ SUITE(copy_tests)
CHECK(dst[i + src.size()] == src[i]); CHECK(dst[i + src.size()] == src[i]);
} }
} }
} }
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
TEST(incompatible_type) TEST_CASE("incompatible_type")
{ {
std::array<int, 4> src{1, 2, 3, 4}; std::array<int, 4> src{1, 2, 3, 4};
std::array<int*, 12> dst{}; std::array<int*, 12> dst{};
@ -184,11 +181,11 @@ SUITE(copy_tests)
copy(src_span_dyn, dst_span_static); copy(src_span_dyn, dst_span_static);
copy(src_span_static, dst_span_dyn); copy(src_span_static, dst_span_dyn);
copy(src_span_static, dst_span_static); copy(src_span_static, dst_span_static);
} }
#endif #endif
TEST(small_destination_span) TEST_CASE("small_destination_span")
{ {
std::array<int, 12> src{1, 2, 3, 4}; std::array<int, 12> src{1, 2, 3, 4};
std::array<int, 4> dst{}; std::array<int, 4> dst{};
@ -197,14 +194,11 @@ SUITE(copy_tests)
span<int> dst_span_dyn(dst); span<int> dst_span_dyn(dst);
span<int, 4> dst_span_static(dst); span<int, 4> dst_span_static(dst);
CHECK_THROW(copy(src_span_dyn, dst_span_dyn), fail_fast); CHECK_THROWS_AS(copy(src_span_dyn, dst_span_dyn), fail_fast);
CHECK_THROW(copy(src_span_dyn, dst_span_static), fail_fast); CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast);
CHECK_THROW(copy(src_span_static, dst_span_dyn), fail_fast); CHECK_THROWS_AS(copy(src_span_static, dst_span_dyn), fail_fast);
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static); copy(src_span_static, dst_span_static);
#endif #endif
}
} }
int main() { return UnitTest::RunAllTests(); }

View File

@ -14,38 +14,33 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> #include <gsl/gsl>
using namespace gsl; using namespace gsl;
SUITE(assertion_tests) int f(int i)
{ {
int f(int i)
{
Expects(i > 0 && i < 10); Expects(i > 0 && i < 10);
return i; return i;
} }
TEST(expects) TEST_CASE("expects")
{ {
CHECK(f(2) == 2); CHECK(f(2) == 2);
CHECK_THROW(f(10), fail_fast); CHECK_THROWS_AS(f(10), fail_fast);
} }
int g(int i) int g(int i)
{ {
i++; i++;
Ensures(i > 0 && i < 10); Ensures(i > 0 && i < 10);
return i; return i;
}
TEST(ensures)
{
CHECK(g(2) == 3);
CHECK_THROW(g(9), fail_fast);
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("ensures")
{
CHECK(g(2) == 3);
CHECK_THROWS_AS(g(9), fail_fast);
}

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> #include <gsl/gsl>
@ -23,10 +23,8 @@
using gsl::fail_fast; using gsl::fail_fast;
SUITE(at_tests) TEST_CASE("static_array")
{ {
TEST(static_array)
{
int a[4] = {1, 2, 3, 4}; int a[4] = {1, 2, 3, 4};
const int(&c_a)[4] = a; const int(&c_a)[4] = a;
@ -35,14 +33,14 @@ SUITE(at_tests)
CHECK(&gsl::at(c_a, i) == &a[i]); CHECK(&gsl::at(c_a, i) == &a[i]);
} }
CHECK_THROW(gsl::at(a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at(c_a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROW(gsl::at(c_a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
} }
TEST(std_array) TEST_CASE("std_array")
{ {
std::array<int, 4> a = {1, 2, 3, 4}; std::array<int, 4> a = {1, 2, 3, 4};
const std::array<int, 4>& c_a = a; const std::array<int, 4>& c_a = a;
@ -51,14 +49,14 @@ SUITE(at_tests)
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
} }
CHECK_THROW(gsl::at(a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at(c_a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROW(gsl::at(c_a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
} }
TEST(StdVector) TEST_CASE("StdVector")
{ {
std::vector<int> a = {1, 2, 3, 4}; std::vector<int> a = {1, 2, 3, 4};
const std::vector<int>& c_a = a; const std::vector<int>& c_a = a;
@ -67,14 +65,14 @@ SUITE(at_tests)
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]); CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
} }
CHECK_THROW(gsl::at(a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at(c_a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROW(gsl::at(c_a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
} }
TEST(InitializerList) TEST_CASE("InitializerList")
{ {
std::initializer_list<int> a = {1, 2, 3, 4}; std::initializer_list<int> a = {1, 2, 3, 4};
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
@ -82,11 +80,10 @@ SUITE(at_tests)
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1); CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
} }
CHECK_THROW(gsl::at(a, -1), fail_fast); CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at({1, 2, 3, 4}, -1), fail_fast); CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast);
CHECK_THROW(gsl::at({1, 2, 3, 4}, 4), fail_fast); CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast);
}
} }
#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910 #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
@ -111,5 +108,3 @@ static constexpr bool test_constexpr()
static_assert(test_constexpr(), "FAIL"); static_assert(test_constexpr(), "FAIL");
#endif #endif
int main() { return UnitTest::RunAllTests(); }

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/multi_span> #include <gsl/multi_span>
@ -28,10 +28,8 @@ namespace
void use(std::ptrdiff_t&) {} void use(std::ptrdiff_t&) {}
} }
SUITE(bounds_test) TEST_CASE("basic_bounds")
{ {
TEST(basic_bounds)
{
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) { for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
for (decltype(point)::size_type j = 0; for (decltype(point)::size_type j = 0;
j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++) j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
@ -40,19 +38,19 @@ SUITE(bounds_test)
use(point[static_cast<std::size_t>(j)]); use(point[static_cast<std::size_t>(j)]);
} }
} }
} }
TEST(bounds_basic) TEST_CASE("bounds_basic")
{ {
static_bounds<3, 4, 5> b; static_bounds<3, 4, 5> b;
const auto a = b.slice(); const auto a = b.slice();
(void) a; (void) a;
static_bounds<4, dynamic_range, 2> x{4}; static_bounds<4, dynamic_range, 2> x{4};
x.slice().slice(); x.slice().slice();
} }
TEST(arrayview_iterator) TEST_CASE("arrayview_iterator")
{ {
static_bounds<4, dynamic_range, 2> bounds{3}; static_bounds<4, dynamic_range, 2> bounds{3};
const auto itr = bounds.begin(); const auto itr = bounds.begin();
@ -67,10 +65,10 @@ SUITE(bounds_test)
} }
fill(av.begin(), av.end(), 0); fill(av.begin(), av.end(), 0);
#endif #endif
} }
TEST(bounds_convertible) TEST_CASE("bounds_convertible")
{ {
static_bounds<7, 4, 2> b1; static_bounds<7, 4, 2> b1;
static_bounds<7, dynamic_range, 2> b2 = b1; static_bounds<7, dynamic_range, 2> b2 = b1;
(void) b2; (void) b2;
@ -88,13 +86,10 @@ SUITE(bounds_test)
static_bounds<34> b6; static_bounds<34> b6;
b5 = static_bounds<20>(); b5 = static_bounds<20>();
CHECK_THROW(b6 = b5, fail_fast); CHECK_THROWS_AS(b6 = b5, fail_fast);
b5 = static_bounds<34>(); b5 = static_bounds<34>();
b6 = b5; b6 = b5;
CHECK(b5 == b6); CHECK(b5 == b6);
CHECK(b5.size() == b6.size()); CHECK(b5.size() == b6.size());
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl_byte> #include <gsl/gsl_byte>
@ -31,10 +31,8 @@ using namespace gsl;
namespace namespace
{ {
SUITE(byte_tests) TEST_CASE("construction")
{ {
TEST(construction)
{
{ {
const byte b = static_cast<byte>(4); const byte b = static_cast<byte>(4);
CHECK(static_cast<unsigned char>(b) == 4); CHECK(static_cast<unsigned char>(b) == 4);
@ -60,10 +58,10 @@ SUITE(byte_tests)
// byte b { 14 }; // byte b { 14 };
// CHECK(static_cast<unsigned char>(b) == 14); // CHECK(static_cast<unsigned char>(b) == 14);
//} //}
} }
TEST(bitwise_operations) TEST_CASE("bitwise_operations")
{ {
const byte b = to_byte<0xFF>(); const byte b = to_byte<0xFF>();
byte a = to_byte<0x00>(); byte a = to_byte<0x00>();
@ -96,10 +94,10 @@ SUITE(byte_tests)
CHECK(a == to_byte<0xF0>()); CHECK(a == to_byte<0xF0>());
a >>= 4; a >>= 4;
CHECK(a == to_byte<0x0F>()); CHECK(a == to_byte<0x0F>());
} }
TEST(to_integer) TEST_CASE("to_integer")
{ {
const byte b = to_byte<0x12>(); const byte b = to_byte<0x12>();
CHECK(0x12 == gsl::to_integer<char>(b)); CHECK(0x12 == gsl::to_integer<char>(b));
@ -114,22 +112,20 @@ SUITE(byte_tests)
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error // CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error // CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
} }
int modify_both(gsl::byte & b, int& i) int modify_both(gsl::byte & b, int& i)
{ {
i = 10; i = 10;
b = to_byte<5>(); b = to_byte<5>();
return i; return i;
} }
TEST(aliasing) TEST_CASE("aliasing")
{ {
int i{0}; int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i); const int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i); CHECK(res == i);
}
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } }

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> #include <gsl/gsl>
@ -95,13 +95,10 @@ std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
: "false"; : "false";
} }
SUITE(NotNullTests) bool helper(not_null<int*> p) { return *p == 12; }
TEST_CASE("TestNotNullConstructors")
{ {
bool helper(not_null<int*> p) { return *p == 12; }
TEST(TestNotNullConstructors)
{
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
not_null<int*> p = nullptr; // yay...does not compile! not_null<int*> p = nullptr; // yay...does not compile!
not_null<std::vector<char>*> p = 0; // yay...does not compile! not_null<std::vector<char>*> p = 0; // yay...does not compile!
@ -121,10 +118,10 @@ SUITE(NotNullTests)
not_null<std::shared_ptr<int>> x( not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
} }
TEST(TestNotNullCasting) TEST_CASE("TestNotNullCasting")
{ {
MyBase base; MyBase base;
MyDerived derived; MyDerived derived;
Unrelated unrelated; Unrelated unrelated;
@ -144,20 +141,20 @@ SUITE(NotNullTests)
#endif #endif
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get()); not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get())); CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
} }
TEST(TestNotNullAssignment) TEST_CASE("TestNotNullAssignment")
{ {
int i = 12; int i = 12;
not_null<int*> p = &i; not_null<int*> p = &i;
CHECK(helper(p)); CHECK(helper(p));
int* q = nullptr; int* q = nullptr;
CHECK_THROW(p = q, fail_fast); CHECK_THROWS_AS(p = q, fail_fast);
} }
TEST(TestNotNullRawPointerComparison) TEST_CASE("TestNotNullRawPointerComparison")
{ {
int ints[2] = {42, 43}; int ints[2] = {42, 43};
int* p1 = &ints[0]; int* p1 = &ints[0];
const int* p2 = &ints[1]; const int* p2 = &ints[1];
@ -186,10 +183,10 @@ SUITE(NotNullTests)
CHECK((NotNull1(p1) >= NotNull1(p1)) == true); CHECK((NotNull1(p1) >= NotNull1(p1)) == true);
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
} }
TEST(TestNotNullSharedPtrComparison) TEST_CASE("TestNotNullSharedPtrComparison")
{ {
auto sp1 = std::make_shared<int>(42); auto sp1 = std::make_shared<int>(42);
auto sp2 = std::make_shared<const int>(43); auto sp2 = std::make_shared<const int>(43);
@ -217,10 +214,10 @@ SUITE(NotNullTests)
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true); CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2)); CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1)); CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
} }
TEST(TestNotNullCustomPtrComparison) TEST_CASE("TestNotNullCustomPtrComparison")
{ {
int ints[2] = {42, 43}; int ints[2] = {42, 43};
CustomPtr<int> p1(&ints[0]); CustomPtr<int> p1(&ints[0]);
CustomPtr<const int> p2(&ints[1]); CustomPtr<const int> p2(&ints[1]);
@ -249,7 +246,4 @@ SUITE(NotNullTests)
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true"); CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> #include <gsl/gsl>
@ -22,18 +22,13 @@
using namespace gsl; using namespace gsl;
SUITE(owner_tests) void f(int* i) { *i += 1; }
{
void f(int* i) { *i += 1; }
TEST(basic_test) TEST_CASE("basic_test")
{ {
owner<int*> p = new int(120); owner<int*> p = new int(120);
CHECK(*p == 120); CHECK(*p == 120);
f(p); f(p);
CHECK(*p == 121); CHECK(*p == 121);
delete p; delete p;
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/multi_span> #include <gsl/multi_span>
@ -38,20 +38,18 @@ struct DerivedClass : BaseClass
}; };
} }
SUITE(strided_span_tests) TEST_CASE("span_section_test")
{ {
TEST(span_section_test)
{
int a[30][4][5]; int a[30][4][5];
const auto av = as_multi_span(a); const auto av = as_multi_span(a);
const auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2}); const auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2});
const auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1}); const auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1});
(void) subsub; (void) subsub;
} }
TEST(span_section) TEST_CASE("span_section")
{ {
std::vector<int> data(5 * 10); std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0); std::iota(begin(data), end(data), 0);
const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>()); const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
@ -66,10 +64,10 @@ SUITE(strided_span_tests)
CHECK((av_section_2[{0, 0}] == 24)); CHECK((av_section_2[{0, 0}] == 24));
CHECK((av_section_2[{0, 1}] == 25)); CHECK((av_section_2[{0, 1}] == 25));
CHECK((av_section_2[{1, 0}] == 34)); CHECK((av_section_2[{1, 0}] == 34));
} }
TEST(strided_span_constructors) TEST_CASE("strided_span_constructors")
{ {
// Check stride constructor // Check stride constructor
{ {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
@ -78,12 +76,12 @@ SUITE(strided_span_tests)
strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T
CHECK(sav1.bounds().index_bounds() == index<1>{9}); CHECK(sav1.bounds().index_bounds() == index<1>{9});
CHECK(sav1.bounds().stride() == 1); CHECK(sav1.bounds().stride() == 1);
CHECK(sav1[0] == 1 && sav1[8] == 9); CHECK((sav1[0] == 1 && sav1[8] == 9));
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
CHECK(sav2.bounds().index_bounds() == index<1>{4}); CHECK(sav2.bounds().index_bounds() == index<1>{4});
CHECK(sav2.bounds().strides() == index<1>{2}); CHECK(sav2.bounds().strides() == index<1>{2});
CHECK(sav2[0] == 1 && sav2[3] == 7); CHECK((sav2[0] == 1 && sav2[3] == 7));
strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T
CHECK((sav3.bounds().index_bounds() == index<2>{2, 2})); CHECK((sav3.bounds().index_bounds() == index<2>{2, 2}));
@ -266,10 +264,10 @@ SUITE(strided_span_tests)
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
CHECK(&sav2_ref == &sav2); CHECK(&sav2_ref == &sav2);
} }
} }
TEST(strided_span_slice) TEST_CASE("strided_span_slice")
{ {
std::vector<int> data(5 * 10); std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0); std::iota(begin(data), end(data), 0);
const multi_span<int, 5, 10> src = const multi_span<int, 5, 10> src =
@ -292,10 +290,10 @@ SUITE(strided_span_tests)
CHECK(sav[4][0] == 40); CHECK(sav[4][0] == 40);
CHECK(sav[4][9] == 49); CHECK(sav[4][9] == 49);
} }
TEST(strided_span_column_major) TEST_CASE("strided_span_column_major")
{ {
// strided_span may be used to accomodate more peculiar // strided_span may be used to accomodate more peculiar
// use cases, such as column-major multidimensional array // use cases, such as column-major multidimensional array
// (aka. "FORTRAN" layout). // (aka. "FORTRAN" layout).
@ -324,25 +322,25 @@ SUITE(strided_span_tests)
CHECK((cm_sec[{0, 1}] == 9)); CHECK((cm_sec[{0, 1}] == 9));
CHECK((cm_sec[{1, 0}] == 11)); CHECK((cm_sec[{1, 0}] == 11));
CHECK((cm_sec[{2, 1}] == 15)); CHECK((cm_sec[{2, 1}] == 15));
} }
TEST(strided_span_bounds) TEST_CASE("strided_span_bounds")
{ {
int arr[] = {0, 1, 2, 3}; int arr[] = {0, 1, 2, 3};
multi_span<int> av(arr); multi_span<int> av(arr);
{ {
// incorrect sections // incorrect sections
CHECK_THROW(av.section(0, 0)[0], fail_fast); CHECK_THROWS_AS(av.section(0, 0)[0], fail_fast);
CHECK_THROW(av.section(1, 0)[0], fail_fast); CHECK_THROWS_AS(av.section(1, 0)[0], fail_fast);
CHECK_THROW(av.section(1, 1)[1], fail_fast); CHECK_THROWS_AS(av.section(1, 1)[1], fail_fast);
CHECK_THROW(av.section(2, 5), fail_fast); CHECK_THROWS_AS(av.section(2, 5), fail_fast);
CHECK_THROW(av.section(5, 2), fail_fast); CHECK_THROWS_AS(av.section(5, 2), fail_fast);
CHECK_THROW(av.section(5, 0), fail_fast); CHECK_THROWS_AS(av.section(5, 0), fail_fast);
CHECK_THROW(av.section(0, 5), fail_fast); CHECK_THROWS_AS(av.section(0, 5), fail_fast);
CHECK_THROW(av.section(5, 5), fail_fast); CHECK_THROWS_AS(av.section(5, 5), fail_fast);
} }
{ {
@ -350,19 +348,19 @@ SUITE(strided_span_tests)
strided_span<int, 1> sav{av, {{4}, {}}}; strided_span<int, 1> sav{av, {{4}, {}}};
CHECK(sav[0] == 0); CHECK(sav[0] == 0);
CHECK(sav[3] == 0); CHECK(sav[3] == 0);
CHECK_THROW(sav[4], fail_fast); CHECK_THROWS_AS(sav[4], fail_fast);
} }
{ {
// zero extent // zero extent
strided_span<int, 1> sav{av, {{}, {1}}}; strided_span<int, 1> sav{av, {{}, {1}}};
CHECK_THROW(sav[0], fail_fast); CHECK_THROWS_AS(sav[0], fail_fast);
} }
{ {
// zero extent and stride // zero extent and stride
strided_span<int, 1> sav{av, {{}, {}}}; strided_span<int, 1> sav{av, {{}, {}}};
CHECK_THROW(sav[0], fail_fast); CHECK_THROWS_AS(sav[0], fail_fast);
} }
{ {
@ -370,7 +368,7 @@ SUITE(strided_span_tests)
strided_span<int, 1> sav{arr, {4, 1}}; strided_span<int, 1> sav{arr, {4, 1}};
CHECK(sav.bounds().index_bounds() == index<1>{4}); CHECK(sav.bounds().index_bounds() == index<1>{4});
CHECK(sav[3] == 3); CHECK(sav[3] == 3);
CHECK_THROW(sav[4], fail_fast); CHECK_THROWS_AS(sav[4], fail_fast);
} }
{ {
@ -378,7 +376,7 @@ SUITE(strided_span_tests)
strided_span<int, 1> sav{arr, {2, 1}}; strided_span<int, 1> sav{arr, {2, 1}};
CHECK(sav.bounds().index_bounds() == index<1>{2}); CHECK(sav.bounds().index_bounds() == index<1>{2});
CHECK(sav[1] == 1); CHECK(sav[1] == 1);
CHECK_THROW(sav[2], fail_fast); CHECK_THROWS_AS(sav[2], fail_fast);
} }
{ {
@ -387,35 +385,35 @@ SUITE(strided_span_tests)
CHECK(sav.bounds().index_bounds() == index<1>{2}); CHECK(sav.bounds().index_bounds() == index<1>{2});
CHECK(sav[0] == 0); CHECK(sav[0] == 0);
CHECK(sav[1] == 3); CHECK(sav[1] == 3);
CHECK_THROW(sav[2], fail_fast); CHECK_THROWS_AS(sav[2], fail_fast);
} }
{ {
// bounds cross data boundaries - from static arrays // bounds cross data boundaries - from static arrays
CHECK_THROW((strided_span<int, 1>{arr, {3, 2}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 2}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{arr, {3, 3}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 3}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{arr, {4, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{arr, {4, 5}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{arr, {5, 1}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 1}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{arr, {5, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 5}}), fail_fast);
} }
{ {
// bounds cross data boundaries - from array view // bounds cross data boundaries - from array view
CHECK_THROW((strided_span<int, 1>{av, {3, 2}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 2}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av, {3, 3}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 3}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av, {4, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av, {4, 5}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av, {5, 1}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 1}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av, {5, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 5}}), fail_fast);
} }
{ {
// bounds cross data boundaries - from dynamic arrays // bounds cross data boundaries - from dynamic arrays
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast);
CHECK_THROW((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast); CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast);
} }
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
@ -440,10 +438,10 @@ SUITE(strided_span_tests)
strided_span<int, 2> sav14{av.as_multi_span(dim<2>(), dim<2>()), {{1, 1, 1}, {1}}}; strided_span<int, 2> sav14{av.as_multi_span(dim<2>(), dim<2>()), {{1, 1, 1}, {1}}};
} }
#endif #endif
} }
TEST(strided_span_type_conversion) TEST_CASE("strided_span_type_conversion")
{ {
int arr[] = {0, 1, 2, 3}; int arr[] = {0, 1, 2, 3};
multi_span<int> av(arr); multi_span<int> av(arr);
@ -469,8 +467,8 @@ SUITE(strided_span_tests)
strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>(); strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>();
CHECK(sav3[0][0] == 0); CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2); CHECK(sav3[1][0] == 2);
CHECK_THROW(sav3[1][1], fail_fast); CHECK_THROWS_AS(sav3[1][1], fail_fast);
CHECK_THROW(sav3[0][1], fail_fast); CHECK_THROWS_AS(sav3[0][1], fail_fast);
} }
// retype strided array with regular strides - from multi_span // retype strided array with regular strides - from multi_span
@ -482,8 +480,8 @@ SUITE(strided_span_tests)
strided_span<int, 2> sav3 = sav2.as_strided_span<int>(); strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
CHECK(sav3[0][0] == 0); CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2); CHECK(sav3[1][0] == 2);
CHECK_THROW(sav3[1][1], fail_fast); CHECK_THROWS_AS(sav3[1][1], fail_fast);
CHECK_THROW(sav3[0][1], fail_fast); CHECK_THROWS_AS(sav3[0][1], fail_fast);
} }
// retype strided array with not enough elements - last dimension of the array is too small // retype strided array with not enough elements - last dimension of the array is too small
@ -492,7 +490,7 @@ SUITE(strided_span_tests)
multi_span<const byte, 2, dynamic_range> bytes2 = multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds}; strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
} }
// retype strided array with not enough elements - strides are too small // retype strided array with not enough elements - strides are too small
@ -501,7 +499,7 @@ SUITE(strided_span_tests)
multi_span<const byte, 2, dynamic_range> bytes2 = multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds}; strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
} }
// retype strided array with not enough elements - last dimension does not divide by the new // retype strided array with not enough elements - last dimension does not divide by the new
@ -511,7 +509,7 @@ SUITE(strided_span_tests)
multi_span<const byte, 2, dynamic_range> bytes2 = multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds}; strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
} }
// retype strided array with not enough elements - strides does not divide by the new // retype strided array with not enough elements - strides does not divide by the new
@ -521,34 +519,34 @@ SUITE(strided_span_tests)
multi_span<const byte, 2, dynamic_range> bytes2 = multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds}; strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
} }
// retype strided array with irregular strides - from raw data // retype strided array with irregular strides - from raw data
{ {
strided_bounds<1> bounds{bytes.size() / 2, 2}; strided_bounds<1> bounds{bytes.size() / 2, 2};
strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds}; strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
} }
// retype strided array with irregular strides - from multi_span // retype strided array with irregular strides - from multi_span
{ {
strided_bounds<1> bounds{bytes.size() / 2, 2}; strided_bounds<1> bounds{bytes.size() / 2, 2};
strided_span<const byte, 1> sav2{bytes, bounds}; strided_span<const byte, 1> sav2{bytes, bounds};
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast); CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
}
} }
}
TEST(empty_strided_spans) TEST_CASE("empty_strided_spans")
{ {
{ {
multi_span<int, 0> empty_av(nullptr); multi_span<int, 0> empty_av(nullptr);
strided_span<int, 1> empty_sav{empty_av, {0, 1}}; strided_span<int, 1> empty_sav{empty_av, {0, 1}};
CHECK(empty_sav.bounds().index_bounds() == index<1>{0}); CHECK(empty_sav.bounds().index_bounds() == index<1>{0});
CHECK_THROW(empty_sav[0], fail_fast); CHECK_THROWS_AS(empty_sav[0], fail_fast);
CHECK_THROW(empty_sav.begin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
CHECK_THROW(empty_sav.cbegin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
for (const auto& v : empty_sav) { for (const auto& v : empty_sav) {
(void) v; (void) v;
@ -560,19 +558,19 @@ SUITE(strided_span_tests)
strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}}; strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}};
CHECK(empty_sav.bounds().index_bounds() == index<1>{0}); CHECK(empty_sav.bounds().index_bounds() == index<1>{0});
CHECK_THROW(empty_sav[0], fail_fast); CHECK_THROWS_AS(empty_sav[0], fail_fast);
CHECK_THROW(empty_sav.begin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
CHECK_THROW(empty_sav.cbegin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
for (const auto& v : empty_sav) { for (const auto& v : empty_sav) {
(void) v; (void) v;
CHECK(false); CHECK(false);
} }
} }
} }
void iterate_every_other_element(multi_span<int, dynamic_range> av) void iterate_every_other_element(multi_span<int, dynamic_range> av)
{ {
// pick every other element // pick every other element
auto length = av.size() / 2; auto length = av.size() / 2;
@ -594,10 +592,10 @@ SUITE(strided_span_tests)
CHECK(num == av[2 * idx + 1]); CHECK(num == av[2 * idx + 1]);
idx++; idx++;
} }
} }
TEST(strided_span_section_iteration) TEST_CASE("strided_span_section_iteration")
{ {
int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3}; int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3};
// static bounds // static bounds
@ -611,10 +609,10 @@ SUITE(strided_span_tests)
multi_span<int, dynamic_range> av(arr, 8); multi_span<int, dynamic_range> av(arr, 8);
iterate_every_other_element(av); iterate_every_other_element(av);
} }
} }
TEST(dynamic_strided_span_section_iteration) TEST_CASE("dynamic_strided_span_section_iteration")
{ {
auto arr = new int[8]; auto arr = new int[8];
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
arr[2 * i] = 4 + i; arr[2 * i] = 4 + i;
@ -625,10 +623,10 @@ SUITE(strided_span_tests)
iterate_every_other_element(av); iterate_every_other_element(av);
delete[] arr; delete[] arr;
} }
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av) void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
{ {
const int expected[6] = {2, 3, 10, 11, 18, 19}; const int expected[6] = {2, 3, 10, 11, 18, 19};
auto section = av.section({0, 1, 0}, {3, 1, 2}); auto section = av.section({0, 1, 0}, {3, 1, 2});
@ -651,10 +649,10 @@ SUITE(strided_span_tests)
CHECK(num == expected[i]); CHECK(num == expected[i]);
i++; i++;
} }
} }
TEST(strided_span_section_iteration_3d) TEST_CASE("strided_span_section_iteration_3d")
{ {
int arr[3][4][2]{}; int arr[3][4][2]{};
for (auto i = 0; i < 3; ++i) { for (auto i = 0; i < 3; ++i) {
for (auto j = 0; j < 4; ++j) for (auto j = 0; j < 4; ++j)
@ -665,10 +663,10 @@ SUITE(strided_span_tests)
multi_span<int, 3, 4, 2> av = arr; multi_span<int, 3, 4, 2> av = arr;
iterate_second_slice(av); iterate_second_slice(av);
} }
} }
TEST(dynamic_strided_span_section_iteration_3d) TEST_CASE("dynamic_strided_span_section_iteration_3d")
{ {
const auto height = 12, width = 2; const auto height = 12, width = 2;
const auto size = height * width; const auto size = height * width;
@ -697,10 +695,10 @@ SUITE(strided_span_tests)
iterate_second_slice(av); iterate_second_slice(av);
} }
delete[] arr; delete[] arr;
} }
TEST(strided_span_conversion) TEST_CASE("strided_span_conversion")
{ {
// get an multi_span of 'c' values from the list of X's // get an multi_span of 'c' values from the list of X's
struct X struct X
@ -744,14 +742,11 @@ SUITE(strided_span_tests)
strided_span<int, 1> result = transposed[0]; strided_span<int, 1> result = transposed[0];
CHECK(result.bounds().index_bounds()[0] == 4); CHECK(result.bounds().index_bounds()[0] == 4);
CHECK_THROW(result.bounds().index_bounds()[1], fail_fast); CHECK_THROWS_AS(result.bounds().index_bounds()[1], fail_fast);
int i = 0; int i = 0;
for (auto& num : result) { for (auto& num : result) {
CHECK(num == arr[i].c); CHECK(num == arr[i].c);
i++; i++;
} }
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> //owner #include <gsl/gsl> //owner
#include <gsl/string_span> #include <gsl/string_span>
@ -26,35 +26,32 @@
using namespace std; using namespace std;
using namespace gsl; using namespace gsl;
SUITE(string_span_tests) TEST_CASE("TestLiteralConstruction")
{ {
TEST(TestLiteralConstruction)
{
cwstring_span<> v = ensure_z(L"Hello"); cwstring_span<> v = ensure_z(L"Hello");
CHECK(5 == v.length()); CHECK(5 == v.length());
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
wstring_span<> v2 = ensure0(L"Hello"); wstring_span<> v2 = ensure0(L"Hello");
#endif #endif
} }
TEST(TestConstructFromStdString) TEST_CASE("TestConstructFromStdString")
{ {
std::string s = "Hello there world"; std::string s = "Hello there world";
cstring_span<> v = s; cstring_span<> v = s;
CHECK(v.length() == static_cast<cstring_span<>::index_type>(s.length())); CHECK(v.length() == static_cast<cstring_span<>::index_type>(s.length()));
} }
TEST(TestConstructFromStdVector) TEST_CASE("TestConstructFromStdVector")
{ {
std::vector<char> vec(5, 'h'); std::vector<char> vec(5, 'h');
string_span<> v{vec}; string_span<> v{vec};
CHECK(v.length() == static_cast<string_span<>::index_type>(vec.size())); CHECK(v.length() == static_cast<string_span<>::index_type>(vec.size()));
} }
TEST(TestStackArrayConstruction) TEST_CASE("TestStackArrayConstruction")
{ {
wchar_t stack_string[] = L"Hello"; wchar_t stack_string[] = L"Hello";
{ {
@ -76,25 +73,25 @@ SUITE(string_span_tests)
wstring_span<> v = stack_string; wstring_span<> v = stack_string;
CHECK(v.length() == 5); CHECK(v.length() == 5);
} }
} }
TEST(TestConstructFromConstCharPointer) TEST_CASE("TestConstructFromConstCharPointer")
{ {
const char* s = "Hello"; const char* s = "Hello";
cstring_span<> v = ensure_z(s); cstring_span<> v = ensure_z(s);
CHECK(v.length() == 5); CHECK(v.length() == 5);
} }
TEST(TestConversionToConst) TEST_CASE("TestConversionToConst")
{ {
char stack_string[] = "Hello"; char stack_string[] = "Hello";
string_span<> v = ensure_z(stack_string); string_span<> v = ensure_z(stack_string);
cstring_span<> v2 = v; cstring_span<> v2 = v;
CHECK(v.length() == v2.length()); CHECK(v.length() == v2.length());
} }
TEST(TestConversionFromConst) TEST_CASE("TestConversionFromConst")
{ {
char stack_string[] = "Hello"; char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string); cstring_span<> v = ensure_z(stack_string);
(void) v; (void) v;
@ -102,10 +99,10 @@ SUITE(string_span_tests)
string_span<> v2 = v; string_span<> v2 = v;
string_span<> v3 = "Hello"; string_span<> v3 = "Hello";
#endif #endif
} }
TEST(TestToString) TEST_CASE("TestToString")
{ {
auto s = gsl::to_string(cstring_span<>{}); auto s = gsl::to_string(cstring_span<>{});
CHECK(s.length() == 0); CHECK(s.length() == 0);
@ -114,10 +111,10 @@ SUITE(string_span_tests)
auto s2 = gsl::to_string(v); auto s2 = gsl::to_string(v);
CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length()); CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
CHECK(s2.length() == 5); CHECK(s2.length() == 5);
} }
TEST(TestToBasicString) TEST_CASE("TestToBasicString")
{ {
auto s = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>( auto s = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(
cstring_span<>{}); cstring_span<>{});
CHECK(s.length() == 0); CHECK(s.length() == 0);
@ -127,10 +124,10 @@ SUITE(string_span_tests)
auto s2 = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(v); auto s2 = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(v);
CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length()); CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
CHECK(s2.length() == 5); CHECK(s2.length() == 5);
} }
TEST(EqualityAndImplicitConstructors) TEST_CASE("EqualityAndImplicitConstructors")
{ {
{ {
cstring_span<> span = "Hello"; cstring_span<> span = "Hello";
cstring_span<> span1; cstring_span<> span1;
@ -353,10 +350,10 @@ SUITE(string_span_tests)
// comparison of spans from the same vector before and after move (ok) // comparison of spans from the same vector before and after move (ok)
CHECK(span1 == span2); CHECK(span1 == span2);
} }
} }
TEST(ComparisonAndImplicitConstructors) TEST_CASE("ComparisonAndImplicitConstructors")
{ {
{ {
cstring_span<> span = "Hello"; cstring_span<> span = "Hello";
@ -424,9 +421,9 @@ SUITE(string_span_tests)
// comparison to vector of charaters with no null termination // comparison to vector of charaters with no null termination
CHECK(span >= string_span<>(vec)); CHECK(span >= string_span<>(vec));
} }
} }
TEST(ConstrutorsEnsureZ) TEST_CASE("ConstrutorsEnsureZ")
{ {
// remove z from literals // remove z from literals
{ {
cstring_span<> sp = "hello"; cstring_span<> sp = "hello";
@ -453,10 +450,10 @@ SUITE(string_span_tests)
delete[] ptr; delete[] ptr;
} }
} }
TEST(Constructors) TEST_CASE("Constructors")
{ {
// creating cstring_span // creating cstring_span
// from span of a final extent // from span of a final extent
@ -748,27 +745,27 @@ SUITE(string_span_tests)
const string_span<> span = tmp; const string_span<> span = tmp;
CHECK(span.length() == 5); CHECK(span.length() == 5);
} }
} }
template <typename T> template <typename T>
T move_wrapper(T && t) T move_wrapper(T && t)
{ {
return std::move(t); return std::move(t);
} }
template <class T> template <class T>
T create() T create()
{ {
return T{}; return T{};
} }
template <class T> template <class T>
void use(basic_string_span<T, gsl::dynamic_extent>) void use(basic_string_span<T, gsl::dynamic_extent>)
{ {
} }
TEST(MoveConstructors) TEST_CASE("MoveConstructors")
{ {
// move string_span // move string_span
{ {
cstring_span<> span = "Hello"; cstring_span<> span = "Hello";
@ -839,19 +836,19 @@ SUITE(string_span_tests)
use<char>(create<std::vector<char>>()); use<char>(create<std::vector<char>>());
#endif #endif
} }
} }
TEST(Conversion) TEST_CASE("Conversion")
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
cstring_span<> span = "Hello"; cstring_span<> span = "Hello";
cwstring_span<> wspan{span}; cwstring_span<> wspan{span};
CHECK(wspan.length() == 5); CHECK(wspan.length() == 5);
#endif #endif
} }
czstring_span<> CreateTempName(string_span<> span) czstring_span<> CreateTempName(string_span<> span)
{ {
Expects(span.size() > 1); Expects(span.size() > 1);
int last = 0; int last = 0;
@ -865,10 +862,10 @@ SUITE(string_span_tests)
auto ret = span.subspan(0, 4); auto ret = span.subspan(0, 4);
return {ret}; return {ret};
} }
TEST(zstring) TEST_CASE("zstring")
{ {
// create zspan from zero terminated string // create zspan from zero terminated string
{ {
@ -888,7 +885,7 @@ SUITE(string_span_tests)
buf[0] = 'a'; buf[0] = 'a';
auto workaround_macro = [&]() { zstring_span<> zspan({buf, 1}); }; auto workaround_macro = [&]() { zstring_span<> zspan({buf, 1}); };
CHECK_THROW(workaround_macro(), fail_fast); CHECK_THROWS_AS(workaround_macro(), fail_fast);
} }
// usage scenario: create zero-terminated temp file name and pass to a legacy API // usage scenario: create zero-terminated temp file name and pass to a legacy API
@ -902,10 +899,10 @@ SUITE(string_span_tests)
CHECK(*(str + 3) == '\0'); CHECK(*(str + 3) == '\0');
} }
} }
} }
cwzstring_span<> CreateTempNameW(wstring_span<> span) cwzstring_span<> CreateTempNameW(wstring_span<> span)
{ {
Expects(span.size() > 1); Expects(span.size() > 1);
int last = 0; int last = 0;
@ -919,10 +916,10 @@ SUITE(string_span_tests)
auto ret = span.subspan(0, 4); auto ret = span.subspan(0, 4);
return {ret}; return {ret};
} }
TEST(wzstring) TEST_CASE("wzstring")
{ {
// create zspan from zero terminated string // create zspan from zero terminated string
{ {
@ -942,7 +939,7 @@ SUITE(string_span_tests)
buf[0] = L'a'; buf[0] = L'a';
const auto workaround_macro = [&]() { wzstring_span<> zspan({buf, 1}); }; const auto workaround_macro = [&]() { wzstring_span<> zspan({buf, 1}); };
CHECK_THROW(workaround_macro(), fail_fast); CHECK_THROWS_AS(workaround_macro(), fail_fast);
} }
// usage scenario: create zero-terminated temp file name and pass to a legacy API // usage scenario: create zero-terminated temp file name and pass to a legacy API
@ -956,14 +953,11 @@ SUITE(string_span_tests)
CHECK(*(str + 3) == L'\0'); CHECK(*(str + 3) == L'\0');
} }
} }
} }
TEST(Issue305) TEST_CASE("Issue305")
{ {
std::map<gsl::cstring_span<>, int> foo = {{"foo", 0}, {"bar", 1}}; std::map<gsl::cstring_span<>, int> foo = {{"foo", 0}, {"bar", 1}};
CHECK(foo["foo"] == 0); CHECK(foo["foo"] == 0);
CHECK(foo["bar"] == 1); CHECK(foo["bar"] == 1);
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

18
tests/test.cpp Normal file
View File

@ -0,0 +1,18 @@
///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#define CATCH_CONFIG_MAIN
#include <catch/catch.hpp>

@ -1 +0,0 @@
Subproject commit c331bb0deaaf92659a31887c029ee34cac2ab19e

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl> #include <gsl/gsl>
@ -22,22 +22,20 @@
using namespace gsl; using namespace gsl;
SUITE(utils_tests) void f(int& i) { i += 1; }
{
void f(int& i) { i += 1; }
TEST(finally_lambda) TEST_CASE("finally_lambda")
{ {
int i = 0; int i = 0;
{ {
auto _ = finally([&]() { f(i); }); auto _ = finally([&]() { f(i); });
CHECK(i == 0); CHECK(i == 0);
} }
CHECK(i == 1); CHECK(i == 1);
} }
TEST(finally_lambda_move) TEST_CASE("finally_lambda_move")
{ {
int i = 0; int i = 0;
{ {
auto _1 = finally([&]() { f(i); }); auto _1 = finally([&]() { f(i); });
@ -53,32 +51,32 @@ SUITE(utils_tests)
CHECK(i == 1); CHECK(i == 1);
} }
CHECK(i == 1); CHECK(i == 1);
} }
TEST(finally_function_with_bind) TEST_CASE("finally_function_with_bind")
{ {
int i = 0; int i = 0;
{ {
auto _ = finally(std::bind(&f, std::ref(i))); auto _ = finally(std::bind(&f, std::ref(i)));
CHECK(i == 0); CHECK(i == 0);
} }
CHECK(i == 1); CHECK(i == 1);
} }
int j = 0; int j = 0;
void g() { j += 1; } void g() { j += 1; }
TEST(finally_function_ptr) TEST_CASE("finally_function_ptr")
{ {
j = 0; j = 0;
{ {
auto _ = finally(&g); auto _ = finally(&g);
CHECK(j == 0); CHECK(j == 0);
} }
CHECK(j == 1); CHECK(j == 1);
} }
TEST(narrow_cast) TEST_CASE("narrow_cast")
{ {
int n = 120; int n = 120;
char c = narrow_cast<char>(n); char c = narrow_cast<char>(n);
CHECK(c == 120); CHECK(c == 120);
@ -86,16 +84,16 @@ SUITE(utils_tests)
n = 300; n = 300;
unsigned char uc = narrow_cast<unsigned char>(n); unsigned char uc = narrow_cast<unsigned char>(n);
CHECK(uc == 44); CHECK(uc == 44);
} }
TEST(narrow) TEST_CASE("narrow")
{ {
int n = 120; int n = 120;
const char c = narrow<char>(n); const char c = narrow<char>(n);
CHECK(c == 120); CHECK(c == 120);
n = 300; n = 300;
CHECK_THROW(narrow<char>(n), narrowing_error); CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
const auto int32_max = std::numeric_limits<int32_t>::max(); const auto int32_max = std::numeric_limits<int32_t>::max();
const auto int32_min = std::numeric_limits<int32_t>::min(); const auto int32_min = std::numeric_limits<int32_t>::min();
@ -104,12 +102,9 @@ SUITE(utils_tests)
CHECK(narrow<uint32_t>(int32_t(1)) == 1); CHECK(narrow<uint32_t>(int32_t(1)) == 1);
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max)); CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
CHECK_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error); CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
CHECK_THROW(narrow<uint32_t>(int32_min), narrowing_error); CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
n = -42; n = -42;
CHECK_THROW(narrow<unsigned>(n), narrowing_error); CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }