1 /* Self tests for gdb::observers, GDB notifications to observers.
3 Copyright (C) 2003-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "common/selftest.h"
22 #include "common/observable.h"
27 gdb::observers::observable
<int> test_notification ("test_notification");
29 static int test_first_observer
= 0;
30 static int test_second_observer
= 0;
31 static int test_third_observer
= 0;
34 test_first_notification_function (int arg
)
36 test_first_observer
++;
40 test_second_notification_function (int arg
)
42 test_second_observer
++;
46 test_third_notification_function (int arg
)
48 test_third_observer
++;
52 notify_check_counters (int one
, int two
, int three
)
55 test_first_observer
= 0;
56 test_second_observer
= 0;
57 test_third_observer
= 0;
59 test_notification
.notify (0);
61 SELF_CHECK (one
== test_first_observer
);
62 SELF_CHECK (two
== test_second_observer
);
63 SELF_CHECK (three
== test_third_observer
);
69 /* First, try sending a notification without any observer
71 notify_check_counters (0, 0, 0);
73 const gdb::observers::token token1
{}, token2
{} , token3
{};
75 /* Now, attach one observer, and send a notification. */
76 test_notification
.attach (&test_second_notification_function
, token2
);
77 notify_check_counters (0, 1, 0);
79 /* Remove the observer, and send a notification. */
80 test_notification
.detach (token2
);
81 notify_check_counters (0, 0, 0);
83 /* With a new observer. */
84 test_notification
.attach (&test_first_notification_function
, token1
);
85 notify_check_counters (1, 0, 0);
87 /* With 2 observers. */
88 test_notification
.attach (&test_second_notification_function
, token2
);
89 notify_check_counters (1, 1, 0);
91 /* With 3 observers. */
92 test_notification
.attach (&test_third_notification_function
, token3
);
93 notify_check_counters (1, 1, 1);
95 /* Remove middle observer. */
96 test_notification
.detach (token2
);
97 notify_check_counters (1, 0, 1);
99 /* Remove first observer. */
100 test_notification
.detach (token1
);
101 notify_check_counters (0, 0, 1);
103 /* Remove last observer. */
104 test_notification
.detach (token3
);
105 notify_check_counters (0, 0, 0);
107 /* Go back to 3 observers, and remove them in a different
109 test_notification
.attach (&test_first_notification_function
, token1
);
110 test_notification
.attach (&test_second_notification_function
, token2
);
111 test_notification
.attach (&test_third_notification_function
, token3
);
112 notify_check_counters (1, 1, 1);
114 /* Remove the third observer. */
115 test_notification
.detach (token3
);
116 notify_check_counters (1, 1, 0);
118 /* Remove the second observer. */
119 test_notification
.detach (token2
);
120 notify_check_counters (1, 0, 0);
122 /* Remove first observer, no more observers. */
123 test_notification
.detach (token1
);
124 notify_check_counters (0, 0, 0);
127 } /* namespace observers */
128 } /* namespace selftests */
131 _initialize_observer_selftest ()
133 selftests::register_test ("gdb::observers",
134 selftests::observers::run_tests
);