Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/test/run.hpp"
30 #include "lib/test/test-helper.hpp"
31 
32 #include "lib/optional-ref.hpp"
33 
34 
35 
36 namespace lib {
37 namespace test{
38 
39  using ::Test;
40  using lib::test::randStr;
41 
42  using std::string;
43 
44 
45 
46  /***************************************************************************/
58  class OptionalRef_test : public Test
59  {
60 
61 
62  virtual void
63  run (Arg)
64  {
65  string s1 (randStr(50));
66  string s2 (randStr(50));
67 
68  typedef OptionalRef<string> SRef;
69 
70  SRef r1;
71  CHECK (!r1);
72  VERIFY_ERROR (BOTTOM_VALUE, r1() );
73  VERIFY_ERROR (BOTTOM_VALUE, s1 == r1 );
74  VERIFY_ERROR (BOTTOM_VALUE, r1 == s1 );
75  CHECK (!r1.points_to (s1));
76 
77  r1.link_to (s1);
78  CHECK (r1);
79  CHECK (r1 == s1);
80  CHECK (s1 == r1);
81  CHECK (r1.points_to (s1));
82 
83  SRef r2(s2);
84  CHECK (r2);
85  CHECK (r2 == s2);
86  CHECK (r2.points_to (s2));
87  CHECK (!r2.points_to (s1));
88  CHECK (!r1.points_to (s2));
89  CHECK (r2 != r1);
90  CHECK (r1 != r2);
91 
92  r2.link_to (s1);
93  CHECK (r2);
94  CHECK (r2 == s1);
95  CHECK (r2 == r1);
96  CHECK (r1 == r2);
97  CHECK (r2.points_to (s1));
98  CHECK (!r2.points_to (s2));
99 
100  r2.clear();
101  CHECK (!r2);
102  VERIFY_ERROR (BOTTOM_VALUE, r2() );
103  VERIFY_ERROR (BOTTOM_VALUE, s1 == r2 );
104  VERIFY_ERROR (BOTTOM_VALUE, r2 == s1 );
105  VERIFY_ERROR (BOTTOM_VALUE, r2 == s2 );
106 
107  CHECK (r1 != r2); // comparison with bottom ref allowed
108  CHECK (r2 != r1);
109 
110  //OptionalRef objects are copyable values
111  r2 = r1;
112  CHECK (r2);
113  CHECK (r2 == r1);
114  CHECK (r1 == r2);
115  CHECK (r2 == s1);
116 
117  r1.link_to (s2);
118  CHECK (r2 != r1); // but they are indeed independent instances
119  CHECK (r1 != r2);
120  CHECK (r2 == s1);
121  CHECK (r2 != s2);
122  CHECK (r1 == s2);
123 
124  SRef r3(r2);
125  CHECK (r3);
126  CHECK (r3 == r2);
127  CHECK (r2 == r3);
128  CHECK (r3 == s1);
129 
130  CHECK (r3 != r1);
131  CHECK (r1 != r3);
132  CHECK (r3 != s2);
133 
134  // access is secured even after destruction
135  CHECK (r3);
136  r3.~SRef(); // don't try this at home!
137  CHECK (!r3);
138  VERIFY_ERROR (BOTTOM_VALUE, r3 == s1 );
139  CHECK (r3 != r2);
140 
141  r2.clear();
142  CHECK (!r2);
143  CHECK (r3 == r2);
144  CHECK (r2 == r3);
145  }
146 
147  };
148 
149  LAUNCHER (OptionalRef_test, "unit common");
150 
151 
152 }} // namespace lib::test
Optional or switchable link to an existing object.
a checked, switchable reference.
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify a statement indeed raises an exception.
Implementation namespace for support and library code.
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:69
Simple test class runner.
A collection of frequently used helper functions to support unit testing.