From ac316fd054692670e426e8395f700091604d44f1 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Mon, 12 Feb 2018 17:42:03 +0000 Subject: [PATCH 2/3] Move unit tests to gdb/unittests/ --- gdb/Makefile.in | 1 + gdb/observer.c | 116 ------------------------------- gdb/unittests/observer-selftests.c | 135 +++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 116 deletions(-) create mode 100644 gdb/unittests/observer-selftests.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 844f92d7cde..2b09850bd6c 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -421,6 +421,7 @@ SUBDIR_UNITTESTS_SRCS = \ unittests/memory-map-selftests.c \ unittests/memrange-selftests.c \ unittests/offset-type-selftests.c \ + unittests/observer-selftests.c \ unittests/optional-selftests.c \ unittests/ptid-selftests.c \ unittests/rsp-low-selftests.c \ diff --git a/gdb/observer.c b/gdb/observer.c index 7e44c8c0181..836b083125d 100644 --- a/gdb/observer.c +++ b/gdb/observer.c @@ -21,7 +21,6 @@ #include "observer.h" #include "command.h" #include "gdbcmd.h" -#include "selftest.h" namespace gdb { @@ -76,116 +75,6 @@ DEFINE_OBSERVER (inferior_call_post); DEFINE_OBSERVER (register_changed); DEFINE_OBSERVER (user_selected_context_changed); -#ifdef GDB_SELF_TEST - -namespace selftests -{ - -/* This observer is used for internal testing. */ -observable test_notification ("test_notification"); - -static int test_first_observer = 0; -static int test_second_observer = 0; -static int test_third_observer = 0; - -static void -test_first_notification_function (int arg) -{ - test_first_observer++; -} - -static void -test_second_notification_function (int arg) -{ - test_second_observer++; -} - -static void -test_third_notification_function (int arg) -{ - test_third_observer++; -} - -static void -notify_check_counters (int one, int two, int three) -{ - /* Reset. */ - test_first_observer = 0; - test_second_observer = 0; - test_third_observer = 0; - /* Notify. */ - test_notification.notify (0); - /* Check. */ - SELF_CHECK (one == test_first_observer); - SELF_CHECK (two == test_second_observer); - SELF_CHECK (three == test_third_observer); -} - -static void -observer_self_tests () -{ - /* First, try sending a notification without any observer - attached. */ - notify_check_counters (0, 0, 0); - - const gdb::observers::token token1, token2, token3; - - /* Now, attach one observer, and send a notification. */ - test_notification.attach (&test_second_notification_function, token2); - notify_check_counters (0, 1, 0); - - /* Remove the observer, and send a notification. */ - test_notification.detach (token2); - notify_check_counters (0, 0, 0); - - /* With a new observer. */ - test_notification.attach (&test_first_notification_function, token1); - notify_check_counters (1, 0, 0); - - /* With 2 observers. */ - test_notification.attach (&test_second_notification_function, token2); - notify_check_counters (1, 1, 0); - - /* With 3 observers. */ - test_notification.attach (&test_third_notification_function, token3); - notify_check_counters (1, 1, 1); - - /* Remove middle observer. */ - test_notification.detach (token2); - notify_check_counters (1, 0, 1); - - /* Remove first observer. */ - test_notification.detach (token1); - notify_check_counters (0, 0, 1); - - /* Remove last observer. */ - test_notification.detach (token3); - notify_check_counters (0, 0, 0); - - /* Go back to 3 observers, and remove them in a different - order... */ - test_notification.attach (&test_first_notification_function, token1); - test_notification.attach (&test_second_notification_function, token2); - test_notification.attach (&test_third_notification_function, token3); - notify_check_counters (1, 1, 1); - - /* Remove the third observer. */ - test_notification.detach (token3); - notify_check_counters (1, 1, 0); - - /* Remove the second observer. */ - test_notification.detach (token2); - notify_check_counters (1, 0, 0); - - /* Remove first observer, no more observers. */ - test_notification.detach (token1); - notify_check_counters (0, 0, 0); -} - -} /* namespace selftests */ - -#endif - } /* namespace observers */ } /* namespace gdb */ @@ -207,9 +96,4 @@ When non-zero, observer debugging is enabled."), NULL, show_observer_debug, &setdebuglist, &showdebuglist); - -#if GDB_SELF_TEST - selftests::register_test ("gdb::observers", - gdb::observers::selftests::observer_self_tests); -#endif } diff --git a/gdb/unittests/observer-selftests.c b/gdb/unittests/observer-selftests.c new file mode 100644 index 00000000000..6f2f7e8bccf --- /dev/null +++ b/gdb/unittests/observer-selftests.c @@ -0,0 +1,135 @@ +/* Self tests for gdb::observers, GDB notifications to observers. + + Copyright (C) 2003-2018 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +#include "defs.h" +#include "selftest.h" +#include "common/observable.h" + +namespace selftests { +namespace observers { + +gdb::observers::observable test_notification ("test_notification"); + +static int test_first_observer = 0; +static int test_second_observer = 0; +static int test_third_observer = 0; + +static void +test_first_notification_function (int arg) +{ + test_first_observer++; +} + +static void +test_second_notification_function (int arg) +{ + test_second_observer++; +} + +static void +test_third_notification_function (int arg) +{ + test_third_observer++; +} + +static void +notify_check_counters (int one, int two, int three) +{ + /* Reset. */ + test_first_observer = 0; + test_second_observer = 0; + test_third_observer = 0; + /* Notify. */ + test_notification.notify (0); + /* Check. */ + SELF_CHECK (one == test_first_observer); + SELF_CHECK (two == test_second_observer); + SELF_CHECK (three == test_third_observer); +} + +static void +run_tests () +{ + /* First, try sending a notification without any observer + attached. */ + notify_check_counters (0, 0, 0); + + const gdb::observers::token token1, token2, token3; + + /* Now, attach one observer, and send a notification. */ + test_notification.attach (&test_second_notification_function, token2); + notify_check_counters (0, 1, 0); + + /* Remove the observer, and send a notification. */ + test_notification.detach (token2); + notify_check_counters (0, 0, 0); + + /* With a new observer. */ + test_notification.attach (&test_first_notification_function, token1); + notify_check_counters (1, 0, 0); + + /* With 2 observers. */ + test_notification.attach (&test_second_notification_function, token2); + notify_check_counters (1, 1, 0); + + /* With 3 observers. */ + test_notification.attach (&test_third_notification_function, token3); + notify_check_counters (1, 1, 1); + + /* Remove middle observer. */ + test_notification.detach (token2); + notify_check_counters (1, 0, 1); + + /* Remove first observer. */ + test_notification.detach (token1); + notify_check_counters (0, 0, 1); + + /* Remove last observer. */ + test_notification.detach (token3); + notify_check_counters (0, 0, 0); + + /* Go back to 3 observers, and remove them in a different + order... */ + test_notification.attach (&test_first_notification_function, token1); + test_notification.attach (&test_second_notification_function, token2); + test_notification.attach (&test_third_notification_function, token3); + notify_check_counters (1, 1, 1); + + /* Remove the third observer. */ + test_notification.detach (token3); + notify_check_counters (1, 1, 0); + + /* Remove the second observer. */ + test_notification.detach (token2); + notify_check_counters (1, 0, 0); + + /* Remove first observer, no more observers. */ + test_notification.detach (token1); + notify_check_counters (0, 0, 0); +} + +} /* namespace observers */ +} /* namespace selftests */ + +void +_initialize_observer_selftest () +{ + selftests::register_test ("gdb::observers", + selftests::observers::run_tests); +} -- 2.14.3