Skip to content

Commit aa8f29c

Browse files
committed
is(): Add test for is as value with variant
1 parent e16bb2e commit aa8f29c

File tree

5 files changed

+298
-0
lines changed

5 files changed

+298
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
auto in(int min, int max) {
2+
return [=](int x){ return min <= x && x <= max; };
3+
}
4+
5+
test: (forward v) = {
6+
if v is (42) {
7+
std::cout << " 42";
8+
}
9+
if v is (24) {
10+
std::cout << " 24";
11+
}
12+
if v is (100) {
13+
std::cout << " 100";
14+
}
15+
if v is (-100) {
16+
std::cout << " -100";
17+
}
18+
if v is (314) {
19+
std::cout << " 314";
20+
}
21+
if v is (std::optional<int>(100)) {
22+
std::cout << " std::optional<int>(100)";
23+
}
24+
if v is (std::any(-100)) {
25+
std::cout << " std::any(-100)";
26+
}
27+
if v is (new<int>(1000)) {
28+
std::cout << " std::unique_ptr<int>(1000)";
29+
}
30+
i : int = 314;
31+
if v is (i&) {
32+
std::cout << " *int(314)";
33+
}
34+
if v is (in(0,100)) {
35+
std::cout << " in(0,100)";
36+
}
37+
std::cout << "\n---" << std::endl;
38+
}
39+
40+
my_variant: type == std::variant<std::monostate, int, int, std::optional<int>, std::any, *int, std::unique_ptr<int>>;
41+
42+
main: () -> int = {
43+
44+
v: std::variant<std::monostate, int, int, std::optional<int>, std::any, *int, std::unique_ptr<int>, my_variant> = ();
45+
46+
header(1, "std::monostate");
47+
v..emplace<0>();
48+
test(v);
49+
50+
header(1, "int(42)");
51+
v..emplace<1>(42);
52+
test(v);
53+
54+
header(1, "int(24)");
55+
v..emplace<2>(24);
56+
test(v);
57+
58+
header(1, "std::optional<int>(100)");
59+
v..emplace<3>(100);
60+
test(v);
61+
62+
header(1, "std::any(-100)");
63+
v..emplace<4>(-100);
64+
test(v);
65+
66+
i : int = 314;
67+
header(1, "*int(314)");
68+
v..emplace<5>(i&);
69+
test(v);
70+
71+
header(1, "std::unique_ptr<int>(1000)");
72+
v..emplace<6>(new<int>(1000));
73+
test(v);
74+
75+
header(1, "my_variant(std::monostate)");
76+
v..emplace<7>();
77+
test(v);
78+
79+
header(1, "my_variant(int(42))");
80+
v..emplace<7>();
81+
std::get<7>(v)..emplace<1>(42);
82+
test(v);
83+
84+
header(1, "my_variant(int(24))");
85+
v..emplace<7>();
86+
std::get<7>(v)..emplace<2>(24);
87+
test(v);
88+
89+
header(1, "my_variant(std::optional<int>(100))");
90+
v..emplace<7>();
91+
std::get<7>(v)..emplace<3>(100);
92+
test(v);
93+
94+
header(1, "my_variant(std::any(-100))");
95+
v..emplace<7>();
96+
std::get<7>(v)..emplace<4>(-100);
97+
test(v);
98+
99+
header(1, "my_variant(*int(314))");
100+
v..emplace<7>();
101+
std::get<7>(v)..emplace<5>(i&);
102+
test(v);
103+
104+
header(1, "my_variant(std::unique_ptr<int>(1000))");
105+
v..emplace<7>();
106+
std::get<7>(v)..emplace<6>(new<int>(1000));
107+
test(v);
108+
}
109+
110+
header: (lvl : int, msg: std::string) = {
111+
std::cout << std::string(lvl, '#') << " " << msg << std::endl;
112+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# std::monostate
2+
3+
---
4+
# int(42)
5+
42 in(0,100)
6+
---
7+
# int(24)
8+
24 in(0,100)
9+
---
10+
# std::optional<int>(100)
11+
100 std::optional<int>(100)
12+
---
13+
# std::any(-100)
14+
15+
---
16+
# *int(314)
17+
18+
---
19+
# std::unique_ptr<int>(1000)
20+
21+
---
22+
# my_variant(std::monostate)
23+
24+
---
25+
# my_variant(int(42))
26+
27+
---
28+
# my_variant(int(24))
29+
30+
---
31+
# my_variant(std::optional<int>(100))
32+
33+
---
34+
# my_variant(std::any(-100))
35+
36+
---
37+
# my_variant(*int(314))
38+
39+
---
40+
# my_variant(std::unique_ptr<int>(1000))
41+
42+
---

regression-tests/test-results/apple-clang-14-c++2b/mixed-is-as-value-with-variant.cpp.output

Whitespace-only changes.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
3+
//=== Cpp2 type declarations ====================================================
4+
5+
6+
#include "cpp2util.h"
7+
8+
#line 1 "mixed-is-as-value-with-variant.cpp2"
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "mixed-is-as-value-with-variant.cpp2"
14+
auto in(int min, int max) {
15+
return [=](int x){ return min <= x && x <= max; };
16+
}
17+
18+
#line 5 "mixed-is-as-value-with-variant.cpp2"
19+
auto test(auto&& v) -> void;
20+
21+
#line 40 "mixed-is-as-value-with-variant.cpp2"
22+
using my_variant = std::variant<std::monostate,int,int,std::optional<int>,std::any,int*,std::unique_ptr<int>>;
23+
24+
[[nodiscard]] auto main() -> int;
25+
26+
#line 110 "mixed-is-as-value-with-variant.cpp2"
27+
auto header(cpp2::impl::in<int> lvl, cpp2::impl::in<std::string> msg) -> void;
28+
29+
//=== Cpp2 function definitions =================================================
30+
31+
#line 1 "mixed-is-as-value-with-variant.cpp2"
32+
33+
#line 5 "mixed-is-as-value-with-variant.cpp2"
34+
auto test(auto&& v) -> void{
35+
if (cpp2::impl::is(v, (42))) {
36+
std::cout << " 42";
37+
}
38+
if (cpp2::impl::is(v, (24))) {
39+
std::cout << " 24";
40+
}
41+
if (cpp2::impl::is(v, (100))) {
42+
std::cout << " 100";
43+
}
44+
if (cpp2::impl::is(v, (-100))) {
45+
std::cout << " -100";
46+
}
47+
if (cpp2::impl::is(v, (314))) {
48+
std::cout << " 314";
49+
}
50+
if (cpp2::impl::is(v, (std::optional<int>(100)))) {
51+
std::cout << " std::optional<int>(100)";
52+
}
53+
if (cpp2::impl::is(v, (std::any(-100)))) {
54+
std::cout << " std::any(-100)";
55+
}
56+
if (cpp2::impl::is(v, (cpp2_new<int>(1000)))) {
57+
std::cout << " std::unique_ptr<int>(1000)";
58+
}
59+
int i {314};
60+
if (cpp2::impl::is(v, (&i))) {
61+
std::cout << " *int(314)";
62+
}
63+
if (cpp2::impl::is(CPP2_FORWARD(v), (in(0, 100)))) {
64+
std::cout << " in(0,100)";
65+
}
66+
std::cout << "\n---" << std::endl;
67+
}
68+
69+
#line 42 "mixed-is-as-value-with-variant.cpp2"
70+
[[nodiscard]] auto main() -> int{
71+
72+
std::variant<std::monostate,int,int,std::optional<int>,std::any,int*,std::unique_ptr<int>,my_variant> v {};
73+
74+
header(1, "std::monostate");
75+
v.emplace<0>();
76+
test(v);
77+
78+
header(1, "int(42)");
79+
v.emplace<1>(42);
80+
test(v);
81+
82+
header(1, "int(24)");
83+
v.emplace<2>(24);
84+
test(v);
85+
86+
header(1, "std::optional<int>(100)");
87+
v.emplace<3>(100);
88+
test(v);
89+
90+
header(1, "std::any(-100)");
91+
v.emplace<4>(-100);
92+
test(v);
93+
94+
int i {314};
95+
header(1, "*int(314)");
96+
v.emplace<5>(&i);
97+
test(v);
98+
99+
header(1, "std::unique_ptr<int>(1000)");
100+
v.emplace<6>(cpp2_new<int>(1000));
101+
test(v);
102+
103+
header(1, "my_variant(std::monostate)");
104+
v.emplace<7>();
105+
test(v);
106+
107+
header(1, "my_variant(int(42))");
108+
v.emplace<7>();
109+
std::get<7>(v).emplace<1>(42);
110+
test(v);
111+
112+
header(1, "my_variant(int(24))");
113+
v.emplace<7>();
114+
std::get<7>(v).emplace<2>(24);
115+
test(v);
116+
117+
header(1, "my_variant(std::optional<int>(100))");
118+
v.emplace<7>();
119+
std::get<7>(v).emplace<3>(100);
120+
test(v);
121+
122+
header(1, "my_variant(std::any(-100))");
123+
v.emplace<7>();
124+
std::get<7>(v).emplace<4>(-100);
125+
test(v);
126+
127+
header(1, "my_variant(*int(314))");
128+
v.emplace<7>();
129+
std::get<7>(v).emplace<5>(&i);
130+
test(v);
131+
132+
header(1, "my_variant(std::unique_ptr<int>(1000))");
133+
v.emplace<7>();
134+
std::get<7>(v).emplace<6>(cpp2_new<int>(1000));
135+
test(cpp2::move(v));
136+
}
137+
138+
#line 110 "mixed-is-as-value-with-variant.cpp2"
139+
auto header(cpp2::impl::in<int> lvl, cpp2::impl::in<std::string> msg) -> void{
140+
std::cout << std::string(lvl, '#') << " " << msg << std::endl;
141+
}
142+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-is-as-value-with-variant.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+

0 commit comments

Comments
 (0)