You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: regression-tests/mixed-overview-of-as-casts.cpp2
+115-5Lines changed: 115 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -18,42 +18,53 @@ auto expect_throws(auto l) -> std::string {
18
18
struct ThrowingConstruction {
19
19
constexpr ThrowingConstruction() = default;
20
20
ThrowingConstruction(int) { throw 1; }
21
+
22
+
operator std::string() const { return "TC"; }
21
23
};
22
24
25
+
const char ctab[] = "ctab";
26
+
23
27
main: () = {
24
-
print_header();
28
+
print_header("nonesuch");
25
29
{ // nonesuch
26
30
// i := 42;
27
31
print("(i as A) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
28
32
}
33
+
34
+
print_header("smaller to bigger, bigger to smaller");
29
35
{// smaller to bigger, bigger to smaller
30
36
print("(u8(12) as u16) is (u16(12))", (u8(12) as u16) is (u16(12)), true);
31
37
print("(u16(12) as u8) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
32
38
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)));
33
39
print("(3.14d as float) is close_to(3.14f)", "N/A", "N/A", "static assert - tested in separate tests");
34
40
}
41
+
print_header("signed/unsigned");
35
42
{// signed/unsigned
36
43
print("( u8(12) as i16) is (i16(12))", ( u8(12) as i16) is (i16(12)), true);
37
44
print("(i16(12) as u8) is ( u8(12))", "N/A", "N/A", "static assert - tested in separate tests");
38
45
print("( 12 as u8) is (u8(12))", (12 as u8) is (u8(12)), true);
39
46
print("( 12u as i8) is (i8(12))", (12u as i8) is (i8(12)), true);
40
47
}
48
+
print_header("integral to floating, floating to integral");
41
49
{// integral to floating, floating to integral
42
50
print("( 12 as double) is (12.0)", (12 as double) is (12.0), true);
43
51
print("( 12.0 as int) is (12)", "N/A", "N/A", "static assert - tested in separate tests");
44
52
}
53
+
print_header("custom types casting");
45
54
{// custom types casting
46
55
print("(X<12>() as int) is (int(X<12>()))", (X<12>() as int) is (int(X<12>())), true);
47
56
}
48
57
{
49
58
print("(3.14 as std::optional<int>) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests");
50
59
print("(3 as std::optional<double>) is (std::optional<double>(3))", (3 as std::optional<double>) is (std::optional<double>(3)), true);
51
60
}
61
+
print_header("base_of");
52
62
{// base_of
53
63
print("( A() as A ) is A", ( A() as A ) is A, true);
54
64
print("( C() as A ) is A", ( C() as A ) is A, true);
55
65
print("( B() as A ) is nonesuch_", "N/A", "N/A", "static assert - tested in separate tests");
56
66
}
67
+
print_header("olymorphic types");
57
68
{// Polymorphic types
58
69
vc: VC = ();
59
70
vp0 : *VA<0> = vc&;
@@ -75,16 +86,18 @@ main: () = {
75
86
print("( vp2* as VC )", expect_throws(:() = ( vp2$* as VC );), "throws");
76
87
77
88
}
89
+
print_header("Variant");
78
90
{// Variant
79
91
v : std::variant<int, long, float, double, std::string> = 42;
80
92
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));
82
94
print("( v{42} as float )", expect_throws(:() = ( v$ as float );), "throws");
83
95
84
96
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")));
86
98
print("( v{\"string\"} as int )", expect_throws(:() = ( v$ as int );), "throws");
87
99
}
100
+
print_header("Variant and empty");
88
101
{// Variant and empty
89
102
myvariant: type == std::variant<ThrowingConstruction, std::monostate, int>;
90
103
@@ -117,19 +130,22 @@ main: () = {
117
130
v = 42;
118
131
}
119
132
133
+
print_header("any");
120
134
{// any
121
135
a : std::any = 12;
122
136
print("( a{12} as int ) is (12)", ( a as int ) is (12), true);
123
137
print("( a{12} as std::string )", expect_throws(:() = a$ as std::string;), "throws");
124
138
}
125
139
140
+
print_header("optional");
126
141
{// optional
127
142
o : std::optional = 42;
128
143
print("( o{42} as int ) is (42)", ( o as int ) is (42), true);
129
144
print("( o{42} as long ) is (42l)", ( o as long ) is (42l), true);
130
145
print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", ( o as std::tuple<long> ) is (std::tuple<long>(42)), true);
131
146
}
132
147
148
+
print_header("string");
133
149
{// string
134
150
print("( \"xyzzy\" as std::string ) is std::string", ( "xyzzy" as std::string ) is std::string, true, "xyzzy" as std::string);
135
151
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: () = {
138
154
print("( as_const(s){string} as std::string ) is std::string", ( std::as_const(s) as std::string ) is std::string, true);
139
155
print("( s{string} as std::string ) is std::string", ( s as std::string ) is std::string, true);
140
156
}
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
0 commit comments