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,188 +23,182 @@
using namespace std; using namespace std;
using namespace gsl; using namespace gsl;
SUITE(copy_tests) TEST_CASE("same_type")
{ {
// dynamic source and destination span
TEST(same_type)
{ {
// dynamic source and destination span std::array<int, 5> src{1, 2, 3, 4, 5};
{ std::array<int, 10> dst{};
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<int> src_span(src); span<int> src_span(src);
span<int> dst_span(dst); span<int> dst_span(dst);
copy(src_span, dst_span); copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size())); copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) { for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]); CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]); CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and dynamic destination span
{
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<int, 5> src_span(src);
span<int> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// dynamic source and static destination span
{
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<int> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and destination span
{
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<int, 5> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
} }
} }
TEST(compatible_type) // static source and dynamic destination span
{ {
// dynamic source and destination span std::array<int, 5> src{1, 2, 3, 4, 5};
{ std::array<int, 10> dst{};
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short> src_span(src); span<int, 5> src_span(src);
span<int> dst_span(dst); span<int> dst_span(dst);
copy(src_span, dst_span); copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size())); copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) { for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]); CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]); CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and dynamic destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short, 5> src_span(src);
span<int> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// dynamic source and static destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short, 5> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
} }
} }
#ifdef CONFIRM_COMPILATION_ERRORS // dynamic source and static destination span
TEST(incompatible_type)
{ {
std::array<int, 4> src{1, 2, 3, 4}; std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int*, 12> dst{}; std::array<int, 10> dst{};
span<int> src_span_dyn(src); span<int> src_span(src);
span<int, 4> src_span_static(src); span<int, 10> dst_span(dst);
span<int*> dst_span_dyn(dst);
span<int*, 4> dst_span_static(dst);
// every line should produce a compilation error copy(src_span, dst_span);
copy(src_span_dyn, dst_span_dyn); copy(src_span, dst_span.subspan(src_span.size()));
copy(src_span_dyn, dst_span_static);
copy(src_span_static, dst_span_dyn); for (std::size_t i = 0; i < src.size(); ++i) {
copy(src_span_static, dst_span_static); CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
} }
#endif
TEST(small_destination_span) // static source and destination span
{ {
std::array<int, 12> src{1, 2, 3, 4}; std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 4> dst{}; std::array<int, 10> dst{};
span<int> src_span_dyn(src); span<int, 5> src_span(src);
span<int, 12> src_span_static(src); span<int, 10> dst_span(dst);
span<int> dst_span_dyn(dst);
span<int, 4> dst_span_static(dst);
CHECK_THROW(copy(src_span_dyn, dst_span_dyn), fail_fast); copy(src_span, dst_span);
CHECK_THROW(copy(src_span_dyn, dst_span_static), fail_fast); copy(src_span, dst_span.subspan(src_span.size()));
CHECK_THROW(copy(src_span_static, dst_span_dyn), fail_fast);
#ifdef CONFIRM_COMPILATION_ERRORS for (std::size_t i = 0; i < src.size(); ++i) {
copy(src_span_static, dst_span_static); CHECK(dst[i] == src[i]);
#endif CHECK(dst[i + src.size()] == src[i]);
}
} }
} }
int main() { return UnitTest::RunAllTests(); } TEST_CASE("compatible_type")
{
// dynamic source and destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short> src_span(src);
span<int> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and dynamic destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short, 5> src_span(src);
span<int> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// dynamic source and static destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
// static source and destination span
{
std::array<short, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<short, 5> src_span(src);
span<int, 10> 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) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
}
}
}
#ifdef CONFIRM_COMPILATION_ERRORS
TEST_CASE("incompatible_type")
{
std::array<int, 4> src{1, 2, 3, 4};
std::array<int*, 12> dst{};
span<int> src_span_dyn(src);
span<int, 4> src_span_static(src);
span<int*> dst_span_dyn(dst);
span<int*, 4> 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_CASE("small_destination_span")
{
std::array<int, 12> src{1, 2, 3, 4};
std::array<int, 4> dst{};
span<int> src_span_dyn(src);
span<int, 12> src_span_static(src);
span<int> dst_span_dyn(dst);
span<int, 4> dst_span_static(dst);
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_dyn), fail_fast);
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast);
CHECK_THROWS_AS(copy(src_span_static, dst_span_dyn), fail_fast);
#ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static);
#endif
}

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);
{ return i;
Expects(i > 0 && i < 10);
return i;
}
TEST(expects)
{
CHECK(f(2) == 2);
CHECK_THROW(f(10), fail_fast);
}
int g(int i)
{
i++;
Ensures(i > 0 && i < 10);
return i;
}
TEST(ensures)
{
CHECK(g(2) == 3);
CHECK_THROW(g(9), fail_fast);
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("expects")
{
CHECK(f(2) == 2);
CHECK_THROWS_AS(f(10), fail_fast);
}
int g(int i)
{
i++;
Ensures(i > 0 && i < 10);
return i;
}
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,70 +23,67 @@
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};
{ const int(&c_a)[4] = a;
int a[4] = {1, 2, 3, 4};
const int(&c_a)[4] = a;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[i]); CHECK(&gsl::at(a, i) == &a[i]);
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_THROW(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at(c_a, -1), fail_fast);
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
} }
TEST(std_array) CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
{ CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
std::array<int, 4> a = {1, 2, 3, 4}; CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
const std::array<int, 4>& c_a = a; CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) { TEST_CASE("std_array")
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)]); std::array<int, 4> a = {1, 2, 3, 4};
} const std::array<int, 4>& c_a = a;
CHECK_THROW(gsl::at(a, -1), fail_fast); for (int i = 0; i < 4; ++i) {
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK_THROW(gsl::at(c_a, -1), fail_fast); CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
} }
TEST(StdVector) CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
{ CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
std::vector<int> a = {1, 2, 3, 4}; CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
const std::vector<int>& c_a = a; CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) { TEST_CASE("StdVector")
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)]); std::vector<int> a = {1, 2, 3, 4};
} const std::vector<int>& c_a = a;
CHECK_THROW(gsl::at(a, -1), fail_fast); for (int i = 0; i < 4; ++i) {
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK_THROW(gsl::at(c_a, -1), fail_fast); CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
} }
TEST(InitializerList) CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
{ CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
std::initializer_list<int> a = {1, 2, 3, 4}; CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) { TEST_CASE("InitializerList")
CHECK(gsl::at(a, i) == i + 1); {
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1); std::initializer_list<int> a = {1, 2, 3, 4};
}
CHECK_THROW(gsl::at(a, -1), fail_fast); for (int i = 0; i < 4; ++i) {
CHECK_THROW(gsl::at(a, 4), fail_fast); CHECK(gsl::at(a, i) == i + 1);
CHECK_THROW(gsl::at({1, 2, 3, 4}, -1), fail_fast); CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
CHECK_THROW(gsl::at({1, 2, 3, 4}, 4), fail_fast);
} }
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);
} }
#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,73 +28,68 @@ 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 (decltype(point)::size_type j = 0;
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) { j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
for (decltype(point)::size_type j = 0; {
j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++) use(j);
{ use(point[static_cast<std::size_t>(j)]);
use(j);
use(point[static_cast<std::size_t>(j)]);
}
} }
} }
TEST(bounds_basic)
{
static_bounds<3, 4, 5> b;
const auto a = b.slice();
(void) a;
static_bounds<4, dynamic_range, 2> x{4};
x.slice().slice();
}
TEST(arrayview_iterator)
{
static_bounds<4, dynamic_range, 2> bounds{3};
const auto itr = bounds.begin();
(void) itr;
#ifdef CONFIRM_COMPILATION_ERRORS
multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
auto itr2 = av.cbegin();
for (auto& v : av) {
v = 4;
}
fill(av.begin(), av.end(), 0);
#endif
}
TEST(bounds_convertible)
{
static_bounds<7, 4, 2> b1;
static_bounds<7, dynamic_range, 2> b2 = b1;
(void) b2;
#ifdef CONFIRM_COMPILATION_ERRORS
static_bounds<7, dynamic_range, 1> b4 = b2;
#endif
static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<7, 4, 2> b4 = b3;
(void) b4;
static_bounds<dynamic_range> b11;
static_bounds<dynamic_range> b5;
static_bounds<34> b6;
b5 = static_bounds<20>();
CHECK_THROW(b6 = b5, fail_fast);
b5 = static_bounds<34>();
b6 = b5;
CHECK(b5 == b6);
CHECK(b5.size() == b6.size());
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("bounds_basic")
{
static_bounds<3, 4, 5> b;
const auto a = b.slice();
(void) a;
static_bounds<4, dynamic_range, 2> x{4};
x.slice().slice();
}
TEST_CASE("arrayview_iterator")
{
static_bounds<4, dynamic_range, 2> bounds{3};
const auto itr = bounds.begin();
(void) itr;
#ifdef CONFIRM_COMPILATION_ERRORS
multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
auto itr2 = av.cbegin();
for (auto& v : av) {
v = 4;
}
fill(av.begin(), av.end(), 0);
#endif
}
TEST_CASE("bounds_convertible")
{
static_bounds<7, 4, 2> b1;
static_bounds<7, dynamic_range, 2> b2 = b1;
(void) b2;
#ifdef CONFIRM_COMPILATION_ERRORS
static_bounds<7, dynamic_range, 1> b4 = b2;
#endif
static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<7, 4, 2> b4 = b3;
(void) b4;
static_bounds<dynamic_range> b11;
static_bounds<dynamic_range> b5;
static_bounds<34> b6;
b5 = static_bounds<20>();
CHECK_THROWS_AS(b6 = b5, fail_fast);
b5 = static_bounds<34>();
b6 = b5;
CHECK(b5 == b6);
CHECK(b5.size() == b6.size());
}

View File

@ -14,7 +14,7 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h> #include <catch/catch.hpp>
#include <gsl/gsl_byte> #include <gsl/gsl_byte>
@ -31,105 +31,101 @@ 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);
}
{
const byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
}
{
const byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 12);
}
{
const unsigned char uc = 12;
const byte b = to_byte(uc);
CHECK(static_cast<unsigned char>(b) == 12);
}
// waiting for C++17 enum class direct initializer support
//{
// byte b { 14 };
// CHECK(static_cast<unsigned char>(b) == 14);
//}
} }
TEST(bitwise_operations)
{ {
const byte b = to_byte<0xFF>(); const byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
byte a = to_byte<0x00>();
CHECK((b | a) == to_byte<0xFF>());
CHECK(a == to_byte<0x00>());
a |= b;
CHECK(a == to_byte<0xFF>());
a = to_byte<0x01>();
CHECK((b & a) == to_byte<0x01>());
a &= b;
CHECK(a == to_byte<0x01>());
CHECK((b ^ a) == to_byte<0xFE>());
CHECK(a == to_byte<0x01>());
a ^= b;
CHECK(a == to_byte<0xFE>());
a = to_byte<0x01>();
CHECK(~a == to_byte<0xFE>());
a = to_byte<0xFF>();
CHECK((a << 4) == to_byte<0xF0>());
CHECK((a >> 4) == to_byte<0x0F>());
a <<= 4;
CHECK(a == to_byte<0xF0>());
a >>= 4;
CHECK(a == to_byte<0x0F>());
} }
TEST(to_integer)
{ {
const byte b = to_byte<0x12>(); const byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 12);
CHECK(0x12 == gsl::to_integer<char>(b)); }
CHECK(0x12 == gsl::to_integer<short>(b)); {
CHECK(0x12 == gsl::to_integer<long>(b)); const unsigned char uc = 12;
CHECK(0x12 == gsl::to_integer<long long>(b)); const byte b = to_byte(uc);
CHECK(static_cast<unsigned char>(b) == 12);
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
} }
int modify_both(gsl::byte & b, int& i) // waiting for C++17 enum class direct initializer support
{ //{
i = 10; // byte b { 14 };
b = to_byte<5>(); // CHECK(static_cast<unsigned char>(b) == 14);
return i; //}
}
TEST(aliasing)
{
int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i);
}
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("bitwise_operations")
{
const byte b = to_byte<0xFF>();
byte a = to_byte<0x00>();
CHECK((b | a) == to_byte<0xFF>());
CHECK(a == to_byte<0x00>());
a |= b;
CHECK(a == to_byte<0xFF>());
a = to_byte<0x01>();
CHECK((b & a) == to_byte<0x01>());
a &= b;
CHECK(a == to_byte<0x01>());
CHECK((b ^ a) == to_byte<0xFE>());
CHECK(a == to_byte<0x01>());
a ^= b;
CHECK(a == to_byte<0xFE>());
a = to_byte<0x01>();
CHECK(~a == to_byte<0xFE>());
a = to_byte<0xFF>();
CHECK((a << 4) == to_byte<0xF0>());
CHECK((a >> 4) == to_byte<0x0F>());
a <<= 4;
CHECK(a == to_byte<0xF0>());
a >>= 4;
CHECK(a == to_byte<0x0F>());
}
TEST_CASE("to_integer")
{
const byte b = to_byte<0x12>();
CHECK(0x12 == gsl::to_integer<char>(b));
CHECK(0x12 == gsl::to_integer<short>(b));
CHECK(0x12 == gsl::to_integer<long>(b));
CHECK(0x12 == gsl::to_integer<long long>(b));
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
}
int modify_both(gsl::byte & b, int& i)
{
i = 10;
b = to_byte<5>();
return i;
}
TEST_CASE("aliasing")
{
int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i);
}
}

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,161 +95,155 @@ 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!
not_null<int*> p; // yay...does not compile! not_null<int*> p; // yay...does not compile!
std::unique_ptr<int> up = std::make_unique<int>(120); std::unique_ptr<int> up = std::make_unique<int>(120);
not_null<int*> p = up; not_null<int*> p = up;
// Forbid non-nullptr assignable types // Forbid non-nullptr assignable types
not_null<std::vector<int>> f(std::vector<int>{1}); not_null<std::vector<int>> f(std::vector<int>{1});
not_null<int> z(10); not_null<int> z(10);
not_null<std::vector<int>> y({1, 2}); not_null<std::vector<int>> y({1, 2});
#endif #endif
int i = 12; int i = 12;
auto rp = RefCounted<int>(&i); auto rp = RefCounted<int>(&i);
not_null<int*> p(rp); not_null<int*> p(rp);
CHECK(p.get() == &i); CHECK(p.get() == &i);
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)
{
MyBase base;
MyDerived derived;
Unrelated unrelated;
not_null<Unrelated*> u = &unrelated;
(void) u;
not_null<MyDerived*> p = &derived;
not_null<MyBase*> q = &base;
q = p; // allowed with heterogeneous copy ctor
CHECK(q == p);
#ifdef CONFIRM_COMPILATION_ERRORS
q = u; // no viable conversion possible between MyBase* and Unrelated*
p = q; // not possible to implicitly convert MyBase* to MyDerived*
not_null<Unrelated*> r = p;
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
}
TEST(TestNotNullAssignment)
{
int i = 12;
not_null<int*> p = &i;
CHECK(helper(p));
int* q = nullptr;
CHECK_THROW(p = q, fail_fast);
}
TEST(TestNotNullRawPointerComparison)
{
int ints[2] = {42, 43};
int* p1 = &ints[0];
const int* p2 = &ints[1];
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == true);
CHECK((NotNull1(p1) == NotNull2(p2)) == false);
CHECK((NotNull1(p1) != NotNull1(p1)) == false);
CHECK((NotNull1(p1) != NotNull2(p2)) == true);
CHECK((NotNull1(p1) < NotNull1(p1)) == false);
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == false);
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == true);
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
CHECK((NotNull1(p1) >= NotNull1(p1)) == true);
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
TEST(TestNotNullSharedPtrComparison)
{
auto sp1 = std::make_shared<int>(42);
auto sp2 = std::make_shared<const int>(43);
using NotNullSp1 = not_null<decltype(sp1)>;
using NotNullSp2 = not_null<decltype(sp2)>;
CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
}
TEST(TestNotNullCustomPtrComparison)
{
int ints[2] = {42, 43};
CustomPtr<int> p1(&ints[0]);
CustomPtr<const int> p2(&ints[1]);
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == "true");
CHECK((NotNull1(p1) == NotNull2(p2)) == "false");
CHECK((NotNull1(p1) != NotNull1(p1)) == "false");
CHECK((NotNull1(p1) != NotNull2(p2)) == "true");
CHECK((NotNull1(p1) < NotNull1(p1)) == "false");
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == "false");
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("TestNotNullCasting")
{
MyBase base;
MyDerived derived;
Unrelated unrelated;
not_null<Unrelated*> u = &unrelated;
(void) u;
not_null<MyDerived*> p = &derived;
not_null<MyBase*> q = &base;
q = p; // allowed with heterogeneous copy ctor
CHECK(q == p);
#ifdef CONFIRM_COMPILATION_ERRORS
q = u; // no viable conversion possible between MyBase* and Unrelated*
p = q; // not possible to implicitly convert MyBase* to MyDerived*
not_null<Unrelated*> r = p;
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
}
TEST_CASE("TestNotNullAssignment")
{
int i = 12;
not_null<int*> p = &i;
CHECK(helper(p));
int* q = nullptr;
CHECK_THROWS_AS(p = q, fail_fast);
}
TEST_CASE("TestNotNullRawPointerComparison")
{
int ints[2] = {42, 43};
int* p1 = &ints[0];
const int* p2 = &ints[1];
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == true);
CHECK((NotNull1(p1) == NotNull2(p2)) == false);
CHECK((NotNull1(p1) != NotNull1(p1)) == false);
CHECK((NotNull1(p1) != NotNull2(p2)) == true);
CHECK((NotNull1(p1) < NotNull1(p1)) == false);
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == false);
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == true);
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
CHECK((NotNull1(p1) >= NotNull1(p1)) == true);
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
TEST_CASE("TestNotNullSharedPtrComparison")
{
auto sp1 = std::make_shared<int>(42);
auto sp2 = std::make_shared<const int>(43);
using NotNullSp1 = not_null<decltype(sp1)>;
using NotNullSp2 = not_null<decltype(sp2)>;
CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
}
TEST_CASE("TestNotNullCustomPtrComparison")
{
int ints[2] = {42, 43};
CustomPtr<int> p1(&ints[0]);
CustomPtr<const int> p2(&ints[1]);
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == "true");
CHECK((NotNull1(p1) == NotNull2(p2)) == "false");
CHECK((NotNull1(p1) != NotNull1(p1)) == "false");
CHECK((NotNull1(p1) != NotNull2(p2)) == "true");
CHECK((NotNull1(p1) < NotNull1(p1)) == "false");
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == "false");
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}

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; }
TEST_CASE("basic_test")
{ {
void f(int* i) { *i += 1; } owner<int*> p = new int(120);
CHECK(*p == 120);
TEST(basic_test) f(p);
{ CHECK(*p == 121);
owner<int*> p = new int(120); delete p;
CHECK(*p == 120);
f(p);
CHECK(*p == 121);
delete p;
}
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

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,94 +22,89 @@
using namespace gsl; using namespace gsl;
SUITE(utils_tests) void f(int& i) { i += 1; }
TEST_CASE("finally_lambda")
{ {
void f(int& i) { i += 1; } int i = 0;
TEST(finally_lambda)
{ {
int i = 0; auto _ = finally([&]() { f(i); });
{ CHECK(i == 0);
auto _ = finally([&]() { f(i); });
CHECK(i == 0);
}
CHECK(i == 1);
}
TEST(finally_lambda_move)
{
int i = 0;
{
auto _1 = finally([&]() { f(i); });
{
auto _2 = std::move(_1);
CHECK(i == 0);
}
CHECK(i == 1);
{
auto _2 = std::move(_1);
CHECK(i == 1);
}
CHECK(i == 1);
}
CHECK(i == 1);
}
TEST(finally_function_with_bind)
{
int i = 0;
{
auto _ = finally(std::bind(&f, std::ref(i)));
CHECK(i == 0);
}
CHECK(i == 1);
}
int j = 0;
void g() { j += 1; }
TEST(finally_function_ptr)
{
j = 0;
{
auto _ = finally(&g);
CHECK(j == 0);
}
CHECK(j == 1);
}
TEST(narrow_cast)
{
int n = 120;
char c = narrow_cast<char>(n);
CHECK(c == 120);
n = 300;
unsigned char uc = narrow_cast<unsigned char>(n);
CHECK(uc == 44);
}
TEST(narrow)
{
int n = 120;
const char c = narrow<char>(n);
CHECK(c == 120);
n = 300;
CHECK_THROW(narrow<char>(n), narrowing_error);
const auto int32_max = std::numeric_limits<int32_t>::max();
const auto int32_min = std::numeric_limits<int32_t>::min();
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
CHECK_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
CHECK_THROW(narrow<uint32_t>(int32_min), narrowing_error);
n = -42;
CHECK_THROW(narrow<unsigned>(n), narrowing_error);
} }
CHECK(i == 1);
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } TEST_CASE("finally_lambda_move")
{
int i = 0;
{
auto _1 = finally([&]() { f(i); });
{
auto _2 = std::move(_1);
CHECK(i == 0);
}
CHECK(i == 1);
{
auto _2 = std::move(_1);
CHECK(i == 1);
}
CHECK(i == 1);
}
CHECK(i == 1);
}
TEST_CASE("finally_function_with_bind")
{
int i = 0;
{
auto _ = finally(std::bind(&f, std::ref(i)));
CHECK(i == 0);
}
CHECK(i == 1);
}
int j = 0;
void g() { j += 1; }
TEST_CASE("finally_function_ptr")
{
j = 0;
{
auto _ = finally(&g);
CHECK(j == 0);
}
CHECK(j == 1);
}
TEST_CASE("narrow_cast")
{
int n = 120;
char c = narrow_cast<char>(n);
CHECK(c == 120);
n = 300;
unsigned char uc = narrow_cast<unsigned char>(n);
CHECK(uc == 44);
}
TEST_CASE("narrow")
{
int n = 120;
const char c = narrow<char>(n);
CHECK(c == 120);
n = 300;
CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
const auto int32_max = std::numeric_limits<int32_t>::max();
const auto int32_min = std::numeric_limits<int32_t>::min();
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
n = -42;
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
}