| 1 | /* Self tests for gdb::observers, GDB notifications to observers. |
| 2 | |
| 3 | Copyright (C) 2003-2018 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "selftest.h" |
| 22 | #include "common/observable.h" |
| 23 | |
| 24 | namespace selftests { |
| 25 | namespace observers { |
| 26 | |
| 27 | gdb::observers::observable<int> test_notification ("test_notification"); |
| 28 | |
| 29 | static int test_first_observer = 0; |
| 30 | static int test_second_observer = 0; |
| 31 | static int test_third_observer = 0; |
| 32 | |
| 33 | static void |
| 34 | test_first_notification_function (int arg) |
| 35 | { |
| 36 | test_first_observer++; |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | test_second_notification_function (int arg) |
| 41 | { |
| 42 | test_second_observer++; |
| 43 | } |
| 44 | |
| 45 | static void |
| 46 | test_third_notification_function (int arg) |
| 47 | { |
| 48 | test_third_observer++; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | notify_check_counters (int one, int two, int three) |
| 53 | { |
| 54 | /* Reset. */ |
| 55 | test_first_observer = 0; |
| 56 | test_second_observer = 0; |
| 57 | test_third_observer = 0; |
| 58 | /* Notify. */ |
| 59 | test_notification.notify (0); |
| 60 | /* Check. */ |
| 61 | SELF_CHECK (one == test_first_observer); |
| 62 | SELF_CHECK (two == test_second_observer); |
| 63 | SELF_CHECK (three == test_third_observer); |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | run_tests () |
| 68 | { |
| 69 | /* First, try sending a notification without any observer |
| 70 | attached. */ |
| 71 | notify_check_counters (0, 0, 0); |
| 72 | |
| 73 | const gdb::observers::token token1, token2, token3; |
| 74 | |
| 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); |
| 78 | |
| 79 | /* Remove the observer, and send a notification. */ |
| 80 | test_notification.detach (token2); |
| 81 | notify_check_counters (0, 0, 0); |
| 82 | |
| 83 | /* With a new observer. */ |
| 84 | test_notification.attach (&test_first_notification_function, token1); |
| 85 | notify_check_counters (1, 0, 0); |
| 86 | |
| 87 | /* With 2 observers. */ |
| 88 | test_notification.attach (&test_second_notification_function, token2); |
| 89 | notify_check_counters (1, 1, 0); |
| 90 | |
| 91 | /* With 3 observers. */ |
| 92 | test_notification.attach (&test_third_notification_function, token3); |
| 93 | notify_check_counters (1, 1, 1); |
| 94 | |
| 95 | /* Remove middle observer. */ |
| 96 | test_notification.detach (token2); |
| 97 | notify_check_counters (1, 0, 1); |
| 98 | |
| 99 | /* Remove first observer. */ |
| 100 | test_notification.detach (token1); |
| 101 | notify_check_counters (0, 0, 1); |
| 102 | |
| 103 | /* Remove last observer. */ |
| 104 | test_notification.detach (token3); |
| 105 | notify_check_counters (0, 0, 0); |
| 106 | |
| 107 | /* Go back to 3 observers, and remove them in a different |
| 108 | order... */ |
| 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); |
| 113 | |
| 114 | /* Remove the third observer. */ |
| 115 | test_notification.detach (token3); |
| 116 | notify_check_counters (1, 1, 0); |
| 117 | |
| 118 | /* Remove the second observer. */ |
| 119 | test_notification.detach (token2); |
| 120 | notify_check_counters (1, 0, 0); |
| 121 | |
| 122 | /* Remove first observer, no more observers. */ |
| 123 | test_notification.detach (token1); |
| 124 | notify_check_counters (0, 0, 0); |
| 125 | } |
| 126 | |
| 127 | } /* namespace observers */ |
| 128 | } /* namespace selftests */ |
| 129 | |
| 130 | void |
| 131 | _initialize_observer_selftest () |
| 132 | { |
| 133 | selftests::register_test ("gdb::observers", |
| 134 | selftests::observers::run_tests); |
| 135 | } |