VirtualBox

source: kBuild/vendor/sed/current/gnulib-tests/test-verify.c@ 3613

Last change on this file since 3613 was 3611, checked in by bird, 7 months ago

vendor/sed/current: GNU sed 4.9 (sed-4.9.tar.xz sha256:6e226b732e1cd739464ad6862bd1a1aba42d7982922da7a53519631d24975181)

  • Property svn:mime-type set to application/javascript
File size: 3.1 KB
Line 
1/* Test the "verify" module.
2
3 Copyright (C) 2005, 2009-2022 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18/* Written by Bruno Haible. */
19
20#include <config.h>
21
22#include "verify.h"
23
24#ifndef EXP_FAIL
25# define EXP_FAIL 0
26#endif
27
28/* ======================= Test verify, verify_expr ======================= */
29
30int gx;
31enum { A, B, C };
32
33#if EXP_FAIL == 1
34verify (gx >= 0); /* should give ERROR: non-constant expression */
35#endif
36verify (C == 2); /* should be ok */
37#if EXP_FAIL == 2
38verify (1 + 1 == 3); /* should give ERROR */
39#endif
40verify (1 == 1); verify (1 == 1); /* should be ok */
41
42enum
43{
44 item = verify_expr (1 == 1, 10 * 0 + 17) /* should be ok */
45};
46
47static int
48function (int n)
49{
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 */
58
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;
65}
66
67/* ============================== Test assume ============================== */
68
69static int
70f (int a)
71{
72 return a;
73}
74
75typedef struct { unsigned int context : 4; unsigned int halt : 1; } state;
76
77void test_assume_expressions (state *s);
78int test_assume_optimization (int x);
79_Noreturn void test_assume_noreturn (void);
80
81void
82test_assume_expressions (state *s)
83{
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);
89}
90
91int
92test_assume_optimization (int x)
93{
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);
101}
102
103_Noreturn void
104test_assume_noreturn (void)
105{
106 /* Check that the compiler's data-flow analysis recognizes 'assume (0)'.
107 This function should not elicit a warning. */
108 assume (0);
109}
110
111/* ============================== Main ===================================== */
112int
113main (void)
114{
115 state s = { 0, 1 };
116 test_assume_expressions (&s);
117 test_assume_optimization (5);
118 return !(function (0) == 0 && function (1) == 8);
119}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette