[gdb] Add selftest for tui_setup_io

Add a selftest for the problem described in commit b171f68e945 ("[gdb/tui]
Make tui_setup_io more robust").

Reviewed-By: Guinevere Larsen <guinevere@redhat.com>
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index 4b4bc81..642b88e 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -46,6 +46,7 @@
 #include "pager.h"
 #include "gdbsupport/gdb-checked-static-cast.h"
 #include "logging-file.h"
+#include "gdbsupport/selftest.h"
 
 /* This redefines CTRL if it is not already defined, so it must come
    after terminal state related include files like <term.h> and
@@ -1328,3 +1329,36 @@ tui_getc (FILE *fp)
       return 0;
     }
 }
+
+#if GDB_SELF_TEST
+namespace selftests {
+namespace tui {
+namespace io {
+
+static void
+run_tests ()
+{
+  if (!tui_active)
+    {
+      /* Calling tui_setup_io (0) when tui is disabled should have no effect.  */
+      tui_setup_io (0);
+
+      /* If the output streams are reduced to nullptrs, then the self-test
+	 infrastructure will crash when trying to report these failures.  */
+      SELF_CHECK (*redirectable_stdout () != nullptr);
+      SELF_CHECK (*redirectable_stderr () != nullptr);
+      SELF_CHECK (*redirectable_stdlog () != nullptr);
+    }
+}
+
+} /* namespace io */
+} /* namespace tui */
+} /* namespace selftests */
+#endif /* GDB_SELF_TEST */
+
+INIT_GDB_FILE (tui_io)
+{
+#if GDB_SELF_TEST
+  selftests::register_test ("tui-io", selftests::tui::io::run_tests);
+#endif
+}