Skip to content

Commit

Permalink
Moved gsl_transition to a samples folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Gringauze committed Aug 13, 2018
1 parent e1923d0 commit e8fc0b8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 78 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ target_include_directories(gsl_tests_config INTERFACE
${CMAKE_BINARY_DIR}/external/include
)

# for tests to find the samples
target_include_directories(gsl_tests_config INTERFACE
${CMAKE_SOURCE_DIR}/samples
)

# set definitions for tests
target_compile_definitions(gsl_tests_config INTERFACE
GSL_THROW_ON_CONTRACT_VIOLATION
Expand Down Expand Up @@ -106,6 +111,7 @@ add_gsl_test(utils_tests)
add_gsl_test(owner_tests)
add_gsl_test(byte_tests)
add_gsl_test(algorithm_tests)
add_gsl_test(sloppy_notnull_tests)


# No exception tests
Expand Down
78 changes: 0 additions & 78 deletions tests/notnull_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...

#include <gsl/gsl_transition> // for sloppy_not_null
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>

#include <algorithm> // for addressof
Expand Down Expand Up @@ -329,83 +328,6 @@ TEST_CASE("TestNotNullCustomPtrComparison")
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}

bool sloppy_helper(sloppy_not_null<int*> p) { return *p == 12; }
bool sloppy_helper_const(sloppy_not_null<const int*> p) { return *p == 12; }

TEST_CASE("TestSloppyNotNull")
{
{
// raw ptr <-> sloppy_not_null
int x = 42;

sloppy_not_null<int*> snn = &x;

sloppy_helper(&x);
sloppy_helper_const(&x);

CHECK(*snn == 42);
}

{
// sloppy_not_null -> sloppy_not_null
int x = 42;

sloppy_not_null<int*> snn1{&x};
sloppy_not_null<int*> snn2{&x};

sloppy_helper(snn1);
sloppy_helper_const(snn1);

CHECK(snn1 == snn2);
}

{
// sloppy_not_null -> not_null
int x = 42;

sloppy_not_null<int*> snn{&x};

not_null<int*> nn1 = snn;
not_null<int*> nn2{snn};

helper(snn);
helper_const(snn);

CHECK(snn == nn1);
CHECK(snn == nn2);
}

{
// not_null -> sloppy_not_null
int x = 42;

not_null<int*> nn{&x};

sloppy_not_null<int*> snn1{nn};
sloppy_not_null<int*> snn2 = nn;

sloppy_helper(nn);
sloppy_helper_const(nn);

CHECK(snn1 == nn);
CHECK(snn2 == nn);

std::hash<sloppy_not_null<int*>> hash_snn;
std::hash<not_null<int*>> hash_nn;

CHECK(hash_nn(snn1) == hash_nn(nn));
CHECK(hash_snn(snn1) == hash_nn(nn));
CHECK(hash_nn(snn1) == hash_nn(snn2));
CHECK(hash_snn(snn1) == hash_snn(nn));
}

#ifdef CONFIRM_COMPILATION_ERRORS
{
sloppy_not_null<int*> p{nullptr};
}
#endif
}

#if defined(__cplusplus) && (__cplusplus >= 201703L)
TEST_CASE("TestNotNullConstructorTypeDeduction")
{
Expand Down
111 changes: 111 additions & 0 deletions tests/sloppy_notnull_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...

#include <gsl_transition> // for sloppy_not_null
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>

namespace gsl
{
struct fail_fast;
} // namespace gsl

using namespace gsl;


bool helper(not_null<int*> p) { return *p == 12; }
bool helper_const(not_null<const int*> p) { return *p == 12; }

bool sloppy_helper(sloppy_not_null<int*> p) { return *p == 12; }
bool sloppy_helper_const(sloppy_not_null<const int*> p) { return *p == 12; }

TEST_CASE("TestSloppyNotNull")
{
{
// raw ptr <-> sloppy_not_null
int x = 42;

sloppy_not_null<int*> snn = &x;

sloppy_helper(&x);
sloppy_helper_const(&x);

CHECK(*snn == 42);
}

{
// sloppy_not_null -> sloppy_not_null
int x = 42;

sloppy_not_null<int*> snn1{&x};
sloppy_not_null<int*> snn2{&x};

sloppy_helper(snn1);
sloppy_helper_const(snn1);

CHECK(snn1 == snn2);
}

{
// sloppy_not_null -> not_null
int x = 42;

sloppy_not_null<int*> snn{&x};

not_null<int*> nn1 = snn;
not_null<int*> nn2{snn};

helper(snn);
helper_const(snn);

CHECK(snn == nn1);
CHECK(snn == nn2);
}

{
// not_null -> sloppy_not_null
int x = 42;

not_null<int*> nn{&x};

sloppy_not_null<int*> snn1{nn};
sloppy_not_null<int*> snn2 = nn;

sloppy_helper(nn);
sloppy_helper_const(nn);

CHECK(snn1 == nn);
CHECK(snn2 == nn);

std::hash<sloppy_not_null<int*>> hash_snn;
std::hash<not_null<int*>> hash_nn;

CHECK(hash_nn(snn1) == hash_nn(nn));
CHECK(hash_snn(snn1) == hash_nn(nn));
CHECK(hash_nn(snn1) == hash_nn(snn2));
CHECK(hash_snn(snn1) == hash_snn(nn));
}

#ifdef CONFIRM_COMPILATION_ERRORS
{
sloppy_not_null<int*> p{nullptr};
}
#endif
}

static_assert(std::is_nothrow_move_constructible<sloppy_not_null<void*>>::value,
"sloppy_not_null must be no-throw move constructible");

0 comments on commit e8fc0b8

Please sign in to comment.