1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 1998-2020 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 <http://www.gnu.org/licenses/>.
17
18# This file was written by Elena Zannoni (ezannoni@cygnus.com)
19
20# Tests for correctenss of logical operators, associativity and
21# precedence with integer type variables
22
23
24#
25# test running programs
26#
27
28standard_testfile int-type.c
29
30if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
31    untested "failed to compile"
32    return -1
33}
34
35if [get_compiler_info] {
36    return -1
37}
38
39clean_restart ${binfile}
40
41
42#
43# set it up at a breakpoint so we can play with the variable values
44#
45
46if ![runto_main] then {
47    perror "couldn't run to breakpoint"
48    continue
49}
50
51proc evaluate { vars ops } {
52    for {set vari 0} {$vari < [llength $vars]} {incr vari} {
53	set var [lindex $vars $vari]
54	for {set opi 0} {$opi < [llength $ops]} {incr opi} {
55	    set op [lindex [lindex $ops $opi] 0]
56	    set val [lindex [lindex $ops $opi] [expr $vari + 1]]
57	    gdb_test "print $var, $op" " = $val" "evaluate $op; variables $var; expecting $val"
58	}
59    }
60}
61
62# Unary
63
64evaluate {
65    {x = 0} {x = 1}
66} {
67    { {x}   0 1 }
68    { {!x}  1 0 }
69    { {!!x} 0 1 }
70}
71
72# Binary (with unary)
73
74evaluate {
75    {x = 0, y = 0} {x = 0, y = 1} {x = 1, y = 0} {x = 1, y = 1}
76} {
77    { {x && y}   0 0 0 1 }
78    { {!x && y}  0 1 0 0 }
79    { {x && !y}  0 0 1 0 }
80    { {!x && !y} 1 0 0 0 }
81
82    { {x || y}   0 1 1 1 }
83    { {!x || y}  1 1 0 1 }
84    { {x || !y}  1 0 1 1 }
85    { {!x || !y} 1 1 1 0 }
86
87    { {x < y}    0 1 0 0 }
88    { {x <= y}   1 1 0 1 }
89    { {x == y}   1 0 0 1 }
90    { {x != y}   0 1 1 0 }
91    { {x >= y}   1 0 1 1 }
92    { {x > y}    0 0 1 0 }
93}
94
95# Full table of &&, || combinations, followed by random mix of unary ops
96
97evaluate {
98    {x = 0, y = 0, z = 0} {x = 0, y = 0, z = 1} {x = 0, y = 1, z = 0} {x = 0, y = 1, z = 1}
99    {x = 1, y = 0, z = 0} {x = 1, y = 0, z = 1} {x = 1, y = 1, z = 0} {x = 1, y = 1, z = 1}
100} {
101    { {x && y && z}    0 0 0 0  0 0 0 1 }
102    { {x || y && z}    0 0 0 1  1 1 1 1 }
103    { {x && y || z}    0 1 0 1  0 1 1 1 }
104    { {x || y || z}    0 1 1 1  1 1 1 1 }
105
106    { {x || !y && z}   0 1 0 0  1 1 1 1 }
107    { {!x || y && z}   1 1 1 1  0 0 0 1 }
108    { {!x || y && !z}  1 1 1 1  0 0 1 0 }
109}
110
111# More complex operations
112
113evaluate {
114    {x = 1, y = 2, w = 3, z = 3}
115    {x = 1, y = 2, w = 1, z = 3}
116    {x = 2, y = 2, w = 2, z = 3}
117} {
118    { {x > y || w == z}   1 0 0 }
119    { {x >= y && w != z}  0 0 1 }
120    { {! x > y || w + z}  1 1 1 }
121}
122