Skip to content

Commit 59d5806

Browse files
committed
Add test for overview of is inspections
1 parent a464e80 commit 59d5806

5 files changed

+703
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
int* raw_null = nullptr;
2+
3+
auto expect_throws(auto l) -> bool {
4+
try {
5+
l();
6+
} catch (...) {
7+
return true;
8+
}
9+
return false;
10+
}
11+
12+
struct ThrowingConstruction {
13+
constexpr ThrowingConstruction() = default;
14+
ThrowingConstruction(int) { throw 1; }
15+
};
16+
17+
18+
main: () = {
19+
20+
print_header();
21+
// is template
22+
{
23+
v : std::vector = (1, 2, 3);
24+
print("v is vector", v is std::vector, true);
25+
print("v is array", v is std::array, false);
26+
print("v is optional", v is std::optional, false);
27+
a : std::array<int,4> = (4,3,2,1);
28+
print("a is array", a is std::array, true);
29+
print("a is vector", a is std::vector, false);
30+
print("a is optional", a is std::optional, false);
31+
o : std::optional = 42;
32+
print("o is array", o is std::array, false);
33+
print("o is vector", o is std::vector, false);
34+
print("o is optional", o is std::optional, true);
35+
36+
}
37+
38+
// is type
39+
{
40+
a: A = ();
41+
b: B = ();
42+
c: C = ();
43+
print("C is A", true , false, "not implemented yet");
44+
print("C is B", false , true, "not implemented yet");
45+
46+
print("a is A", a is A, true);
47+
print("b is A", b is A, false);
48+
print("c is A", c is A, true);
49+
}
50+
{
51+
vc: VC = ();
52+
ptr_va0: *VA<0> = vc&;
53+
ptr_va1: *VA<1> = vc&;
54+
55+
print("ptr_va0 is VC", ptr_va0 is VC, true);
56+
print("ptr_va1 is VC", ptr_va1 is VC, true);
57+
print("ptr_va0 is VA<1>", ptr_va0 is VA<1>, true);
58+
print("ptr_va1 is VA<0>", ptr_va1 is VA<0>, true);
59+
60+
print("ptr_va0* is VC", ptr_va0* is VC, true);
61+
print("ptr_va1* is VC", ptr_va1* is VC, true);
62+
print("ptr_va0* is VA<1>", ptr_va0* is VA<1>, true);
63+
print("ptr_va1* is VA<0>", ptr_va1* is VA<0>, true);
64+
}
65+
66+
// pointer-like empty check
67+
{
68+
print("raw_null is empty", raw_null is cpp2::empty, true);
69+
print("nullptr is empty", nullptr is cpp2::empty, true);
70+
print("shared_ptr() is empty", std::shared_ptr<int>() is cpp2::empty, true);
71+
print("unique_ptr() is empty", std::unique_ptr<int>() is cpp2::empty, true);
72+
73+
i := 42;
74+
print("i& is empty", i& is cpp2::empty, false);
75+
print("std::make_shared<int>(42) is empty", std::make_shared<int>(42) is cpp2::empty, false);
76+
print("std::make_unique<int>(44) is empty", std::make_unique<int>(44) is cpp2::empty, false);
77+
}
78+
79+
// values check
80+
{
81+
i := 42;
82+
print("i{42} is empty", i is cpp2::empty, false);
83+
print("i{42} is 24", i is 24, false);
84+
print("i{42} is 42", i is 42, true);
85+
print("i{42} is 42u", i is 42u, true);
86+
print("i{42} is 42L", i is 42L, true);
87+
print("i{42} is 42.0", i is 42.0, true);
88+
print("i{42} is 42.0f", i is 42.0f, true);
89+
print("3.14f is 3.14", 3.14f is 3.14, false);
90+
close_to := :(v) -> _ = :(x) -> bool = {
91+
return std::abs(v$ - x) < std::max<std::common_type_t<std::decay_t<decltype(x)>,std::decay_t<decltype(v$)>>>(std::numeric_limits<std::decay_t<decltype(x)>>::epsilon(), std::numeric_limits<std::decay_t<decltype(v$)>>::epsilon());
92+
};
93+
print("3.14f is (close_to(3.14 ))", 3.14f is (close_to(3.14 )), true);
94+
print("3.14 is (close_to(3.14f))", 3.14 is (close_to(3.14f)), true);
95+
}
96+
97+
// predicate
98+
{
99+
d := 3.14;
100+
101+
print("d{3.14} is (:(x) -> bool = x>0;)", d is (:(x) -> bool = x>0;), true);
102+
print("d{3.14} is (:(x:int) -> bool = x>0;)", d is (:(x:int) -> bool = x>0;), false);
103+
print("d{3.14} is (:(x:std::string) -> bool = x.ssize()>5;)", d is (:(x:std::string) -> bool = x.ssize()>5;), false);
104+
print("std::string(\"abcdefg\") is (:(x:std::string) -> bool = x.ssize()>5;)", std::string("abcdefg") is (:(x:std::string) -> bool = x.ssize()>5;), true);
105+
106+
print("d{3.14} is (pred_i)", d is (pred_i), false);
107+
print("d{3.14} is (pred_d)", d is (pred_d), true);
108+
print("d{3.14} is (pred_)", true, true);
109+
110+
print("d{3.14} is (:<T:std::floating_point> () -> _ = true;)", d is (:<T:std::floating_point> () -> _ = true;), true);
111+
print("d{3.14} is (:<T:std::floating_point> () = {})", d is (:<T:std::floating_point> () = {}), true);
112+
print("d{3.14} is (:<T:std::integral> () = {})", d is (:<T:std::integral> () = {}), false);
113+
}
114+
115+
// is variant value
116+
{
117+
v : std::variant<int, long, float, double, std::string> = (42);
118+
119+
print("v{42} is 42", v is 42, true);
120+
print("v{42} is int", v is int, true);
121+
print("v{42} is int", v is double, false);
122+
print("v{42} is 42.0", v is 42.0, true);
123+
print("v{42} is 24", v is 24, false);
124+
print("v{42} is (std::string(\"hello\"))", v is (std::string("hello")), false);
125+
126+
127+
v = std::string("hello");
128+
print("v{hello} is (std::string(\"hello\"))", v is (std::string("hello")), true);
129+
print("v{hello} is 42", v is 42, false);
130+
print("v{hello} is empty", v is cpp2::empty, false);
131+
print("v{hello} is int", v is int, false);
132+
print("v{hello} is std::string", v is std::string, true);
133+
}
134+
135+
// is variant value
136+
{
137+
v : std::variant<int, ThrowingConstruction, std::monostate> = ();
138+
print("v{int} is empty", v is cpp2::empty, false);
139+
140+
v = std::monostate();
141+
print("v{monostate} is empty", v is cpp2::empty, true);
142+
143+
expect_throws(:() = v&$*.emplace<1>(42););
144+
print("v{valueless_by_exception} is empty", v is cpp2::empty, true, "is valueless: " + cpp2::to_string(v.valueless_by_exception()));
145+
146+
}
147+
148+
// any is type
149+
{
150+
a : std::any = 42;
151+
152+
print("a{42} is int", a is int, true);
153+
print("a{42} is double", a is double, false);
154+
print("a{42} is empty", a is cpp2::empty, false);
155+
156+
print("std::any() is empty", std::any() is cpp2::empty, true);
157+
}
158+
159+
// any is value
160+
{
161+
a : std::any = 42;
162+
163+
print("a{42} is 42", a is 42, true);
164+
print("a{42} is 24", a is 24, false);
165+
print("a{42} is 42L", a is 42L, false);
166+
print("std::any(3.14) is 3", std::any(3.14) is 3, false);
167+
168+
print("a{42} is :(v)->bool = v.has_value();", a is :(v)->bool = v.has_value();, true);
169+
print("a{42} is :(v:std::any)->bool = v.has_value();", a is :(v:std::any)->bool = v.has_value();, true);
170+
print("a{42} is :(v:int)->bool = v>0;", a is :(v:int)->bool = v>0;, false);
171+
}
172+
173+
// optional is type
174+
{
175+
o : std::optional = 42;
176+
177+
print("o{42} is int", o is int, true);
178+
print("o{42} is empty", o is cpp2::empty, false);
179+
print("std::optional<int>() is empty", std::optional<int>() is cpp2::empty, true);
180+
}
181+
182+
// optional is value
183+
{
184+
o : std::optional = 42;
185+
186+
print("o{42} is 42", o is 42, true);
187+
print("o{42} is 24", o is 24, false);
188+
print("o{42} is 42.0", o is 42.0, true);
189+
190+
print("o{42} is :(v) -> bool = v* > 0;", o is :(v) -> bool = v* > 0;, true);
191+
print("o{42} is :(v:std::optional<int>) -> bool = v* > 0;", o is :(v:std::optional<int>) -> bool = v* > 0;, true);
192+
print("o{42} is :(v:std::optional<long>) -> bool = v* > 0;", o is :(v:std::optional<long>) -> bool = v* > 0;, true);
193+
print("std::optional(3.14) is :(v:std::optional<int>) -> bool = v* == 3;", std::optional(3.14) is :(v:std::optional<int>) -> bool = v* == 3;, false);
194+
195+
}
196+
197+
}
198+
199+
A: type = {}
200+
B: type = {}
201+
C: type = {
202+
this: A = ();
203+
}
204+
205+
VA: @polymorphic_base <I:int> type = {}
206+
207+
VC: type = {
208+
this: VA<0>;
209+
this: VA<1>;
210+
}
211+
212+
213+
pred_i: (x : int ) -> bool = {
214+
return x > 0;
215+
}
216+
217+
pred_d: (x : double ) -> bool = {
218+
return x > 0;
219+
}
220+
221+
pred_: (x) -> bool = {
222+
return x > 0;
223+
}
224+
225+
col : std::array<int, 5> = (70, 8, 8, 8, 30);
226+
227+
print: (what, value, expected, comment) = {
228+
l := :(value) -> std::string = {
229+
if value {
230+
return "true";
231+
} else {
232+
return "false";
233+
}
234+
};
235+
print(what, l(value), l(expected), inspect (value == expected) -> std::string { is (true) = "OK"; is _ = "FAILED!";}, comment );
236+
}
237+
238+
print: (what, value, expected) = {
239+
print(what, value, expected, std::string());
240+
}
241+
242+
print: (what, value, expected, result, comment) = {
243+
std::cout << "|" << std::setw(col[0]) << std::right << what;
244+
std::cout << "|" << std::setw(col[1]) << std::internal << value;
245+
std::cout << "|" << std::setw(col[2]) << std::internal << expected;
246+
std::cout << "|" << std::setw(col[3]) << std::internal << result;
247+
std::cout << "|" << std::setw(col[4]) << std::left << std::setprecision(20) << comment;
248+
std::cout << "|" << std::endl;
249+
}
250+
251+
print_header: () = {
252+
print("Test", "Actual", "Expected", "Result", "Comment");
253+
print( std::string(col[0]-1,'-')+":"
254+
, ":"+std::string(col[1]-2,'-')+":"
255+
, ":"+std::string(col[2]-2,'-')+":"
256+
, ":"+std::string(col[3]-2,'-')+":"
257+
, ":"+std::string(col[4]-1,'-')
258+
);
259+
}
260+
261+
#include <iomanip>

0 commit comments

Comments
 (0)