[PATCH 7/7] [python] API for macros: Add tests.

matt rice
2011-08-24T15:12:02Z

2011-08-23 Matt Rice <ratmice@gmail.com>

PR python/10807
* gdb.python/py-macro.exp: Ditto.
* gdb.python/py-macro.c: New file.
---
gdb/testsuite/gdb.python/py-macro.c | 87 +++++++++
gdb/testsuite/gdb.python/py-macro.exp | 338 +++++++++++++++++++++++++++++++++
2 files changed, 425 insertions(+), 0 deletions(-)
create mode 100644 gdb/testsuite/gdb.python/py-macro.c
create mode 100644 gdb/testsuite/gdb.python/py-macro.exp

diff --git a/gdb/testsuite/gdb.python/py-macro.c b/gdb/testsuite/gdb.python/py-macro.c
new file mode 100644
index 0000000..3397eb5
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-macro.c
@@ -0,0 +1,87 @@
+/* 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. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef DEF_MACROS
+
+ #ifdef ONE
+ #ifdef FOO
+ #undef FOO
+ #endif
+
+ #define FOO "hello"
+ #else
+ #undef FOO
+ #endif
+
+
+ #ifdef TWO
+ #ifdef FOO
+ #undef FOO
+ #endif
+ #define FOO " "
+ #endif
+
+ #ifdef THREE
+ #ifdef FOO
+ #undef FOO
+ #endif
+ #define FOO(a,b)
+ #endif
+
+ #ifdef FOUR
+ #ifdef FOO
+ #undef FOO
+ #endif
+ #define FOO(a) foo = a
+ #endif
+#else
+
+int main (int argc, const char **argv)
+{
+ char *foo;
+
+ #define DEF_MACROS
+ #define ONE
+ #include "py-macro.c"
+ foo = FOO;
+
+ #define TWO
+ #include "py-macro.c"
+ foo = FOO;
+
+ #define THREE
+ #include "py-macro.c"
+ foo = "world"FOO(0,1);
+
+ #undef THREE
+ #include "py-macro.c"
+ foo = FOO;
+
+ #undef TWO
+ #include "py-macro.c"
+ foo = FOO;
+
+ #undef ONE
+ #include "py-macro.c"
+ foo = (char *)0;
+
+ #define FOUR
+ #include "py-macro.c"
+ FOO ("the end.");
+
+ return 0;
+}
+#endif
+
diff --git a/gdb/testsuite/gdb.python/py-macro.exp b/gdb/testsuite/gdb.python/py-macro.exp
new file mode 100644
index 0000000..203e1b4
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-macro.exp
@@ -0,0 +1,338 @@
+# 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. If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests the mechanism
+# exposing macros to Python.
+
+if $tracelevel then {
+ strace $tracelevel
+}
+
+load_lib gdb-python.exp
+
+gdb_start
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+set testfile "py-macro"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+get_compiler_info ${binfile}
+if [test_compiler_info gcc*] {
+ lappend options additional_flags=-g3
+} else {
+ untested ${testfile}.exp
+ return -1
+}
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} $options] } {
+ untested ${testfile}.exp
+ return -1
+}
+
+# Start with a fresh gdb.
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+
+gdb_py_test_multiple "setup macro test objects" \
+ "python" "" \
+ "def find_objfile():" "" \
+ " for objfile in gdb.objfiles():" "" \
+ " if objfile.filename == \"$binfile\":" "" \
+ " print objfile.filename" "" \
+ " return objfile" "" \
+ "def here_macro(name):" "" \
+ " return gdb.decode_line(\"*\$pc\")\[1\]\[0\].macro_named(name)" "" \
+ "macros = list()" "" \
+ "end" ""
+
+gdb_test "python objfile = find_objfile()" "$binfile" "found objfile"
+gdb_test "python print objfile.symtabs()" \
+ "\\\[\\\]" "symtabs is empty"
+
+if ![runto_main] {
+ untested ${testfile}.exp
+ return -1
+}
+
+gdb_py_test_silent_cmd "python m = objfile.symtabs()\[0\].macros()" "get all macros" 1
+
+gdb_py_test_silent_cmd "python include_guard = here_macro(\"DEF_MACROS\")" \
+ "get guard macro #1" \
+ 1
+
+gdb_test "python print include_guard.is_valid()" \
+ "True" \
+ "guard is valid"
+
+gdb_test "python print include_guard.name() == \"DEF_MACROS\"" \
+ "True" \
+ "guard name"
+
+gdb_test "python print include_guard.is_function_like() == False" \
+ "True" \
+ "guard isnt function like"
+
+gdb_test "python print include_guard.argv() == None" \
+ "True" \
+ "guard has no args"
+
+gdb_test "python print 'foo' + include_guard.definition() + 'bar'" \
+ "foobar" \
+ "guard definition is empty"
+
+gdb_test "python print include_guard.include_trail()\[0\]\[0\]" \
+ "$srcfile.*" \
+ "guard trail has srcfile"
+
+#<gdb.Macro include_trail=[('./gdb.python/py-macro.c', 8), ('./gdb.python/py-macro.c', 43)] FOO="hello">
+gdb_test "python print include_guard" \
+"<gdb\.Macro DEF_MACROS include_trail=\\\[\\\('$srcdir/$subdir/$srcfile', \[0-9\]+\\\).*\\\]>" \
+"guard string rep"
+
+gdb_py_test_silent_cmd "python macros.append(include_guard)" \
+ "add to macros list" \
+ 0
+
+gdb_py_test_silent_cmd "python foo = here_macro(\"FOO\")" \
+ "get FOO macro #1" \
+ 1
+
+gdb_test "python print foo.is_valid()" \
+ "True" \
+ "FOO macro #1 is valid"
+
+gdb_test "python print foo.name()" \
+ "FOO" \
+ "FOO macro #1 name"
+
+gdb_test "python print foo.is_function_like()" \
+ "False" \
+ "FOO macro #1 isnt function like"
+
+gdb_test "python print foo.argv()" \
+ "None" \
+ "FOO macro #1 has no args"
+
+gdb_test "python print foo.definition()" \
+ "\"hello\"" \
+ "FOO macro #1 definition is \"hello\""
+
+gdb_test "python print include_guard.include_trail()\[0\]\[0\]" \
+ "$srcfile.*" \
+ "FOO macro #1 has srcfile"
+
+gdb_test "python print foo" \
+"<gdb\.Macro FOO=\"hello\" include_trail=\\\[\\\('$srcdir/$subdir/$srcfile', \[0-9\]+\\\).*\\\]>" \
+"FOO macro #1 string rep"
+
+gdb_py_test_silent_cmd "python macros.append(foo)" \
+ "add to macros list" \
+ 0
+
+gdb_test "next" "" ""
+gdb_py_test_silent_cmd "python foo = here_macro(\"FOO\")" \
+ "get FOO macro #2" \
+ 1
+
+gdb_test "python print foo.is_valid()" \
+ "True" \
+ "FOO macro #2 is valid"
+
+gdb_test "python print foo.name()" \
+ "FOO" \
+ "FOO macro #2 name"
+
+gdb_test "python print foo.is_function_like()" \
+ "False" \
+ "FOO macro #2 isnt function like"
+
+gdb_test "python print foo.argv()" \
+ "None" \
+ "FOO macro #2 has no args"
+
+gdb_test "python print foo.definition()" \
+ "\" \"" \
+ "FOO macro #2 definition is \" \""
+
+gdb_test "python print foo.include_trail()\[0\]\[0\]" \
+ "$srcfile.*" \
+ "FOO macro #2 has srcfile"
+
+gdb_test "python print foo" \
+"<gdb\.Macro FOO=\" \" include_trail=\\\[\\\('$srcdir/$subdir/$srcfile', \[0-9\]+\\\).*\\\]>" \
+"FOO macro #2 string rep"
+
+gdb_py_test_silent_cmd "python macros.append(foo)" \
+ "add to macros list" \
+ 0
+
+gdb_test "next" "" ""
+gdb_py_test_silent_cmd "python foo = here_macro(\"FOO\")" \
+ "get FOO macro #3" \
+ 1
+gdb_test "python print foo.is_valid()" \
+ "True" \
+ "FOO macro #3 is valid"
+
+gdb_test "python print foo.name()" \
+ "FOO" \
+ "FOO macro #3 name"
+
+gdb_test "python print foo.is_function_like()" \
+ "True" \
+ "FOO macro #3 is function like"
+
+gdb_test "python print foo.argv()" \
+ "\\\['a', 'b'\\\]" \
+ "FOO macro #3 has args"
+
+gdb_test "python print 'foo' + foo.definition() + 'bar'" \
+ "foobar" \
+ "FOO macro #3 definition is empty"
+
+gdb_test "python print include_guard.include_trail()\[0\]\[0\]" \
+ "$srcfile.*" \
+ "FOO macro #3 has srcfile"
+
+gdb_test "python print foo" \
+"<gdb\.Macro FOO\\\(a, b\\\) include_trail=\\\[\\\('$srcdir/$subdir/$srcfile', \[0-9\]+\\\).*\\\]>" \
+"FOO macro #3 string rep"
+
+gdb_py_test_silent_cmd "python macros.append(foo)" \
+ "add to macros list" \
+ 0
+gdb_test "next" "" ""
+gdb_test "next" "" ""
+gdb_test "next" "" ""
+gdb_py_test_silent_cmd "python foo = here_macro(\"FOO\")" \
+ "get FOO macro #4" \
+ 1
+gdb_test "python print foo" \
+ "None" \
+ "there is no Foo macro #4"
+
+gdb_test "next" "" ""
+gdb_py_test_silent_cmd "python foo = here_macro(\"FOO\")" \
+ "get FOO macro #5" \
+ 1
+
+gdb_test "python print foo.is_valid()" \
+ "True" \
+ "FOO macro #5 is valid"
+
+gdb_test "python print foo.name()" \
+ "" \
+ "FOO macro #5 name"
+
+gdb_test "python print foo.is_function_like()" \
+ "True" \
+ "FOO macro #5 is function like"
+
+gdb_test "python print foo.argv()" \
+ "\\\['a'\\\]" \
+ "FOO macro #5 has args"
+
+gdb_test "python print foo.definition()" \
+ "foo = a" \
+ "FOO macro #5 definition ok"
+
+gdb_test "python print include_guard.include_trail()\[0\]\[0\]" \
+ "$srcfile.*" \
+ "FOO macro #5 has srcfile"
+
+gdb_test "python print foo" \
+"<gdb\.Macro FOO\\\(a\\\)=foo = a include_trail=\\\[\\\('$srcdir/$subdir/$srcfile', \[0-9\]+\\\).*\\\]>" \
+"FOO macro #5 string rep"
+gdb_py_test_silent_cmd "python macros.append(foo)" \
+ "add to macros list" \
+ 0
+
+# this could find some ref count bugs if they were to happen for a singleton.
+gdb_py_test_multiple "run macros a couple of times" \
+ "python" "" \
+ "c = 0" "" \
+ "while c > 3:" "" \
+ " c = c + 1" "" \
+ " for macro in objfile.symtabs()\[0\].macros():" "" \
+ " macro.name()" "" \
+ " macro.is_function_like()" "" \
+ " macro.argv()" "" \
+ " macro.include_path()" "" \
+ " macro.definition()" "" \
+ " str(macro)" "" \
+ " hash(macro)" "" \
+ "end" ""
+
+gdb_py_test_multiple "find selected macros in the big list of macros" \
+ "python" "" \
+ "set1 = set(objfile.symtabs()\[0\].macros())" "" \
+ "set2 = set(macros)" "" \
+ "intersect = (set1 & set2)" "" \
+ "set3 = set(filter(lambda(x): x.name() == \"FOO\", objfile.symtabs()\[0\].macros()))" "" \
+ "intersect2 = (set3 & set2)" "" \
+ "print \"set2\", map(lambda(x): x.name(), set2)" "" \
+ "print \"set3\", map(lambda(x): x.name(), set3)" "" \
+ "print \"intersect\", map(lambda(x): x.name(), intersect)" "" \
+ "print \"intersect2\", map(lambda(x): x.name(), intersect2)" "" \
+ "print \"set2 - intersect\", map(lambda(x): x.name(), set2 - intersect)" "" \
+ "print \"intersect - set2\", map(lambda(x): x.name(), intersect - set2)" "" \
+ "print \"set2 - intersect2\", map(lambda(x): x.name(), set2 - intersect2)" "" \
+ "print \"intersect2 - set2\", map(lambda(x): x.name(), intersect2 - set2)" "" \
+ "end" ""
+
+gdb_test "python print len(set2)" "5" "macro set length 5"
+gdb_test "python print len(set1) > len(set2)" "True" "all macros is longer."
+gdb_test "python print set1 >= set2" "True" "macro set2 is a subset"
+gdb_test "python print len(intersect - set2), len(set2 - intersect)" \
+ "0 0" \
+ "macro set intersection equality"
+gdb_test "python print len(intersect2 - set2)" \
+ "0" \
+ "macro set intersection equality 2"
+gdb_test "python print len(set2 - intersect2), (set2 - intersect2).pop()" \
+ "1 <gdb.Macro DEF_MACROS.*>" \
+ "macro set intersection 3"
+
+gdb_unload
+# test is_valid()
+gdb_test "python print include_guard.is_valid()" "False" \
+ "guard macro is invalid after unload"
+
+gdb_test "python print foo.is_valid()" "False" \
+ "foo macro is invalid after unload"
+
+# test individual method's to make sure they throw exceptions.
+gdb_test "python print foo.argv()" "Macro is invalid.*" \
+ "foo argv method is invalid after unload"
+
+gdb_test "python print foo.definition()" "Macro is invalid.*" \
+ "foo definition method is invalid after unload"
+
+gdb_test "python print foo.include_trail()" "Macro is invalid.*" \
+ "foo include_trail method is invalid after unload"
+
+gdb_test "python print foo.is_function_like()" "Macro is invalid.*" \
+ "foo is_function_like method is invalid after unload"
+
+gdb_test "python print foo.name()" "Macro is invalid.*" \
+ "foo name method is invalid after unload"
+
+gdb_test "python print str(foo)" "Macro is invalid.*" \
+ "str(foo) method is invalid after unload"
+
--
1.7.4.4