| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- //
- // copy_ptr.t.h
- // pointers
- //
- // Created by Sam Jaffe on 1/5/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "copy_ptr.hpp"
- class copy_ptr_TestSuite : public CxxTest::TestSuite {
- private:
- struct copy_me {};
- class base {
- public:
- virtual ~base() {}
- virtual base* clone() const = 0;
- virtual int id() const = 0;
- };
-
- class derived_0 : public base {
- public:
- static const constexpr int ID = 0;
- virtual base * clone() const override { return new derived_0; }
- virtual int id() const override { return ID; }
- };
-
- class derived_1 : public base {
- public:
- static const constexpr int ID = 1;
- virtual base * clone() const override { return new derived_1; }
- virtual int id() const override { return ID; }
- };
- public:
- void test_copy_new_obj() {
- copy_ptr<copy_me> c1 { new copy_me };
- copy_ptr<copy_me> c2 { c1 };
- TS_ASSERT_DIFFERS( c1.get(), c2.get() );
- }
-
- void test_copy_object_is_deep_equals() {
- using vec_t = std::vector<int>;
- vec_t my_vec = { 1, 3, 5, 3, 6, 1, 2, -1, 0 };
- copy_ptr<vec_t> c1{ new vec_t{ my_vec } };
- TS_ASSERT_EQUALS( *c1, my_vec );
- copy_ptr<vec_t> c2{ c1 };
- TS_ASSERT_EQUALS( *c2, *c1 );
- }
-
- void test_clone_polymorpic_object() {
- using ptr_t = clone_ptr<base, &base::clone>;
- ptr_t c0 { new derived_0 };
- TS_ASSERT_EQUALS( ptr_t( c0 )->id(), derived_0::ID);
- ptr_t c1 { new derived_1 };
- TS_ASSERT_EQUALS( ptr_t( c1 )->id(), derived_1::ID);
- }
-
- void test_does_own() const {
- bool has_delete{false};
- struct test_t {
- ~test_t() { _r = true; }
- bool & _r;
- };
- test_t * test_struct = new test_t{has_delete};
- copy_ptr<test_t, nullptr>{ std::move(test_struct) };
- TS_ASSERT_EQUALS(has_delete, true);
- }
- };
|