Skip to content

Commit 4f15948

Browse files
committed
Update is and as overview tests
1 parent b2aac74 commit 4f15948

File tree

4 files changed

+419
-152
lines changed

4 files changed

+419
-152
lines changed

regression-tests/mixed-overview-of-as-casts.cpp2

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,53 @@ auto expect_throws(auto l) -> std::string {
1818
struct ThrowingConstruction {
1919
constexpr ThrowingConstruction() = default;
2020
ThrowingConstruction(int) { throw 1; }
21+
22+
operator std::string() const { return "TC"; }
2123
};
2224

25+
const char ctab[] = "ctab";
26+
2327
main: () = {
24-
print_header();
28+
print_header("nonesuch");
2529
{ // nonesuch
2630
// i := 42;
2731
print("(i as A) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
2832
}
33+
34+
print_header("smaller to bigger, bigger to smaller");
2935
{// smaller to bigger, bigger to smaller
3036
print("(u8(12) as u16) is (u16(12))", (u8(12) as u16) is (u16(12)), true);
3137
print("(u16(12) as u8) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
3238
print("(3.14f as double) is close_to(3.14f)", (3.14f as double) is (close_to(3.14f)), true, std::abs(3.14 - (3.14f as double)));
3339
print("(3.14d as float) is close_to(3.14f)", "N/A", "N/A", "static assert - tested in separate tests");
3440
}
41+
print_header("signed/unsigned");
3542
{// signed/unsigned
3643
print("( u8(12) as i16) is (i16(12))", ( u8(12) as i16) is (i16(12)), true);
3744
print("(i16(12) as u8) is ( u8(12))", "N/A", "N/A", "static assert - tested in separate tests");
3845
print("( 12 as u8) is (u8(12))", (12 as u8) is (u8(12)), true);
3946
print("( 12u as i8) is (i8(12))", (12u as i8) is (i8(12)), true);
4047
}
48+
print_header("integral to floating, floating to integral");
4149
{// integral to floating, floating to integral
4250
print("( 12 as double) is (12.0)", (12 as double) is (12.0), true);
4351
print("( 12.0 as int) is (12)", "N/A", "N/A", "static assert - tested in separate tests");
4452
}
53+
print_header("custom types casting");
4554
{// custom types casting
4655
print("(X<12>() as int) is (int(X<12>()))", (X<12>() as int) is (int(X<12>())), true);
4756
}
4857
{
4958
print("(3.14 as std::optional<int>) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
5059
print("(3 as std::optional<double>) is (std::optional<double>(3))", (3 as std::optional<double>) is (std::optional<double>(3)), true);
5160
}
61+
print_header("base_of");
5262
{// base_of
5363
print("( A() as A ) is A", ( A() as A ) is A, true);
5464
print("( C() as A ) is A", ( C() as A ) is A, true);
5565
print("( B() as A ) is nonesuch_", "N/A", "N/A", "static assert - tested in separate tests");
5666
}
67+
print_header("olymorphic types");
5768
{// Polymorphic types
5869
vc: VC = ();
5970
vp0 : *VA<0> = vc&;
@@ -75,16 +86,18 @@ main: () = {
7586
print("( vp2* as VC )", expect_throws(:() = ( vp2$* as VC );), "throws");
7687

7788
}
89+
print_header("Variant");
7890
{// Variant
7991
v : std::variant<int, long, float, double, std::string> = 42;
8092

81-
print("( v{42} as int ) is (42)", ( v as int ) is (42), true);
93+
print("( v{42} as int ) is (42)", ( v as int ), (42));
8294
print("( v{42} as float )", expect_throws(:() = ( v$ as float );), "throws");
8395

8496
v = "string";
85-
print("( v{\"string\"} as std::string )", ( v as std::string ) is (std::string("string")), true);
97+
print("( v{\"string\"} as std::string )", ( v as std::string ), (std::string("string")));
8698
print("( v{\"string\"} as int )", expect_throws(:() = ( v$ as int );), "throws");
8799
}
100+
print_header("Variant and empty");
88101
{// Variant and empty
89102
myvariant: type == std::variant<ThrowingConstruction, std::monostate, int>;
90103

@@ -117,19 +130,22 @@ main: () = {
117130
v = 42;
118131
}
119132

133+
print_header("any");
120134
{// any
121135
a : std::any = 12;
122136
print("( a{12} as int ) is (12)", ( a as int ) is (12), true);
123137
print("( a{12} as std::string )", expect_throws(:() = a$ as std::string;), "throws");
124138
}
125139

140+
print_header("optional");
126141
{// optional
127142
o : std::optional = 42;
128143
print("( o{42} as int ) is (42)", ( o as int ) is (42), true);
129144
print("( o{42} as long ) is (42l)", ( o as long ) is (42l), true);
130145
print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", ( o as std::tuple<long> ) is (std::tuple<long>(42)), true);
131146
}
132147

148+
print_header("string");
133149
{// string
134150
print("( \"xyzzy\" as std::string ) is std::string", ( "xyzzy" as std::string ) is std::string, true, "xyzzy" as std::string);
135151
print("( std::string(\"xyzzy\") as std::string ) is std::string", ( std::string("xyzzy") as std::string ) is std::string, true, std::string("xyzzy") as std::string);
@@ -138,6 +154,99 @@ main: () = {
138154
print("( as_const(s){string} as std::string ) is std::string", ( std::as_const(s) as std::string ) is std::string, true);
139155
print("( s{string} as std::string ) is std::string", ( s as std::string ) is std::string, true);
140156
}
157+
158+
print_header("variable as std::string with cpp2::to_string overload");
159+
{
160+
print("std::any() as std::string", expect_throws(:() = (std::any() as std::string);), "throws");
161+
print("true as std::string", (true as std::string), "true");
162+
print("false as std::string", (false as std::string), "false");
163+
164+
c : char = 'a';
165+
print("c as std::string", (c as std::string), "a");
166+
167+
cstr: * const char = "cstr";
168+
print("cstr as std::string", (cstr as std::string), "cstr");
169+
170+
arr : std::array<char,6> = "array";
171+
arr_ptr : *char = arr.data();
172+
print("arr_ptr as std::string", (arr_ptr as std::string), "array");
173+
print("ctab as std::string", (ctab as std::string), "ctab");
174+
175+
sv : std::string_view = ctab;
176+
print("sv as std::string", (sv as std::string), "ctab");
177+
178+
str : std::string = ctab;
179+
print("str as std::string", (str as std::string), "ctab");
180+
181+
oi : std::optional<int> = 42;
182+
print("oi as std::string", (oi as std::string), "42");
183+
os : std::optional<std::string> = "std::string";
184+
print("os as std::string", (os as std::string), "std::string");
185+
186+
print("optional<optional<string>>{\"oostring\"} as std::string", (std::optional<std::optional<std::string>>("oostring") as std::string), "oostring");
187+
print("optional<int>{} as std::string", (std::optional<int>() as std::string), "(empty)");
188+
189+
print("std::monostate{} as std::string", (std::monostate() as std::string), "(empty)");
190+
191+
{
192+
myvariant: type == std::variant<ThrowingConstruction, std::monostate, int>;
193+
194+
v : myvariant = ();
195+
expect_throws(:() = v&$*.emplace<0>(42););
196+
197+
print("variant{value_less_be_exception} as std::string", (v as std::string), "(empty)");
198+
199+
v = std::monostate();
200+
print("variant{std::monostate} as std::string", (v as std::string), "(empty)");
201+
202+
v.emplace<2>(42);
203+
print("variant{42} as std::string", (v as std::string), "42");
204+
}
205+
{
206+
recursive_variant : type == std::variant<std::variant<int,std::monostate>,std::variant<double,std::monostate>, std::monostate>;
207+
208+
v : recursive_variant = 42;
209+
print("variant{variant{42}} as std::string", (v as std::string), "42");
210+
211+
v.emplace<0>(std::monostate());
212+
print("variant{variant{std::monostate}} as std::string", (v as std::string), "(empty)");
213+
214+
v = std::monostate();
215+
print("variant{std::monostate} as std::string", (v as std::string), "(empty)");
216+
217+
v.emplace<1>(3.14);
218+
print("variant{variant{3.14}} as std::string", (v as std::string), "3.140000");
219+
}
220+
221+
{
222+
p : std::pair<int, double> = (42, 3.14);
223+
print("pair{42, 3.14} as std::string", (p as std::string), "(42, 3.140000)");
224+
}
225+
226+
{
227+
p : std::pair<int, std::pair<long, long>> = (42, std::pair(2l,3l));
228+
print("pair{42, pair{2,3}} as std::string", (p as std::string), "(42, (2, 3))");
229+
}
230+
231+
{
232+
p : std::pair<int, std::variant<int, long, std::string, std::monostate>> = (42, "test");
233+
print("pair{42, variant{\"test\"}} as std::string", (p as std::string), "(42, test)");
234+
p.second = std::monostate();
235+
print("pair{42, variant{std::monostate}} as std::string", (p as std::string), "(42, (empty))");
236+
}
237+
{
238+
print("ThrowingConstruction{} as std::string", (ThrowingConstruction() as std::string), "TC");
239+
}
240+
{
241+
// A() as std::string; // error: static assertion failed due to requirement 'program_violates_type_safety_guarantee<std::string, A>': No cpp2::to_string overload exists for this type!
242+
// std::variant<int, A>(42) as std::string; // error: static assertion failed due to requirement 'program_violates_type_safety_guarantee<std::string, std::variant<int, A>>': One of aggregate types has no cpp2::to_string overload - if you're sure you want this, use `cpp2::to_string() to force the conversion
243+
print("cpp2::to_string(std::variant<int, A>(42))", (cpp2::to_string(std::variant<int, A>(42))), "42");
244+
}
245+
{
246+
a : std::any = "string" as std::string;
247+
print("std::any{\"string\"} as std::string", (a as std::string), "string");
248+
}
249+
}
141250
}
142251

143252
A: type = {}
@@ -169,7 +278,7 @@ pred_: (x) -> bool = {
169278
return x > 0;
170279
}
171280

172-
col : std::array<int, 5> = (70, 8, 8, 8, 40);
281+
col : std::array<int, 5> = (70, 14, 14, 14, 40);
173282

174283
print: (what, value, expected, comment) = {
175284
l := :(value) -> std::string = {
@@ -195,7 +304,8 @@ print: (what, value, expected, result, comment) = {
195304
std::cout << "|" << std::endl;
196305
}
197306

198-
print_header: () = {
307+
print_header: (title) = {
308+
std::cout << "\n# (title)$\n\n";
199309
print("Test", "Actual", "Expected", "Result", "Comment");
200310
print( std::string(col[0]-1,'-')+":"
201311
, ":"+std::string(col[1]-2,'-')+":"

regression-tests/mixed-overview-of-is-inspections.cpp2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ main: () = {
8080
a: A = ();
8181
b: B = ();
8282
c: C = ();
83-
print("C is A", cpp2::is<C, A>() , true, "not expressed in cpp2");
84-
print("C is B", cpp2::is<C, B>() , false, "not expressed in cpp2");
85-
8683
print("a is A", a is A, true);
8784
print("b is A", b is A, false);
8885
print("c is A", c is A, true);
@@ -93,6 +90,11 @@ main: () = {
9390
ptr_va1: *VA<1> = vc&;
9491
cptr_va0: * const VA<0> = vc&;
9592

93+
print("vc is VA<0>", vc is VA<0>, true);
94+
print("vc is VA<1>", vc is VA<1>, true);
95+
print("vc& is *VA<0>", vc& is *VA<0>, true);
96+
print("vc& is *VA<1>", vc& is *VA<1>, true);
97+
9698
print("ptr_va0 is *VC", ptr_va0 is *VC, true);
9799
print("ptr_va1 is *VC", ptr_va1 is *VC, true);
98100
print("ptr_va0 is *VA<1>", ptr_va0 is *VA<1>, true);

0 commit comments

Comments
 (0)