Patchwork [v2] Add selftests for range_contains and insert_into_bit_range_vector

login
register
mail settings
Submitter Simon Marchi
Date April 7, 2018, 2:40 p.m.
Message ID <20180407144046.20223-1-simon.marchi@polymtl.ca>
Download mbox | patch
Permalink /patch/26638/
State New
Headers show

Comments

Simon Marchi - April 7, 2018, 2:40 p.m.
[Oops, I had some formatting fixes not committed, here's an updated
 version]

I had a patch that converted struct value's VEC into std::vector (as was
done recently by Tom), and had added some selftests to make sure I
didn't mess things up.  I thought that since they're already written I
might as well post them.

gdb/ChangeLog:

	* value.c: Include "selftest.h".
	(test_range_contains): New.
	(check_ranges_vector): New.
	(test_insert_into_bit_range_vector): New.
	(_initialize_values): Register selftests.
---
 gdb/value.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)

Patch

diff --git a/gdb/value.c b/gdb/value.c
index 3d6595f9509d..7101e289d48f 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -41,6 +41,7 @@ 
 #include "user-regs.h"
 #include <algorithm>
 #include "completer.h"
+#include "selftest.h"
 
 /* Definition of a user function.  */
 struct internal_function
@@ -3903,6 +3904,132 @@  isvoid_internal_fn (struct gdbarch *gdbarch,
   return value_from_longest (builtin_type (gdbarch)->builtin_int, ret);
 }
 
+namespace selftests
+{
+static void test_range_contains (void)
+{
+  std::vector<range> ranges;
+  range r;
+
+  /* [10, 14] */
+  r.offset = 10;
+  r.length = 5;
+  ranges.push_back (r);
+
+  /* [20, 24] */
+  r.offset = 20;
+  r.length = 5;
+  ranges.push_back (r);
+
+  /* [2, 6] */
+  SELF_CHECK (!ranges_contain (ranges, 2, 5));
+  /* [9, 13] */
+  SELF_CHECK (ranges_contain (ranges, 9, 5));
+  /* [10, 11] */
+  SELF_CHECK (ranges_contain (ranges, 10, 2));
+  /* [10, 14] */
+  SELF_CHECK (ranges_contain (ranges, 10, 5));
+  /* [13, 18] */
+  SELF_CHECK (ranges_contain (ranges, 13, 6));
+  /* [14, 18] */
+  SELF_CHECK (ranges_contain (ranges, 14, 5));
+  /* [15, 18] */
+  SELF_CHECK (!ranges_contain (ranges, 15, 4));
+  /* [16, 19] */
+  SELF_CHECK (!ranges_contain (ranges, 16, 4));
+  /* [16, 21] */
+  SELF_CHECK (ranges_contain (ranges, 16, 6));
+  /* [21, 21] */
+  SELF_CHECK (ranges_contain (ranges, 21, 1));
+  /* [21, 25] */
+  SELF_CHECK (ranges_contain (ranges, 21, 5));
+  /* [26, 28] */
+  SELF_CHECK (!ranges_contain (ranges, 26, 3));
+}
+
+/* Check that RANGES contains the expected ranges.  EXPECTED_N is the expected
+   length of RANGES.  The variable arguments are pairs of OFFSET/LENGTH
+   corresponding to the expected ranges.  */
+
+static bool check_ranges_vector (const std::vector<range> &ranges,
+				 int expected_n, ...)
+{
+  if (expected_n != ranges.size ())
+    return false;
+
+  va_list vl;
+  va_start (vl, expected_n);
+
+  bool ret = true;
+  for (const range &r : ranges)
+    {
+      LONGEST offset = va_arg (vl, LONGEST);
+      LONGEST length = va_arg (vl, LONGEST);
+
+      if (r.offset != offset || r.length != length)
+	{
+	  ret = false;
+	  break;
+	}
+    }
+
+  va_end (vl);
+
+  return ret;
+}
+
+static void test_insert_into_bit_range_vector (void)
+{
+  std::vector<range> ranges;
+
+  /* [10, 14] */
+  insert_into_bit_range_vector (&ranges, 10, 5);
+  SELF_CHECK (check_ranges_vector (ranges, 1,
+				   10, 5));
+
+  /* [10, 14] */
+  insert_into_bit_range_vector (&ranges, 11, 4);
+  SELF_CHECK (check_ranges_vector (ranges, 1,
+				   10, 5));
+
+  /* [10, 14] [20, 24] */
+  insert_into_bit_range_vector (&ranges, 20, 5);
+  SELF_CHECK (check_ranges_vector (ranges, 2,
+				   10, 5,
+				   20, 5));
+
+  /* [10, 14] [17, 24] */
+  insert_into_bit_range_vector (&ranges, 17, 5);
+  SELF_CHECK (check_ranges_vector (ranges, 2,
+				   10, 5,
+				   17, 8));
+
+  /* [2, 8] [10, 14] [17, 24] */
+  insert_into_bit_range_vector (&ranges, 2, 7);
+  SELF_CHECK (check_ranges_vector (ranges, 3,
+				   2, 7,
+				   10, 5,
+				   17, 8));
+
+  /* [2, 14] [17, 24] */
+  insert_into_bit_range_vector (&ranges, 9, 1);
+  SELF_CHECK (check_ranges_vector (ranges, 2,
+				   2, 13,
+				   17, 8));
+
+  /* [2, 14] [17, 24] */
+  insert_into_bit_range_vector (&ranges, 9, 1);
+  SELF_CHECK (check_ranges_vector (ranges, 2,
+				   2, 13,
+				   17, 8));
+
+  /* [2, 33] */
+  insert_into_bit_range_vector (&ranges, 4, 30);
+  SELF_CHECK (check_ranges_vector (ranges, 1,
+				   2, 32));
+}
+} /* namespace selftests */
+
 void
 _initialize_values (void)
 {
@@ -3954,4 +4081,9 @@  prevents future values, larger than this size, from being allocated."),
 			    set_max_value_size,
 			    show_max_value_size,
 			    &setlist, &showlist);
+#if GDB_SELF_TEST
+  selftests::register_test ("range_contains", selftests::test_range_contains);
+  selftests::register_test ("insert_into_bit_range_vector",
+			    selftests::test_insert_into_bit_range_vector);
+#endif
 }