| 1 |
// basic test to ensure pre contracts work for free templates |
| 2 |
// { dg-do run } |
| 3 |
// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" } |
| 4 |
#include <cstdio> |
| 5 |
|
| 6 |
template<typename T> |
| 7 |
int body(int a) |
| 8 |
[[ pre: a > 0 ]] |
| 9 |
{ |
| 10 |
T t = a * 2.5; |
| 11 |
return t; |
| 12 |
} |
| 13 |
|
| 14 |
template<typename T> |
| 15 |
int none(int a) |
| 16 |
[[ pre: a > 0 ]] |
| 17 |
{ |
| 18 |
return -a; |
| 19 |
} |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
int arg0(T t) |
| 23 |
[[ pre: t > 0 ]] |
| 24 |
{ |
| 25 |
return -t - 10; |
| 26 |
} |
| 27 |
|
| 28 |
template<typename T> |
| 29 |
int arg1(int a, T t) |
| 30 |
[[ pre: a > 0 ]] |
| 31 |
[[ pre: t > 0 ]] |
| 32 |
{ |
| 33 |
return -t * a; |
| 34 |
} |
| 35 |
|
| 36 |
template<typename T> |
| 37 |
T ret(int a) |
| 38 |
[[ pre: a > 0 ]] |
| 39 |
{ |
| 40 |
return -a; |
| 41 |
} |
| 42 |
|
| 43 |
int main(int, char**) |
| 44 |
{ |
| 45 |
printf("%d\n", body<int>(-1)); |
| 46 |
printf("%d\n", body<double>(-2)); |
| 47 |
printf("%d\n", none<int>(-1)); |
| 48 |
printf("%d\n", none<double>(-2)); |
| 49 |
printf("%d\n", arg0(-1)); |
| 50 |
printf("%d\n", arg0(-2.9)); |
| 51 |
printf("%d\n", arg1(-3, -1)); |
| 52 |
printf("%d\n", arg1(-4, -2.9)); |
| 53 |
printf("%d\n", (int)ret<int>(-3)); |
| 54 |
printf("%d\n", (int)ret<double>(-4.9)); |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } } |
| 60 |
// { dg-output "contract violation in function body<int> at .*.C:8: .*(\n|\r\n|\r)" } |
| 61 |
// { dg-output "-2(\n|\r\n|\r)" } |
| 62 |
// { dg-output "contract violation in function body<double> at .*.C:8: .*(\n|\r\n|\r)" } |
| 63 |
// { dg-output "-5(\n|\r\n|\r)" } |
| 64 |
// { dg-output "contract violation in function none<int> at .*.C:16: .*(\n|\r\n|\r)" } |
| 65 |
// { dg-output "1(\n|\r\n|\r)" } |
| 66 |
// { dg-output "contract violation in function none<double> at .*.C:16: .*(\n|\r\n|\r)" } |
| 67 |
// { dg-output "2(\n|\r\n|\r)" } |
| 68 |
// { dg-output "contract violation in function arg0<int> at .*.C:23: .*(\n|\r\n|\r)" } |
| 69 |
// { dg-output "-9(\n|\r\n|\r)" } |
| 70 |
// { dg-output "contract violation in function arg0<double> at .*.C:23: .*(\n|\r\n|\r)" } |
| 71 |
// { dg-output "-7(\n|\r\n|\r)" } |
| 72 |
// { dg-output "contract violation in function arg1<int> at .*.C:30: .*(\n|\r\n|\r)" } |
| 73 |
// { dg-output "contract violation in function arg1<int> at .*.C:31: .*(\n|\r\n|\r)" } |
| 74 |
// { dg-output "-3(\n|\r\n|\r)" } |
| 75 |
// { dg-output "contract violation in function arg1<double> at .*.C:30: .*(\n|\r\n|\r)" } |
| 76 |
// { dg-output "contract violation in function arg1<double> at .*.C:31: .*(\n|\r\n|\r)" } |
| 77 |
// { dg-output "-11(\n|\r\n|\r)" } |
| 78 |
// { dg-output "contract violation in function ret<int> at .*.C:38: .*(\n|\r\n|\r)" } |
| 79 |
// { dg-output "3(\n|\r\n|\r)" } |
| 80 |
// { dg-output "contract violation in function ret<double> at .*.C:38: .*(\n|\r\n|\r)" } |
| 81 |
// { dg-output "4(\n|\r\n|\r)" } |
| 82 |
|