Debian Codegraph @

src/python/PyImathTest/testStringTable.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <PyImathStringTable.h>
11 #include <iostream>
12 #include <assert.h>
13
14 using namespace std;
15 using namespace PyImath;
16
17 namespace {
18
19 void
20 testString()
21 {
22 StringTable st;
23
24 size_t size = st.size();
25 assert(size == 0);
26 assert(!st.hasString("foo"));
27
28 StringTableIndex index = st.intern("foo");
29 assert(index == st.lookup("foo"));
30 assert(st.lookup(index) == "foo");
31 assert(st.hasString("foo"));
32
33 size = st.size();
34 assert(size == 1);
35
36 bool thrown = false;
37 try {
38 index = st.lookup("bar");
39 } catch (std::exception &e) {
40 thrown = true;
41 }
42 assert (thrown);
43
44 thrown = false;
45 try {
46 std::string s = st.lookup(StringTableIndex(1));
47 } catch (std::exception &e) {
48 thrown = true;
49 }
50 assert (thrown);
51
52 assert(!st.hasString("bar"));
53
54 index = st.intern("bar");
55 assert(index == st.lookup("bar"));
56 assert(st.lookup(index) == "bar");
57 assert(0 == st.lookup("foo").index());
58 assert(st.lookup(StringTableIndex(0)) == "foo");
59 assert(st.hasString("bar"));
60 }
61
62 void
63 testWString()
64 {
65 WStringTable st;
66
67 size_t size = st.size();
68 assert(size == 0);
69 assert(!st.hasString(L"foo"));
70
71 StringTableIndex index = st.intern(L"foo");
72 assert(index == st.lookup(L"foo"));
73 assert(st.lookup(index) == L"foo");
74 assert(st.hasString(L"foo"));
75
76 size = st.size();
77 assert(size == 1);
78
79 bool thrown = false;
80 try {
81 index = st.lookup(L"bar");
82 } catch (std::exception &e) {
83 thrown = true;
84 }
85 assert (thrown);
86
87 thrown = false;
88 try {
89 std::wstring s = st.lookup(StringTableIndex(1));
90 } catch (std::exception &e) {
91 thrown = true;
92 }
93 assert (thrown);
94
95 assert(!st.hasString(L"bar"));
96
97 index = st.intern(L"bar");
98 assert(index == st.lookup(L"bar"));
99 assert(st.lookup(index) == L"bar");
100 assert(0 == st.lookup(L"foo").index());
101 assert(st.lookup(StringTableIndex(0)) == L"foo");
102 assert(st.hasString(L"bar"));
103 }
104
105 } // namespace
106
107
108 void
109 testStringTable()
110 {
111 cout << "Testing string table:" << endl;
112
113 try
114 {
115 testString();
116 testWString();
117 }
118 catch (const exception &e)
119 {
120 cerr << "Unexpected exception: " << e.what() << endl;
121 assert(false);
122 }
123
124 cout << "ok\n" << endl;
125 }