Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
optional-ref-test.cpp
Go to the documentation of this file.
1/*
2 OptionalRef(Test) - verify an optional and switchable object link
3
4 Copyright (C)
5 2010, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
20#include "lib/test/run.hpp"
22
23#include "lib/optional-ref.hpp"
24
25
26
27namespace lib {
28namespace test{
29
30 using ::Test;
32
33 using std::string;
34
35
36
37 /***************************************************************************/
49 class OptionalRef_test : public Test
50 {
51
52
53 virtual void
54 run (Arg)
55 {
56 string s1 (randStr(50));
57 string s2 (randStr(50));
58
59 typedef OptionalRef<string> SRef;
60
61 SRef r1;
62 CHECK (!r1);
63 VERIFY_ERROR (BOTTOM_VALUE, r1() );
64 VERIFY_ERROR (BOTTOM_VALUE, s1 == r1 );
65 VERIFY_ERROR (BOTTOM_VALUE, r1 == s1 );
66 CHECK (!r1.points_to (s1));
67
68 r1.link_to (s1);
69 CHECK (r1);
70 CHECK (r1 == s1);
71 CHECK (s1 == r1);
72 CHECK (r1.points_to (s1));
73
74 SRef r2(s2);
75 CHECK (r2);
76 CHECK (r2 == s2);
77 CHECK (r2.points_to (s2));
78 CHECK (!r2.points_to (s1));
79 CHECK (!r1.points_to (s2));
80 CHECK (r2 != r1);
81 CHECK (r1 != r2);
82
83 r2.link_to (s1);
84 CHECK (r2);
85 CHECK (r2 == s1);
86 CHECK (r2 == r1);
87 CHECK (r1 == r2);
88 CHECK (r2.points_to (s1));
89 CHECK (!r2.points_to (s2));
90
91 r2.clear();
92 CHECK (!r2);
93 VERIFY_ERROR (BOTTOM_VALUE, r2() );
94 VERIFY_ERROR (BOTTOM_VALUE, s1 == r2 );
95 VERIFY_ERROR (BOTTOM_VALUE, r2 == s1 );
96 VERIFY_ERROR (BOTTOM_VALUE, r2 == s2 );
97
98 CHECK (r1 != r2); // comparison with bottom ref allowed
99 CHECK (r2 != r1);
100
101 //OptionalRef objects are copyable values
102 r2 = r1;
103 CHECK (r2);
104 CHECK (r2 == r1);
105 CHECK (r1 == r2);
106 CHECK (r2 == s1);
107
108 r1.link_to (s2);
109 CHECK (r2 != r1); // but they are indeed independent instances
110 CHECK (r1 != r2);
111 CHECK (r2 == s1);
112 CHECK (r2 != s2);
113 CHECK (r1 == s2);
114
115 SRef r3(r2);
116 CHECK (r3);
117 CHECK (r3 == r2);
118 CHECK (r2 == r3);
119 CHECK (r3 == s1);
120
121 CHECK (r3 != r1);
122 CHECK (r1 != r3);
123 CHECK (r3 != s2);
124
125 // access is secured even after destruction
126 CHECK (r3);
127 r3.~SRef(); // don't try this at home!
128 CHECK (!r3);
129 VERIFY_ERROR (BOTTOM_VALUE, r3 == s1 );
130 CHECK (r3 != r2);
131
132 r2.clear();
133 CHECK (!r2);
134 CHECK (r3 == r2);
135 CHECK (r2 == r3);
136 }
137
138 };
139
140 LAUNCHER (OptionalRef_test, "unit common");
141
142
143}} // namespace lib::test
Optional or switchable link to an existing object.
string randStr(size_t len)
create garbage string of given length
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
a checked, switchable reference.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.