Skip to content

Commit fb8924f

Browse files
Merge pull request #46 from NinaRanns/contracts_noexcept_tests
adding more noexcept function tests, renaming existing tests
2 parents 3822383 + 24eb432 commit fb8924f

25 files changed

+937
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a post condition on a constructor with function
4+
// try block
5+
// { dg-do run }
6+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe " }
7+
8+
#include <experimental/contract>
9+
#include <exception>
10+
#include <cstdlib>
11+
12+
struct MyException{};
13+
14+
// Test that there is an active exception when we reach the terminate handler.
15+
void my_term()
16+
{
17+
try { throw; }
18+
catch(MyException) { std::exit(0); }
19+
}
20+
21+
22+
void handle_contract_violation(const std::experimental::contract_violation& violation)
23+
{
24+
throw MyException{};
25+
}
26+
27+
struct X
28+
{
29+
30+
X(const int x) noexcept post(x>1) try
31+
{
32+
int i = 1;
33+
}
34+
catch(...) {}
35+
};
36+
37+
int main()
38+
{
39+
std::set_terminate (my_term);
40+
try
41+
{
42+
X x(-42);
43+
} catch (...) {
44+
}
45+
// We should not get here
46+
return 1;
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a pre condition on a constructor with function
4+
// try block
5+
// { dg-do run }
6+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe " }
7+
8+
#include <experimental/contract>
9+
#include <exception>
10+
#include <cstdlib>
11+
12+
struct MyException{};
13+
14+
// Test that there is an active exception when we reach the terminate handler.
15+
void my_term()
16+
{
17+
try { throw; }
18+
catch(MyException) { std::exit(0); }
19+
}
20+
21+
22+
void handle_contract_violation(const std::experimental::contract_violation& violation)
23+
{
24+
throw MyException{};
25+
}
26+
27+
struct X
28+
{
29+
30+
X(int x) noexcept pre(x>1) try
31+
{
32+
int i = 1;
33+
}
34+
catch(...) {}
35+
};
36+
37+
int main()
38+
{
39+
std::set_terminate (my_term);
40+
try
41+
{
42+
X x(-42);
43+
} catch (...) {
44+
}
45+
// We should not get here
46+
return 1;
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a pre condition on a constructor
4+
// { dg-do run }
5+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe " }
6+
7+
#include <experimental/contract>
8+
#include <exception>
9+
#include <cstdlib>
10+
11+
struct MyException{};
12+
13+
// Test that there is an active exception when we reach the terminate handler.
14+
void my_term()
15+
{
16+
try { throw; }
17+
catch(MyException) { std::exit(0); }
18+
}
19+
20+
21+
void handle_contract_violation(const std::experimental::contract_violation& violation)
22+
{
23+
throw MyException{};
24+
}
25+
26+
struct X
27+
{
28+
29+
X(int x) noexcept pre(x>1)
30+
{
31+
try{
32+
int i = 1;
33+
}
34+
catch(...) {
35+
}
36+
}
37+
};
38+
39+
int main()
40+
{
41+
std::set_terminate (my_term);
42+
try
43+
{
44+
X x(-42);
45+
} catch (...) {
46+
}
47+
// We should not get here
48+
return 1;
49+
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a pre condition on a destructor
4+
// { dg-do compile }
5+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe " }
6+
7+
#include <experimental/contract>
8+
#include <exception>
9+
#include <cstdlib>
10+
11+
struct MyException{};
12+
13+
// Test that there is an active exception when we reach the terminate handler.
14+
void my_term()
15+
{
16+
try { throw; }
17+
catch(MyException) { std::exit(0); }
18+
}
19+
20+
21+
void handle_contract_violation(const std::experimental::contract_violation& violation)
22+
{
23+
throw MyException{};
24+
}
25+
26+
const int x = -42;
27+
28+
struct X
29+
{
30+
31+
~X() noexcept pre(x>1)
32+
{
33+
try{}
34+
catch(...){
35+
}
36+
}
37+
};
38+
39+
int main()
40+
{
41+
std::set_terminate (my_term);
42+
try
43+
{
44+
X x;
45+
} catch (...) {
46+
}
47+
// We should not get here
48+
return 1;
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a pre condition on an implictly noexcept
4+
// destructor
5+
// { dg-do run }
6+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe " }
7+
8+
#include <experimental/contract>
9+
#include <exception>
10+
#include <cstdlib>
11+
12+
struct MyException{};
13+
14+
// Test that there is an active exception when we reach the terminate handler.
15+
void my_term()
16+
{
17+
try { throw; }
18+
catch(MyException) { std::exit(0); }
19+
}
20+
21+
22+
void handle_contract_violation(const std::experimental::contract_violation& violation)
23+
{
24+
throw MyException{};
25+
}
26+
27+
const int x = -42;
28+
29+
struct X
30+
{
31+
32+
~X() pre(x>1) try
33+
{
34+
}
35+
catch(...) {}
36+
};
37+
38+
int main()
39+
{
40+
std::set_terminate (my_term);
41+
try
42+
{
43+
X x;
44+
} catch (...) {
45+
}
46+
// We should not get here
47+
return 1;
48+
49+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a post condition on a member function
4+
// with caller side checks.
5+
// { dg-do run }
6+
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe -fcontracts-nonattr-client-contracts=all " }
7+
8+
#include <experimental/contract>
9+
#include <iostream>
10+
#include <exception>
11+
#include <cstdlib>
12+
13+
struct MyException{};
14+
15+
// Test that there is an active exception when we reach the terminate handler.
16+
void my_term()
17+
{
18+
try { throw; }
19+
catch(MyException) { std::exit(0); }
20+
}
21+
22+
23+
void handle_contract_violation(const std::experimental::contract_violation& violation)
24+
{
25+
throw MyException{};
26+
}
27+
28+
void f(const int x) noexcept post(x >= 0)
29+
{
30+
try{
31+
int i = 1;
32+
}
33+
catch(...) {
34+
}
35+
}
36+
37+
int main()
38+
{
39+
40+
std::set_terminate (my_term);
41+
try
42+
{
43+
f(-42);
44+
} catch (...) {
45+
}
46+
// We should not get here
47+
return 1;
48+
}

gcc/testsuite/g++.dg/contracts/cpp26/callerside_checks-noexcept.C renamed to gcc/testsuite/g++.dg/contracts/cpp26/callerside-checks/freefunc-noexcept-pre.C

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Throwing violation handler in a pre/post check on a noexcept function
2+
// behaves as if the function exited via an exception.
3+
// This tests the behaviour of a post condition on a member function
4+
// with caller side checks.
15
// { dg-do run }
26
// { dg-options "-std=c++2a -fcontracts -fcontracts-nonattr -fcontract-evaluation-semantic=observe -fcontracts-nonattr-client-contracts=all " }
37

@@ -21,7 +25,14 @@ void handle_contract_violation(const std::experimental::contract_violation& viol
2125
throw MyException{};
2226
}
2327

24-
void f(int x) noexcept pre(x >= 0) {}
28+
void f(int x) noexcept pre(x >= 0)
29+
{
30+
try{
31+
int i = 1;
32+
}
33+
catch(...) {
34+
}
35+
}
2536

2637
int main()
2738
{
@@ -32,4 +43,6 @@ int main()
3243
f(-42);
3344
} catch (...) {
3445
}
46+
// We should not get here
47+
return 1;
3548
}

0 commit comments

Comments
 (0)