1# Tests of overloaded operators resolution.
2# Copyright 1998-2023 Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# written by Elena Zannoni (ezannoni@cygnus.com)
18#
19# source file "userdef.cc"
20#
21
22if { [skip_stl_tests] } { return }
23
24standard_testfile .cc
25
26if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
27    return -1
28}
29
30if {![runto_main]} {
31    return
32}
33
34gdb_test "break marker1" \
35    "Breakpoint .*${srcfile}.*"
36
37gdb_test "cont" \
38    "Break.* marker1(\\(\\)|) \\(\\) at .*:$decimal.*" \
39    "continue to marker1"
40
41gdb_test "up" " in main .*" "up from marker1"
42
43gdb_test "print one + two" "\\\$\[0-9\]* = {x = 6, y = 8}"
44
45# If GDB fails to restore the selected frame properly after the
46# inferior function call above (see GDB PR 1155 for an explanation of
47# why this might happen), all the subsequent tests will fail.  We
48# should detect report that failure, but let the marker call finish so
49# that the rest of the tests can run undisturbed.
50gdb_test_multiple "frame" "re-selected 'main' frame after inferior call" {
51    -re "#0  marker1.*$gdb_prompt $" {
52        setup_kfail "gdb/1155" s390-*-linux-gnu
53        fail "re-selected 'main' frame after inferior call"
54        gdb_test "finish" ".*main.*at .*userdef.cc:.*// marker1-returns-here.*" \
55                "finish call to marker1"
56    }
57    -re "#1  ($hex in )?main.*$gdb_prompt $" {
58        pass "re-selected 'main' frame after inferior call"
59    }
60}
61
62gdb_test "print one - two" "\\\$\[0-9\]* = {x = -2, y = -2}"
63
64gdb_test "print one * two" "\\\$\[0-9\]* = {x = 8, y = 15}"
65
66gdb_test "print one / two" "\\\$\[0-9\]* = {x = 0, y = 0}"
67
68gdb_test "print one % two" "\\\$\[0-9\]* = {x = 2, y = 3}"
69
70gdb_test "print one && two" "\\\$\[0-9\]* = 1\[\r\n\]"
71
72gdb_test "print one || two" "\\\$\[0-9\]* = 1\[\r\n\]"
73
74gdb_test "print one & two" "\\\$\[0-9\]* = {x = 0, y = 1}"
75
76gdb_test "print one | two" "\\\$\[0-9\]* = {x = 6, y = 7}"
77
78gdb_test "print one ^ two" "\\\$\[0-9\]* = {x = 6, y = 6}"
79
80gdb_test "print one < two" "\\\$\[0-9\]* = 1\[\r\n\]"
81
82gdb_test "print one <= two" "\\\$\[0-9\]* = 1\[\r\n\]"
83
84gdb_test "print one > two" "\\\$\[0-9\]* = 0\[\r\n\]"
85
86gdb_test "print one >= two" "\\\$\[0-9\]* = 0\[\r\n\]"
87
88gdb_test "print one == two" "\\\$\[0-9\]* = 0\[\r\n\]"
89gdb_test "print one.operator== (two)" "\\\$\[0-9\]* = 0\[\r\n\]"
90
91gdb_test "print one != two" "\\\$\[0-9\]* = 1\[\r\n\]"
92
93# Can't really check the output of this one without knowing
94# target integer width.  Make sure we don't try to call
95# the iostreams operator instead, though.
96gdb_test "print one << 31" "\\\$\[0-9\]* = {x = -?\[0-9\]*, y = -?\[0-9\]*}"
97
98# Should be fine even on < 32-bit targets.
99gdb_test "print one >> 31" "\\\$\[0-9\]* = {x = 0, y = 0}"
100
101gdb_test "print !one" "\\\$\[0-9\]* = 0\[\r\n\]"
102
103# Assumes 2's complement.  So does everything...
104gdb_test "print +one" "\\\$\[0-9\]* = {x = 2, y = 3}"
105
106gdb_test "print ~one" "\\\$\[0-9\]* = {x = -3, y = -4}"
107
108gdb_test "print -one" "\\\$\[0-9\]* = {x = -2, y = -3}"
109
110gdb_test "print one++" "\\\$\[0-9\]* = {x = 2, y = 4}"
111
112gdb_test "print ++one" "\\\$\[0-9\]* = {x = 3, y = 4}"
113
114gdb_test "print one--" "\\\$\[0-9\]* = {x = 3, y = 3}"
115
116gdb_test "print --one" "\\\$\[0-9\]* = {x = 2, y = 3}"
117
118gdb_test "print one += 7" "\\\$\[0-9\]* = {x = 9, y = 10}"
119
120gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
121
122# Check that GDB tolerates whitespace in operator names.
123gdb_test "break A2::operator+" ".*Breakpoint $decimal at.*"
124gdb_test "break A2::operator +" ".*Breakpoint $decimal at.*"
125
126# Check that GDB handles operator* correctly.
127gdb_test "print c" "\\\$\[0-9\]* = {m = {z = .*}}"
128gdb_test "print *c" "\\\$\[0-9\]* = \\(Member &\\) @$hex: {z = .*}"
129gdb_test "print &*c" "\\\$\[0-9\]* = \\(Member \\*\\) $hex"
130gdb_test "ptype &*c" "type = (struct|class) Member {(\[\r\n \]+public:)?\[\r\n \]+int z;\[\r\n\].*} \\*"
131
132gdb_test "print operator== (mem1, mem2)" " = false"
133gdb_test "print operator== (mem1, mem1)" " = true"
134
135gdb_exit
136return 0
137