1 # Copyright 2003-2014 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file was written by Joel Brobecker (brobecker@gnat.com), derived
19 load_lib selftest-support.exp
21 proc attach_first_observer { message } {
22 gdb_test_no_output "set \$first_obs = observer_attach_test_notification (&observer_test_first_notification_function)" \
23 "$message; attach first observer"
26 proc attach_second_observer { message } {
27 gdb_test_no_output "set \$second_obs = observer_attach_test_notification (&observer_test_second_notification_function)" \
28 "$message; attach second observer"
31 proc attach_third_observer { message } {
32 gdb_test_no_output "set \$third_obs = observer_attach_test_notification (&observer_test_third_notification_function)" \
33 "$message; attach third observer"
36 proc detach_first_observer { message } {
37 gdb_test_no_output "call observer_detach_test_notification (\$first_obs)" \
38 "$message; detach first observer"
41 proc detach_second_observer { message } {
42 gdb_test_no_output "call observer_detach_test_notification (\$second_obs)" \
43 "$message; detach second observer"
46 proc detach_third_observer { message } {
47 gdb_test_no_output "call observer_detach_test_notification (\$third_obs)" \
48 "$message; detach third observer"
51 proc check_counters { first second third message } {
52 gdb_test "print observer_test_first_observer" \
53 ".\[0-9\]+ =.*$first" \
54 "$message; check first observer counter value"
55 gdb_test "print observer_test_second_observer" \
56 ".\[0-9\]+ =.*$second" \
57 "$message; check second observer counter value"
58 gdb_test "print observer_test_third_observer" \
59 ".\[0-9\]+ =.*$third" \
60 "$message; check third observer counter value"
63 proc reset_counters { message } {
64 gdb_test_no_output "set variable observer_test_first_observer = 0" \
65 "$message; reset first observer counter"
66 gdb_test_no_output "set variable observer_test_second_observer = 0" \
67 "$message; reset second observer counter"
68 gdb_test_no_output "set variable observer_test_third_observer = 0" \
69 "$message; reset third observer counter"
72 proc test_notifications { first second third message args } {
73 # Do any initialization
74 for {set i 0} {$i < [llength $args]} {incr i} {
75 [lindex $args $i] $message
77 reset_counters $message
78 # Call observer_notify_test_notification. Note that this procedure
79 # takes one argument, but this argument is ignored by the observer
80 # callbacks we have installed. So we just pass an arbitrary value.
81 gdb_test_no_output "call observer_notify_test_notification (0)" \
82 "$message; sending notification"
83 check_counters $first $second $third $message
86 proc test_observer {} {
87 # First, try sending a notification without any observer attached.
88 test_notifications 0 0 0 "no observer attached"
90 # Now, attach one observer, and send a notification.
91 test_notifications 0 1 0 "second observer attached" \
92 attach_second_observer
94 # Remove the observer, and send a notification.
95 test_notifications 0 0 0 "second observer detached" \
96 detach_second_observer
98 # With a new observer.
99 test_notifications 1 0 0 "1st observer added" \
100 attach_first_observer
103 test_notifications 1 1 0 "2nd observer added" \
104 attach_second_observer
107 test_notifications 1 1 1 "3rd observer added" \
108 attach_third_observer
110 # Remove middle observer.
111 test_notifications 1 0 1 "2nd observer removed" \
112 detach_second_observer
114 # Remove first observer.
115 test_notifications 0 0 1 "1st observer removed" \
116 detach_first_observer
118 # Remove last observer.
119 test_notifications 0 0 0 "3rd observer removed" \
120 detach_third_observer
122 # Go back to 3 observers, and remove them in a different order...
123 test_notifications 1 1 1 "three observers added" \
124 attach_first_observer \
125 attach_second_observer \
126 attach_third_observer
128 # Remove the third observer.
129 test_notifications 1 1 0 "third observer removed" \
130 detach_third_observer
132 # Remove the second observer.
133 test_notifications 1 0 0 "second observer removed" \
134 detach_second_observer
136 # Remove the first observer, no more observers.
137 test_notifications 0 0 0 "first observer removed" \
138 detach_first_observer
143 do_self_tests captured_main test_observer