Lumiera  0.pre.03
»edit your freedom«
symbol-test.cpp
Go to the documentation of this file.
1 /*
2  Symbol(Test) - verify basic properties of a Symbol datatype
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-cout.hpp"
31 #include "lib/util.hpp"
32 
33 #include "lib/symbol.hpp"
34 
35 #include <string>
36 #include <map>
37 
38 using util::typeStr;
39 using util::isSameObject;
40 using util::isnil;
41 using std::string;
42 using std::map;
43 
44 
45 
46 namespace lib {
47 namespace test{
48 
49 
50  /***********************************************************/
61  class Symbol_test : public Test
62  {
63 
64  virtual void
65  run (Arg)
66  {
67  checkLiteral();
68  checkSymbolCreation();
69  checkComparisons();
71  }
72 
73 
74  void
75  checkLiteral()
76  {
77  Literal li1 ("one");
78  Literal li2 (li1);
79  Literal li3 ("one ");
80 
81  cout << li1 << endl;
82  cout << showSizeof(li1) << endl;
83  CHECK (sizeof(Literal) == sizeof(char*));
84 
85  CHECK (li1 == li2);
86  CHECK (!isSameObject (li1,li2));
87  CHECK (li1 != li3);
88  CHECK (li2 != li3);
89  CHECK (li3 != li2);
90 
91  CHECK ("string" == typeStr (li1 + string{"night"}));
92  CHECK ("string" == typeStr (string{"minus " +li1}));
93  cout << li1 + string{"night"} << endl;
94  cout << string{"minus " +li1} << endl;
95  cout << li2+string("..") << string("..")+li2 << endl;
96 
97  CHECK (hash_value(li1) == hash_value(li2));
98  CHECK (hash_value(li2) != hash_value(li3));
99  }
100 
101 
102  void
103  checkEmptyLiteral()
104  {
105  Literal nn1 (0);
106  Literal nn2 ("");
107 
108  CHECK (isnil (nn1));
109  CHECK (isnil (nn2));
110 
111  Literal nnn (" ");
112  CHECK (!isnil (nnn));
113  }
114 
115 
116  void
117  checkSymbolCreation()
118  {
119  Literal l1("1");
120  Symbol sy1("1");
121  Symbol sy2(l1);
122 
123  CHECK (sy1 == sy2);
124  CHECK (not isSameObject (l1,sy1));
125  CHECK (not isSameObject (sy1,sy2));
126  CHECK (l1.c() != sy1.c());
127  CHECK (sy1.c() == sy2.c());
128 
129  Symbol sy3;
130  CHECK (not sy3);
131  CHECK (sy3 == "⟂");
132  CHECK (isnil(sy3));
133  CHECK (sy1 != sy3);
134 
135  CHECK (not Symbol{"⟂"});
136  CHECK (sy3 == Symbol{"⟂"});
137  CHECK (sy3.c() == Symbol{"⟂"}.c());
138  CHECK (Symbol{}.c() == Symbol{"⟂"}.c());
139 
140  // EMPTY and BOTTOM are distinct Symbols, yet both count as "empty"
141  CHECK (Symbol::EMPTY == Symbol{""});
142  CHECK (Symbol::BOTTOM == Symbol{"⟂"});
143  CHECK (Symbol::EMPTY != Symbol::BOTTOM);
144  CHECK (Symbol{nullptr} == Symbol::BOTTOM);
145  CHECK (Symbol::EMPTY.empty());
146  CHECK (Symbol::BOTTOM.empty());
147  CHECK (not Symbol::FAILURE.empty());
148  CHECK (isnil (Symbol{"⟂"}));
149  CHECK (isnil (Symbol{""}));
150 
151  // re-assignment
152  sy3 = Symbol{l1};
153  CHECK (!isnil(sy3));
154  CHECK (sy1 == sy3);
155 
156  Symbol sy4{sy3, "11"};
157  CHECK (sy4 == "1.11");
158  CHECK (not isSameObject(sy4.c(), "1.11"));
159  CHECK (sy4.c() == Symbol{"1.11"}.c());
160  CHECK (sy4.c() == (const char*)sy4);
161  CHECK (hash_value(sy4) == hash_value(Symbol{"1.11"}));
162  }
163 
164 
165  void
166  checkComparisons()
167  {
168  const char* s1 = "1";
169  const char* s3 = "11";
170  const char* s2 = s3+1;
171 
172  CHECK (s1 != s2);
173  CHECK (s1 != s3);
174  CHECK (s2 != s3);
175 
176  Literal l1(s1);
177  Literal l2(s2);
178  Literal l3(s3);
179 
180  CHECK (l1 == l2);
181  CHECK (l1 != l3);
182  CHECK (l3 != l1);
183  CHECK (l2 != l3);
184  CHECK (l3 != l2);
185 
186  CHECK (l1 == s1);
187  CHECK (s1 == l1);
188  CHECK (l1 == s2);
189  CHECK (s2 == l1);
190  CHECK (l1 != s3);
191  CHECK (s3 != l1);
192  CHECK (not isSameObject(l1, l2));
193  CHECK (not isSameObject(l1.c(), l2.c()));
194 
195  Symbol y1{s1};
196  Symbol y2{l2};
197  Symbol y3{"11"};
198 
199  CHECK (y1 == y2);
200  CHECK (y1.c() == y2.c());
201  CHECK (not isSameObject (y1.c(), y2.c()));
202  CHECK ( isSameObject (*y1.c(), *y2.c()));
203  CHECK (y1 != y3);
204  CHECK (y3 != y1);
205  CHECK (y2 != y3);
206  CHECK (y3 != y2);
207 
208  CHECK (y1 == l1); CHECK (l1 == y1);
209  CHECK (y1 == s1); CHECK (s1 == y1);
210  CHECK (y1 == l2); CHECK (l2 == y1);
211  CHECK (y1 == s2); CHECK (s2 == y1);
212  CHECK (y3 != l1); CHECK (l1 != y3);
213  CHECK (y3 != s1); CHECK (s1 != y3);
214  CHECK (y3 != l2); CHECK (l2 != y3);
215  CHECK (y3 != s2); CHECK (s2 != y3);
216  }
217 
218 
225  void
227  {
228  map<Literal, int> mli;
229  map<Symbol, int> myi;
230  map<string, int> msi;
231 
232  Literal l1{"1"}, l2{"2"};
233  Symbol y1{l1}, y2{l2};
234  string s1{y1}, s2{"2"};
235 
236  mli[l1] = 1; myi[y1] = 1; msi[s1] = 1;
237  mli[l2] = 2; myi[y2] = 2; msi[s2] = 2;
238 
239  CHECK (mli[l1] == 1);
240  CHECK (mli[l2] == 2);
241  CHECK (myi[y1] == 1);
242  CHECK (myi[y2] == 2);
243  CHECK (msi[s1] == 1);
244  CHECK (msi[s2] == 2);
245 
246  const char* x = "11";
247  ++x;
248  CHECK (x != l1.c());
249  CHECK (x == l1);
250  CHECK (x == y1);
251  CHECK (x == s1);
252 
253  CHECK (mli[x] == 0); // Note: not found, since it is a different pointer
254  CHECK (myi[x] == 1); // Note: same string mapped to same ptr in Symbol
255  CHECK (msi[x] == 1);
256  }
257  };
258 
259  LAUNCHER (Symbol_test, "unit common");
260 
261 
262 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:57
Implementation namespace for support and library code.
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Marker types to indicate a literal string and a Symbol.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
HashVal hash_value(QueryText const &entry)
support using queries in hashtables.
Definition: query-text.cpp:61
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372