Lumiera  0.pre.03
»edit your freedom«
util-identity-test.cpp
Go to the documentation of this file.
1 /*
2  UtilIdentity(Test) - helpers for identity and memory location
3 
4  Copyright (C)
5  2024, 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/format-obj.hpp"
21 #include "lib/util.hpp"
22 
23 #include <utility>
24 #include <string>
25 
26 using std::move;
27 using std::string;
28 
29 
30 namespace util {
31 namespace test {
32 
33 
34 
35  /*****************************************************************/
53  class UtilIdentity_test : public Test
54  {
55 
56  void
57  run (Arg)
58  {
59  verify_getAdr();
60  verify_addrID();
63  }
64 
65 
69  void
71  {
72  CHECK (getAdr (this) == this);
73  CHECK (getAdr (*this) == this);
74 
75  CStr aloof[2] = {"reality", "check"};
76  CHECK (getAdr (aloof) == &aloof);
77  CHECK (getAdr (&aloof[0]) == &aloof);
78  CHECK (getAdr (&aloof[1]) == aloof+1);
79  CHECK (getAdr ( aloof[0]) == aloof[0]);
80  CHECK (getAdr ( aloof[1]) == aloof[1]);
81  }
82 
84  void
86  {
87  uint ui[2] = {2,3};
88  CHECK (addrID (ui[1]) == addrID (ui[0]) + sizeof(uint));
89 
90  uint* up{ui+1};
91  CHECK (addrID (ui[1]) == addrID (up));
92  }
93 
94 
96  struct Boo
97  {
98  short moo;
99  string woo;
100 
101  Boo()
102  : moo(rani(1000))
103  , woo{toString(moo-1)}
104  { }
105  };
106 
107  struct SuBoo : Boo
108  {
109  using Boo::Boo;
110  size_t poo{addrID (this)};
111  };
112 
113  static Boo* asBoo (void* mem) { return static_cast<Boo*> (mem); }
114 
115 
119  void
121  {
122  Boo boo;
123  Boo booo;
124  Boo* boop = &boo;
125  Boo const* const beep = boop;
126  CHECK (boo.moo != booo.moo);
127  CHECK (boo.moo == boop->moo);
128  CHECK ( isSameAdr (boop, beep));
129  CHECK (not isSameAdr (&boop,&beep));
130  CHECK ( isSameAdr (boo, beep));
131  CHECK ( isSameAdr (*beep, boo ));
132  CHECK (not isSameAdr (*beep, booo));
133  CHECK ( isSameAdr (boo, boo.moo));
134  CHECK ( isSameAdr (boo, &boo.moo));
135  CHECK (not isSameAdr (boo.moo, booo));
136  CHECK ( isSameAdr (booo, asBoo(&booo.moo)));
137  CHECK (not isSameAdr (booo, asBoo(&booo.woo)));
138 
139  // handles also void*
140  const void* voo = boop;
141  CHECK ( isSameAdr (voo, boo));
142  CHECK ( isSameAdr (voo, boop));
143  CHECK (not isSameAdr (voo, booo));
144  CHECK ( isSameAdr (voo, asBoo(&boo.moo)));
145  CHECK (not isSameAdr (voo, asBoo(&booo.moo)));
146  CHECK (not isSameAdr (voo, asBoo(&boo.woo)));
147 
148  // RValue taken by ref
149  Boo&& roo = move(boo);
150  CHECK ( isSameAdr (roo, boo));
151  CHECK ( isSameAdr (voo, roo));
152  CHECK (not isSameAdr (voo, Boo{roo}));
153 
154  // type information disregarded
155  SuBoo* suBoo = static_cast<SuBoo*>(&boo);
156  CHECK ( isSameAdr (boo, suBoo));
157  CHECK ( isSameAdr (boo, suBoo->moo));
158  CHECK ( isSameAdr (voo, suBoo->moo));
159  CHECK (not isSameAdr (voo, suBoo->poo));
160 
161  SuBoo sudo{*suBoo};
162  CHECK (not isSameAdr (sudo, boo));
163  CHECK (not isSameAdr (sudo, suBoo));
164  suBoo = &sudo;
165  CHECK ( isSameAdr (sudo.poo, suBoo->poo));
166  }
167 
169  void
171  {
172  Boo boo;
173  Boo booo;
174  Boo* boop = &boo;
175  Boo* woop = boop;
176  Boo& foop = *woop;
177  CHECK ( isSameObject (boo, boo ));
178  CHECK ( isSameObject (booo, booo));
179  CHECK (not isSameObject (boo, booo));
180  CHECK (not isSameObject (booo, boo ));
181  // pointers count as »objects« and are not dereferenced
182  CHECK (not isSameObject (boop, woop));
183  CHECK (not isSameObject (boop, booo));
184  CHECK (not isSameObject (boop, boo ));
185  CHECK (not isSameObject (booo, woop));
186  CHECK (not isSameObject (boo , woop));
187  CHECK ( isSameObject (boo , foop));
188  CHECK ( isSameObject (foop, boo ));
189  CHECK (not isSameObject (foop, boop));
190  CHECK (not isSameObject (foop, woop));
191  CHECK (not isSameObject (foop, &boo ));
192  CHECK ( isSameObject (foop, *boop));
193  CHECK ( isSameObject (*boop, foop));
194 
195  // RValue taken by ref
196  Boo&& roo = move(boo);
197  CHECK ( isSameObject (roo, boo));
198  CHECK (not isSameObject (boo, Boo{roo}));
199 
200  // type information disregarded
201  SuBoo* suBoo = static_cast<SuBoo*>(&boo);
202  CHECK ( isSameObject (boo, *suBoo));
203  CHECK ( isSameObject (boo, suBoo->moo));
204  CHECK (not isSameObject (boo, suBoo->woo));
205 
206  // void* is treated as a distinct object
207  void* voo = boop;
208  CHECK (not isSameObject (voo , boop));
209  CHECK (not isSameObject (voo , boo));
210  CHECK (not isSameObject (voo , &boo)); // ...not getting anywhere...
211  CHECK (not isSameObject (voo , asBoo(&boo)));
212  CHECK (not isSameObject (boo , asBoo(&boo)));
213  }
214  };
215 
216 
217  LAUNCHER (UtilIdentity_test, "unit common");
218 
219 }} // namespace util::test
220 
Definition: run.hpp:40
const void * getAdr(X &x)
extract address but strip any type info
Definition: util.hpp:377
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
size_t addrID(X const &x)
generate an unique numeric ID based on the referred entity
Definition: util.hpp:391
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:191
Simple functions to represent objects, for debugging and diagnostics.
bool isSameAdr(A const &a, B const &b)
compare plain object address identity, disregarding type.
Definition: util.hpp:411
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