2015-08-20 21:09:14 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-04-20 10:51:37 -04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// blanket turn off warnings from CppCoreCheck from catch
|
|
|
|
// so people aren't annoyed by them when running the tool.
|
|
|
|
#pragma warning(disable : 26440 26426) // from catch
|
|
|
|
|
2018-08-13 05:22:02 -04:00
|
|
|
// Fix VS2015 build breaks in Release
|
|
|
|
#pragma warning(disable : 4702) // unreachable code
|
2018-08-13 00:44:17 -04:00
|
|
|
#endif
|
|
|
|
|
2019-12-04 15:30:58 -05:00
|
|
|
#if __clang__ || __GNUC__
|
|
|
|
//disable warnings from gtest
|
|
|
|
#pragma GCC diagnostic push
|
2019-12-04 15:48:13 -05:00
|
|
|
#pragma GCC diagnostic ignored "-Wundef"
|
2019-12-19 17:05:02 -05:00
|
|
|
#endif // __clang__ || __GNUC__
|
|
|
|
|
2019-12-06 19:03:47 -05:00
|
|
|
#if __clang__
|
2019-12-04 15:30:58 -05:00
|
|
|
#pragma GCC diagnostic ignored "-Wglobal-constructors"
|
|
|
|
#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
|
|
|
|
#pragma GCC diagnostic ignored "-Wcovered-switch-default"
|
2019-12-19 17:05:02 -05:00
|
|
|
#endif // __clang__
|
2019-12-04 15:30:58 -05:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
#include <gtest/gtest.h>
|
2015-08-20 21:09:14 -04:00
|
|
|
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <algorithm> // for addressof
|
|
|
|
#include <memory> // for shared_ptr, make_shared, operator<, opera...
|
|
|
|
#include <sstream> // for operator<<, ostringstream, basic_ostream:...
|
|
|
|
#include <stdint.h> // for uint16_t
|
|
|
|
#include <string> // for basic_string, operator==, string, operator<<
|
|
|
|
#include <typeinfo> // for type_info
|
|
|
|
|
2018-08-13 12:55:48 -04:00
|
|
|
namespace gsl
|
|
|
|
{
|
2017-11-28 10:13:49 -05:00
|
|
|
struct fail_fast;
|
2018-08-13 12:55:48 -04:00
|
|
|
} // namespace gsl
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2015-09-29 19:41:37 -04:00
|
|
|
using namespace gsl;
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2019-12-19 17:05:02 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static constexpr char deathstring[] = "Expected Death";
|
2019-12-12 13:55:26 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
struct MyBase
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct MyDerived : public MyBase
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct Unrelated
|
|
|
|
{
|
|
|
|
};
|
2015-08-20 21:09:14 -04:00
|
|
|
|
|
|
|
// stand-in for a user-defined ref-counted class
|
2017-04-20 10:51:37 -04:00
|
|
|
template <typename T>
|
2015-08-20 21:09:14 -04:00
|
|
|
struct RefCounted
|
|
|
|
{
|
|
|
|
RefCounted(T* p) : p_(p) {}
|
|
|
|
operator T*() { return p_; }
|
|
|
|
T* p_;
|
|
|
|
};
|
|
|
|
|
2017-04-12 20:34:39 -04:00
|
|
|
// user defined smart pointer with comparison operators returning non bool value
|
|
|
|
template <typename T>
|
|
|
|
struct CustomPtr
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
CustomPtr(T* p) : p_(p) {}
|
|
|
|
operator T*() { return p_; }
|
|
|
|
bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
|
|
|
|
T* p_ = nullptr;
|
2017-04-12 20:34:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
2017-04-20 10:51:37 -04:00
|
|
|
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
|
|
|
: "false";
|
2017-04-12 20:34:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:18:55 -04:00
|
|
|
struct NonCopyableNonMovable
|
|
|
|
{
|
|
|
|
NonCopyableNonMovable() = default;
|
|
|
|
NonCopyableNonMovable(const NonCopyableNonMovable&) = delete;
|
|
|
|
NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete;
|
|
|
|
NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
|
|
|
|
NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
|
|
|
|
};
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
bool helper(not_null<int*> p) { return *p == 12; }
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
bool helper_const(not_null<const int*> p) { return *p == 12; }
|
|
|
|
|
|
|
|
int* return_pointer() { return nullptr; }
|
2019-12-19 17:05:02 -05:00
|
|
|
} //namespace
|
2017-05-31 14:18:55 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullConstructors)
|
2015-08-20 21:09:14 -04:00
|
|
|
{
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
{
|
2015-08-20 21:09:14 -04:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
not_null<int*> p = nullptr; // yay...does not compile!
|
|
|
|
not_null<std::vector<char>*> p1 = 0; // yay...does not compile!
|
|
|
|
not_null<int*> p2; // yay...does not compile!
|
|
|
|
std::unique_ptr<int> up = std::make_unique<int>(120);
|
|
|
|
not_null<int*> p3 = 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});
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
2015-09-25 12:41:40 -04:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
std::set_terminate([] {
|
|
|
|
std::cerr << "Expected Death. TestNotNullConstructors";
|
|
|
|
std::abort();
|
|
|
|
});
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
{
|
|
|
|
// from shared pointer
|
|
|
|
int i = 12;
|
|
|
|
auto rp = RefCounted<int>(&i);
|
|
|
|
not_null<int*> p(rp);
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(p.get() == &i);
|
2018-02-12 12:31:09 -05:00
|
|
|
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
not_null<std::shared_ptr<int>> x(
|
|
|
|
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
|
|
|
|
|
|
|
int* pi = nullptr;
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH((not_null<decltype(pi)>(pi)), deathstring);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// from pointer to local
|
|
|
|
int t = 42;
|
|
|
|
|
|
|
|
not_null<int*> x = &t;
|
|
|
|
helper(&t);
|
|
|
|
helper_const(&t);
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// from raw pointer
|
|
|
|
// from not_null pointer
|
|
|
|
|
|
|
|
int t = 42;
|
|
|
|
int* p = &t;
|
|
|
|
|
|
|
|
not_null<int*> x = p;
|
|
|
|
helper(p);
|
|
|
|
helper_const(p);
|
|
|
|
helper(x);
|
|
|
|
helper_const(x);
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// from raw const pointer
|
|
|
|
// from not_null const pointer
|
|
|
|
|
|
|
|
int t = 42;
|
|
|
|
const int* cp = &t;
|
|
|
|
|
|
|
|
not_null<const int*> x = cp;
|
|
|
|
helper_const(cp);
|
|
|
|
helper_const(x);
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// from not_null const pointer, using auto
|
|
|
|
int t = 42;
|
|
|
|
const int* cp = &t;
|
|
|
|
|
|
|
|
auto x = not_null<const int*>{cp};
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// from returned pointer
|
|
|
|
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(helper(return_pointer()), deathstring);
|
|
|
|
EXPECT_DEATH(helper_const(return_pointer()), deathstring);
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
template <typename T>
|
2017-11-14 11:14:20 -05:00
|
|
|
void ostream_helper(T v)
|
|
|
|
{
|
|
|
|
not_null<T*> p(&v);
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
std::ostringstream ref;
|
2019-08-31 17:35:04 -04:00
|
|
|
os << static_cast<void*>(p);
|
|
|
|
ref << static_cast<void*>(&v);
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(os.str() == ref.str());
|
2017-11-14 11:14:20 -05:00
|
|
|
}
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
std::ostringstream ref;
|
|
|
|
os << *p;
|
|
|
|
ref << v;
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(os.str() == ref.str());
|
2017-11-14 11:14:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullostream)
|
2017-11-14 11:14:20 -05:00
|
|
|
{
|
|
|
|
ostream_helper<int>(17);
|
|
|
|
ostream_helper<float>(21.5f);
|
2019-01-15 13:27:34 -05:00
|
|
|
ostream_helper<double>(3.4566e-7);
|
2017-11-14 11:14:20 -05:00
|
|
|
ostream_helper<char>('c');
|
|
|
|
ostream_helper<uint16_t>(0x0123u);
|
|
|
|
ostream_helper<const char*>("cstring");
|
|
|
|
ostream_helper<std::string>("string");
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullCasting)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
MyBase base;
|
|
|
|
MyDerived derived;
|
|
|
|
Unrelated unrelated;
|
2018-04-24 22:11:43 -04:00
|
|
|
not_null<Unrelated*> u{&unrelated};
|
2017-07-13 16:53:56 -04:00
|
|
|
(void) u;
|
2018-04-24 22:11:43 -04:00
|
|
|
not_null<MyDerived*> p{&derived};
|
|
|
|
not_null<MyBase*> q(&base);
|
2017-07-13 16:53:56 -04:00
|
|
|
q = p; // allowed with heterogeneous copy ctor
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(q == p);
|
2015-08-20 21:09:14 -04:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
q = u; // no viable conversion possible between MyBase* and Unrelated*
|
|
|
|
p = q; // not possible to implicitly convert MyBase* to MyDerived*
|
2015-09-28 03:35:18 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
not_null<Unrelated*> r = p;
|
|
|
|
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
|
2015-08-20 21:09:14 -04:00
|
|
|
#endif
|
2018-04-24 22:11:43 -04:00
|
|
|
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullAssignment)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2019-12-12 13:55:26 -05:00
|
|
|
std::set_terminate([] {
|
|
|
|
std::cerr << "Expected Death. TestNotNullAssignmentd";
|
|
|
|
std::abort();
|
|
|
|
});
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
int i = 12;
|
2018-04-24 22:11:43 -04:00
|
|
|
not_null<int*> p(&i);
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(helper(p));
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
int* q = nullptr;
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(p = not_null<int*>(q), deathstring);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullRawPointerComparison)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int ints[2] = {42, 43};
|
|
|
|
int* p1 = &ints[0];
|
|
|
|
const int* p2 = &ints[1];
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
|
|
|
using NotNull2 = not_null<decltype(p2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == true);
|
|
|
|
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == false);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == false);
|
|
|
|
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == true);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == false);
|
|
|
|
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == false);
|
|
|
|
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == true);
|
|
|
|
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullDereferenceOperator)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2017-04-12 20:34:39 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto sp1 = std::make_shared<NonCopyableNonMovable>();
|
2017-04-12 20:34:39 -04:00
|
|
|
|
|
|
|
using NotNullSp1 = not_null<decltype(sp1)>;
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
|
|
|
|
EXPECT_TRUE(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-13 12:55:48 -04:00
|
|
|
int ints[1] = {42};
|
2017-07-13 16:53:56 -04:00
|
|
|
CustomPtr<int> p1(&ints[0]);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(typeid(*NotNull1(p1)) == typeid(*p1));
|
|
|
|
EXPECT_TRUE(*NotNull1(p1) == 42);
|
2017-07-13 16:53:56 -04:00
|
|
|
*NotNull1(p1) = 43;
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(ints[0] == 43);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int v = 42;
|
|
|
|
gsl::not_null<int*> p(&v);
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(typeid(*p) == typeid(*(&v)));
|
2017-07-13 16:53:56 -04:00
|
|
|
*p = 43;
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(v == 43);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullSharedPtrComparison)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto sp1 = std::make_shared<int>(42);
|
|
|
|
auto sp2 = std::make_shared<const int>(43);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNullSp1 = not_null<decltype(sp1)>;
|
|
|
|
using NotNullSp2 = not_null<decltype(sp2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
|
|
|
|
EXPECT_TRUE((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
|
|
|
|
EXPECT_TRUE((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
|
|
|
|
EXPECT_TRUE((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
|
|
|
|
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
|
|
|
|
EXPECT_TRUE((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullCustomPtrComparison)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int ints[2] = {42, 43};
|
|
|
|
CustomPtr<int> p1(&ints[0]);
|
|
|
|
CustomPtr<const int> p2(&ints[1]);
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
using NotNull1 = not_null<decltype(p1)>;
|
|
|
|
using NotNull2 = not_null<decltype(p2)>;
|
2017-04-12 20:34:39 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == "true");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == "false");
|
2017-05-30 13:06:21 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == "false");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == "true");
|
2017-07-13 16:53:56 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == "false");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
2017-07-13 16:53:56 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == "false");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
2015-08-20 21:09:14 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == "true");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
2017-07-13 16:53:56 -04:00
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE((NotNull1(p1) >= NotNull1(p1)) == "true");
|
|
|
|
EXPECT_TRUE((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
|
|
|
|
EXPECT_TRUE((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2018-03-03 18:53:23 -05:00
|
|
|
|
2018-06-07 16:36:56 -04:00
|
|
|
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
2018-08-13 00:44:17 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
2018-06-07 16:36:56 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
int i = 42;
|
|
|
|
|
|
|
|
not_null x{&i};
|
|
|
|
helper(not_null{&i});
|
|
|
|
helper_const(not_null{&i});
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-06-07 16:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int i = 42;
|
|
|
|
int* p = &i;
|
|
|
|
|
|
|
|
not_null x{p};
|
|
|
|
helper(not_null{p});
|
|
|
|
helper_const(not_null{p});
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-06-07 16:36:56 -04:00
|
|
|
}
|
2019-12-12 19:48:59 -05:00
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
std::set_terminate([] {
|
|
|
|
std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
|
|
|
|
std::abort();
|
|
|
|
});
|
2018-06-07 16:36:56 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
auto workaround_macro = []() {
|
|
|
|
int* p1 = nullptr;
|
2018-08-13 00:44:17 -04:00
|
|
|
const not_null x{p1};
|
2018-06-07 16:36:56 -04:00
|
|
|
};
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(workaround_macro(), deathstring);
|
2018-06-07 16:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto workaround_macro = []() {
|
|
|
|
const int* p1 = nullptr;
|
2018-08-13 00:44:17 -04:00
|
|
|
const not_null x{p1};
|
2018-06-07 16:36:56 -04:00
|
|
|
};
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(workaround_macro(), deathstring);
|
2018-06-07 16:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(helper(not_null{p}), deathstring);
|
|
|
|
EXPECT_DEATH(helper_const(not_null{p}), deathstring);
|
2018-06-07 16:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
|
|
|
not_null x{nullptr};
|
|
|
|
helper(not_null{nullptr});
|
|
|
|
helper_const(not_null{nullptr});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(notnull_tests, TestMakeNotNull)
|
2018-08-13 05:22:02 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
int i = 42;
|
|
|
|
|
|
|
|
const auto x = make_not_null(&i);
|
|
|
|
helper(make_not_null(&i));
|
|
|
|
helper_const(make_not_null(&i));
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int i = 42;
|
|
|
|
int* p = &i;
|
|
|
|
|
|
|
|
const auto x = make_not_null(p);
|
|
|
|
helper(make_not_null(p));
|
|
|
|
helper_const(make_not_null(p));
|
|
|
|
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
|
2019-12-12 13:55:26 -05:00
|
|
|
std::set_terminate([] {
|
|
|
|
std::cerr << "Expected Death. TestMakeNotNull";
|
|
|
|
std::abort();
|
|
|
|
});
|
|
|
|
|
2018-08-13 05:22:02 -04:00
|
|
|
{
|
|
|
|
const auto workaround_macro = []() {
|
|
|
|
int* p1 = nullptr;
|
|
|
|
const auto x = make_not_null(p1);
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-08-13 05:22:02 -04:00
|
|
|
};
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(workaround_macro(), deathstring);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto workaround_macro = []() {
|
|
|
|
const int* p1 = nullptr;
|
|
|
|
const auto x = make_not_null(p1);
|
2019-12-04 16:46:50 -05:00
|
|
|
EXPECT_TRUE(*x == 42);
|
2018-08-13 05:22:02 -04:00
|
|
|
};
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(workaround_macro(), deathstring);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(helper(make_not_null(p)), deathstring);
|
|
|
|
EXPECT_DEATH(helper_const(make_not_null(p)), deathstring);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
2019-12-12 14:15:41 -05:00
|
|
|
EXPECT_DEATH(make_not_null(nullptr), deathstring);
|
|
|
|
EXPECT_DEATH(helper(make_not_null(nullptr)), deathstring);
|
|
|
|
EXPECT_DEATH(helper_const(make_not_null(nullptr)), deathstring);
|
2018-08-13 05:22:02 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Dev/annagrin/remove explicit not null constructor (#743)
* Added c++17 test configurations for clang5.0 and clang6.0
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* Removed explicit not_null constructor, sloppy_not_null, added strict_not_null
We added explicit not_null constructor in version 2.0.0.
It proved very difficult to switch to the new version for
large code bases that adopted previous versions of gsl,
due to not_null used extensively in the code. Still, using
explicit constructor is very benefitial for new code, since
it encorages better API design and make null checks intentional.
To resolve the issue, this change:
- removes explicit keyword from not_null constructor
- removes unneded sloppy_not_null type
- adds strict_not_null type to behave the same way as v2 not_null
- updates tests
* fixed build break for gcc7
* added more tests
* added more non-compiling tests
* Addressed PR comments and suppressed a code analysis warning
* Fixed test failure in not_null tests
2019-01-14 19:45:47 -05:00
|
|
|
static_assert(std::is_nothrow_move_constructible<not_null<void*>>::value,
|
|
|
|
"not_null must be no-throw move constructible");
|
2019-12-04 15:30:58 -05:00
|
|
|
|
|
|
|
#if __clang__ || __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
2019-12-19 17:05:02 -05:00
|
|
|
#endif // __clang__ || __GNUC__
|