summaryrefslogtreecommitdiffstats
path: root/gnulib-tests/test-verify.c
blob: 4ba89c03338e00eb1ca8e8fe5d061ebd9ba7171a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Test the "verify" module.

   Copyright (C) 2005, 2009-2022 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible.  */

#include <config.h>

#include "verify.h"

#ifndef EXP_FAIL
# define EXP_FAIL 0
#endif

/* ======================= Test verify, verify_expr ======================= */

int gx;
enum { A, B, C };

#if EXP_FAIL == 1
verify (gx >= 0);                 /* should give ERROR: non-constant expression */
#endif
verify (C == 2);                  /* should be ok */
#if EXP_FAIL == 2
verify (1 + 1 == 3);              /* should give ERROR */
#endif
verify (1 == 1); verify (1 == 1); /* should be ok */

enum
{
  item = verify_expr (1 == 1, 10 * 0 + 17) /* should be ok */
};

static int
function (int n)
{
#if EXP_FAIL == 3
  verify (n >= 0);                  /* should give ERROR: non-constant expression */
#endif
  verify (C == 2);                  /* should be ok */
#if EXP_FAIL == 4
  verify (1 + 1 == 3);              /* should give ERROR */
#endif
  verify (1 == 1); verify (1 == 1); /* should be ok */

  if (n)
    return ((void) verify_expr (1 == 1, 1), verify_expr (1 == 1, 8)); /* should be ok */
#if EXP_FAIL == 5
  return verify_expr (1 == 2, 5); /* should give ERROR */
#endif
  return 0;
}

/* ============================== Test assume ============================== */

static int
f (int a)
{
  return a;
}

typedef struct { unsigned int context : 4; unsigned int halt : 1; } state;

void test_assume_expressions (state *s);
int test_assume_optimization (int x);
_Noreturn void test_assume_noreturn (void);

void
test_assume_expressions (state *s)
{
  /* Check that 'assume' accepts a function call, even of a non-const
     function.  */
  assume (f (1));
  /* Check that 'assume' accepts a bit-field expression.  */
  assume (s->halt);
}

int
test_assume_optimization (int x)
{
  /* Check that the compiler uses 'assume' for optimization.
     This function, when compiled with optimization, should have code
     equivalent to
       return x + 3;
     Use 'objdump --disassemble test-verify.o' to verify this.  */
  assume (x >= 4);
  return (x > 1 ? x + 3 : 2 * x + 10);
}

_Noreturn void
test_assume_noreturn (void)
{
  /* Check that the compiler's data-flow analysis recognizes 'assume (0)'.
     This function should not elicit a warning.  */
  assume (0);
}

/* ============================== Main ===================================== */
int
main (void)
{
  state s = { 0, 1 };
  test_assume_expressions (&s);
  test_assume_optimization (5);
  return !(function (0) == 0 && function (1) == 8);
}