/
main.cpp
114 lines (93 loc) · 3.01 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @file main.cpp
*
* StringPool unit tests. @ingroup tests
*
* @author Copyright © 2010-2013 Daniel Swanson <danij@dengine.net>
* @author Copyright © 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <de/StringPool>
#include <de/Reader>
#include <de/Writer>
#include <iostream>
using namespace de;
int main(int, char **)
{
try
{
StringPool p;
String s = String("Hello");
DE_ASSERT(!p.isInterned(s));
DE_ASSERT(p.empty());
// First string.
p.intern(s);
DE_ASSERT(p.isInterned(s) == 1);
// Re-insertion.
DE_ASSERT(p.intern(s) == 1);
// Case insensitivity.
s = String("heLLO");
DE_ASSERT(p.intern(s) == 1);
// Another string.
s = String("abc");
String const &is = p.internAndRetrieve(s);
DE_ASSERT(!is.compare(s));
DE_UNUSED(is);
String s2 = String("ABC");
String const &is2 = p.internAndRetrieve(s2);
DE_ASSERT(!is2.compare(s));
DE_UNUSED(is2);
DE_ASSERT(p.intern(is2) == 2);
DE_ASSERT(p.size() == 2);
//p.print();
DE_ASSERT(!p.empty());
p.setUserValue(1, 1234);
DE_ASSERT(p.userValue(1) == 1234);
DE_ASSERT(p.userValue(2) == 0);
s = String("HELLO");
p.remove(s);
DE_ASSERT(!p.isInterned(s));
DE_ASSERT(p.size() == 1);
DE_ASSERT(!p.string(2).compare("abc"));
s = String("Third!");
DE_ASSERT(p.intern(s) == 1);
DE_ASSERT(p.size() == 2);
s = String("FOUR");
p.intern(s);
p.removeById(1); // "Third!"
// Serialize.
Block b;
Writer(b) << p;
std::cout << "Serialized StringPool to " << b.size() << " bytes." << std::endl;
// Deserialize.
StringPool p2;
Reader(b) >> p2;
//p2.print();
DE_ASSERT(p2.size() == 2);
DE_ASSERT(!p2.string(2).compare("abc"));
DE_ASSERT(!p2.string(3).compare("FOUR"));
s = String("hello again");
DE_ASSERT(p2.intern(s) == 1);
p.clear();
DE_ASSERT(p.empty());
}
catch (Error const &err)
{
err.warnPlainText();
}
debug("Exiting main()...");
return 0;
}