forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parse.cpp
More file actions
297 lines (247 loc) · 6.55 KB
/
test_parse.cpp
File metadata and controls
297 lines (247 loc) · 6.55 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <tests/doctest.h>
#include <iostream>
#include <sstream>
#include <chrono>
#include <string>
#include <lpython/bigint.h>
using LCompilers::TRY;
using LCompilers::Result;
using LCompilers::LPython::BigInt::is_int_ptr;
using LCompilers::LPython::BigInt::ptr_to_int;
using LCompilers::LPython::BigInt::int_to_ptr;
using LCompilers::LPython::BigInt::string_to_largeint;
using LCompilers::LPython::BigInt::largeint_to_string;
using LCompilers::LPython::BigInt::MAX_SMALL_INT;
using LCompilers::LPython::BigInt::MIN_SMALL_INT;
// Print any vector like iterable to a string
template <class T>
inline std::ostream &print_vec(std::ostream &out, T &d)
{
out << "[";
for (auto p = d.begin(); p != d.end(); p++) {
if (p != d.begin())
out << ", ";
out << *p;
}
out << "]";
return out;
}
namespace doctest {
// Convert std::vector<T> to string for doctest
template<typename T> struct StringMaker<std::vector<T>> {
static String convert(const std::vector<T> &value) {
std::ostringstream oss;
print_vec(oss, value);
return oss.str().c_str();
}
};
}
class TokenizerError0 {
};
TEST_CASE("Test Big Int") {
int64_t i;
void *p, *p2;
/* Integer tests */
i = 0;
CHECK(!is_int_ptr(i));
i = 5;
CHECK(!is_int_ptr(i));
i = -5;
CHECK(!is_int_ptr(i));
// Largest integer that is allowed is 2^62-1
i = 4611686018427387903LL;
CHECK(i == MAX_SMALL_INT);
CHECK(!is_int_ptr(i)); // this is an integer
i = 4611686018427387904LL;
CHECK(is_int_ptr(i)); // this is a pointer
// Smallest integer that is allowed is -2^63
i = -9223372036854775808ULL;
CHECK(i == MIN_SMALL_INT);
CHECK(!is_int_ptr(i)); // this is an integer
i = -9223372036854775809ULL; // This does not fit into a signed 64bit int
CHECK(is_int_ptr(i)); // this is a pointer
/* Pointer tests */
// Smallest pointer value is 0 (nullptr)
p = nullptr;
i = ptr_to_int(p);
CHECK(is_int_ptr(i));
p2 = int_to_ptr(i);
CHECK(p == p2);
// Second smallest pointer value aligned to 4 is 4
p = (void*)4;
i = ptr_to_int(p);
CHECK(is_int_ptr(i));
p2 = int_to_ptr(i);
CHECK(p == p2);
// Maximum pointer value aligned to 4 is (2^64-1)-3
p = (void*)18446744073709551612ULL;
i = ptr_to_int(p);
CHECK(is_int_ptr(i));
p2 = int_to_ptr(i);
CHECK(p == p2);
/* Big int tests */
Allocator al(1024);
LCompilers::Str s;
char *cs;
s.from_str(al, "123");
i = string_to_largeint(al, s);
CHECK(is_int_ptr(i));
cs = largeint_to_string(i);
CHECK(std::string(cs) == "123");
s.from_str(al, "123567890123456789012345678901234567890");
i = string_to_largeint(al, s);
CHECK(is_int_ptr(i));
cs = largeint_to_string(i);
CHECK(std::string(cs) == "123567890123456789012345678901234567890");
}
TEST_CASE("Test LCompilers::Vec") {
Allocator al(1024);
LCompilers::Vec<int> v;
v.reserve(al, 2);
CHECK(v.size() == 0);
CHECK(v.capacity() == 2);
v.push_back(al, 1);
CHECK(v.size() == 1);
CHECK(v.capacity() == 2);
CHECK(v.p[0] == 1);
CHECK(v[0] == 1);
v.push_back(al, 2);
CHECK(v.size() == 2);
CHECK(v.capacity() == 2);
CHECK(v.p[0] == 1);
CHECK(v.p[1] == 2);
CHECK(v[0] == 1);
CHECK(v[1] == 2);
v.push_back(al, 3);
CHECK(v.size() == 3);
CHECK(v.capacity() == 4);
CHECK(v.p[0] == 1);
CHECK(v.p[1] == 2);
CHECK(v.p[2] == 3);
CHECK(v[0] == 1);
CHECK(v[1] == 2);
CHECK(v[2] == 3);
v.push_back(al, 4);
CHECK(v.size() == 4);
CHECK(v.capacity() == 4);
CHECK(v.p[0] == 1);
CHECK(v.p[1] == 2);
CHECK(v.p[2] == 3);
CHECK(v.p[3] == 4);
CHECK(v[0] == 1);
CHECK(v[1] == 2);
CHECK(v[2] == 3);
CHECK(v[3] == 4);
v.push_back(al, 5);
CHECK(v.size() == 5);
CHECK(v.capacity() == 8);
CHECK(v.p[0] == 1);
CHECK(v.p[1] == 2);
CHECK(v.p[2] == 3);
CHECK(v.p[3] == 4);
CHECK(v.p[4] == 5);
CHECK(v[0] == 1);
CHECK(v[1] == 2);
CHECK(v[2] == 3);
CHECK(v[3] == 4);
CHECK(v[4] == 5);
std::vector<int> sv = v.as_vector();
CHECK(sv.size() == 5);
CHECK(&(sv[0]) != &(v.p[0]));
CHECK(sv[0] == 1);
CHECK(sv[1] == 2);
CHECK(sv[2] == 3);
CHECK(sv[3] == 4);
CHECK(sv[4] == 5);
}
TEST_CASE("LCompilers::Vec iterators") {
Allocator al(1024);
LCompilers::Vec<int> v;
v.reserve(al, 2);
v.push_back(al, 1);
v.push_back(al, 2);
v.push_back(al, 3);
v.push_back(al, 4);
// Check reference (auto)
int i = 0;
for (auto &a : v) {
i += a;
}
CHECK(i == 10);
// Check reference (must be const)
i = 0;
for (const int &a : v) {
i += a;
}
CHECK(i == 10);
// Check direct type (auto)
i = 0;
for (auto a : v) {
i += a;
}
CHECK(i == 10);
// Check direct type (const)
i = 0;
for (const int a : v) {
i += a;
}
CHECK(i == 10);
// Check direct type (non const)
i = 0;
for (int a : v) {
i += a;
}
CHECK(i == 10);
}
TEST_CASE("Test LCompilers::Str") {
Allocator al(1024);
LCompilers::Str s;
const char *data = "Some string.";
s.p = const_cast<char*>(data);
s.n = 2;
CHECK(s.size() == 2);
CHECK(s.p == data);
CHECK(s.str() == "So");
std::string scopy = s.str();
CHECK(s.p != &scopy[0]);
CHECK(scopy == "So");
CHECK(scopy[0] == 'S');
CHECK(scopy[1] == 'o');
CHECK(scopy[2] == '\x00');
char *copy = s.c_str(al);
CHECK(s.p != copy);
CHECK(copy[0] == 'S');
CHECK(copy[1] == 'o');
CHECK(copy[2] == '\x00');
}
TEST_CASE("Test LCompilers::Allocator") {
Allocator al(32);
// Size is what we asked (32) plus alignment (8) = 40
CHECK(al.size_total() == 40);
// Fits in the pre-allocated chunk
al.alloc(32);
CHECK(al.size_total() == 40);
// Chunk doubles
al.alloc(32);
CHECK(al.size_total() == 80);
// Chunk doubles
al.alloc(90);
CHECK(al.size_total() == 160);
// We asked more than can fit in the doubled chunk (2*160),
// so the chunk will be equal to what we asked (1024) plus alignment (8)
al.alloc(1024);
CHECK(al.size_total() == 1032);
}
TEST_CASE("Test LCompilers::Allocator 2") {
Allocator al(32);
int *p = al.allocate<int>();
p[0] = 5;
p = al.allocate<int>(3);
p[0] = 1;
p[1] = 2;
p[2] = 3;
std::vector<int> *v = al.make_new<std::vector<int>>(5);
CHECK(v->size() == 5);
// Must manually call the destructor:
v->~vector<int>();
}