diff mbox

[02/17] Add test-bitmap.c to gcc/unittests

Message ID 1433949898-22033-3-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm June 10, 2015, 3:24 p.m. UTC
gcc/unittests/ChangeLog:
	* test-bitmap.c: New file.
---
 gcc/unittests/test-bitmap.c | 117 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)
 create mode 100644 gcc/unittests/test-bitmap.c

Comments

Jeff Law June 23, 2015, 7:14 p.m. UTC | #1
On 06/10/2015 09:24 AM, David Malcolm wrote:
> gcc/unittests/ChangeLog:
> 	* test-bitmap.c: New file.
> ---
>   gcc/unittests/test-bitmap.c | 117 ++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 117 insertions(+)
>   create mode 100644 gcc/unittests/test-bitmap.c
>
> diff --git a/gcc/unittests/test-bitmap.c b/gcc/unittests/test-bitmap.c
> new file mode 100644
> index 0000000..38adff3
> --- /dev/null
> +++ b/gcc/unittests/test-bitmap.c
So...  We have a long standing issue that we can't iterate on a bitmap 
that is changing.  I can't recall if it's bits going from set to unset 
that causes a problem or vice-versa or both.

There's been talk of putting some sanity checking in the bitmap code to 
detect this error.  A unit testing framework of this nature would be a 
great way to verify that sanity checking worked.

More generally a unit testing framework would allow us to build a suite 
that does a positive test on our sanity checking.  ie, set up the 
invalid state and see if the sanity checking bits complain -- things we 
can't do from a source file.

Anyway, the test itself seems like a reasonable start, we just have to 
settle the framework issues.


Jeff
diff mbox

Patch

diff --git a/gcc/unittests/test-bitmap.c b/gcc/unittests/test-bitmap.c
new file mode 100644
index 0000000..38adff3
--- /dev/null
+++ b/gcc/unittests/test-bitmap.c
@@ -0,0 +1,117 @@ 
+/* Unit tests for GCC's bitmap-handling.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC 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, or (at your option) any later
+version.
+
+GCC 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 GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include <gtest/gtest.h>
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "opts.h"
+#include "bitmap.h"
+
+namespace {
+
+/* Freshly-created bitmaps ought to be empty.  */
+
+TEST (bitmap_test, gc_alloc)
+{
+  bitmap b = bitmap_gc_alloc ();
+  EXPECT_TRUE (bitmap_empty_p (b));
+}
+
+/* Verify bitmap_set_range.  */
+
+TEST (bitmap_test, set_range)
+{
+  bitmap b = bitmap_gc_alloc ();
+  EXPECT_TRUE (bitmap_empty_p (b));
+
+  bitmap_set_range (b, 7, 5);
+  EXPECT_FALSE (bitmap_empty_p (b));
+  EXPECT_EQ (5, bitmap_count_bits (b));
+
+  /* Verify bitmap_bit_p at the boundaries.  */
+  EXPECT_FALSE (bitmap_bit_p (b, 6));
+  EXPECT_TRUE (bitmap_bit_p (b, 7));
+  EXPECT_TRUE (bitmap_bit_p (b, 11));
+  EXPECT_FALSE (bitmap_bit_p (b, 12));
+}
+
+/* Verify splitting a range into two pieces using bitmap_clear_bit.  */
+
+TEST (bitmap_test, clear_bit_in_middle)
+{
+  bitmap b = bitmap_gc_alloc ();
+
+  /* Set b to [100..200].  */
+  bitmap_set_range (b, 100, 100);
+  EXPECT_EQ (100, bitmap_count_bits (b));
+
+  /* Clear a bit in the middle.  */
+  bool changed = bitmap_clear_bit (b, 150);
+  EXPECT_TRUE (changed);
+  EXPECT_EQ (99, bitmap_count_bits (b));
+  EXPECT_TRUE (bitmap_bit_p (b, 149));
+  EXPECT_FALSE (bitmap_bit_p (b, 150));
+  EXPECT_TRUE (bitmap_bit_p (b, 151));
+}
+
+/* Verify bitmap_copy.  */
+
+TEST (bitmap_test, copying)
+{
+  bitmap src = bitmap_gc_alloc ();
+  bitmap_set_range (src, 40, 10);
+
+  bitmap dst = bitmap_gc_alloc ();
+  EXPECT_FALSE (bitmap_equal_p (src, dst));
+  bitmap_copy (dst, src);
+  EXPECT_TRUE (bitmap_equal_p (src, dst));
+
+  /* Verify that we can make them unequal again...  */
+  bitmap_set_range (src, 70, 5);
+  EXPECT_FALSE (bitmap_equal_p (src, dst));
+
+  /* ...and that changing src after the copy didn't affect
+     the other: */
+  EXPECT_FALSE (bitmap_bit_p (dst, 70));
+}
+
+/* Verify bitmap_single_bit_set_p.  */
+TEST (bitmap_test, bitmap_single_bit_set_p)
+{
+  bitmap b = bitmap_gc_alloc ();
+
+  EXPECT_FALSE (bitmap_single_bit_set_p (b));
+
+  bitmap_set_range (b, 42, 1);
+  EXPECT_TRUE (bitmap_single_bit_set_p (b));
+  EXPECT_EQ (42, bitmap_first_set_bit (b));
+
+  bitmap_set_range (b, 1066, 1);
+  EXPECT_FALSE (bitmap_single_bit_set_p (b));
+  EXPECT_EQ (42, bitmap_first_set_bit (b));
+
+  bitmap_clear_range (b, 0, 100);
+  EXPECT_TRUE (bitmap_single_bit_set_p (b));
+  EXPECT_EQ (1066, bitmap_first_set_bit (b));
+}
+
+}  // anon namespace