/
S_ExpressionTest.cpp
250 lines (223 loc) · 7.62 KB
/
S_ExpressionTest.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "S_Expression.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <limits>
#include <sstream>
#include <string>
#include <unordered_set>
#include <boost/functional/hash.hpp>
using namespace sparta;
void check_s_expr_istream(s_expr_istream& input) { EXPECT_TRUE(input.good()); }
s_expr parse(const std::string& str) {
std::istringstream str_input(str);
s_expr_istream input(str_input);
s_expr output;
input >> output;
// Using an EXPECT_* in a function that returns anything other than void
// causes a compile-time error.
check_s_expr_istream(input);
return output;
}
void erroneous_parse(const std::string& str, size_t count, std::string& what) {
std::istringstream str_input(str);
s_expr_istream input(str_input);
s_expr expr;
while (count-- > 0) {
input >> expr;
}
EXPECT_TRUE(input.fail());
what = input.what();
}
TEST(S_ExpressionTest, basicOperations) {
auto e1 = s_expr({s_expr("cons"), s_expr("a"),
s_expr({s_expr("cons"), s_expr("b"),
s_expr({s_expr("cons"), s_expr("c"), s_expr()})})});
std::string e1_out = "(cons a (cons b (cons c ())))";
EXPECT_EQ(e1_out, e1.str());
EXPECT_EQ(e1, parse(e1_out));
EXPECT_EQ(e1, e1);
auto e1_2 = e1;
EXPECT_EQ(e1, e1_2);
EXPECT_FALSE(e1.is_atom());
EXPECT_FALSE(e1.is_int32());
EXPECT_FALSE(e1.is_string());
EXPECT_TRUE(e1.is_list());
EXPECT_FALSE(e1.is_nil());
EXPECT_EQ(3, e1.size());
EXPECT_EQ("cons", e1[0].get_string());
EXPECT_EQ("a", e1[1].get_string());
EXPECT_EQ(3, e1[2].size());
EXPECT_EQ("cons", e1[2][0].get_string());
EXPECT_EQ("b", e1[2][1].get_string());
EXPECT_EQ(3, e1[2][2].size());
EXPECT_EQ("cons", e1[2][2][0].get_string());
EXPECT_EQ("c", e1[2][2][1].get_string());
EXPECT_TRUE(e1[2][2][2].is_nil());
EXPECT_EQ(0, e1[2][2][2].size());
std::vector<s_expr> v2({s_expr(0), s_expr(-1),
s_expr(std::numeric_limits<int32_t>::min()),
s_expr(std::numeric_limits<int32_t>::max())});
auto e2 = s_expr(v2);
std::string e2_out;
{
std::ostringstream out;
out << "(#0 #-1 #" << std::numeric_limits<int32_t>::min() << " #"
<< std::numeric_limits<int32_t>::max() << ")";
e2_out = out.str();
}
EXPECT_EQ(e2_out, e2.str());
EXPECT_EQ(e2, parse(e2_out));
EXPECT_NE(e1, e2);
{
auto a1 = s_expr({s_expr("A"), s_expr({s_expr(-1), s_expr()})});
auto a2 = s_expr({s_expr("A"), s_expr({s_expr(-1), s_expr()})});
EXPECT_EQ(a1, a2);
}
std::vector<s_expr> v3({s_expr("abcd"), s_expr("a_b1"), s_expr("12345"),
s_expr("#abc{}()123!"),
s_expr("1ab\tcd\nef\"gh\"i")});
auto e3 = s_expr(v3.begin(), v3.end());
std::string e3_out =
"(abcd a_b1 12345 \"#abc{}()123!\" \"1ab\tcd\nef\\\"gh\\\"i\")";
EXPECT_EQ(e3_out, e3.str());
EXPECT_EQ(e3, parse(e3_out));
auto e4 = s_expr("123");
std::string e4_out = "123";
EXPECT_TRUE(e4.is_atom());
EXPECT_FALSE(e4.is_int32());
EXPECT_TRUE(e4.is_string());
EXPECT_FALSE(e4.is_list());
EXPECT_FALSE(e4.is_nil());
EXPECT_EQ(e4, parse(e4_out));
auto e5 = s_expr(123);
std::string e5_out = "#123";
EXPECT_TRUE(e5.is_atom());
EXPECT_TRUE(e5.is_int32());
EXPECT_FALSE(e5.is_string());
EXPECT_FALSE(e5.is_list());
EXPECT_FALSE(e5.is_nil());
EXPECT_EQ(e5, parse(e5_out));
std::unordered_set<s_expr, boost::hash<s_expr>> set1_5{e1, e2, e3, e4, e5};
EXPECT_THAT(set1_5, ::testing::UnorderedElementsAre(e1, e2, e3, e4, e5));
{
std::ostringstream out;
out << "\n\n " << e2 << "\t\n\r \t\t" << e1 << " " << e3 << e4
<< "\n\n\n\n"
<< e5;
std::istringstream str_input(out.str());
s_expr_istream input(str_input);
s_expr i1, i2;
input >> i1 >> i2;
EXPECT_EQ(i1, e2);
EXPECT_EQ(i2, e1);
EXPECT_TRUE(input.good());
std::vector<s_expr> exprs{i1, i2};
for (;;) {
s_expr e;
input >> e;
if (!input.good()) {
break;
}
exprs.push_back(e);
}
EXPECT_TRUE(input.eoi());
EXPECT_THAT(exprs, ::testing::ElementsAre(e2, e1, e3, e4, e5));
}
s_expr e6 = parse("(123#123()abc\"def\"\"gh()i\")");
EXPECT_EQ("(123 #123 () abc def \"gh()i\")", e6.str());
s_expr e7 = s_expr({s_expr("A"), s_expr("")});
EXPECT_EQ("(A \"\")", e7.str());
{
std::istringstream str_input(e7.str());
s_expr_istream input(str_input);
s_expr i;
input >> i;
EXPECT_EQ(e7, i);
}
std::string error;
erroneous_parse("((a) b ()", 1, error);
EXPECT_EQ("On line 1: Incomplete S-expression", error);
erroneous_parse("(\n(a)\nb\n()\n", 1, error);
EXPECT_EQ("On line 5: Incomplete S-expression", error);
erroneous_parse("((a) b c))", 2, error);
EXPECT_EQ("On line 1: Extra ')' encountered", error);
erroneous_parse(R"(
(
(a)
b
c
))
)", 2, error);
EXPECT_EQ("On line 6: Extra ')' encountered", error);
erroneous_parse("(a b #9999999999999)", 1, error);
EXPECT_EQ("On line 1: Error parsing int32_t literal", error);
erroneous_parse("(a b #-9999999999999)", 1, error);
EXPECT_EQ("On line 1: Error parsing int32_t literal", error);
erroneous_parse("(a b \"abcdef)", 1, error);
EXPECT_EQ("On line 1: Error parsing string literal", error);
erroneous_parse("123, (a b c)", 2, error);
EXPECT_EQ("On line 1: Unexpected character encountered: ','", error);
erroneous_parse(R"(;Should only take 1 endline in an inline comment\n\n\n
(
(const-string "foo\n\bar")
123, (a b c)
)
)", 2, error);
EXPECT_EQ("On line 4: Unexpected character encountered: ','", error);
erroneous_parse(R"(;The error should be on line 2
(123, (a b c) ; End of line 2
)", 2, error);
EXPECT_EQ("On line 2: Unexpected character encountered: ','", error);
}
TEST(S_ExpressionTest, patternMatching) {
auto e1 = parse("((a #1) (b #2))");
EXPECT_TRUE(s_patn({s_patn({s_patn("a"), s_patn(1)}),
s_patn({s_patn("b"), s_patn(2)})})
.match_with(e1));
s_expr x, y, z;
EXPECT_TRUE(
s_patn({s_patn({s_patn("a"), s_patn(x)}), s_patn({s_patn("b")}, y)})
.match_with(e1));
EXPECT_TRUE(x.is_int32());
EXPECT_EQ(1, x.get_int32());
EXPECT_TRUE(y.is_list());
EXPECT_EQ(s_expr({s_expr(2)}), y);
std::string a, b;
int32_t one, two;
EXPECT_TRUE(s_patn({s_patn({s_patn(&a), s_patn(&one)}),
s_patn({s_patn(&b), s_patn(&two)})})
.match_with(e1));
EXPECT_EQ("a", a);
EXPECT_EQ("b", b);
EXPECT_EQ(1, one);
EXPECT_EQ(2, two);
EXPECT_TRUE(
s_patn({s_patn({s_patn(x), s_patn()}), s_patn({s_patn(y), s_patn()})})
.match_with(e1));
EXPECT_TRUE(x.is_string());
EXPECT_EQ("a", x.get_string());
EXPECT_TRUE(y.is_string());
EXPECT_EQ("b", y.get_string());
EXPECT_TRUE(
s_patn({s_patn({s_patn(), s_patn(1)}), s_patn()}, x).match_with(e1));
EXPECT_TRUE(x.is_nil());
EXPECT_FALSE(s_patn({s_patn(x), s_patn(y), s_patn(z)}).match_with(e1));
EXPECT_FALSE(s_patn({s_patn("a")}, y).match_with(e1));
EXPECT_FALSE(
s_patn({s_patn({s_patn("b"), s_patn(x)}), s_patn(y)}).match_with(e1));
EXPECT_FALSE(
s_patn({s_patn({s_patn("a"), s_patn(2)}), s_patn(y)}).match_with(e1));
auto e2 = parse("(() (()))");
EXPECT_TRUE(s_patn({s_patn({}), s_patn({s_patn({})})}).match_with(e2));
auto e3 = parse("(a b () (c d) e)");
EXPECT_TRUE(s_patn({s_patn("a"), s_patn(x), s_patn(y)}, z).match_with(e3));
EXPECT_EQ(s_expr("b"), x);
EXPECT_TRUE(y.is_nil());
EXPECT_EQ(parse("((c d) e)"), z);
}