Skip to content

Commit

Permalink
Add additional tests to overview of is & as
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsajdak committed Oct 2, 2023
1 parent 9a6bed6 commit f82a67b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 40 deletions.
12 changes: 12 additions & 0 deletions regression-tests/mixed-overview-of-as-casts.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ main: () = {
vc: VC = ();
vp0 : *VA<0> = vc&;
vp1 : *VA<1> = vc&;
cvp0 : * const VA<0> = vc&;

print("( vp0 as *VC ) is (vc&)", ( vp0 as *VC ) is (vc&), true);
print("( vp1 as *VC ) is (vc&)", ( vp1 as *VC ) is (vc&), true);
print("( vp0 as *VA<1> ) is (vp1)", ( vp0 as *VA<1> ) is (vp1), true);
print("( cvp0 as *VC ) is (std::as_const(vc)&)", ( cvp0 as *VC ) is (std::as_const(vc)&), true );
print("( cvp0 as * const VC ) is (std::as_const(vc)&)", ( cvp0 as * const VC ) is (std::as_const(vc)&), true );

print("( vp0* as VC )& is (vc&)", ( vp0* as VC )& is (vc&), true);
print("( vp1* as VC )& is (vc&)", ( vp1* as VC )& is (vc&), true);
Expand Down Expand Up @@ -126,6 +129,15 @@ main: () = {
print("( o{42} as long ) is (42l)", ( o as long ) is (42l), true);
print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", ( o as std::tuple<long> ) is (std::tuple<long>(42)), true);
}

{// string
print("( \"xyzzy\" as std::string ) is std::string", ( "xyzzy" as std::string ) is std::string, true, "xyzzy" as std::string);
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);
s : std::string = "string";

print("( as_const(s){string} as std::string ) is std::string", ( std::as_const(s) as std::string ) is std::string, true);
print("( s{string} as std::string ) is std::string", ( s as std::string ) is std::string, true);
}
}

A: type = {}
Expand Down
24 changes: 22 additions & 2 deletions regression-tests/mixed-overview-of-is-inspections.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ main: () = {
vc: VC = ();
ptr_va0: *VA<0> = vc&;
ptr_va1: *VA<1> = vc&;
cptr_va0: * const VA<0> = vc&;

print("ptr_va0 is VC", ptr_va0 is VC, true);
print("ptr_va1 is VC", ptr_va1 is VC, true);
print("ptr_va0 is VA<1>", ptr_va0 is VA<1>, true);
print("ptr_va1 is VA<0>", ptr_va1 is VA<0>, true);
print("cptr_va0 is VC", cptr_va0 is VC, true);

print("ptr_va0* is VC", ptr_va0* is VC, true);
print("ptr_va1* is VC", ptr_va1* is VC, true);
Expand Down Expand Up @@ -93,7 +95,18 @@ main: () = {
print("3.14f is (close_to(3.14 ))", 3.14f is (close_to(3.14 )), true);
print("3.14 is (close_to(3.14f))", 3.14 is (close_to(3.14f)), true);
}
/*
// type_trait
{
i : int = 42;
ci : const int = 24;

print("i{int} is std::is_const", i is std::is_const, false);
print("ci{const int} is std::is_const", ci is std::is_const, true);
print("ci{const int} is std::is_integral", ci is std::is_integral, true);
print("ci{const int} is std::is_floating_point", ci is std::is_floating_point, false);
}
*/
// predicate
{
d := 3.14;
Expand All @@ -114,22 +127,28 @@ main: () = {

// is variant value
{
v : std::variant<int, long, float, double, std::string> = (42);
v : std::variant<int, long, float, double, std::string, std::vector<int>> = (42);

print("v{42} is 42", v is 42, true);
print("v{42} is int", v is int, true);
print("v{42} is int", v is double, false);
print("v{42} is 42.0", v is 42.0, true);
print("v{42} is 24", v is 24, false);
print("v{42} is (std::string(\"hello\"))", v is (std::string("hello")), false);

print("v{42} is std::integral", v is (:<T:std::integral> () = {}), true);
print("v{42} is std::floating_point", v is (:<T:std::floating_point> () = {}), false);

v = std::string("hello");
print("v{hello} is (std::string(\"hello\"))", v is (std::string("hello")), true);
print("v{hello} is 42", v is 42, false);
print("v{hello} is empty", v is cpp2::empty, false);
print("v{hello} is int", v is int, false);
print("v{hello} is std::string", v is std::string, true);

v = :std::vector = (1,2,3,4);
print("v{std::vector{1,2,3,4}} is std::vector", v is std::vector, true );
print("v{std::vector{1,2,3,4}} is std::map", v is std::map, false);
print("v{std::vector{1,2,3,4}} is std::variant",v is std::variant,true);
}

// is variant value
Expand Down Expand Up @@ -259,3 +278,4 @@ print_header: () = {
}

#include <iomanip>
#include <map>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
| ( vp0 as *VC ) is (vc&)| true| true| OK| |
| ( vp1 as *VC ) is (vc&)| true| true| OK| |
| ( vp0 as *VA<1> ) is (vp1)| true| true| OK| |
| ( cvp0 as *VC ) is (std::as_const(vc)&)| true| true| OK| |
| ( cvp0 as * const VC ) is (std::as_const(vc)&)| true| true| OK| |
| ( vp0* as VC )& is (vc&)| true| true| OK| |
| ( vp1* as VC )& is (vc&)| true| true| OK| |
| ( vp0* as VA<1> )& is (vp1)| true| true| OK| |
Expand All @@ -45,3 +47,7 @@
| ( o{42} as int ) is (42)| true| true| OK| |
| ( o{42} as long ) is (42l)| true| true| OK| |
| ( o{42} as std::tuple<long> ) is (std::tuple<long>(42))| true| true| OK| |
| ( "xyzzy" as std::string ) is std::string| true| true| OK|xyzzy |
| ( std::string("xyzzy") as std::string ) is std::string| true| true| OK|xyzzy |
| ( as_const(s){string} as std::string ) is std::string| true| true| OK| |
| ( s{string} as std::string ) is std::string| true| true| OK| |
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
| ptr_va1 is VC| true| true| OK| |
| ptr_va0 is VA<1>| true| true| OK| |
| ptr_va1 is VA<0>| true| true| OK| |
| cptr_va0 is VC| true| true| OK| |
| ptr_va0* is VC| true| true| OK| |
| ptr_va1* is VC| true| true| OK| |
| ptr_va0* is VA<1>| true| true| OK| |
Expand Down Expand Up @@ -55,11 +56,16 @@
| v{42} is 42.0| true| true| OK| |
| v{42} is 24| false| false| OK| |
| v{42} is (std::string("hello"))| false| false| OK| |
| v{42} is std::integral| true| true| OK| |
| v{42} is std::floating_point| false| false| OK| |
| v{hello} is (std::string("hello"))| true| true| OK| |
| v{hello} is 42| false| false| OK| |
| v{hello} is empty| false| false| OK| |
| v{hello} is int| false| false| OK| |
| v{hello} is std::string| true| true| OK| |
| v{std::vector{1,2,3,4}} is std::vector| true| true| OK| |
| v{std::vector{1,2,3,4}} is std::map| false| false| OK| |
| v{std::vector{1,2,3,4}} is std::variant| true| true| OK| |
| v{int} is empty| false| false| OK| |
| v{monostate} is empty| true| true| OK| |
| v{valueless_by_exception} is empty| true| true| OK|is valueless: true |
Expand Down
48 changes: 30 additions & 18 deletions regression-tests/test-results/mixed-overview-of-as-casts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
#include "cpp2util.h"


#line 131 "mixed-overview-of-as-casts.cpp2"
#line 143 "mixed-overview-of-as-casts.cpp2"
class A;
class B;
class C;


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

class VC;


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


Expand Down Expand Up @@ -50,28 +50,28 @@ struct ThrowingConstruction {
auto main() -> int;


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

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

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


#line 135 "mixed-overview-of-as-casts.cpp2"
#line 147 "mixed-overview-of-as-casts.cpp2"
};

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

#line 137 "mixed-overview-of-as-casts.cpp2"
#line 149 "mixed-overview-of-as-casts.cpp2"
};

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


#line 142 "mixed-overview-of-as-casts.cpp2"
#line 154 "mixed-overview-of-as-casts.cpp2"
};

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


#line 146 "mixed-overview-of-as-casts.cpp2"
#line 158 "mixed-overview-of-as-casts.cpp2"
};

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


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


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


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

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


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


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


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

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

#include <iomanip>

Expand Down Expand Up @@ -182,10 +182,13 @@ auto main() -> int{
VC vc {};
VA<0>* vp0 {&vc};
VA<1>* vp1 {&vc};
VA<0> const* cvp0 {&vc};

print("( vp0 as *VC ) is (vc&)", cpp2::is((cpp2::as_<VC*>(vp0)), (&vc)), true);
print("( vp1 as *VC ) is (vc&)", cpp2::is((cpp2::as_<VC*>(vp1)), (&vc)), true);
print("( vp0 as *VA<1> ) is (vp1)", cpp2::is((cpp2::as_<VA<1>*>(vp0)), (vp1)), true);
print("( cvp0 as *VC ) is (std::as_const(vc)&)", cpp2::is((cpp2::as_<VC*>(cvp0)), (&std::as_const(vc))), true);
print("( cvp0 as * const VC ) is (std::as_const(vc)&)", cpp2::is((cpp2::as_<VC const*>(std::move(cvp0))), (&std::as_const(vc))), true);

print("( vp0* as VC )& is (vc&)", cpp2::is(&(cpp2::as_<VC>(*cpp2::assert_not_null(vp0))), (&vc)), true);
print("( vp1* as VC )& is (vc&)", cpp2::is(&(cpp2::as_<VC>(*cpp2::assert_not_null(vp1))), (&vc)), true);
Expand Down Expand Up @@ -250,11 +253,20 @@ auto main() -> int{
print("( o{42} as long ) is (42l)", cpp2::is((cpp2::as_<long>(o)), (42l)), true);
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);
}

{// string
print("( \"xyzzy\" as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>("xyzzy"))), true, cpp2::as_<std::string>("xyzzy"));
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")));
std::string s {"string"};

print("( as_const(s){string} as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>(std::as_const(s)))), true);
print("( s{string} as std::string ) is std::string", cpp2::is<std::string>((cpp2::as_<std::string>(std::move(s)))), true);
}
}

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

#line 148 "mixed-overview-of-as-casts.cpp2"
#line 160 "mixed-overview-of-as-casts.cpp2"
[[nodiscard]] auto pred_i(cpp2::in<int> x) -> bool{
return cpp2::cmp_greater(x,0);
}
Expand Down
Loading

0 comments on commit f82a67b

Please sign in to comment.