nstrftime, c-nstrftime tests: Avoid test failures on native Windows.
[gnulib.git] / tests / test-verify.c
blob1d8f43c7a721e33e25593f60dd74b3e3d869a2be
1 /* Test the "verify" module.
3 Copyright (C) 2005, 2009-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible. */
20 #include <config.h>
22 #include "verify.h"
24 #ifndef EXP_FAIL
25 # define EXP_FAIL 0
26 #endif
28 /* ======================= Test verify, verify_expr ======================= */
30 int gx;
31 enum { A, B, C };
33 #if EXP_FAIL == 1
34 verify (gx >= 0); /* should give ERROR: non-constant expression */
35 #endif
36 verify (C == 2); /* should be ok */
37 #if EXP_FAIL == 2
38 verify (1 + 1 == 3); /* should give ERROR */
39 #endif
40 verify (1 == 1); verify (1 == 1); /* should be ok */
42 enum
44 item = verify_expr (1 == 1, 10 * 0 + 17) /* should be ok */
47 static int
48 function (int n)
50 #if EXP_FAIL == 3
51 verify (n >= 0); /* should give ERROR: non-constant expression */
52 #endif
53 verify (C == 2); /* should be ok */
54 #if EXP_FAIL == 4
55 verify (1 + 1 == 3); /* should give ERROR */
56 #endif
57 verify (1 == 1); verify (1 == 1); /* should be ok */
59 if (n)
60 return ((void) verify_expr (1 == 1, 1), verify_expr (1 == 1, 8)); /* should be ok */
61 #if EXP_FAIL == 5
62 return verify_expr (1 == 2, 5); /* should give ERROR */
63 #endif
64 return 0;
67 /* ============================== Test assume ============================== */
69 static int
70 f (int a)
72 return a;
75 typedef struct { unsigned int context : 4; unsigned int halt : 1; } state;
77 void test_assume_expressions (state *s);
78 int test_assume_optimization (int x);
79 _Noreturn void test_assume_noreturn (void);
81 void
82 test_assume_expressions (state *s)
84 /* Check that 'assume' accepts a function call, even of a non-const
85 function. */
86 assume (f (1));
87 /* Check that 'assume' accepts a bit-field expression. */
88 assume (s->halt);
91 int
92 test_assume_optimization (int x)
94 /* Check that the compiler uses 'assume' for optimization.
95 This function, when compiled with optimization, should have code
96 equivalent to
97 return x + 3;
98 Use 'objdump --disassemble test-verify.o' to verify this. */
99 assume (x >= 4);
100 return (x > 1 ? x + 3 : 2 * x + 10);
103 _Noreturn void
104 test_assume_noreturn (void)
106 /* Check that the compiler's data-flow analysis recognizes 'assume (0)'.
107 This function should not elicit a warning. */
108 assume (0);
111 /* ============================== Main ===================================== */
113 main (void)
115 state s = { 0, 1 };
116 test_assume_expressions (&s);
117 test_assume_optimization (5);
118 return !(function (0) == 0 && function (1) == 8);