From patchwork Wed Jun 1 21:20:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 628850 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rKjhG6v8Zz9t3n for ; Thu, 2 Jun 2016 07:11:10 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=TxRYGElT; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=c/EG1dXX1tJek0x8qmxL84/ZBYTohMSQoTpqDD09tS9s7zMN61uzm D5tHr5X8eXhoYf9olGUHJdd/9IL/cpNmEBm82Qy03SwwJhpmJXK8pw7h/HNd7X5U ikBdsX67YQm+KLdhmxFIS0OsrOYX0tAu9n4x7TXtOXMWJyYr4h2cHo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=KQFtbsfDjFHACWcDsCo4xacNnpE=; b=TxRYGElT0Ay6zXOBoDE9 4HKiTThHBIB4vUPNa0AwxmWM0p0humA1FCiJEUrICex3Or4vyz0lPGGRPXUsG+Xs CI31rTDflTa0edFqzqfrlJnlgZvD5Kpt+AUhcfTpV/CVJpR7OZT1G4FWXfy/Sj2j Dh6PJKGown8TxFFg1pe0ILU= Received: (qmail 16457 invoked by alias); 1 Jun 2016 21:10:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 16130 invoked by uid 89); 1 Jun 2016 21:10:40 -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; Wed, 01 Jun 2016 21:10:32 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8D9h-0007wS-Iu for gcc-patches@gcc.gnu.org; Wed, 01 Jun 2016 16:54:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36114) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8D9h-0007wL-Ao for gcc-patches@gcc.gnu.org; Wed, 01 Jun 2016 16:54:21 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (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 D34C5A10DA for ; Wed, 1 Jun 2016 20:54:20 +0000 (UTC) Received: from c64.redhat.com (vpn-239-103.phx2.redhat.com [10.3.239.103]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u51Ks4We027176; Wed, 1 Jun 2016 16:54:20 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: Bernd Schmidt , Jeff Law , David Malcolm Subject: [PATCH 21/21] Add selftests to vec.c Date: Wed, 1 Jun 2016 17:20:03 -0400 Message-Id: <1464816003-35862-22-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1464816003-35862-1-git-send-email-dmalcolm@redhat.com> References: <1447952699-40820-1-git-send-email-dmalcolm@redhat.com> <1464816003-35862-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 Jeff approved an earlier version of this (as unittests/test-vec.c): https://gcc.gnu.org/ml/gcc-patches/2015-10/msg03308.html > 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 puts the tests at the end of vec.c. gcc/ChangeLog: * vec.c: Include "selftest.h". (class vec_test): New test subclass. (vec_test, quick_push): New selftest. (vec_test, safe_push): New selftest. (vec_test, truncate): New selftest. (vec_test, safe_grow_cleared): New selftest. (vec_test, pop): New selftest. (vec_test, safe_insert): New selftest. (vec_test, ordered_remove): New selftest. (vec_test, unordered_remove): New selftest. (vec_test, block_remove): New selftest. (reverse_cmp): New function. (vec_test, qsort): New selftest. --- gcc/vec.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/gcc/vec.c b/gcc/vec.c index a483d5b..20fed31 100644 --- a/gcc/vec.c +++ b/gcc/vec.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "hash-table.h" +#include "selftest.h" /* vNULL is an empty type with a template cast operation that returns a zero-initialized vec instance. Use this when you want @@ -188,3 +189,144 @@ dump_vec_loc_statistics (void) { vec_mem_desc.dump (VEC_ORIGIN); } + +#ifndef GENERATOR_FILE +#if CHECKING_P + +namespace { + +class vec_test : public ::selftest::test +{ + protected: + /* Add the range [START..LIMIT) to V. */ + void + safe_push_range (vec &v, int start, int limit) + { + for (int i = start; i < limit; i++) + v.safe_push (i); + } +}; + +TEST_F (vec_test, quick_push) +{ + auto_vec v; + EXPECT_EQ (0, v.length ()); + v.reserve (3); + EXPECT_EQ (0, v.length ()); + EXPECT_TRUE (v.space (3)); + v.quick_push (5); + v.quick_push (6); + v.quick_push (7); + EXPECT_EQ (3, v.length ()); + EXPECT_EQ (5, v[0]); + EXPECT_EQ (6, v[1]); + EXPECT_EQ (7, v[2]); +} + +TEST_F (vec_test, safe_push) +{ + auto_vec v; + EXPECT_EQ (0, v.length ()); + v.safe_push (5); + v.safe_push (6); + v.safe_push (7); + EXPECT_EQ (3, v.length ()); + EXPECT_EQ (5, v[0]); + EXPECT_EQ (6, v[1]); + EXPECT_EQ (7, v[2]); +} + +TEST_F (vec_test, truncate) +{ + auto_vec v; + EXPECT_EQ (0, v.length ()); + safe_push_range (v, 0, 10); + EXPECT_EQ (10, v.length ()); + + v.truncate (5); + EXPECT_EQ (5, v.length ()); +} + +TEST_F (vec_test, safe_grow_cleared) +{ + auto_vec v; + EXPECT_EQ (0, v.length ()); + v.safe_grow_cleared (50); + EXPECT_EQ (50, v.length ()); + EXPECT_EQ (0, v[0]); + EXPECT_EQ (0, v[49]); +} + +TEST_F (vec_test, pop) +{ + auto_vec v; + safe_push_range (v, 5, 20); + EXPECT_EQ (15, v.length ()); + + int last = v.pop (); + EXPECT_EQ (19, last); + EXPECT_EQ (14, v.length ()); +} + +TEST_F (vec_test, safe_insert) +{ + auto_vec v; + safe_push_range (v, 0, 10); + v.safe_insert (5, 42); + EXPECT_EQ (4, v[4]); + EXPECT_EQ (42, v[5]); + EXPECT_EQ (5, v[6]); + EXPECT_EQ (11, v.length ()); +} + +TEST_F (vec_test, ordered_remove) +{ + auto_vec v; + safe_push_range (v, 0, 10); + v.ordered_remove (5); + EXPECT_EQ (4, v[4]); + EXPECT_EQ (6, v[5]); + EXPECT_EQ (9, v.length ()); +} + +TEST_F (vec_test, unordered_remove) +{ + auto_vec v; + safe_push_range (v, 0, 10); + v.unordered_remove (5); + EXPECT_EQ (9, v.length ()); +} + +TEST_F (vec_test, block_remove) +{ + auto_vec v; + safe_push_range (v, 0, 10); + v.block_remove (5, 3); + EXPECT_EQ (3, v[3]); + EXPECT_EQ (4, v[4]); + EXPECT_EQ (8, v[5]); + EXPECT_EQ (9, v[6]); + EXPECT_EQ (7, v.length ()); +} + +static int reverse_cmp (const void *p_i, const void *p_j) +{ + return *(const int *)p_j - *(const int *)p_i; +} + +TEST_F (vec_test, qsort) +{ + auto_vec v; + safe_push_range (v, 0, 10); + v.qsort (reverse_cmp); + EXPECT_EQ (9, v[0]); + EXPECT_EQ (8, v[1]); + EXPECT_EQ (1, v[8]); + EXPECT_EQ (0, v[9]); + EXPECT_EQ (10, v.length ()); +} + +} // anon namespace + +#endif /* #if CHECKING_P */ +#endif /* #ifndef GENERATOR_FILE */