From patchwork Tue Oct 27 19:49:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 536819 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 CD649141364 for ; Wed, 28 Oct 2015 06:33:28 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=IAmd8sEs; 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=ho+DLqlTQZ8IjCTzhxdVYHnhQGSK910ammR0jTsJ2kJTuQHgzqd+E Xc3U4eTmxn9SLiVDnLhXjlq8INBS/T8XCq7u63+j1vyFnB2DLBrzaoWGvUZxhMOw E18xUXpNwMXo2fprPChTAxY84VuwFpL0nt70zyMvz+rLdhdLDiCxew= 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=yOwscE+jLp27CTh7wptdCUWAUXE=; b=IAmd8sEsJxvHsbpF6P0U SSw+CKMJcvk4jRzCnUPZto9XWD3vRvNzgEqLnVcWYfVAVKkGahWru/+Wi9Ypfh3U vaxXHhGjbSzD3XZEIuigwrFTth+n7N+E2M8uvXfcMpaC0lSoIqldFAIcOr1lARc1 gPMgan86ejNpGNj8E+qsH6g= Received: (qmail 22749 invoked by alias); 27 Oct 2015 19:31:54 -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 22705 invoked by uid 89); 27 Oct 2015 19:31:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 27 Oct 2015 19:31:46 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 4C4D6AACC7 for ; Tue, 27 Oct 2015 19:31:44 +0000 (UTC) Received: from c64.redhat.com (vpn-230-173.phx2.redhat.com [10.3.230.173]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9RJVW5p018778; Tue, 27 Oct 2015 15:31:43 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 15/16] Add test-vec.c to unittests Date: Tue, 27 Oct 2015 15:49:14 -0400 Message-Id: <1445975355-37660-16-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1445975355-37660-1-git-send-email-dmalcolm@redhat.com> References: <5589B2FB.8010500@redhat.com> <1445975355-37660-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes gcc/testsuite/ChangeLog: * unittests/test-vec.c: New file. --- gcc/testsuite/unittests/test-vec.c | 161 +++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 gcc/testsuite/unittests/test-vec.c diff --git a/gcc/testsuite/unittests/test-vec.c b/gcc/testsuite/unittests/test-vec.c new file mode 100644 index 0000000..c5b9d78 --- /dev/null +++ b/gcc/testsuite/unittests/test-vec.c @@ -0,0 +1,161 @@ +/* Unit tests for vec<>. + 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 +. */ + +#include "config.h" +#include "gtest/gtest.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "opts.h" +#include "vec.h" + +namespace { + +class vec_test : public ::testing::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