|
| 1 | +int* raw_null = nullptr; |
| 2 | + |
| 3 | +template<int I> struct X { operator int() const { return I; } }; |
| 4 | + |
| 5 | +close_to: (v) -> _ = :(x) -> bool = { |
| 6 | + return std::abs(v$ - x) <= std::max<std::common_type_t<std::decay_t<decltype(x)>,std::decay_t<decltype(v$)>>>(std::numeric_limits<std::decay_t<decltype(x)>>::epsilon(), std::numeric_limits<std::decay_t<decltype(v$)>>::epsilon()); |
| 7 | +}; |
| 8 | + |
| 9 | +auto expect_throws(auto l) -> std::string { |
| 10 | + try { |
| 11 | + l(); |
| 12 | + } catch (...) { |
| 13 | + return "throws"; |
| 14 | + } |
| 15 | + return "nothrow"; |
| 16 | +} |
| 17 | + |
| 18 | +struct ThrowingConstruction { |
| 19 | + constexpr ThrowingConstruction() = default; |
| 20 | + ThrowingConstruction(int) { throw 1; } |
| 21 | +}; |
| 22 | + |
| 23 | +main: () = { |
| 24 | + print_header(); |
| 25 | + { // nonesuch |
| 26 | + i := 42; |
| 27 | + print("(i as A) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests"); |
| 28 | + } |
| 29 | + {// smaller to bigger, bigger to smaller |
| 30 | + print("(u8(12) as u16) is (u16(12))", (u8(12) as u16) is (u16(12)), true); |
| 31 | + print("(u16(12) as u8) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests"); |
| 32 | + 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 | + print("(3.14d as float) is close_to(3.14f)", "N/A", "N/A", "static assert - tested in separate tests"); |
| 34 | + } |
| 35 | + {// signed/unsigned |
| 36 | + print("( u8(12) as i16) is (i16(12))", ( u8(12) as i16) is (i16(12)), true); |
| 37 | + print("(i16(12) as u8) is ( u8(12))", "N/A", "N/A", "static assert - tested in separate tests"); |
| 38 | + print("( 12 as u8) is (u8(12))", (12 as u8) is (u8(12)), true); |
| 39 | + print("( 12u as i8) is (i8(12))", (12u as i8) is (i8(12)), true); |
| 40 | + } |
| 41 | + {// integral to floating, floating to integral |
| 42 | + print("( 12 as double) is (12.0)", (12 as double) is (12.0), true); |
| 43 | + print("( 12.0 as int) is (12)", "N/A", "N/A", "static assert - tested in separate tests"); |
| 44 | + } |
| 45 | + {// custom types casting |
| 46 | + print("(X<12>() as int) is (int(X<12>()))", (X<12>() as int) is (int(X<12>())), true); |
| 47 | + } |
| 48 | + { |
| 49 | + print("(3.14 as std::optional<int>) is (nonesuch)", "N/A", "N/A", "static assert - tested in separate tests"); |
| 50 | + print("(3 as std::optional<double>) is (std::optional<double>(3))", (3 as std::optional<double>) is (std::optional<double>(3)), true); |
| 51 | + } |
| 52 | + {// base_of |
| 53 | + print("( A() as A ) is A", ( A() as A ) is A, true); |
| 54 | + print("( C() as A ) is A", ( C() as A ) is A, true); |
| 55 | + print("( B() as A ) is nonesuch_", "N/A", "N/A", "static assert - tested in separate tests"); |
| 56 | + } |
| 57 | + {// Polymorphic types |
| 58 | + vc: VC = (); |
| 59 | + vp0 : *VA<0> = vc&; |
| 60 | + vp1 : *VA<1> = vc&; |
| 61 | + |
| 62 | + print("( vp0 as *VC ) is (vc&)", ( vp0 as *VC ) is (vc&), true); |
| 63 | + print("( vp1 as *VC ) is (vc&)", ( vp1 as *VC ) is (vc&), true); |
| 64 | + print("( vp0 as *VA<1> ) is (vp1)", ( vp0 as *VA<1> ) is (vp1), true); |
| 65 | + |
| 66 | + print("( vp0* as VC )& is (vc&)", ( vp0* as VC )& is (vc&), true); |
| 67 | + print("( vp1* as VC )& is (vc&)", ( vp1* as VC )& is (vc&), true); |
| 68 | + print("( vp0* as VA<1> )& is (vp1)", ( vp0* as VA<1> )& is (vp1), true); |
| 69 | + |
| 70 | + vd: VD = (); |
| 71 | + vp2 : *VA<2> = vd&; |
| 72 | + print("( vp2* as VC )", expect_throws(:() = ( vp2$* as VC ) is (cpp2::nonesuch);), "throws"); |
| 73 | + |
| 74 | + } |
| 75 | + {// Variant |
| 76 | + v : std::variant<int, long, float, double, std::string> = 42; |
| 77 | + |
| 78 | + print("( v{42} as int ) is (42)", ( v as int ) is (42), true); |
| 79 | + print("( v{42} as float ) is (cpp2::nonesuch)", expect_throws(:() = ( v$ as float ) is(cpp2::nonesuch);), "throws"); |
| 80 | + |
| 81 | + v = "string"; |
| 82 | + print("( v{\"string\"} as std::string )", ( v as std::string ) is (std::string("string")), true); |
| 83 | + print("( v{\"string\"} as int )", expect_throws(:() = ( v$ as int );), "throws"); |
| 84 | + } |
| 85 | + {// Variant and empty |
| 86 | + myvariant: type == std::variant<ThrowingConstruction, std::monostate, int>; |
| 87 | + |
| 88 | + v : myvariant = (); |
| 89 | + expect_throws(:() = v&$*.emplace<0>(42);); |
| 90 | + // expect_throws(:() = std::ref(v&$*).emplace<0>(42);); // TODO: check why it is not working? |
| 91 | + |
| 92 | + print("( v{after throw} as ThrowingConstruction )", expect_throws(:() = ( v$ as ThrowingConstruction );), "throws"); |
| 93 | + |
| 94 | + v.emplace<2>(12); |
| 95 | + print("( v{12} as int ) is (12)", ( v as int ) is (12), true); |
| 96 | + |
| 97 | + v.emplace<1>(); |
| 98 | + // l-value variant |
| 99 | + print("( v{monostate} as int )", expect_throws(:() = ( v&$* as int );), "throws"); |
| 100 | + print("( v{monostate} as std::monostate ) is std::monostate", ( v as std::monostate ) is std::monostate, true); |
| 101 | + print("( v{monostate} as std::monostate ) is cpp2::empty", ( v as std::monostate ) is cpp2::empty, true, "what is expected behaviour?"); |
| 102 | + |
| 103 | + // r-value variant |
| 104 | + print("( myvariant(std::monostate()) as int )", expect_throws(:() = ( myvariant(std::monostate()) as int );), "throws"); |
| 105 | + print("( myvariant(std::monostate()) as std::monostate ) is std::monostate", ( myvariant(std::monostate()) as std::monostate ) is std::monostate, true); |
| 106 | + print("( myvariant(std::monostate()) as std::monostate ) is cpp2::empty", ( myvariant(std::monostate()) as std::monostate ) is cpp2::empty, true, "what is expected behaviour?"); |
| 107 | + |
| 108 | + // const variant |
| 109 | + print("( as_const(v){monostate} as int )", expect_throws(:() = ( std::as_const(v&$*) as int );), "throws"); |
| 110 | + print("( as_const(v){monostate} as std::monostate ) is std::monostate", ( std::as_const(v) as std::monostate ) is std::monostate, true); |
| 111 | + print("( as_const(v){monostate} as std::monostate ) is cpp2::empty", ( std::as_const(v) as std::monostate ) is cpp2::empty, true, "what is expected behaviour?"); |
| 112 | + |
| 113 | + v = 42; |
| 114 | + } |
| 115 | + |
| 116 | + {// any |
| 117 | + a : std::any = 12; |
| 118 | + print("( a{12} as int ) is (12)", ( a as int ) is (12), true); |
| 119 | + print("( a{12} as std::string )", expect_throws(:() = a$ as std::string;), "throws"); |
| 120 | + print("std::any(12) as std::string", std::any(12) as std::string, "std::any"); |
| 121 | + } |
| 122 | + |
| 123 | + {// optional |
| 124 | + o : std::optional = 42; |
| 125 | + print("( o{42} as int ) is (42)", ( o as int ) is (42), true); |
| 126 | + print("( o{42} as long ) is (42l)", ( o as long ) is (42l), true); |
| 127 | + print("( o{42} as std::tuple<long> ) is (std::tuple<long>(42))", ( o as std::tuple<long> ) is (std::tuple<long>(42)), true); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +A: type = {} |
| 132 | +B: type = {} |
| 133 | +C: type = { |
| 134 | + this: A = (); |
| 135 | +} |
| 136 | + |
| 137 | +VA: @polymorphic_base <I:int> type = {} |
| 138 | + |
| 139 | +VC: type = { |
| 140 | + this: VA<0>; |
| 141 | + this: VA<1>; |
| 142 | +} |
| 143 | + |
| 144 | +VD : type = { |
| 145 | + this: VA<2>; |
| 146 | +} |
| 147 | + |
| 148 | +pred_i: (x : int ) -> bool = { |
| 149 | + return x > 0; |
| 150 | +} |
| 151 | + |
| 152 | +pred_d: (x : double ) -> bool = { |
| 153 | + return x > 0; |
| 154 | +} |
| 155 | + |
| 156 | +pred_: (x) -> bool = { |
| 157 | + return x > 0; |
| 158 | +} |
| 159 | + |
| 160 | +col : std::array<int, 5> = (70, 8, 8, 8, 40); |
| 161 | + |
| 162 | +print: (what, value, expected, comment) = { |
| 163 | + l := :(value) -> std::string = { |
| 164 | + return inspect value -> std::string { |
| 165 | + is (true ) = "true" ; |
| 166 | + is (false) = "false"; |
| 167 | + is _ = cpp2::to_string(value); |
| 168 | + }; |
| 169 | + }; |
| 170 | + print(what, l(value), l(expected), inspect (value == expected) -> std::string { is (true) = "OK"; is _ = "FAILED!";}, comment ); |
| 171 | +} |
| 172 | + |
| 173 | +print: (what, value, expected) = { |
| 174 | + print(what, value, expected, std::string()); |
| 175 | +} |
| 176 | + |
| 177 | +print: (what, value, expected, result, comment) = { |
| 178 | + std::cout << "|" << std::setw(col[0]) << std::right << what; |
| 179 | + std::cout << "|" << std::setw(col[1]) << std::internal << value; |
| 180 | + std::cout << "|" << std::setw(col[2]) << std::internal << expected; |
| 181 | + std::cout << "|" << std::setw(col[3]) << std::internal << result; |
| 182 | + std::cout << "|" << std::setw(col[4]) << std::left << std::setprecision(20) << comment; |
| 183 | + std::cout << "|" << std::endl; |
| 184 | +} |
| 185 | + |
| 186 | +print_header: () = { |
| 187 | + print("Test", "Actual", "Expected", "Result", "Comment"); |
| 188 | + print( std::string(col[0]-1,'-')+":" |
| 189 | + , ":"+std::string(col[1]-2,'-')+":" |
| 190 | + , ":"+std::string(col[2]-2,'-')+":" |
| 191 | + , ":"+std::string(col[3]-2,'-')+":" |
| 192 | + , ":"+std::string(col[4]-1,'-') |
| 193 | + ); |
| 194 | +} |
| 195 | + |
| 196 | +#include <iomanip> |
0 commit comments