diff mbox series

Add selftest for vec::reverse

Message ID 1507306833-12644-1-git-send-email-dmalcolm@redhat.com
State New
Headers show
Series Add selftest for vec::reverse | expand

Commit Message

David Malcolm Oct. 6, 2017, 4:20 p.m. UTC
Martin: I noticed that your switch expansion patch added a
  vec::reverse ()
method.  Here's a proposed selftest for it, mostly to verify
that it handles even vs odd lengths (which it does).

Only lightly tested; hope this is useful.
Dave

gcc/ChangeLog:
	* vec.c (selftest::test_reverse): New function.
	(selftest::vec_c_tests): Call it.
---
 gcc/vec.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

Comments

Martin Liška Dec. 29, 2017, 2:39 p.m. UTC | #1
On 10/06/2017 06:20 PM, David Malcolm wrote:
> Martin: I noticed that your switch expansion patch added a
>   vec::reverse ()
> method.  Here's a proposed selftest for it, mostly to verify
> that it handles even vs odd lengths (which it does).
> 
> Only lightly tested; hope this is useful.
> Dave

Hi.

I like it! Do you want to commit it before there will be
a consumer of the function?

Martin

> 
> gcc/ChangeLog:
> 	* vec.c (selftest::test_reverse): New function.
> 	(selftest::vec_c_tests): Call it.
> ---
>  gcc/vec.c | 38 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 38 insertions(+)
> 
> diff --git a/gcc/vec.c b/gcc/vec.c
> index d612703..5d70973 100644
> --- a/gcc/vec.c
> +++ b/gcc/vec.c
> @@ -359,6 +359,43 @@ test_qsort ()
>    ASSERT_EQ (10, v.length ());
>  }
>  
> +/* Verify that vec::reverse works correctly.  */
> +
> +static void
> +test_reverse ()
> +{
> +  /* Reversing an empty vec ought to be a no-op.  */
> +  {
> +    auto_vec <int> v;
> +    ASSERT_EQ (0, v.length ());
> +    v.reverse ();
> +    ASSERT_EQ (0, v.length ());
> +  }
> +
> +  /* Verify reversing a vec with even length.  */
> +  {
> +    auto_vec <int> v;
> +    safe_push_range (v, 0, 4);
> +    v.reverse ();
> +    ASSERT_EQ (3, v[0]);
> +    ASSERT_EQ (2, v[1]);
> +    ASSERT_EQ (1, v[2]);
> +    ASSERT_EQ (0, v[3]);
> +    ASSERT_EQ (4, v.length ());
> +  }
> +
> +  /* Verify reversing a vec with odd length.  */
> +  {
> +    auto_vec <int> v;
> +    safe_push_range (v, 0, 3);
> +    v.reverse ();
> +    ASSERT_EQ (2, v[0]);
> +    ASSERT_EQ (1, v[1]);
> +    ASSERT_EQ (0, v[2]);
> +    ASSERT_EQ (3, v.length ());
> +  }
> +}
> +
>  /* Run all of the selftests within this file.  */
>  
>  void
> @@ -374,6 +411,7 @@ vec_c_tests ()
>    test_unordered_remove ();
>    test_block_remove ();
>    test_qsort ();
> +  test_reverse ();
>  }
>  
>  } // namespace selftest
>
David Malcolm Dec. 29, 2017, 5:09 p.m. UTC | #2
On Fri, 2017-12-29 at 15:39 +0100, Martin Liška wrote:
> On 10/06/2017 06:20 PM, David Malcolm wrote:
> > Martin: I noticed that your switch expansion patch added a
> >   vec::reverse ()
> > method.  Here's a proposed selftest for it, mostly to verify
> > that it handles even vs odd lengths (which it does).
> > 
> > Only lightly tested; hope this is useful.
> > Dave
> 
> Hi.
> 
> I like it! Do you want to commit it before there will be
> a consumer of the function?
> 
> Martin

Looks like vec::reverse isn't in trunk yet.  I don't have a strong
opinion on whether vec::reverse should be added without a "real"
consumer, or added when the consumer is added, but feel free to add my
selftest patch into whichever patch adds vec::reverse.
diff mbox series

Patch

diff --git a/gcc/vec.c b/gcc/vec.c
index d612703..5d70973 100644
--- a/gcc/vec.c
+++ b/gcc/vec.c
@@ -359,6 +359,43 @@  test_qsort ()
   ASSERT_EQ (10, v.length ());
 }
 
+/* Verify that vec::reverse works correctly.  */
+
+static void
+test_reverse ()
+{
+  /* Reversing an empty vec ought to be a no-op.  */
+  {
+    auto_vec <int> v;
+    ASSERT_EQ (0, v.length ());
+    v.reverse ();
+    ASSERT_EQ (0, v.length ());
+  }
+
+  /* Verify reversing a vec with even length.  */
+  {
+    auto_vec <int> v;
+    safe_push_range (v, 0, 4);
+    v.reverse ();
+    ASSERT_EQ (3, v[0]);
+    ASSERT_EQ (2, v[1]);
+    ASSERT_EQ (1, v[2]);
+    ASSERT_EQ (0, v[3]);
+    ASSERT_EQ (4, v.length ());
+  }
+
+  /* Verify reversing a vec with odd length.  */
+  {
+    auto_vec <int> v;
+    safe_push_range (v, 0, 3);
+    v.reverse ();
+    ASSERT_EQ (2, v[0]);
+    ASSERT_EQ (1, v[1]);
+    ASSERT_EQ (0, v[2]);
+    ASSERT_EQ (3, v.length ());
+  }
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -374,6 +411,7 @@  vec_c_tests ()
   test_unordered_remove ();
   test_block_remove ();
   test_qsort ();
+  test_reverse ();
 }
 
 } // namespace selftest