From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 125035 invoked by alias); 10 Jun 2015 15:28:54 -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 125004 invoked by uid 89); 10 Jun 2015 15:28:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 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, 10 Jun 2015 15:28:50 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z2heC-0000OD-Kv for gcc-patches@gcc.gnu.org; Wed, 10 Jun 2015 11:10:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55496) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2heC-0000Ng-Dv for gcc-patches@gcc.gnu.org; Wed, 10 Jun 2015 11:10:32 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 2AEDD36B37B for ; Wed, 10 Jun 2015 15:10:32 +0000 (UTC) Received: from c64.redhat.com (vpn-230-17.phx2.redhat.com [10.3.230.17]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5AFAMM0007589; Wed, 10 Jun 2015 11:10:31 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 14/17] Add test-wide-int.c to gcc/unittests Date: Wed, 10 Jun 2015 15:34:00 -0000 Message-Id: <1433949898-22033-15-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1433949898-22033-1-git-send-email-dmalcolm@redhat.com> References: <1433949898-22033-1-git-send-email-dmalcolm@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-IsSubscribed: yes X-SW-Source: 2015-06/txt/msg00784.txt.bz2 gcc/unittests/ChangeLog: * test-wide-int.c: New file. --- gcc/unittests/test-wide-int.c | 186 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 gcc/unittests/test-wide-int.c diff --git a/gcc/unittests/test-wide-int.c b/gcc/unittests/test-wide-int.c new file mode 100644 index 0000000..089e80d --- /dev/null +++ b/gcc/unittests/test-wide-int.c @@ -0,0 +1,186 @@ +/* Unit tests for wide-int. + 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 + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "opts.h" +#include "signop.h" +#include "hash-set.h" +#include "fixed-value.h" +#include "alias.h" +#include "flags.h" +#include "symtab.h" +#include "tree-core.h" +#include "stor-layout.h" +#include "tree.h" +#include "stringpool.h" +#include "stor-layout.h" +#include "rtl.h" +#include "wide-int-print.h" + +namespace { + +/* The types we will run the testcases for. */ +typedef ::testing::Types implementations; + +/* A fixture for running the same tests for all of the wide-int.h types + listed in "implementations" above. + + Unfortunately, for some reason gtest reports all of the tests as: + of type "": + [----------] 2 tests from wide_int_test/0, where TypeParam = + (snip) + [----------] 2 tests from wide_int_test/0 (0 ms total) + [----------] 2 tests from wide_int_test/1, where TypeParam = + (snip) + [----------] 2 tests from wide_int_test/1 (0 ms total) + [----------] 2 tests from wide_int_test/2, where TypeParam = + (snip) + [----------] 2 tests from wide_int_test/2 (0 ms total) +*/ + +template +class wide_int_test : public ::testing::Test +{ + protected: + /* Helper function for building a test value. */ + VALUE_TYPE from_int (int i); +}; + +TYPED_TEST_CASE(wide_int_test, implementations); + +/* Specializations of the fixture for each wide-int type. */ + +template <> +wide_int +wide_int_test ::from_int (int i) +{ + return wi::shwi (i, 32); +} + +template <> +offset_int +wide_int_test ::from_int (int i) +{ + return offset_int (i); +} + +template <> +widest_int +wide_int_test ::from_int (int i) +{ + return widest_int (i); +} + +/* Verify that print_dec (WI, ..., SGN) gives the expected string + representation (using base 10). */ + +void +expect_deceq (const char *expected, const wide_int_ref &wi, signop sgn) +{ + char buf[WIDE_INT_PRINT_BUFFER_SIZE]; + print_dec (wi, buf, sgn); + EXPECT_STREQ (expected, buf); +} + +/* Likewise for base 16. */ + +void +expect_hexeq (const char *expected, const wide_int_ref &wi) +{ + char buf[WIDE_INT_PRINT_BUFFER_SIZE]; + print_hex (wi, buf); + EXPECT_STREQ (expected, buf); +} + +/* Test cases. These will be run for each type in "implementations" above, + with gtest's TYPED_TEST macro defining the types "TypeParam" + and "TestFixture". */ + +TYPED_TEST (wide_int_test, test_printing) +{ + TypeParam a = TestFixture::from_int (42); + expect_deceq ("42", a, SIGNED); + expect_hexeq ("0x2a", a); +} + +TYPED_TEST (wide_int_test, test_ops) +{ + TypeParam a = TestFixture::from_int (7); + TypeParam b = TestFixture::from_int (3); + + /* Using functions. */ + expect_deceq ("-7", wi::neg (a), SIGNED); + expect_deceq ("10", wi::add (a, b), SIGNED); + expect_deceq ("4", wi::sub (a, b), SIGNED); + expect_deceq ("-4", wi::sub (b, a), SIGNED); + expect_deceq ("21", wi::mul (a, b), SIGNED); + + /* Using operators. */ + expect_deceq ("-7", -a, SIGNED); + expect_deceq ("10", a + b, SIGNED); + expect_deceq ("4", a - b, SIGNED); + expect_deceq ("-4", b - a, SIGNED); + expect_deceq ("21", a * b, SIGNED); +} + +TYPED_TEST (wide_int_test, test_comparisons) +{ + TypeParam a = TestFixture::from_int (7); + TypeParam b = TestFixture::from_int (3); + + /* == */ + EXPECT_TRUE (wi::eq_p (a, a)); + EXPECT_FALSE (wi::eq_p (a, b)); + + /* != */ + EXPECT_TRUE (wi::ne_p (a, b)); + EXPECT_FALSE (wi::ne_p (a, a)); + + /* < */ + EXPECT_FALSE (wi::lts_p (a, a)); + EXPECT_FALSE (wi::lts_p (a, b)); + EXPECT_TRUE (wi::lts_p (b, a)); + + /* <= */ + EXPECT_TRUE (wi::les_p (a, a)); + EXPECT_FALSE (wi::les_p (a, b)); + EXPECT_TRUE (wi::les_p (b, a)); + + /* > */ + EXPECT_FALSE (wi::gts_p (a, a)); + EXPECT_TRUE (wi::gts_p (a, b)); + EXPECT_FALSE (wi::gts_p (b, a)); + + /* >= */ + EXPECT_TRUE (wi::ges_p (a, a)); + EXPECT_TRUE (wi::ges_p (a, b)); + EXPECT_FALSE (wi::ges_p (b, a)); + + /* comparison */ + EXPECT_EQ (-1, wi::cmps (b, a)); + EXPECT_EQ (0, wi::cmps (a, a)); + EXPECT_EQ (1, wi::cmps (a, b)); +} + +} /* anon namespace. */ -- 1.8.5.3