Add `set print repeats' tests for C/C++ arrays
[binutils-gdb.git] / gdb / complaints.c
index fecbcd74ce5dc2300efcdf903a224df3b1b02d60..2c2d90a39d2fd0762a58ccd54209bfe5eb0df439 100644 (file)
@@ -1,6 +1,6 @@
 /* Support for complaint handling during symbol reading in GDB.
 
-   Copyright (C) 1990-2021 Free Software Foundation, Inc.
+   Copyright (C) 1990-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -21,6 +21,7 @@
 #include "complaints.h"
 #include "command.h"
 #include "gdbcmd.h"
+#include "gdbsupport/selftest.h"
 #include <unordered_map>
 
 /* Map format strings to counters.  */
@@ -74,6 +75,53 @@ complaints_show_value (struct ui_file *file, int from_tty,
                    value);
 }
 
+#if GDB_SELF_TEST
+namespace selftests {
+
+/* Entry point for complaints unit tests.  */
+
+static void
+test_complaints ()
+{
+  std::unordered_map<const char *, int> tmp;
+  scoped_restore reset_counters = make_scoped_restore (&counters, tmp);
+  scoped_restore reset_stop_whining = make_scoped_restore (&stop_whining, 2);
+
+#define CHECK_COMPLAINT(STR, CNT)                                      \
+  do                                                                   \
+    {                                                                  \
+      std::string output;                                              \
+      execute_fn_to_string (output, []() { complaint (STR); }, false); \
+      std::string expected                                             \
+       = _("During symbol reading: ") + std::string (STR "\n");        \
+      SELF_CHECK (output == expected);                                 \
+      SELF_CHECK (counters[STR] == CNT);                               \
+    } while (0)
+
+#define CHECK_COMPLAINT_SILENT(STR, CNT)                               \
+  do                                                                   \
+    {                                                                  \
+      std::string output;                                              \
+      execute_fn_to_string (output, []() { complaint (STR); }, false); \
+      SELF_CHECK (output.empty ());                                    \
+      SELF_CHECK (counters[STR] == CNT);                               \
+    } while (0)
+
+  CHECK_COMPLAINT ("maintenance complaint 0", 1);
+  CHECK_COMPLAINT ("maintenance complaint 0", 2);
+  CHECK_COMPLAINT_SILENT ("maintenance complaint 0", 3);
+  CHECK_COMPLAINT ("maintenance complaint 1", 1);
+  clear_complaints ();
+  CHECK_COMPLAINT ("maintenance complaint 0", 1);
+
+#undef CHECK_COMPLAINT
+#undef CHECK_COMPLAINT_SILENT
+}
+
+
+} // namespace selftests
+#endif /* GDB_SELF_TEST */
+
 void _initialize_complaints ();
 void
 _initialize_complaints ()
@@ -84,4 +132,8 @@ Set max number of complaints about incorrect symbols."), _("\
 Show max number of complaints about incorrect symbols."), NULL,
                            NULL, complaints_show_value,
                            &setlist, &showlist);
+
+#if GDB_SELF_TEST
+  selftests::register_test ("complaints", selftests::test_complaints);
+#endif /* GDB_SELF_TEST */
 }