diff mbox

[3/9] selftest.h: add temp_override fixture

Message ID 1473381053-18817-4-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm Sept. 9, 2016, 12:30 a.m. UTC
We have a lot of global state in our code.  Ideally we'd reduce the
amount of such global state, but a prerequisite for sane refactoring
is having automated testing in place to ensure that the refactoring
doesn't break anything.

However, the global state itself makes it hard to write such automated
testing.

To break this Catch-22, this patch introduces a class temp_override,
for temporarily assigning a value to a global variable, saving the old
value, and then restoring that old value in a dtor.

gcc/ChangeLog:
	* selftest.h (selftest::temp_override): New class.
---
 gcc/selftest.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Trevor Saunders Sept. 14, 2016, 10:04 p.m. UTC | #1
On Thu, Sep 08, 2016 at 08:30:47PM -0400, David Malcolm wrote:
> We have a lot of global state in our code.  Ideally we'd reduce the
> amount of such global state, but a prerequisite for sane refactoring
> is having automated testing in place to ensure that the refactoring
> doesn't break anything.
> 
> However, the global state itself makes it hard to write such automated
> testing.
> 
> To break this Catch-22, this patch introduces a class temp_override,
> for temporarily assigning a value to a global variable, saving the old
> value, and then restoring that old value in a dtor.

I expect there are uses for this outside of selftests, so I'd suggest
making it more general.  I guess the name is good enough, I would have
done auto_restore, but this is fine.  You just need to put it somewhere
generally accessible though I'm not sure what that would be.

Trev
Jeff Law Sept. 16, 2016, 8:30 p.m. UTC | #2
On 09/08/2016 06:30 PM, David Malcolm wrote:
> We have a lot of global state in our code.  Ideally we'd reduce the
> amount of such global state, but a prerequisite for sane refactoring
> is having automated testing in place to ensure that the refactoring
> doesn't break anything.
>
> However, the global state itself makes it hard to write such automated
> testing.
>
> To break this Catch-22, this patch introduces a class temp_override,
> for temporarily assigning a value to a global variable, saving the old
> value, and then restoring that old value in a dtor.
>
> gcc/ChangeLog:
> 	* selftest.h (selftest::temp_override): New class.
When you need it, this is fine.

jeff
diff mbox

Patch

diff --git a/gcc/selftest.h b/gcc/selftest.h
index e5f5c60..4c50217 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -153,6 +153,35 @@  for_each_line_table_case (void (*testcase) (const line_table_case &));
 
 extern char *read_file (const location &loc, const char *path);
 
+/* A fixture for temporarily overriding a global variable with a new
+   value.  The original value of the variable is captured in the ctor,
+   and restored in the dtor.  */
+
+template <typename T>
+class temp_override
+{
+ public:
+  temp_override (T& var, T new_value)
+  : m_var (var),
+    /* Record the current value of VAR.  */
+    m_old_value (var)
+  {
+    /* Set the var to the new value.  */
+    m_var = new_value;
+  }
+
+  ~temp_override ()
+  {
+    /* Restore the value of the variable to that stored in the
+       ctor.  */
+    m_var = m_old_value;
+  }
+
+ private:
+  T& m_var;
+  T m_old_value;
+};
+
 /* Declarations for specific families of tests (by source file), in
    alphabetical order.  */
 extern void bitmap_c_tests ();