Move from unittest-cpp to catch for unit testing. (#533)

Many thanks to @rianquinn. This should fix #495, #494 and #529.
This commit is contained in:
Neil MacIntosh 2017-07-13 13:53:56 -07:00 committed by GitHub
parent 1f87ef73f1
commit b2ee484334
24 changed files with 4315 additions and 4292 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
CMakeFiles
build
include/catch
tests/CMakeFiles
tests/Debug
*.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.7.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:
include:
@ -61,6 +61,19 @@ matrix:
- env: CLANG_VERSION=3.8 BUILD_TYPE=Release
os: linux
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
os: linux
addons: &gcc5
@ -110,7 +123,7 @@ install:
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.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_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"

View File

@ -1,13 +1,16 @@
cmake_minimum_required(VERSION 2.8.7)
cmake_minimum_required(VERSION 3.1.3)
project(GSL CXX)
include(ExternalProject)
find_package(Git REQUIRED)
# creates a library GSL which is an interface (header files only)
add_library(GSL INTERFACE)
# when minimum version required is 3.8.0 remove if below
# 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)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
@ -16,7 +19,7 @@ if ( CMAKE_MAJOR_VERSION VERSION_LESS 3.7.9)
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
endif()
endif()
else ()
# set the GSL library to be compiled only with c++14
@ -41,7 +44,7 @@ target_include_directories(GSL INTERFACE
)
# add natvis file to the library so it will automatically be loaded into Visual Studio
target_sources(GSL INTERFACE
target_sources(GSL INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis
)

View File

@ -17,6 +17,9 @@ other platforms. Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more inform
# Project Code of Conduct
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
# Usage of Third Party Libraries
This project makes use of the [Catch](https://github.com/philsquared/catch) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Catch.
# Quick Start
## Supported Platforms
The test suite that exercises GSL has been built and passes successfully on the following platforms:<sup>1)</sup>
@ -40,9 +43,7 @@ contributing any changes that were necessary back to this project to benefit the
## Building the tests
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.
* [UnitTest-cpp](https://github.com/Microsoft/unittest-cpp), to be cloned under the [tests/unittest-cpp](./tests/unittest-cpp) directory
of your GSL source.
* [CMake](http://cmake.org), version 3.1.3 or later to be installed and in your PATH.
These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`.
@ -56,11 +57,11 @@ These steps assume the source code of this repository has been cloned into a dir
cmake -G "Visual Studio 14 2015" c:\GSL
3. Build the test suite (in this case, in the Debug configuration, Release is another good choice).
3. Build the test suite (in this case, in the Debug configuration, Release is another good choice).
cmake --build . --config Debug
4. Run the test suite.
4. Run the test suite.
ctest -C Debug

39
ThirdPartyNotices.txt Normal file
View File

@ -0,0 +1,39 @@
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
Do Not Translate or Localize
GSL: Guideline Support Library incorporates third party material from the projects listed below. The original copyright notice and the license under which Microsoft received such third party material are set forth below. Microsoft reserves all other rights not expressly granted, whether by implication, estoppel or otherwise.
1. Catch (https://github.com/philsquared/Catch)
%% Catch NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
=========================================
END OF Catch NOTICES, INFORMATION, AND LICENSE

View File

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

View File

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

View File

@ -37,6 +37,7 @@
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
// blanket turn off warnings from CppCoreCheck for now
// 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
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/unittest-cpp/CMakeLists.txt)
find_package(Git)
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
endif()
list(APPEND CATCH_CMAKE_ARGS
"-DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}"
"-DNO_SELFTEST=true"
)
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.6
CMAKE_ARGS ${CATCH_CMAKE_ARGS}
LOG_DOWNLOAD 1
UPDATE_DISCONNECTED 1
)
# this interface adds compile options to how the tests are run
# 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
target_compile_definitions(gsl_tests_config INTERFACE
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)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name}
UnitTest++
GSL
test_catch
gsl_tests_config
)
add_dependencies(${name} catch)
add_test(
${name}
${name}

View File

@ -14,7 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <catch/catch.hpp>
#include <gsl/gsl_algorithm>
@ -23,188 +23,182 @@
using namespace std;
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, 10> dst{};
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> dst{};
span<int> src_span(src);
span<int> dst_span(dst);
span<int> src_span(src);
span<int> dst_span(dst);
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
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<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]);
}
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<short, 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<short> src_span(src);
span<int> dst_span(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()));
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]);
}
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(incompatible_type)
// dynamic source and static destination span
{
std::array<int, 4> src{1, 2, 3, 4};
std::array<int*, 12> dst{};
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> 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);
span<int> src_span(src);
span<int, 10> dst_span(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);
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]);
}
}
#endif
TEST(small_destination_span)
// static source and destination span
{
std::array<int, 12> src{1, 2, 3, 4};
std::array<int, 4> dst{};
std::array<int, 5> src{1, 2, 3, 4, 5};
std::array<int, 10> 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);
span<int, 5> src_span(src);
span<int, 10> dst_span(dst);
CHECK_THROW(copy(src_span_dyn, dst_span_dyn), fail_fast);
CHECK_THROW(copy(src_span_dyn, dst_span_static), fail_fast);
CHECK_THROW(copy(src_span_static, dst_span_dyn), fail_fast);
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
#ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static);
#endif
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
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>
using namespace gsl;
SUITE(assertion_tests)
int f(int i)
{
int f(int 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);
}
Expects(i > 0 && i < 10);
return i;
}
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>
@ -23,70 +23,67 @@
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) {
CHECK(&gsl::at(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);
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[i]);
CHECK(&gsl::at(c_a, i) == &a[i]);
}
TEST(std_array)
{
std::array<int, 4> a = {1, 2, 3, 4};
const std::array<int, 4>& c_a = a;
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
TEST_CASE("std_array")
{
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);
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);
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
TEST(StdVector)
{
std::vector<int> a = {1, 2, 3, 4};
const std::vector<int>& c_a = a;
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
TEST_CASE("StdVector")
{
std::vector<int> a = {1, 2, 3, 4};
const std::vector<int>& c_a = a;
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);
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
TEST(InitializerList)
{
std::initializer_list<int> a = {1, 2, 3, 4};
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
}
for (int i = 0; i < 4; ++i) {
CHECK(gsl::at(a, i) == i + 1);
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
}
TEST_CASE("InitializerList")
{
std::initializer_list<int> a = {1, 2, 3, 4};
CHECK_THROW(gsl::at(a, -1), fail_fast);
CHECK_THROW(gsl::at(a, 4), fail_fast);
CHECK_THROW(gsl::at({1, 2, 3, 4}, -1), fail_fast);
CHECK_THROW(gsl::at({1, 2, 3, 4}, 4), fail_fast);
for (int i = 0; i < 4; ++i) {
CHECK(gsl::at(a, i) == i + 1);
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
}
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
@ -111,5 +108,3 @@ static constexpr bool test_constexpr()
static_assert(test_constexpr(), "FAIL");
#endif
int main() { return UnitTest::RunAllTests(); }

View File

@ -14,7 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <catch/catch.hpp>
#include <gsl/multi_span>
@ -28,73 +28,68 @@ namespace
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;
j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
{
use(j);
use(point[static_cast<std::size_t>(j)]);
}
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
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)]);
}
}
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>
@ -31,105 +31,101 @@ using namespace gsl;
namespace
{
SUITE(byte_tests)
TEST_CASE("construction")
{
TEST(construction)
{
{
const byte b = static_cast<byte>(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);
//}
const byte b = static_cast<byte>(4);
CHECK(static_cast<unsigned char>(b) == 4);
}
TEST(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>());
const byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
}
TEST(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
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);
}
int modify_both(gsl::byte & b, int& i)
{
i = 10;
b = to_byte<5>();
return i;
}
TEST(aliasing)
{
int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i);
}
}
// waiting for C++17 enum class direct initializer support
//{
// byte b { 14 };
// CHECK(static_cast<unsigned char>(b) == 14);
//}
}
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>
@ -104,192 +104,182 @@ struct NonCopyableNonMovable
NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
};
bool helper(not_null<int*> p) { return *p == 12; }
SUITE(NotNullTests)
TEST_CASE("TestNotNullConstructors")
{
bool helper(not_null<int*> p) { return *p == 12; }
TEST(TestNotNullConstructors)
{
#ifdef CONFIRM_COMPILATION_ERRORS
not_null<int*> p = nullptr; // yay...does not compile!
not_null<std::vector<char>*> p = 0; // yay...does not compile!
not_null<int*> p; // yay...does not compile!
std::unique_ptr<int> up = std::make_unique<int>(120);
not_null<int*> p = up;
not_null<int*> p = nullptr; // yay...does not compile!
not_null<std::vector<char>*> p = 0; // yay...does not compile!
not_null<int*> p; // yay...does not compile!
std::unique_ptr<int> up = std::make_unique<int>(120);
not_null<int*> p = up;
// Forbid non-nullptr assignable types
not_null<std::vector<int>> f(std::vector<int>{1});
not_null<int> z(10);
not_null<std::vector<int>> y({1, 2});
// Forbid non-nullptr assignable types
not_null<std::vector<int>> f(std::vector<int>{1});
not_null<int> z(10);
not_null<std::vector<int>> y({1, 2});
#endif
int i = 12;
auto rp = RefCounted<int>(&i);
not_null<int*> p(rp);
CHECK(p.get() == &i);
int i = 12;
auto rp = RefCounted<int>(&i);
not_null<int*> p(rp);
CHECK(p.get() == &i);
not_null<std::shared_ptr<int>> x(
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));
}
TEST(TestNotNullDereferenceOperator)
{
{
auto sp1 = std::make_shared<NonCopyableNonMovable>();
using NotNullSp1 = not_null<decltype(sp1)>;
CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
}
{
int ints[1] = { 42 };
CustomPtr<int> p1(&ints[0]);
using NotNull1 = not_null<decltype(p1)>;
CHECK(typeid(*NotNull1(p1)) == typeid(*p1));
CHECK(*NotNull1(p1) == 42);
*NotNull1(p1) = 43;
CHECK(ints[0] == 43);
}
{
int v = 42;
gsl::not_null<int*> p(&v);
CHECK(typeid(*p) == typeid(*(&v)));
*p = 43;
CHECK(v == 43);
}
}
not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
}
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));
}
TEST_CASE("TestNotNullDereferenceOperator")
{
{
auto sp1 = std::make_shared<NonCopyableNonMovable>();
using NotNullSp1 = not_null<decltype(sp1)>;
CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
}
{
int ints[1] = { 42 };
CustomPtr<int> p1(&ints[0]);
using NotNull1 = not_null<decltype(p1)>;
CHECK(typeid(*NotNull1(p1)) == typeid(*p1));
CHECK(*NotNull1(p1) == 42);
*NotNull1(p1) = 43;
CHECK(ints[0] == 43);
}
{
int v = 42;
gsl::not_null<int*> p(&v);
CHECK(typeid(*p) == typeid(*(&v)));
*p = 43;
CHECK(v == 43);
}
}
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>
@ -24,28 +24,23 @@
using namespace gsl;
SUITE(owner_tests)
void f(int* i) { *i += 1; }
TEST_CASE("basic_test")
{
void f(int* i) { *i += 1; }
TEST(basic_test)
{
owner<int*> p = new int(120);
CHECK(*p == 120);
f(p);
CHECK(*p == 121);
delete p;
}
TEST(check_pointer_constraint)
{
#ifdef CONFIRM_COMPILATION_ERRORS
{
owner<int> integerTest = 10;
owner<std::shared_ptr<int>> sharedPtrTest(new int(10));
}
#endif
}
owner<int*> p = new int(120);
CHECK(*p == 120);
f(p);
CHECK(*p == 121);
delete p;
}
int main(int, const char* []) { return UnitTest::RunAllTests(); }
TEST_CASE("check_pointer_constraint")
{
#ifdef CONFIRM_COMPILATION_ERRORS
{
owner<int> integerTest = 10;
owner<std::shared_ptr<int>> sharedPtrTest(new int(10));
}
#endif
}

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>
@ -22,94 +22,89 @@
using namespace gsl;
SUITE(utils_tests)
void f(int& i) { i += 1; }
TEST_CASE("finally_lambda")
{
void f(int& i) { i += 1; }
TEST(finally_lambda)
int i = 0;
{
int 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);
auto _ = finally([&]() { f(i); });
CHECK(i == 0);
}
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);
}