1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 1998, 1999, 2004, 2007, 2008, 2009, 2010, 2011
4# Free Software Foundation, Inc.
5
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19# This file was written by Elena Zannoni (ezannoni@cygnus.com)
20
21# Tests for correctenss of logical operators, associativity and
22# precedence with integer type variables
23
24
25if $tracelevel then {
26    strace $tracelevel
27}
28
29#
30# test running programs
31#
32
33set testfile "int-type"
34set srcfile ${testfile}.c
35set binfile ${objdir}/${subdir}/${testfile}
36
37if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
38    untested logical.exp
39    return -1
40}
41
42if [get_compiler_info ${binfile}] {
43    return -1;
44}
45
46gdb_exit
47gdb_start
48gdb_reinitialize_dir $srcdir/$subdir
49gdb_load ${binfile}
50
51
52#
53# set it up at a breakpoint so we can play with the variable values
54#
55
56if ![runto_main] then {
57    perror "couldn't run to breakpoint"
58    continue
59}
60
61proc evaluate { vars ops } {
62    for {set vari 0} {$vari < [llength $vars]} {incr vari} {
63	set var [lindex $vars $vari]
64	for {set opi 0} {$opi < [llength $ops]} {incr opi} {
65	    set op [lindex [lindex $ops $opi] 0]
66	    set val [lindex [lindex $ops $opi] [expr $vari + 1]]
67	    gdb_test "print $var, $op" " = $val" "evaluate $op; variables $var; expecting $val"
68	}
69    }
70}
71
72# Unary
73
74evaluate {
75    {x = 0} {x = 1}
76} {
77    { {x}   0 1 }
78    { {!x}  1 0 }
79    { {!!x} 0 1 }
80}
81
82# Binary (with unary)
83
84evaluate {
85    {x = 0, y = 0} {x = 0, y = 1} {x = 1, y = 0} {x = 1, y = 1}
86} {
87    { {x && y}   0 0 0 1 }
88    { {!x && y}  0 1 0 0 }
89    { {x && !y}  0 0 1 0 }
90    { {!x && !y} 1 0 0 0 }
91
92    { {x || y}   0 1 1 1 }
93    { {!x || y}  1 1 0 1 }
94    { {x || !y}  1 0 1 1 }
95    { {!x || !y} 1 1 1 0 }
96
97    { {x < y}    0 1 0 0 }
98    { {x <= y}   1 1 0 1 }
99    { {x == y}   1 0 0 1 }
100    { {x != y}   0 1 1 0 }
101    { {x >= y}   1 0 1 1 }
102    { {x > y}    0 0 1 0 }
103}
104
105# Full table of &&, || combinations, followed by random mix of unary ops
106
107evaluate {
108    {x = 0, y = 0, z = 0} {x = 0, y = 0, z = 1} {x = 0, y = 1, z = 0} {x = 0, y = 1, z = 1}
109    {x = 1, y = 0, z = 0} {x = 1, y = 0, z = 1} {x = 1, y = 1, z = 0} {x = 1, y = 1, z = 1}
110} {
111    { {x && y && z}    0 0 0 0  0 0 0 1 }
112    { {x || y && z}    0 0 0 1  1 1 1 1 }
113    { {x && y || z}    0 1 0 1  0 1 1 1 }
114    { {x || y || z}    0 1 1 1  1 1 1 1 }
115
116    { {x || !y && z}   0 1 0 0  1 1 1 1 }
117    { {!x || y && z}   1 1 1 1  0 0 0 1 }
118    { {!x || y && !z}  1 1 1 1  0 0 1 0 }
119}
120
121# More complex operations
122
123evaluate {
124    {x = 1, y = 2, w = 3, z = 3}
125    {x = 1, y = 2, w = 1, z = 3}
126    {x = 2, y = 2, w = 2, z = 3}
127} {
128    { {x > y || w == z}   1 0 0 }
129    { {x >= y && w != z}  0 0 1 }
130    { {! x > y || w + z}  1 1 1 }
131}
132