lumiera_/tests/library/symbol-test.cpp
Ichthyostega 806db414dd Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
 * there is no entity "Lumiera.org" which holds any copyrights
 * Lumiera source code is provided under the GPL Version 2+

== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''

The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!

The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00

253 lines
7.4 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Symbol(Test) - verify basic properties of a Symbol datatype
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file symbol-test.cpp
** unit test \ref Symbol_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/format-cout.hpp"
#include "lib/util.hpp"
#include "lib/symbol.hpp"
#include <string>
#include <map>
using util::typeStr;
using util::isSameObject;
using util::isnil;
using std::string;
using std::map;
namespace lib {
namespace test{
/***********************************************************//**
* @test properties of Literal and Symbol data types.
* - a lib::Literal is just a thin wrapper for a `const char *`
* - lib::Symbol uses the same implementation, but relies on
* character string constants _interned_ into a symbol table.
* @todo 2023 this test is very much in preliminary shape, as the
* implementation of a real symbol table remains was lacking.
* At some point, a simplistic implementation of »interned strings«
* was added (TICKET #157) — yet still the actual relevance of
* unique Symbols remains nebulous
*/
class Symbol_test : public Test
{
virtual void
run (Arg)
{
checkLiteral();
checkSymbolCreation();
checkComparisons();
use_as_map_key();
}
void
checkLiteral()
{
Literal li1 ("one");
Literal li2 (li1);
Literal li3 ("one ");
cout << li1 << endl;
cout << showSizeof(li1) << endl;
CHECK (sizeof(Literal) == sizeof(char*));
CHECK (li1 == li2);
CHECK (!isSameObject (li1,li2));
CHECK (li1 != li3);
CHECK (li2 != li3);
CHECK (li3 != li2);
CHECK ("string" == typeStr (li1 + string{"night"}));
CHECK ("string" == typeStr (string{"minus " +li1}));
cout << li1 + string{"night"} << endl;
cout << string{"minus " +li1} << endl;
cout << li2+string("..") << string("..")+li2 << endl;
CHECK (hash_value(li1) == hash_value(li2));
CHECK (hash_value(li2) != hash_value(li3));
}
void
checkEmptyLiteral()
{
Literal nn1 (0);
Literal nn2 ("");
CHECK (isnil (nn1));
CHECK (isnil (nn2));
Literal nnn (" ");
CHECK (!isnil (nnn));
}
void
checkSymbolCreation()
{
Literal l1("1");
Symbol sy1("1");
Symbol sy2(l1);
CHECK (sy1 == sy2);
CHECK (not isSameObject (l1,sy1));
CHECK (not isSameObject (sy1,sy2));
CHECK (l1.c() != sy1.c());
CHECK (sy1.c() == sy2.c());
Symbol sy3;
CHECK (not sy3);
CHECK (sy3 == "");
CHECK (isnil(sy3));
CHECK (sy1 != sy3);
CHECK (not Symbol{""});
CHECK (sy3 == Symbol{""});
CHECK (sy3.c() == Symbol{""}.c());
CHECK (Symbol{}.c() == Symbol{""}.c());
// EMPTY and BOTTOM are distinct Symbols, yet both count as "empty"
CHECK (Symbol::EMPTY == Symbol{""});
CHECK (Symbol::BOTTOM == Symbol{""});
CHECK (Symbol::EMPTY != Symbol::BOTTOM);
CHECK (Symbol{nullptr} == Symbol::BOTTOM);
CHECK (Symbol::EMPTY.empty());
CHECK (Symbol::BOTTOM.empty());
CHECK (not Symbol::FAILURE.empty());
CHECK (isnil (Symbol{""}));
CHECK (isnil (Symbol{""}));
// re-assignment
sy3 = Symbol{l1};
CHECK (!isnil(sy3));
CHECK (sy1 == sy3);
Symbol sy4{sy3, "11"};
CHECK (sy4 == "1.11");
CHECK (not isSameObject(sy4.c(), "1.11"));
CHECK (sy4.c() == Symbol{"1.11"}.c());
CHECK (sy4.c() == (const char*)sy4);
CHECK (hash_value(sy4) == hash_value(Symbol{"1.11"}));
}
void
checkComparisons()
{
const char* s1 = "1";
const char* s3 = "11";
const char* s2 = s3+1;
CHECK (s1 != s2);
CHECK (s1 != s3);
CHECK (s2 != s3);
Literal l1(s1);
Literal l2(s2);
Literal l3(s3);
CHECK (l1 == l2);
CHECK (l1 != l3);
CHECK (l3 != l1);
CHECK (l2 != l3);
CHECK (l3 != l2);
CHECK (l1 == s1);
CHECK (s1 == l1);
CHECK (l1 == s2);
CHECK (s2 == l1);
CHECK (l1 != s3);
CHECK (s3 != l1);
CHECK (not isSameObject(l1, l2));
CHECK (not isSameObject(l1.c(), l2.c()));
Symbol y1{s1};
Symbol y2{l2};
Symbol y3{"11"};
CHECK (y1 == y2);
CHECK (y1.c() == y2.c());
CHECK (not isSameObject (y1.c(), y2.c()));
CHECK ( isSameObject (*y1.c(), *y2.c()));
CHECK (y1 != y3);
CHECK (y3 != y1);
CHECK (y2 != y3);
CHECK (y3 != y2);
CHECK (y1 == l1); CHECK (l1 == y1);
CHECK (y1 == s1); CHECK (s1 == y1);
CHECK (y1 == l2); CHECK (l2 == y1);
CHECK (y1 == s2); CHECK (s2 == y1);
CHECK (y3 != l1); CHECK (l1 != y3);
CHECK (y3 != s1); CHECK (s1 != y3);
CHECK (y3 != l2); CHECK (l2 != y3);
CHECK (y3 != s2); CHECK (s2 != y3);
}
/** @test use Literal and Symbol as keys in a tree map.
* @note neither Literal, nor Symbol defines an `operator<`
* so the map specialisation has to fall back on const char*,
* which are compared based on pointer identity. Contrast this
* with std::string, which does define its own ordering.
*/
void
use_as_map_key()
{
map<Literal, int> mli;
map<Symbol, int> myi;
map<string, int> msi;
Literal l1{"1"}, l2{"2"};
Symbol y1{l1}, y2{l2};
string s1{y1}, s2{"2"};
mli[l1] = 1; myi[y1] = 1; msi[s1] = 1;
mli[l2] = 2; myi[y2] = 2; msi[s2] = 2;
CHECK (mli[l1] == 1);
CHECK (mli[l2] == 2);
CHECK (myi[y1] == 1);
CHECK (myi[y2] == 2);
CHECK (msi[s1] == 1);
CHECK (msi[s2] == 2);
const char* x = "11";
++x;
CHECK (x != l1.c());
CHECK (x == l1);
CHECK (x == y1);
CHECK (x == s1);
CHECK (mli[x] == 0); // Note: not found, since it is a different pointer
CHECK (myi[x] == 1); // Note: same string mapped to same ptr in Symbol
CHECK (msi[x] == 1);
}
};
LAUNCHER (Symbol_test, "unit common");
}} // namespace lib::test