From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 35979 invoked by alias); 2 Jun 2016 20:41:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 35751 invoked by uid 89); 2 Jun 2016 20:41:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 spammy=HX-HELO:eggs.gnu.org, Hx-spam-relays-external:208.118.235.92, H*RU:208.118.235.92 X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (208.118.235.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 02 Jun 2016 20:41:13 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8ZQR-0002qt-7c for gcc-patches@gcc.gnu.org; Thu, 02 Jun 2016 16:41:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35182) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8ZQQ-0002qp-VT for gcc-patches@gcc.gnu.org; Thu, 02 Jun 2016 16:41:07 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7335FC07EFC5 for ; Thu, 2 Jun 2016 20:41:06 +0000 (UTC) Received: from c64.redhat.com (vpn-237-222.phx2.redhat.com [10.3.237.222]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u52Kf2Ww007154; Thu, 2 Jun 2016 16:41:05 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Bernd Schmidt , Jeff Law , David Malcolm Subject: [PATCH 04/16] bitmap.c: add selftests Date: Thu, 02 Jun 2016 20:41:00 -0000 Message-Id: <1464901625-54547-5-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> References: <1464874868.11895.41.camel@redhat.com> <1464901625-54547-1-git-send-email-dmalcolm@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-IsSubscribed: yes X-SW-Source: 2016-06/txt/msg00215.txt.bz2 Jeff pre-approved the plugin version of this (as a new file unittests/test-bitmap.c): https://gcc.gnu.org/ml/gcc-patches/2015-10/msg03284.html with: > OK if/when prereqs are approved. Minor twiddling if we end up moving it > elsewhere or standardizing/reducing header files is pre-approved. This version moves them to bitmap.c and converts them to functions. gcc/ChangeLog: * bitmap.c: Include "selftest.h". (test_gc_alloc): New function. (test_set_range): New function. (test_clear_bit_in_middle): New function. (test_copying): New function. (test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. --- gcc/bitmap.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/gcc/bitmap.c b/gcc/bitmap.c index 0c05512..21149e7 100644 --- a/gcc/bitmap.c +++ b/gcc/bitmap.c @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "bitmap.h" +#include "selftest.h" /* Memory allocation statistics purpose instance. */ mem_alloc_description bitmap_mem_desc; @@ -2162,5 +2163,114 @@ debug (const bitmap_head *ptr) fprintf (stderr, "\n"); } +#if CHECKING_P + +/* Selftests for bitmaps. */ + +/* Freshly-created bitmaps ought to be empty. */ + +static void +test_gc_alloc () +{ + bitmap b = bitmap_gc_alloc (); + ASSERT_TRUE (bitmap_empty_p (b)); +} + +/* Verify bitmap_set_range. */ + +static void +test_set_range () +{ + bitmap b = bitmap_gc_alloc (); + ASSERT_TRUE (bitmap_empty_p (b)); + + bitmap_set_range (b, 7, 5); + ASSERT_FALSE (bitmap_empty_p (b)); + ASSERT_EQ (5, bitmap_count_bits (b)); + + /* Verify bitmap_bit_p at the boundaries. */ + ASSERT_FALSE (bitmap_bit_p (b, 6)); + ASSERT_TRUE (bitmap_bit_p (b, 7)); + ASSERT_TRUE (bitmap_bit_p (b, 11)); + ASSERT_FALSE (bitmap_bit_p (b, 12)); +} + +/* Verify splitting a range into two pieces using bitmap_clear_bit. */ + +static void +test_clear_bit_in_middle () +{ + bitmap b = bitmap_gc_alloc (); + + /* Set b to [100..200]. */ + bitmap_set_range (b, 100, 100); + ASSERT_EQ (100, bitmap_count_bits (b)); + + /* Clear a bit in the middle. */ + bool changed = bitmap_clear_bit (b, 150); + ASSERT_TRUE (changed); + ASSERT_EQ (99, bitmap_count_bits (b)); + ASSERT_TRUE (bitmap_bit_p (b, 149)); + ASSERT_FALSE (bitmap_bit_p (b, 150)); + ASSERT_TRUE (bitmap_bit_p (b, 151)); +} + +/* Verify bitmap_copy. */ + +static void +test_copying () +{ + bitmap src = bitmap_gc_alloc (); + bitmap_set_range (src, 40, 10); + + bitmap dst = bitmap_gc_alloc (); + ASSERT_FALSE (bitmap_equal_p (src, dst)); + bitmap_copy (dst, src); + ASSERT_TRUE (bitmap_equal_p (src, dst)); + + /* Verify that we can make them unequal again... */ + bitmap_set_range (src, 70, 5); + ASSERT_FALSE (bitmap_equal_p (src, dst)); + + /* ...and that changing src after the copy didn't affect + the other: */ + ASSERT_FALSE (bitmap_bit_p (dst, 70)); +} + +/* Verify bitmap_single_bit_set_p. */ +static void +test_bitmap_single_bit_set_p () +{ + bitmap b = bitmap_gc_alloc (); + + ASSERT_FALSE (bitmap_single_bit_set_p (b)); + + bitmap_set_range (b, 42, 1); + ASSERT_TRUE (bitmap_single_bit_set_p (b)); + ASSERT_EQ (42, bitmap_first_set_bit (b)); + + bitmap_set_range (b, 1066, 1); + ASSERT_FALSE (bitmap_single_bit_set_p (b)); + ASSERT_EQ (42, bitmap_first_set_bit (b)); + + bitmap_clear_range (b, 0, 100); + ASSERT_TRUE (bitmap_single_bit_set_p (b)); + ASSERT_EQ (1066, bitmap_first_set_bit (b)); +} + +namespace selftest { + +void +bitmap_c_tests () +{ + test_gc_alloc (); + test_set_range (); + test_clear_bit_in_middle (); + test_copying (); + test_bitmap_single_bit_set_p (); +} + +} // namespace selftest +#endif /* CHECKING_P */ #include "gt-bitmap.h" -- 1.8.5.3