From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22500 invoked by alias); 27 Oct 2015 19:31:52 -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 22436 invoked by uid 89); 27 Oct 2015 19:31:51 -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 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; Tue, 27 Oct 2015 19:31:46 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zr9y8-0002rB-OY for gcc-patches@gcc.gnu.org; Tue, 27 Oct 2015 15:31:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42523) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr9y8-0002qf-Gu for gcc-patches@gcc.gnu.org; Tue, 27 Oct 2015 15:31:40 -0400 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 29DF9C0B590E for ; Tue, 27 Oct 2015 19:31:39 +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 t9RJVW5g018778; Tue, 27 Oct 2015 15:31:38 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 06/16] Add test-folding.c to unittests Date: Tue, 27 Oct 2015 19:35:00 -0000 Message-Id: <1445975355-37660-7-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-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-10/txt/msg02957.txt.bz2 gcc/testsuite/ChangeLog: * unittests/test-folding.c: New file. --- gcc/testsuite/unittests/test-folding.c | 120 +++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 gcc/testsuite/unittests/test-folding.c diff --git a/gcc/testsuite/unittests/test-folding.c b/gcc/testsuite/unittests/test-folding.c new file mode 100644 index 0000000..6623c55 --- /dev/null +++ b/gcc/testsuite/unittests/test-folding.c @@ -0,0 +1,120 @@ +/* Unit tests for GCC's expression folding. + 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 "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 "predict.h" +#include "vec.h" +#include "hashtab.h" +#include "hash-set.h" +#include "machmode.h" +#include "hard-reg-set.h" +#include "input.h" +#include "function.h" +#include "dominance.h" +#include "cfg.h" +#include "cfganal.h" +#include "basic-block.h" +#include "tree-ssa-alias.h" +#include "internal-fn.h" +#include "gimple-fold.h" +#include "gimple-expr.h" +#include "toplev.h" +#include "print-tree.h" +#include "tree-iterator.h" +#include "gimplify.h" +#include "tree-cfg.h" +#include "fold-const.h" + +namespace { + +/* A test fixture for writing tests of folding trees. */ +class tree_folding_test : public ::testing::Test +{ + protected: + void + assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs, + tree constant) + { + EXPECT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs)); + } + + void + assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs, + tree wrapped_expr) + { + tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs); + EXPECT_NE (wrapped_expr, result); + EXPECT_EQ (NON_LVALUE_EXPR, TREE_CODE (result)); + EXPECT_EQ (wrapped_expr, TREE_OPERAND (result, 0)); + } +}; + +TEST_F (tree_folding_test, arithmetic_folding) +{ + tree type = integer_type_node; + tree x = create_tmp_var_raw (type, "x"); + tree zero = build_zero_cst (type); + tree one = build_int_cst (type, 1); + + /* Addition. */ + /* 1 <-- (0 + 1) */ + assert_binop_folds_to_const (zero, PLUS_EXPR, one, + one); + assert_binop_folds_to_const (one, PLUS_EXPR, zero, + one); + + /* (nonlvalue)x <-- (x + 0) */ + assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero, + x); + + /* Subtraction. */ + /* 0 <-- (x - x) */ + assert_binop_folds_to_const (x, MINUS_EXPR, x, + zero); + assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero, + x); + + /* Multiplication. */ + /* 0 <-- (x * 0) */ + assert_binop_folds_to_const (x, MULT_EXPR, zero, + zero); + + /* (nonlvalue)x <-- (x * 1) */ + assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one, + x); +} + +} // anon namespace -- 1.8.5.3