Skip to content

Commit f82a67b

Browse files
committed
Add additional tests to overview of is & as
1 parent 9a6bed6 commit f82a67b

6 files changed

+116
-40
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ main: () = {
5858
vc: VC = ();
5959
vp0 : *VA<0> = vc&;
6060
vp1 : *VA<1> = vc&;
61+
cvp0 : * const VA<0> = vc&;
6162

6263
print("( vp0 as *VC ) is (vc&)", ( vp0 as *VC ) is (vc&), true);
6364
print("( vp1 as *VC ) is (vc&)", ( vp1 as *VC ) is (vc&), true);
6465
print("( vp0 as *VA<1> ) is (vp1)", ( vp0 as *VA<1> ) is (vp1), true);
66+
print("( cvp0 as *VC ) is (std::as_const(vc)&)", ( cvp0 as *VC ) is (std::as_const(vc)&), true );
67+
print("( cvp0 as * const VC ) is (std::as_const(vc)&)", ( cvp0 as * const VC ) is (std::as_const(vc)&), true );
6568

6669
print("( vp0* as VC )& is (vc&)", ( vp0* as VC )& is (vc&), true);
6770
print("( vp1* as VC )& is (vc&)", ( vp1* as VC )& is (vc&), true);
@@ -126,6 +129,15 @@ main: () = {
126129
print("( o{42} as long ) is (42l)", ( o as long ) is (42l), true);
127130
print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", ( o as std::tuple<long> ) is (std::tuple<long>(42)), true);
128131
}
132+
133+
{// string
134+
print("( \"xyzzy\" as std::string ) is std::string", ( "xyzzy" as std::string ) is std::string, true, "xyzzy" as std::string);
135+
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);
136+
s : std::string = "string";
137+
138+
print("( as_const(s){string} as std::string ) is std::string", ( std::as_const(s) as std::string ) is std::string, true);
139+
print("( s{string} as std::string ) is std::string", ( s as std::string ) is std::string, true);
140+
}
129141
}
130142

131143
A: type = {}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ main: () = {
5151
vc: VC = ();
5252
ptr_va0: *VA<0> = vc&;
5353
ptr_va1: *VA<1> = vc&;
54+
cptr_va0: * const VA<0> = vc&;
5455

5556
print("ptr_va0 is VC", ptr_va0 is VC, true);
5657
print("ptr_va1 is VC", ptr_va1 is VC, true);
5758
print("ptr_va0 is VA<1>", ptr_va0 is VA<1>, true);
5859
print("ptr_va1 is VA<0>", ptr_va1 is VA<0>, true);
60+
print("cptr_va0 is VC", cptr_va0 is VC, true);
5961

6062
print("ptr_va0* is VC", ptr_va0* is VC, true);
6163
print("ptr_va1* is VC", ptr_va1* is VC, true);
@@ -93,7 +95,18 @@ main: () = {
9395
print("3.14f is (close_to(3.14 ))", 3.14f is (close_to(3.14 )), true);
9496
print("3.14 is (close_to(3.14f))", 3.14 is (close_to(3.14f)), true);
9597
}
98+
/*
99+
// type_trait
100+
{
101+
i : int = 42;
102+
ci : const int = 24;
96103

104+
print("i{int} is std::is_const", i is std::is_const, false);
105+
print("ci{const int} is std::is_const", ci is std::is_const, true);
106+
print("ci{const int} is std::is_integral", ci is std::is_integral, true);
107+
print("ci{const int} is std::is_floating_point", ci is std::is_floating_point, false);
108+
}
109+
*/
97110
// predicate
98111
{
99112
d := 3.14;
@@ -114,22 +127,28 @@ main: () = {
114127

115128
// is variant value
116129
{
117-
v : std::variant<int, long, float, double, std::string> = (42);
130+
v : std::variant<int, long, float, double, std::string, std::vector<int>> = (42);
118131

119132
print("v{42} is 42", v is 42, true);
120133
print("v{42} is int", v is int, true);
121134
print("v{42} is int", v is double, false);
122135
print("v{42} is 42.0", v is 42.0, true);
123136
print("v{42} is 24", v is 24, false);
124137
print("v{42} is (std::string(\"hello\"))", v is (std::string("hello")), false);
125-
138+
print("v{42} is std::integral", v is (:<T:std::integral> () = {}), true);
139+
print("v{42} is std::floating_point", v is (:<T:std::floating_point> () = {}), false);
126140

127141
v = std::string("hello");
128142
print("v{hello} is (std::string(\"hello\"))", v is (std::string("hello")), true);
129143
print("v{hello} is 42", v is 42, false);
130144
print("v{hello} is empty", v is cpp2::empty, false);
131145
print("v{hello} is int", v is int, false);
132146
print("v{hello} is std::string", v is std::string, true);
147+
148+
v = :std::vector = (1,2,3,4);
149+
print("v{std::vector{1,2,3,4}} is std::vector", v is std::vector, true );
150+
print("v{std::vector{1,2,3,4}} is std::map", v is std::map, false);
151+
print("v{std::vector{1,2,3,4}} is std::variant",v is std::variant,true);
133152
}
134153

135154
// is variant value
@@ -259,3 +278,4 @@ print_header: () = {
259278
}
260279

261280
#include <iomanip>
281+
#include <map>

regression-tests/test-results/apple-clang-14/mixed-overview-of-as-casts.cpp.execution

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
| ( vp0 as *VC ) is (vc&)| true| true| OK| |
2121
| ( vp1 as *VC ) is (vc&)| true| true| OK| |
2222
| ( vp0 as *VA<1> ) is (vp1)| true| true| OK| |
23+
| ( cvp0 as *VC ) is (std::as_const(vc)&)| true| true| OK| |
24+
| ( cvp0 as * const VC ) is (std::as_const(vc)&)| true| true| OK| |
2325
| ( vp0* as VC )& is (vc&)| true| true| OK| |
2426
| ( vp1* as VC )& is (vc&)| true| true| OK| |
2527
| ( vp0* as VA<1> )& is (vp1)| true| true| OK| |
@@ -45,3 +47,7 @@
4547
| ( o{42} as int ) is (42)| true| true| OK| |
4648
| ( o{42} as long ) is (42l)| true| true| OK| |
4749
| ( o{42} as std::tuple<long> ) is (std::tuple<long>(42))| true| true| OK| |
50+
| ( "xyzzy" as std::string ) is std::string| true| true| OK|xyzzy |
51+
| ( std::string("xyzzy") as std::string ) is std::string| true| true| OK|xyzzy |
52+
| ( as_const(s){string} as std::string ) is std::string| true| true| OK| |
53+
| ( s{string} as std::string ) is std::string| true| true| OK| |

regression-tests/test-results/apple-clang-14/mixed-overview-of-is-inspections.cpp.execution

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| ptr_va1 is VC| true| true| OK| |
1919
| ptr_va0 is VA<1>| true| true| OK| |
2020
| ptr_va1 is VA<0>| true| true| OK| |
21+
| cptr_va0 is VC| true| true| OK| |
2122
| ptr_va0* is VC| true| true| OK| |
2223
| ptr_va1* is VC| true| true| OK| |
2324
| ptr_va0* is VA<1>| true| true| OK| |
@@ -55,11 +56,16 @@
5556
| v{42} is 42.0| true| true| OK| |
5657
| v{42} is 24| false| false| OK| |
5758
| v{42} is (std::string("hello"))| false| false| OK| |
59+
| v{42} is std::integral| true| true| OK| |
60+
| v{42} is std::floating_point| false| false| OK| |
5861
| v{hello} is (std::string("hello"))| true| true| OK| |
5962
| v{hello} is 42| false| false| OK| |
6063
| v{hello} is empty| false| false| OK| |
6164
| v{hello} is int| false| false| OK| |
6265
| v{hello} is std::string| true| true| OK| |
66+
| v{std::vector{1,2,3,4}} is std::vector| true| true| OK| |
67+
| v{std::vector{1,2,3,4}} is std::map| false| false| OK| |
68+
| v{std::vector{1,2,3,4}} is std::variant| true| true| OK| |
6369
| v{int} is empty| false| false| OK| |
6470
| v{monostate} is empty| true| true| OK| |
6571
| v{valueless_by_exception} is empty| true| true| OK|is valueless: true |

regression-tests/test-results/mixed-overview-of-as-casts.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
#include "cpp2util.h"
77

88

9-
#line 131 "mixed-overview-of-as-casts.cpp2"
9+
#line 143 "mixed-overview-of-as-casts.cpp2"
1010
class A;
1111
class B;
1212
class C;
1313

1414

15-
#line 137 "mixed-overview-of-as-casts.cpp2"
15+
#line 149 "mixed-overview-of-as-casts.cpp2"
1616
template<int I> class VA;
1717

1818
class VC;
1919

2020

21-
#line 144 "mixed-overview-of-as-casts.cpp2"
21+
#line 156 "mixed-overview-of-as-casts.cpp2"
2222
class VD;
2323

2424

@@ -50,28 +50,28 @@ struct ThrowingConstruction {
5050
auto main() -> int;
5151

5252

53-
#line 131 "mixed-overview-of-as-casts.cpp2"
53+
#line 143 "mixed-overview-of-as-casts.cpp2"
5454
class A {
5555
public: A() = default;
5656
public: A(A const&) = delete; /* No 'that' constructor, suppress copy */
5757
public: auto operator=(A const&) -> void = delete;
5858

59-
#line 131 "mixed-overview-of-as-casts.cpp2"
59+
#line 143 "mixed-overview-of-as-casts.cpp2"
6060
};
6161
class B {
6262
public: B() = default;
6363
public: B(B const&) = delete; /* No 'that' constructor, suppress copy */
6464
public: auto operator=(B const&) -> void = delete;
6565

66-
#line 132 "mixed-overview-of-as-casts.cpp2"
66+
#line 144 "mixed-overview-of-as-casts.cpp2"
6767
};
6868
class C: public A {
6969
public: C() = default;
7070
public: C(C const&) = delete; /* No 'that' constructor, suppress copy */
7171
public: auto operator=(C const&) -> void = delete;
7272

7373

74-
#line 135 "mixed-overview-of-as-casts.cpp2"
74+
#line 147 "mixed-overview-of-as-casts.cpp2"
7575
};
7676

7777
template<int I> class VA {
@@ -81,7 +81,7 @@ public: virtual ~VA() noexcept;
8181
public: VA(VA const&) = delete; /* No 'that' constructor, suppress copy */
8282
public: auto operator=(VA const&) -> void = delete;
8383

84-
#line 137 "mixed-overview-of-as-casts.cpp2"
84+
#line 149 "mixed-overview-of-as-casts.cpp2"
8585
};
8686

8787
class VC: public VA<0>, public VA<1> {
@@ -90,7 +90,7 @@ class VC: public VA<0>, public VA<1> {
9090
public: auto operator=(VC const&) -> void = delete;
9191

9292

93-
#line 142 "mixed-overview-of-as-casts.cpp2"
93+
#line 154 "mixed-overview-of-as-casts.cpp2"
9494
};
9595

9696
class VD: public VA<2> {
@@ -99,38 +99,38 @@ class VD: public VA<2> {
9999
public: auto operator=(VD const&) -> void = delete;
100100

101101

102-
#line 146 "mixed-overview-of-as-casts.cpp2"
102+
#line 158 "mixed-overview-of-as-casts.cpp2"
103103
};
104104

105105
[[nodiscard]] auto pred_i(cpp2::in<int> x) -> bool;
106106

107107

108-
#line 152 "mixed-overview-of-as-casts.cpp2"
108+
#line 164 "mixed-overview-of-as-casts.cpp2"
109109
[[nodiscard]] auto pred_d(cpp2::in<double> x) -> bool;
110110

111111

112-
#line 156 "mixed-overview-of-as-casts.cpp2"
112+
#line 168 "mixed-overview-of-as-casts.cpp2"
113113
[[nodiscard]] auto pred_(auto const& x) -> bool;
114114

115115

116-
#line 160 "mixed-overview-of-as-casts.cpp2"
116+
#line 172 "mixed-overview-of-as-casts.cpp2"
117117
extern std::array<int,5> col;
118118

119119
auto print(auto const& what, auto const& value, auto const& expected, auto const& comment) -> void;
120120

121121

122-
#line 173 "mixed-overview-of-as-casts.cpp2"
122+
#line 185 "mixed-overview-of-as-casts.cpp2"
123123
auto print(auto const& what, auto const& value, auto const& expected) -> void;
124124

125125

126-
#line 177 "mixed-overview-of-as-casts.cpp2"
126+
#line 189 "mixed-overview-of-as-casts.cpp2"
127127
auto print(auto const& what, auto const& value, auto const& expected, auto const& result, auto const& comment) -> void;
128128

129129

130-
#line 186 "mixed-overview-of-as-casts.cpp2"
130+
#line 198 "mixed-overview-of-as-casts.cpp2"
131131
auto print_header() -> void;
132132

133-
#line 195 "mixed-overview-of-as-casts.cpp2"
133+
#line 207 "mixed-overview-of-as-casts.cpp2"
134134

135135
#include <iomanip>
136136

@@ -182,10 +182,13 @@ auto main() -> int{
182182
VC vc {};
183183
VA<0>* vp0 {&vc};
184184
VA<1>* vp1 {&vc};
185+
VA<0> const* cvp0 {&vc};
185186

186187
print("( vp0 as *VC ) is (vc&)", cpp2::is((cpp2::as_<VC*>(vp0)), (&vc)), true);
187188
print("( vp1 as *VC ) is (vc&)", cpp2::is((cpp2::as_<VC*>(vp1)), (&vc)), true);
188189
print("( vp0 as *VA<1> ) is (vp1)", cpp2::is((cpp2::as_<VA<1>*>(vp0)), (vp1)), true);
190+
print("( cvp0 as *VC ) is (std::as_const(vc)&)", cpp2::is((cpp2::as_<VC*>(cvp0)), (&std::as_const(vc))), true);
191+
print("( cvp0 as * const VC ) is (std::as_const(vc)&)", cpp2::is((cpp2::as_<VC const*>(std::move(cvp0))), (&std::as_const(vc))), true);
189192

190193
print("( vp0* as VC )& is (vc&)", cpp2::is(&(cpp2::as_<VC>(*cpp2::assert_not_null(vp0))), (&vc)), true);
191194
print("( vp1* as VC )& is (vc&)", cpp2::is(&(cpp2::as_<VC>(*cpp2::assert_not_null(vp1))), (&vc)), true);
@@ -250,11 +253,20 @@ auto main() -> int{
250253
print("( o{42} as long ) is (42l)", cpp2::is((cpp2::as_<long>(o)), (42l)), true);
251254
print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", cpp2::is((cpp2::as_<std::tuple<long>>(std::move(o))), (std::tuple<long>(42))), true);
252255
}
256+
257+
{// string
258+
print("( \"xyzzy\" as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>("xyzzy"))), true, cpp2::as_<std::string>("xyzzy"));
259+
print("( std::string(\"xyzzy\") as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>(std::string("xyzzy")))), true, cpp2::as_<std::string>(std::string("xyzzy")));
260+
std::string s {"string"};
261+
262+
print("( as_const(s){string} as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>(std::as_const(s)))), true);
263+
print("( s{string} as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>(std::move(s)))), true);
264+
}
253265
}
254266

255267
template <int I> VA<I>::~VA() noexcept{}
256268

257-
#line 148 "mixed-overview-of-as-casts.cpp2"
269+
#line 160 "mixed-overview-of-as-casts.cpp2"
258270
[[nodiscard]] auto pred_i(cpp2::in<int> x) -> bool{
259271
return cpp2::cmp_greater(x,0);
260272
}

0 commit comments

Comments
 (0)