GCC Code Coverage Report


Directory: ./
File: basic/boolean_expression.cpp
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 47 59 79.7%
Functions: 6 6 100.0%
Branches: 53 84 63.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/basic/boolean_expression.h>
41
42 namespace GEO {
43
44 644 BooleanExpression::BooleanExpression(
45 const std::string& expr
46 644 ) : expr_(expr) {
47 644 }
48
49 170122 bool BooleanExpression::operator()(index_t x) const {
50 170122 Context C(expr_, x);
51
1/2
✓ Branch 1 taken 170122 times.
✗ Branch 2 not taken.
340244 return parse_or(C);
52 }
53
54 170122 bool BooleanExpression::parse_or(Context& C) const {
55 170122 bool left = parse_and(C);
56 170122 while(
57
1/2
✓ Branch 1 taken 357844 times.
✗ Branch 2 not taken.
715688 C.cur_char() == '|' ||
58
2/2
✓ Branch 1 taken 344200 times.
✓ Branch 2 taken 13644 times.
715688 C.cur_char() == '^' ||
59
5/6
✓ Branch 0 taken 357844 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 174078 times.
✓ Branch 4 taken 170122 times.
✓ Branch 5 taken 187722 times.
✓ Branch 6 taken 170122 times.
1073532 C.cur_char() == '+' ||
60 344200 C.cur_char() == '-'
61 ) {
62 187722 char op = C.cur_char();
63 187722 C.next_char();
64 187722 bool right = parse_and(C);
65
6/6
✓ Branch 0 taken 174078 times.
✓ Branch 1 taken 13644 times.
✓ Branch 2 taken 79416 times.
✓ Branch 3 taken 94662 times.
✓ Branch 4 taken 38939 times.
✓ Branch 5 taken 40477 times.
201366 left = (op == '-') ? (left && !right) :
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13644 times.
13644 (op == '^') ? (left ^ right) :
67
4/4
✓ Branch 0 taken 12084 times.
✓ Branch 1 taken 1560 times.
✓ Branch 2 taken 2988 times.
✓ Branch 3 taken 9096 times.
13644 (left || right) ;
68 }
69 170122 return left;
70 }
71
72 357844 bool BooleanExpression::parse_and(Context& C) const {
73 357844 bool left = parse_factor(C);
74
5/6
✓ Branch 1 taken 367708 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9864 times.
✓ Branch 5 taken 357844 times.
✓ Branch 6 taken 9864 times.
✓ Branch 7 taken 357844 times.
367708 while(C.cur_char() == '&' || C.cur_char() == '*') {
75 9864 C.next_char();
76 9864 bool right = parse_factor(C);
77
4/4
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 8112 times.
✓ Branch 2 taken 192 times.
✓ Branch 3 taken 1560 times.
9864 left = left && right;
78 }
79 357844 return left;
80 }
81
82 367708 bool BooleanExpression::parse_factor(Context& C) const {
83
4/8
✓ Branch 1 taken 367708 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 367708 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 367708 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 367708 times.
367708 if(C.cur_char() == '!' || C.cur_char() == '~' || C.cur_char() == '-') {
84 ✗ C.next_char();
85 ✗ return !parse_factor(C);
86 }
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 367708 times.
367708 if(C.cur_char() == '(') {
88 ✗ C.next_char();
89 ✗ bool result = parse_or(C);
90 ✗ if(C.cur_char() != ')') {
91 throw std::logic_error(
92 ✗ std::string("Unmatched parenthesis: ")+C.cur_char()
93 ✗ );
94 }
95 ✗ C.next_char();
96 ✗ return result;
97 }
98
2/2
✓ Branch 1 taken 4960 times.
✓ Branch 2 taken 362748 times.
367708 if((C.cur_char() == '*')) {
99 4960 C.next_char();
100 4960 return (C.x_ != 0);
101 }
102
5/8
✓ Branch 1 taken 362748 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 270996 times.
✓ Branch 5 taken 91752 times.
✓ Branch 7 taken 270996 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 362748 times.
✗ Branch 10 not taken.
362748 if((C.cur_char() >= 'A' && C.cur_char() <= 'Z') || C.cur_char() == 'x') {
103 362748 return parse_variable(C);
104 }
105 ✗ throw std::logic_error("Syntax error");
106 }
107
108 362748 bool BooleanExpression::parse_variable(Context& C) const {
109 362748 int bit = 0;
110
5/6
✓ Branch 1 taken 362748 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 91752 times.
✓ Branch 5 taken 270996 times.
✓ Branch 6 taken 91752 times.
✓ Branch 7 taken 270996 times.
362748 if(C.cur_char() >= 'A' && C.cur_char() <= 'Z') {
111 91752 bit = int(C.cur_char()) - int('A');
112 91752 C.next_char();
113 } else {
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 270996 times.
270996 if(C.cur_char() != 'x') {
115 ✗ throw std::logic_error("Syntax error in variable");
116 }
117 270996 C.next_char();
118
5/6
✓ Branch 1 taken 270996 times.
✓ Branch 2 taken 270996 times.
✓ Branch 4 taken 270996 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 270996 times.
✓ Branch 7 taken 270996 times.
541992 while(C.cur_char() >= '0' && C.cur_char() <= '9') {
119 270996 bit = bit * 10 + (int(C.cur_char()) - '0');
120 270996 C.next_char();
121 }
122 }
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362748 times.
362748 if(bit > 31) {
124 ✗ throw std::logic_error("Bit larger than 31");
125 }
126 362748 return ((C.x_ & (index_t(1u) << bit)) != 0);
127 }
128 }
129