public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] [gdb/testsuite] Add cond_wrap proc
@ 2022-10-14 11:09 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2022-10-14 11:09 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1e4be05b752cc94019ed12d6d305e13e55530724

commit 1e4be05b752cc94019ed12d6d305e13e55530724
Author: Tom de Vries <tdevries@suse.de>
Date:   Fri Oct 14 13:09:50 2022 +0200

    [gdb/testsuite] Add cond_wrap proc
    
    Add a new proc cond_wrap, that can be used to replace the repetitive:
    ...
        if { $cond } {
            wrap {
                <body>
            }
        } else {
            <body>
        }
    ...
    with the shorter:
    ...
        cond_wrap $cond wrap {
            <body>
        }
    ...
    
    Tested on x86_64-linux.

Diff:
---
 gdb/testsuite/gdb.testsuite/cond-wrap.exp | 43 +++++++++++++++++++++++++++++++
 gdb/testsuite/lib/gdb.exp                 | 20 ++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/gdb/testsuite/gdb.testsuite/cond-wrap.exp b/gdb/testsuite/gdb.testsuite/cond-wrap.exp
new file mode 100644
index 00000000000..7feb88bd102
--- /dev/null
+++ b/gdb/testsuite/gdb.testsuite/cond-wrap.exp
@@ -0,0 +1,43 @@
+# Copyright 2022 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/>.
+
+# The purpose of this test-case is to test the cond_wrap proc.
+
+# Proc that temporarily sets global variable a to 6, and executes BODY.
+# Note that normally a wrapper needs to guarantee that the restore is executed
+# even if executing body throws an error, but we don't need to bother with this
+# for this test-case.
+proc wrap { body } {
+    global a
+
+    # Save a.
+    set save_a $a
+
+    set a 6
+    uplevel 1 $body
+
+    # Restore a.
+    set a $save_a
+}
+
+# Set initial value of global variable a.
+set a 5
+
+# Verify values of global variable a for cond == 0 and cond == 1.
+foreach_with_prefix cond {0 1} {
+    cond_wrap $cond wrap {
+	gdb_assert {[expr $a - $cond] == 5} "value in conditional wrap"
+    }
+    gdb_assert {$a == 5} "value after conditional wrap"
+}
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 61bc060b2f7..3049a7392a4 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -25,6 +25,26 @@ if {$tool == ""} {
     exit 2
 }
 
+# Execute BODY, if COND wrapped in proc WRAP.
+# Instead of writing the verbose and repetitive:
+#   if { $cond } {
+#     wrap $body
+#   } else {
+#     $body
+#   }
+# we can use instead:
+#   cond_wrap $cond wrap $body
+
+proc cond_wrap { cond wrap body } {
+    if { $cond } {
+	$wrap {
+	    uplevel 1 $body
+	}
+    } else {
+	uplevel 1 $body
+    }
+}
+
 # Add VAR_ID=VAL to ENV_VAR, unless ENV_VAR already contains a VAR_ID setting.
 
 proc set_sanitizer_default { env_var var_id val } {

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-14 11:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-14 11:09 [binutils-gdb] [gdb/testsuite] Add cond_wrap proc Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).