RFC: add a testsuite for libstdc++ pretty-printers

Tom Tromey tromey@redhat.com
Wed Jan 26 22:11:00 GMT 2011


This patch adds a minimal test suite for the libstdc++ pretty-printers.

I based it on Alexandre's gcc-gdb-test.exp code from the gcc test suite.
However, I needed somewhat different functionality, so I modified it in
a few ways.

Adding a new test is very simple: modify simple.cc and add a dg-final
marker to pretty-print the relevant objects.

Let me know what you think.

Tom

2011-01-26  Tom Tromey  <tromey@redhat.com>

	* testsuite/libstdc++-pp/simple.cc: New file.
	* testsuite/lib/gdb-test.exp: New file.
	* testsuite/libstdc++-pp/pp.exp: New file.

Index: testsuite/libstdc++-pp/pp.exp
===================================================================
--- testsuite/libstdc++-pp/pp.exp	(revision 0)
+++ testsuite/libstdc++-pp/pp.exp	(revision 0)
@@ -0,0 +1,46 @@
+#   Copyright (C) 2011 Free Software Foundation, Inc.
+
+# 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; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+load_lib gdb-test.exp
+
+dg-init
+v3-build_support
+
+global GDB
+if ![info exists ::env(GUALITY_GDB_NAME)] {
+    if [info exists GDB] {
+	set guality_gdb_name "$GDB"
+    } else {
+	set guality_gdb_name "[transform gdb]"
+    }
+    setenv GUALITY_GDB_NAME "$guality_gdb_name"
+}
+
+# This can be used to keep the .exe around.  dg-test has an option for
+# this but there is no way to pass it through dg-runtest.
+global dg-interpreter-batch-mode
+set dg-interpreter-batch-mode 1
+
+global DEFAULT_CXXFLAGS
+global PCH_CXXFLAGS
+dg-runtest [lsort [glob $srcdir/$subdir/*.cc]] \
+  "" "$DEFAULT_CXXFLAGS $PCH_CXXFLAGS"
+
+if [info exists guality_gdb_name] {
+    unsetenv GUALITY_GDB_NAME
+}
+
+dg-finish
Index: testsuite/libstdc++-pp/simple.cc
===================================================================
--- testsuite/libstdc++-pp/simple.cc	(revision 0)
+++ testsuite/libstdc++-pp/simple.cc	(revision 0)
@@ -0,0 +1,87 @@
+// { dg-do run }
+// { dg-options "-g" }
+
+// Copyright (C) 2011 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library 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, or (at your option)
+// any later version.
+
+// This library 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 library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string>
+#include <deque>
+#include <bitset>
+#include <iostream>
+#include <list>
+#include <map>
+
+template<class T>
+void
+placeholder(const T &s)
+{
+  std::cout << s;
+}
+
+template<class T, class S>
+void
+placeholder(const std::pair<T,S> &s)
+{
+  std::cout << s.first;
+}
+
+template<class T>
+void
+use(const T &container)
+{
+  for (typename T::const_iterator i = container.begin();
+       i != container.end();
+       ++i)
+    placeholder(*i);
+}
+
+int
+main()
+{
+  std::string str = "zardoz";
+// { dg-final { note-test str "\"zardoz\"" } }
+
+  std::bitset<10> bs;
+  bs[0] = 1;
+  bs[5] = 1;
+  bs[7] = 1;
+// { dg-final { note-test bs {std::bitset = {[0] = 1, [5] = 1, [7] = 1}} } }
+
+  std::deque<std::string> deq;
+  deq.push_back("one");
+  deq.push_back("two");
+// { dg-final { note-test deq {std::deque with 2 elements = {"one", "two"}} } }
+
+  std::list<std::string> lst;
+  lst.push_back("one");
+  lst.push_back("two");
+// { dg-final { note-test lst {std::list = {[0] = "one", [1] = "two"}} } }
+
+  std::map<std::string, int> mp;
+  mp["zardoz"] = 23;
+// { dg-final { note-test mp {std::map with 1 elements = {["zardoz"] = 23}} } }
+
+  placeholder(str); // Mark SPOT
+  std::cout << bs;
+  use(deq);
+  use(lst);
+  use(mp);
+
+  return 0;
+}
+
+// { dg-final { gdb-test SPOT } }
Index: testsuite/lib/gdb-test.exp
===================================================================
--- testsuite/lib/gdb-test.exp	(revision 0)
+++ testsuite/lib/gdb-test.exp	(revision 0)
@@ -0,0 +1,162 @@
+#   Copyright (C) 2009, 2011 Free Software Foundation, Inc.
+
+# 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 GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+global gdb_tests
+set gdb_tests {}
+
+# Scan a file for markers and fill in the gdb_marker array for that
+# file.  Any error in this script is simply thrown; errors here are
+# programming errors in the test suite itself and should not be
+# caught.
+proc scan_gdb_markers {filename} {
+    global gdb_markers
+
+    if {[info exists gdb_markers($filename,-)]} {
+	return
+    }
+
+    set fd [open $filename]
+    set lineno 1
+    while {! [eof $fd]} {
+	set line [gets $fd]
+	if {[regexp -- "Mark (\[a-zA-Z0-9\]+)" $line ignore marker]} {
+	    set gdb_markers($filename,$marker) $lineno
+	}
+	incr lineno
+    }
+    close $fd
+
+    set gdb_markers($filename,-) {}
+}
+
+# Find a marker in a source file, and return the marker's line number.
+proc get_line_number {filename marker} {
+    global gdb_markers
+
+    scan_gdb_markers $filename
+    return $gdb_markers($filename,$marker)
+}
+
+# Make note of a gdb test.  A test consists of a variable name and an
+# expected result.
+proc note-test {var result} {
+    global gdb_tests
+
+    lappend gdb_tests $var $result
+}
+
+# Utility for testing variable values using gdb, invoked via dg-final.
+# Tests all tests indicated by note-test.
+#
+# Argument 0 is the marker on which to put a breakpoint
+# Argument 2 handles expected failures and the like
+proc gdb-test { marker {selector {}} } {
+    if { ![isnative] || [is_remote target] } { return }
+
+    if {[string length $selector] > 0} {
+	switch [dg-process-target $selector] {
+	    "S" { }
+	    "N" { return }
+	    "F" { setup_xfail "*-*-*" }
+	    "P" { }
+	}
+    }
+
+    # This assumes that we are three frames down from dg-test, and that
+    # it still stores the filename of the testcase in a local variable "name".
+    # A cleaner solution would require a new DejaGnu release.
+    upvar 2 name testcase
+    upvar 2 prog prog
+
+    set line [get_line_number $prog $marker]
+
+    set gdb_name $::env(GUALITY_GDB_NAME)
+    set testname "$testcase"
+    set output_file "[file rootname [file tail $prog]].exe"
+    set cmd_file "[file rootname [file tail $prog]].gdb"
+
+    global srcdir
+    set pycode [file join $srcdir .. python libstdcxx v6 printers.py]
+
+    global gdb_tests
+
+    set fd [open $cmd_file "w"]
+    puts $fd "source $pycode"
+    puts $fd "python register_libstdcxx_printers(None)"
+    puts $fd "break $line"
+    puts $fd "run"
+
+    set count 0
+    foreach {var result} $gdb_tests {
+	puts $fd "print $var"
+	incr count
+	set gdb_var($count) $var
+	set gdb_expected($count) $result
+    }
+    set gdb_tests {}
+
+    puts $fd "quit"
+    close $fd
+
+    send_log "Spawning: $gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file\n"
+    set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file"]
+    if { $res < 0 || $res == "" } {
+	unsupported "$testname"
+	return
+    }
+
+    remote_expect target [timeout_value] {
+	-re {^\$([0-9]+) = ([^\n\r]*)[\n\r]+} {
+	    set num $expect_out(1,string)
+	    set first $expect_out(2,string)
+	    if { ![string compare $first $gdb_expected($num)] } {
+		pass "$testname print $gdb_var($num)"
+	    } else {
+		fail "$testname print $gdb_var($num)"
+		verbose "     got =>$first<="
+		verbose "expected =>$gdb_expected($num)<="
+	    }
+
+	    if {$num == $count} {
+		remote_close target
+		return
+	    } else {
+		exp_continue
+	    }
+	}
+
+	-re {Python scripting is not supported in this copy of GDB.[\n\r]+} {
+	    unsupported "$testname"
+	    remote_close target
+	    return
+	}
+
+	-re {^[^$][^\n\r]*[\n\r]+} {
+	    verbose "skipping: $expect_out(buffer)"
+	    exp_continue
+	}
+
+	timeout {
+	    unsupported "$testname"
+	    remote_close target
+	    return
+	}
+    }
+
+    remote_close target
+    unsupported "$testname"
+    return
+}



More information about the Gcc-patches mailing list