aboutsummaryrefslogtreecommitdiff
path: root/tests/notnull_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/notnull_tests.cpp')
-rw-r--r--tests/notnull_tests.cpp123
1 files changed, 65 insertions, 58 deletions
diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp
index 1c2b50e..bb043bb 100644
--- a/tests/notnull_tests.cpp
+++ b/tests/notnull_tests.cpp
@@ -1,33 +1,41 @@
-///////////////////////////////////////////////////////////////////////////////
-//
-// 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.
-//
///////////////////////////////////////////////////////////////////////////////
+//
+// 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 <UnitTest++/UnitTest++.h>
-#include <UnitTest++/UnitTest++.h>
#include <gsl/gsl>
-#include <vector>
+
#include <memory>
#include <string>
+#include <vector>
using namespace gsl;
-struct MyBase {};
-struct MyDerived : public MyBase {};
-struct Unrelated {};
+struct MyBase
+{
+};
+struct MyDerived : public MyBase
+{
+};
+struct Unrelated
+{
+};
// stand-in for a user-defined ref-counted class
-template<typename T>
+template <typename T>
struct RefCounted
{
RefCounted(T* p) : p_(p) {}
@@ -39,95 +47,97 @@ struct RefCounted
template <typename T>
struct CustomPtr
{
- CustomPtr(T* p) : p_(p) {}
- operator T*() { return p_; }
- bool operator !=(std::nullptr_t)const { return p_ != nullptr; }
- T* p_ = nullptr;
+ CustomPtr(T* p) : p_(p) {}
+ operator T*() { return p_; }
+ bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
+ T* p_ = nullptr;
};
template <typename T, typename U>
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
template <typename T, typename U>
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
template <typename T, typename U>
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
template <typename T, typename U>
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
template <typename T, typename U>
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
template <typename T, typename U>
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
- return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
+ return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
+ : "false";
}
-
-
SUITE(NotNullTests)
{
- bool helper(not_null<int*> p)
- {
- return *p == 12;
- }
+ 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<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!
+ 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});
+ 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
+ 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;
+ 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
+ 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*
+ 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);
@@ -139,7 +149,7 @@ SUITE(NotNullTests)
TEST(TestNotNullAssignment)
{
int i = 12;
- not_null<int*> p = &i;
+ not_null<int*> p = &i;
CHECK(helper(p));
int* q = nullptr;
@@ -211,7 +221,7 @@ SUITE(NotNullTests)
TEST(TestNotNullCustomPtrComparison)
{
- int ints[2] = { 42, 43 };
+ int ints[2] = {42, 43};
CustomPtr<int> p1(&ints[0]);
CustomPtr<const int> p2(&ints[1]);
@@ -242,7 +252,4 @@ SUITE(NotNullTests)
}
}
-int main(int, const char *[])
-{
- return UnitTest::RunAllTests();
-}
+int main(int, const char* []) { return UnitTest::RunAllTests(); }