forked from rubinius/rubinius
/
test_symboltable.hpp
176 lines (136 loc) · 5.38 KB
/
test_symboltable.hpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "vm/test/test.hpp"
#include "symboltable.hpp"
#include "configuration.hpp"
#include "builtin/array.hpp"
#include <algorithm>
#include <vector>
#include <string.h>
class TestSymbolTable : public CxxTest::TestSuite, public VMTest {
public:
SymbolTable* symbols;
void setUp() {
create();
symbols = new SymbolTable();
}
void tearDown() {
destroy();
}
void test_lookup_with_c_str() {
Object* sym = symbols->lookup(state, std::string("unique"));
TS_ASSERT(sym->symbol_p());
Object* sym2 = symbols->lookup(state, std::string("unique"));
TS_ASSERT_EQUALS(sym, sym2);
}
void test_lookup_with_empty_string() {
String* str = String::create(state, "");
if(LANGUAGE_18_ENABLED(state)) {
TS_ASSERT_THROWS_ASSERT(symbols->lookup(state, str), const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
} else {
Symbol* sym = symbols->lookup(state, str);
String* str2 = symbols->lookup_string(state, sym);
TS_ASSERT_EQUALS(Qtrue, str->equal(state, str2));
}
}
void test_lookup_with_null_character_in_string() {
String* str1 = String::create(state, "\0", 1);
String* str2 = String::create(state, "\0\0", 2);
if(LANGUAGE_18_ENABLED(state)) {
TS_ASSERT_THROWS_ASSERT(symbols->lookup(state, str1), const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
TS_ASSERT_THROWS_ASSERT(symbols->lookup(state, str2), const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
} else {
Symbol* sym1 = symbols->lookup(state, str1);
Symbol* sym2 = symbols->lookup(state, str2);
TS_ASSERT(sym1 != sym2);
String* str3 = symbols->lookup_string(state, sym1);
String* str4 = symbols->lookup_string(state, sym2);
TS_ASSERT_EQUALS(Qtrue, str1->equal(state, str3));
TS_ASSERT_EQUALS(Qtrue, str2->equal(state, str4));
TS_ASSERT_EQUALS(symbols->size(), 2U);
}
}
void notest_lookup_colliding_hash() {
Object* sym;
Object* sym2;
const char* str = "__uint_fast64_t";
const char* str2 = "TkIF_MOD";
TS_ASSERT_EQUALS(String::hash_str((unsigned char*)str, strlen(str)),
String::hash_str((unsigned char*)str2, strlen(str2)));
sym = symbols->lookup(state, std::string(str));
sym2 = symbols->lookup(state, std::string(str2));
TS_ASSERT(sym != sym2);
}
void notest_lookup_colliding_hash2() {
String* a = String::create(state, "__uint_fast64_t");
String* b = String::create(state, "TkIF_MOD");
TS_ASSERT_EQUALS(a->hash_string(state), b->hash_string(state));
Object* sym;
Object* sym2;
sym = symbols->lookup(state, a);
sym2 = symbols->lookup(state, b);
TS_ASSERT(sym != sym2);
}
void test_lookup_string() {
Symbol* sym = symbols->lookup(state, "circle");
String* str = symbols->lookup_string(state, sym);
TS_ASSERT(!strncmp("circle", str->c_str(state), 6));
}
void test_lookup_nil() {
TS_ASSERT_THROWS_ASSERT(symbols->lookup(state, reinterpret_cast<String*>(Qnil)),
const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
}
void test_lookup_string_nil() {
TS_ASSERT_THROWS_ASSERT(symbols->lookup_string(state, reinterpret_cast<Symbol*>(Qnil)),
const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
}
void test_lookup_debug_str() {
if(LANGUAGE_18_ENABLED(state)) {
String* str1 = String::create(state, "blah\1wha", 8);
Symbol* sym1 = symbols->lookup(state, str1);
std::string symstr1 = symbols->lookup_debug_string(sym1);
TS_ASSERT(!strncmp("blah\\x01wha", symstr1.c_str(), 11));
} else {
String* str1 = String::create(state, "blah\0wha", 8);
Symbol* sym1 = symbols->lookup(state, str1);
std::string symstr1 = symbols->lookup_debug_string(sym1);
TS_ASSERT(!strncmp("blah\\x00wha", symstr1.c_str(), 11));
}
String* str2 = String::create(state, "blahéwha", 8);
Symbol* sym2 = symbols->lookup(state, str2);
std::string symstr2 = symbols->lookup_debug_string(sym2);
TS_ASSERT(!strncmp("blah\\xc3\\xa9wha", symstr2.c_str(), 11));
}
void test_size() {
symbols->lookup(state, "uno");
symbols->lookup(state, "dos");
symbols->lookup(state, "tres");
TS_ASSERT_EQUALS(symbols->size(), 3U);
}
void test_many_symbols() {
size_t size = symbols->size();
for(size_t i = 0; i < 100; i++) {
std::ostringstream stream;
stream << "sym" << i;
symbols->lookup(state, stream.str().c_str());
}
TS_ASSERT(symbols->size() > size);
}
void test_all_as_array() {
std::vector<Symbol*> syms;
syms.push_back(symbols->lookup(state, "__uint_fast64_t"));
syms.push_back(symbols->lookup(state, "ponies"));
syms.push_back(symbols->lookup(state, "cupcakes"));
syms.push_back(symbols->lookup(state, "TkIF_MOD"));
syms.push_back(symbols->lookup(state, "turtles"));
syms.push_back(symbols->lookup(state, "starts"));
Array* ary = symbols->all_as_array(state);
TS_ASSERT_EQUALS(ary->size(), 6U);
for(size_t i = 0; i < ary->size(); i++) {
TS_ASSERT(std::find(syms.begin(), syms.end(), ary->get(state, i)) != syms.end());
}
}
};