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)
5  2009, 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 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/format-cout.hpp"
22 #include "lib/util.hpp"
23 
24 #include "lib/symbol.hpp"
25 
26 #include <string>
27 #include <map>
28 
29 using util::typeStr;
30 using util::isSameObject;
31 using util::isnil;
32 using std::string;
33 using std::map;
34 
35 
36 
37 namespace lib {
38 namespace test{
39 
40 
41  /***********************************************************/
52  class Symbol_test : public Test
53  {
54 
55  virtual void
56  run (Arg)
57  {
58  checkLiteral();
59  checkSymbolCreation();
60  checkComparisons();
62  }
63 
64 
65  void
66  checkLiteral()
67  {
68  Literal li1 ("one");
69  Literal li2 (li1);
70  Literal li3 ("one ");
71 
72  cout << li1 << endl;
73  cout << showSizeof(li1) << endl;
74  CHECK (sizeof(Literal) == sizeof(char*));
75 
76  CHECK (li1 == li2);
77  CHECK (!isSameObject (li1,li2));
78  CHECK (li1 != li3);
79  CHECK (li2 != li3);
80  CHECK (li3 != li2);
81 
82  CHECK ("string" == typeStr (li1 + string{"night"}));
83  CHECK ("string" == typeStr (string{"minus " +li1}));
84  cout << li1 + string{"night"} << endl;
85  cout << string{"minus " +li1} << endl;
86  cout << li2+string("..") << string("..")+li2 << endl;
87 
88  CHECK (hash_value(li1) == hash_value(li2));
89  CHECK (hash_value(li2) != hash_value(li3));
90  }
91 
92 
93  void
94  checkEmptyLiteral()
95  {
96  Literal nn1 (0);
97  Literal nn2 ("");
98 
99  CHECK (isnil (nn1));
100  CHECK (isnil (nn2));
101 
102  Literal nnn (" ");
103  CHECK (!isnil (nnn));
104  }
105 
106 
107  void
108  checkSymbolCreation()
109  {
110  Literal l1("1");
111  Symbol sy1("1");
112  Symbol sy2(l1);
113 
114  CHECK (sy1 == sy2);
115  CHECK (not isSameObject (l1,sy1));
116  CHECK (not isSameObject (sy1,sy2));
117  CHECK (l1.c() != sy1.c());
118  CHECK (sy1.c() == sy2.c());
119 
120  Symbol sy3;
121  CHECK (not sy3);
122  CHECK (sy3 == "⟂");
123  CHECK (isnil(sy3));
124  CHECK (sy1 != sy3);
125 
126  CHECK (not Symbol{"⟂"});
127  CHECK (sy3 == Symbol{"⟂"});
128  CHECK (sy3.c() == Symbol{"⟂"}.c());
129  CHECK (Symbol{}.c() == Symbol{"⟂"}.c());
130 
131  // EMPTY and BOTTOM are distinct Symbols, yet both count as "empty"
132  CHECK (Symbol::EMPTY == Symbol{""});
133  CHECK (Symbol::BOTTOM == Symbol{"⟂"});
134  CHECK (Symbol::EMPTY != Symbol::BOTTOM);
135  CHECK (Symbol{nullptr} == Symbol::BOTTOM);
136  CHECK (Symbol::EMPTY.empty());
137  CHECK (Symbol::BOTTOM.empty());
138  CHECK (not Symbol::FAILURE.empty());
139  CHECK (isnil (Symbol{"⟂"}));
140  CHECK (isnil (Symbol{""}));
141 
142  // re-assignment
143  sy3 = Symbol{l1};
144  CHECK (!isnil(sy3));
145  CHECK (sy1 == sy3);
146 
147  Symbol sy4{sy3, "11"};
148  CHECK (sy4 == "1.11");
149  CHECK (not isSameObject(sy4.c(), "1.11"));
150  CHECK (sy4.c() == Symbol{"1.11"}.c());
151  CHECK (sy4.c() == (const char*)sy4);
152  CHECK (hash_value(sy4) == hash_value(Symbol{"1.11"}));
153  }
154 
155 
156  void
157  checkComparisons()
158  {
159  const char* s1 = "1";
160  const char* s3 = "11";
161  const char* s2 = s3+1;
162 
163  CHECK (s1 != s2);
164  CHECK (s1 != s3);
165  CHECK (s2 != s3);
166 
167  Literal l1(s1);
168  Literal l2(s2);
169  Literal l3(s3);
170 
171  CHECK (l1 == l2);
172  CHECK (l1 != l3);
173  CHECK (l3 != l1);
174  CHECK (l2 != l3);
175  CHECK (l3 != l2);
176 
177  CHECK (l1 == s1);
178  CHECK (s1 == l1);
179  CHECK (l1 == s2);
180  CHECK (s2 == l1);
181  CHECK (l1 != s3);
182  CHECK (s3 != l1);
183  CHECK (not isSameObject(l1, l2));
184  CHECK (not isSameObject(l1.c(), l2.c()));
185 
186  Symbol y1{s1};
187  Symbol y2{l2};
188  Symbol y3{"11"};
189 
190  CHECK (y1 == y2);
191  CHECK (y1.c() == y2.c());
192  CHECK (not isSameObject (y1.c(), y2.c()));
193  CHECK ( isSameObject (*y1.c(), *y2.c()));
194  CHECK (y1 != y3);
195  CHECK (y3 != y1);
196  CHECK (y2 != y3);
197  CHECK (y3 != y2);
198 
199  CHECK (y1 == l1); CHECK (l1 == y1);
200  CHECK (y1 == s1); CHECK (s1 == y1);
201  CHECK (y1 == l2); CHECK (l2 == y1);
202  CHECK (y1 == s2); CHECK (s2 == y1);
203  CHECK (y3 != l1); CHECK (l1 != y3);
204  CHECK (y3 != s1); CHECK (s1 != y3);
205  CHECK (y3 != l2); CHECK (l2 != y3);
206  CHECK (y3 != s2); CHECK (s2 != y3);
207  }
208 
209 
216  void
218  {
219  map<Literal, int> mli;
220  map<Symbol, int> myi;
221  map<string, int> msi;
222 
223  Literal l1{"1"}, l2{"2"};
224  Symbol y1{l1}, y2{l2};
225  string s1{y1}, s2{"2"};
226 
227  mli[l1] = 1; myi[y1] = 1; msi[s1] = 1;
228  mli[l2] = 2; myi[y2] = 2; msi[s2] = 2;
229 
230  CHECK (mli[l1] == 1);
231  CHECK (mli[l2] == 2);
232  CHECK (myi[y1] == 1);
233  CHECK (myi[y2] == 2);
234  CHECK (msi[s1] == 1);
235  CHECK (msi[s2] == 2);
236 
237  const char* x = "11";
238  ++x;
239  CHECK (x != l1.c());
240  CHECK (x == l1);
241  CHECK (x == y1);
242  CHECK (x == s1);
243 
244  CHECK (mli[x] == 0); // Note: not found, since it is a different pointer
245  CHECK (myi[x] == 1); // Note: same string mapped to same ptr in Symbol
246  CHECK (msi[x] == 1);
247  }
248  };
249 
250  LAUNCHER (Symbol_test, "unit common");
251 
252 
253 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:49
Implementation namespace for support and library code.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Marker types to indicate a literal string and a Symbol.
Simplistic 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:52
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421