Skip to content

Commit 322472d

Browse files
committed
Added more regression tests
And fixed a `requires`-needs-parens bug GCC pointed out
1 parent c896061 commit 322472d

22 files changed

+461
-5
lines changed

cpp2util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ constexpr auto is( std::optional<T> const& x ) -> bool
449449
{ return x.has_value(); }
450450

451451
template<typename T, typename U>
452-
requires !std::is_same_v<T,U>
452+
requires (!std::is_same_v<T,U>)
453453
constexpr auto is( std::optional<U> const& x ) -> bool
454454
{ return false; }
455455

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <variant>
4+
#include <any>
5+
#include <optional>
6+
7+
class Shape { public: virtual ~Shape() { } };
8+
class Circle : public Shape { };
9+
class Square : public Shape { };
10+
11+
//--- printing helpers -----------------
12+
13+
print: ( msg: std::string, x: _ ) =
14+
std::cout << msg << x << "\n";
15+
16+
print: ( msg: std::string, b: bool ) =
17+
{
18+
bmsg: * const char;
19+
if b { bmsg = "true"; }
20+
else { bmsg = "false"; }
21+
std::cout << msg << bmsg << "\n";
22+
}
23+
24+
//--- examples -------------------------
25+
26+
main: () -> int =
27+
{
28+
print( "1.1 is int? ", 1.1 is int );
29+
print( "1 is int? ", 1 is int );
30+
31+
c := new<Circle>(); // safe by construction
32+
s : * Shape = c.get(); // safe by Lifetime
33+
print("\ns* is Shape? ", s* is Shape );
34+
print( "s* is Circle? ", s* is Circle );
35+
print( "s* is Square? ", s* is Square );
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
main: () -> int
3+
= {
4+
words: std::vector<std::string> = ( "decorated", "hello", "world" );
5+
6+
first: *std::string = words.front()&;
7+
last : *std::string = words.back()&;
8+
9+
while first <= last {
10+
print_and_decorate(first*);
11+
first++; // unsafe
12+
first + 1;
13+
first[1];
14+
first~;
15+
delete first;
16+
}
17+
}
18+
19+
print_and_decorate: (thing:_) =
20+
std::cout << ">> " << thing << "\n";
21+

regression-tests/pure2-stdio.cpp2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// "A better C than C" ... ?
3+
//
4+
main: () -> int = {
5+
s: std::string = "Fred";
6+
myfile := fopen("xyzzy", "w");
7+
myfile.fprintf( "Hello %s with UFCS!", s.c_str() );
8+
myfile.fclose();
9+
}
10+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
main: () -> int =
3+
{
4+
v: std::variant<int, double> = 42.0;
5+
a: std::any = "xyzzy";
6+
o: std::optional<int> = ();
7+
8+
test_generic(3.14);
9+
test_generic(v);
10+
test_generic(a);
11+
test_generic(o);
12+
13+
std::cout << "\n";
14+
15+
v = 1;
16+
a = 2;
17+
o = 3;
18+
test_generic(42);
19+
test_generic(v);
20+
test_generic(a);
21+
test_generic(o);
22+
}
23+
24+
test_generic: ( x: _ ) = {
25+
msg: std::string = typeid(x).name();
26+
msg += " is int? ";
27+
print( msg, x is int );
28+
}
29+
30+
print: ( msg: std::string, b: bool ) = {
31+
bmsg: * const char;
32+
if b { bmsg = "true"; }
33+
else { bmsg = "false"; }
34+
std::cout << std::setw(40) << msg << bmsg << "\n";
35+
}
36+

regression-tests/run-tests.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
copy ..\*.cpp2 .
33
for %%f in (mixed-*.cpp2) do (
44
echo Starting cppfront.exe %%f
5-
C:\GitHub\cppfront\x64\Debug\cppfront.exe %%f > %%f-output
5+
C:\GitHub\cppfront\x64\Debug\cppfront.exe %%f > %%f-output 2>&1
66
)
77
for %%f in (pure2-*.cpp2) do (
88
echo Starting cppfront.exe %%f -p
9-
C:\GitHub\cppfront\x64\Debug\cppfront.exe %%f -p > %%f-output
9+
C:\GitHub\cppfront\x64\Debug\cppfront.exe %%f -p > %%f-output 2>&1
1010
)
1111
echo Done
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ----- Cpp2 support -----
2+
#include "cpp2util.h"
3+
4+
#line 1 "mixed-type-safety-1.cpp2"
5+
#include <iostream>
6+
#include <string>
7+
#include <variant>
8+
#include <any>
9+
#include <optional>
10+
11+
class Shape { public: virtual ~Shape() { } };
12+
class Circle : public Shape { };
13+
class Square : public Shape { };
14+
15+
#line 13 "mixed-type-safety-1.cpp2"
16+
auto print(cpp2::in<std::string> msg, auto const& x) noexcept -> void;
17+
#line 16 "mixed-type-safety-1.cpp2"
18+
auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) noexcept -> void;
19+
#line 26 "mixed-type-safety-1.cpp2"
20+
[[nodiscard]] auto main() noexcept -> int;
21+
22+
//=== Cpp2 definitions ==========================================================
23+
24+
#line 10 "mixed-type-safety-1.cpp2"
25+
26+
//--- printing helpers -----------------
27+
28+
auto print(cpp2::in<std::string> msg, auto const& x) noexcept -> void
29+
{ std::cout<<msg<< x << "\n"; }
30+
31+
auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) noexcept -> void
32+
{
33+
cpp2::deferred_init<char const*> bmsg;
34+
if (b) { bmsg.construct("true");}
35+
else { bmsg.construct("false");}
36+
std::cout << msg << bmsg.value()<<"\n";
37+
}
38+
39+
//--- examples -------------------------
40+
41+
[[nodiscard]] auto main() noexcept -> int
42+
{
43+
print("1.1 is int? ", cpp2::is<int>(1.1));
44+
print("1 is int? ", cpp2::is<int>(1));
45+
46+
auto c { cpp2_new<Circle>() }; // safe by construction
47+
Shape* s { CPP2_UFCS_0(get, c) }; // safe by Lifetime
48+
print("\ns* is Shape? ", cpp2::is<Shape>(*s));
49+
print("s* is Circle? ", cpp2::is<Circle>(*s));
50+
print("s* is Square? ", cpp2::is<Square>(*s));
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <variant>
4+
#include <any>
5+
#include <optional>
6+
7+
class Shape { public: virtual ~Shape() { } };
8+
class Circle : public Shape { };
9+
class Square : public Shape { };
10+
11+
//--- printing helpers -----------------
12+
13+
print: ( msg: std::string, x: _ ) =
14+
std::cout << msg << x << "\n";
15+
16+
print: ( msg: std::string, b: bool ) =
17+
{
18+
bmsg: * const char;
19+
if b { bmsg = "true"; }
20+
else { bmsg = "false"; }
21+
std::cout << msg << bmsg << "\n";
22+
}
23+
24+
//--- examples -------------------------
25+
26+
main: () -> int =
27+
{
28+
print( "1.1 is int? ", 1.1 is int );
29+
print( "1 is int? ", 1 is int );
30+
31+
c := new<Circle>(); // safe by construction
32+
s : * Shape = c.get(); // safe by Lifetime
33+
print("\ns* is Shape? ", s* is Shape );
34+
print( "s* is Circle? ", s* is Circle );
35+
print( "s* is Square? ", s* is Square );
36+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cppfront 0.1.1 compiler
2+
Copyright (C) Herb Sutter
3+
mixed-type-safety-1.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
4+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 2 "pure2-bounds-safety-pointer-arithmetic-error.cpp2"
7+
[[nodiscard]] auto main() noexcept -> int;
8+
#line 24 "pure2-bounds-safety-pointer-arithmetic-error.cpp2"
9+
auto test_generic(auto const& x) noexcept -> void;
10+
#line 30 "pure2-bounds-safety-pointer-arithmetic-error.cpp2"
11+
auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) noexcept -> void;
12+
#line 36 "pure2-bounds-safety-pointer-arithmetic-error.cpp2"
13+
14+
15+
//=== Cpp2 definitions ==========================================================
16+
17+
#line 1 "pure2-bounds-safety-pointer-arithmetic-error.cpp2"
18+
19+
[[nodiscard]] auto main() noexcept -> int
20+
{
21+
std::variant<int,double> v { 42.0 };
22+
std::any a { "xyzzy" };
23+
std::optional<int> o { };
24+
25+
test_generic(3.14);
26+
test_generic(v);
27+
test_generic(a);
28+
test_generic(o);
29+
30+
std::cout << "\n";
31+
32+
v = 1;
33+
a = 2;
34+
o = 3;
35+
test_generic(42);
36+
test_generic(v);
37+
test_generic(a);
38+
test_generic(o);
39+
}
40+
41+
auto test_generic(auto const& x) noexcept -> void{
42+
std::string msg { typeid(x).name() };
43+
msg += " is int? ";
44+
print(msg, cpp2::is<int>(x));
45+
}
46+
47+
auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) noexcept -> void{
48+
cpp2::deferred_init<char const*> bmsg;
49+
if (b) { bmsg.construct("true");}
50+
else { bmsg.construct("false");}
51+
std::cout << std::setw(40) << msg << bmsg.value()<<"\n";
52+
}

0 commit comments

Comments
 (0)