summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.gdb/observer.exp
blob: a95cab878fc9139ae2d910eb8971c0412f5b304d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright 2003-2017 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file was written by Joel Brobecker (brobecker@gnat.com), derived
# from xfullpath.exp.

load_lib selftest-support.exp

proc attach_first_observer { message } {
    gdb_test_no_output "set \$first_obs = observer_attach_test_notification (&observer_test_first_notification_function)" \
	"$message; attach first observer"
}

proc attach_second_observer { message } {
    gdb_test_no_output "set \$second_obs = observer_attach_test_notification (&observer_test_second_notification_function)" \
	"$message; attach second observer"
}

proc attach_third_observer { message } {
    gdb_test_no_output "set \$third_obs = observer_attach_test_notification (&observer_test_third_notification_function)" \
	"$message; attach third observer"
}

proc detach_first_observer { message } {
    gdb_test_no_output "call observer_detach_test_notification (\$first_obs)" \
	"$message; detach first observer"
}

proc detach_second_observer { message } {
    gdb_test_no_output "call observer_detach_test_notification (\$second_obs)" \
	"$message; detach second observer"
}

proc detach_third_observer { message } {
    gdb_test_no_output "call observer_detach_test_notification (\$third_obs)" \
	"$message; detach third observer"
}

proc check_counters { first second third message } {
    gdb_test "print observer_test_first_observer" \
	".\[0-9\]+ =.*$first" \
	"$message; check first observer counter value"
    gdb_test "print observer_test_second_observer" \
	".\[0-9\]+ =.*$second" \
	"$message; check second observer counter value"
    gdb_test "print observer_test_third_observer" \
	".\[0-9\]+ =.*$third" \
	"$message; check third observer counter value"
}

proc reset_counters { message } {
    gdb_test_no_output "set variable observer_test_first_observer = 0" \
	"$message; reset first observer counter"
    gdb_test_no_output "set variable observer_test_second_observer = 0" \
	"$message; reset second observer counter"
    gdb_test_no_output "set variable observer_test_third_observer = 0" \
	"$message; reset third observer counter"
}

proc test_notifications { first second third message args } {
    # Do any initialization
    for {set i 0} {$i < [llength $args]} {incr i} {
	[lindex $args $i] $message
    }
    reset_counters $message
    # Call observer_notify_test_notification.  Note that this procedure
    # takes one argument, but this argument is ignored by the observer
    # callbacks we have installed.  So we just pass an arbitrary value.
    gdb_test_no_output "call observer_notify_test_notification (0)" \
	"$message; sending notification"
    check_counters $first $second $third $message
}

proc test_observer {} {
    # First, try sending a notification without any observer attached.
    test_notifications 0 0 0 "no observer attached"

    # Now, attach one observer, and send a notification.
    test_notifications 0 1 0 "second observer attached" \
	attach_second_observer

    # Remove the observer, and send a notification.
    test_notifications 0 0 0 "second observer detached" \
	detach_second_observer

    # With a new observer.
    test_notifications 1 0 0 "1st observer added" \
	attach_first_observer

    # With 2 observers.
    test_notifications 1 1 0 "2nd observer added" \
	attach_second_observer

    # With 3 observers.
    test_notifications 1 1 1 "3rd observer added" \
	attach_third_observer

    # Remove middle observer.
    test_notifications 1 0 1 "2nd observer removed" \
	detach_second_observer

    # Remove first observer.
    test_notifications 0 0 1 "1st observer removed" \
	detach_first_observer

    # Remove last observer.
    test_notifications 0 0 0 "3rd observer removed" \
	detach_third_observer

    # Go back to 3 observers, and remove them in a different order...
    test_notifications 1 1 1 "three observers added" \
	attach_first_observer \
	attach_second_observer \
	attach_third_observer 

    # Remove the third observer.
    test_notifications 1 1 0 "third observer removed" \
	detach_third_observer

    # Remove the second observer.
    test_notifications 1 0 0 "second observer removed" \
	detach_second_observer

    # Remove the first observer, no more observers.
    test_notifications 0 0 0 "first observer removed" \
	detach_first_observer

    return 0
}

do_self_tests captured_main test_observer