File: smart_pointer.cpp

package info (click to toggle)
libfilezilla 0.52.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,492 kB
  • sloc: cpp: 30,965; sh: 4,241; makefile: 375; xml: 37
file content (116 lines) | stat: -rw-r--r-- 2,259 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "../lib/libfilezilla/optional.hpp"
#include "../lib/libfilezilla/shared.hpp"

#include "test_utils.hpp"
/*
 * This testsuite asserts the correctness of the
 * smart pointer classes
 */

class smart_pointer_test final : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(smart_pointer_test);
	CPPUNIT_TEST(test_optional);
	CPPUNIT_TEST(test_shared);
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp() {}
	void tearDown() {}

	void test_optional();
	void test_shared();
};

CPPUNIT_TEST_SUITE_REGISTRATION(smart_pointer_test);

namespace fz {
template<typename T>
std::ostream& operator<<(std::ostream& s, sparse_optional<T> const& t) {
	if (!t) {
		s << "null";
	}
	else {
		s << *t;
	}
	return s;
}

template<typename T>
std::ostream& operator<<(std::ostream& s, shared_optional<T> const& t) {
	if (!t) {
		s << "null";
	}
	else {
		s << *t;
	}
	return s;
}
}

void smart_pointer_test::test_optional()
{
	fz::sparse_optional<int> empty, empty2;
	CPPUNIT_ASSERT(!empty);
	CPPUNIT_ASSERT_EQUAL(empty, empty2);

	fz::sparse_optional<int> two(2);
	CPPUNIT_ASSERT(static_cast<bool>(two));
	CPPUNIT_ASSERT_EQUAL(2, *two);

	CPPUNIT_ASSERT(empty < two);

	fz::sparse_optional<int> two2(2);
	fz::sparse_optional<int> three(3);

	CPPUNIT_ASSERT(two < three);
	CPPUNIT_ASSERT_EQUAL(two, two2);
}

void smart_pointer_test::test_shared()
{
	fz::shared_optional<int> empty, empty2;
	CPPUNIT_ASSERT(!empty);
	CPPUNIT_ASSERT_EQUAL(empty, empty2);

	fz::shared_optional<int> two(2);
	CPPUNIT_ASSERT(static_cast<bool>(two));
	CPPUNIT_ASSERT_EQUAL(2, *two);

	CPPUNIT_ASSERT(empty < two);

	fz::shared_optional<int> two2(2);
	fz::shared_optional<int> three(3);

	CPPUNIT_ASSERT(two < three);
	CPPUNIT_ASSERT_EQUAL(two, two2);


	CPPUNIT_ASSERT(&*two != &*two2);
	fz::shared_optional<int> shared_two(two);
	CPPUNIT_ASSERT(&*two == &*shared_two);

	shared_two.get() = 4;
	CPPUNIT_ASSERT(&*two != &*shared_two);
	CPPUNIT_ASSERT_EQUAL(2, *two);


	fz::shared_value<int> v;
	CPPUNIT_ASSERT(static_cast<bool>(v));
	CPPUNIT_ASSERT(v == int());

	{
		fz::shared_optional<int> a(7);
		fz::shared_optional<int> b = a;
		b.clear();
		CPPUNIT_ASSERT_EQUAL(7, *a);
	}

	{
		fz::shared_value<int> a(7);
		fz::shared_value<int> b = a;
		b.clear();
		CPPUNIT_ASSERT_EQUAL(7, *a);
	}

}