From ba446952fe50d3857b753c1763135c2c29c6474a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 10 Jun 2015 06:08:20 -0400
Subject: [PATCH 16/16] Add test-wide-int.c to unittests

gcc/testsuite/ChangeLog:
	* unittests/test-wide-int.c: New file.
---
 gcc/testsuite/unittests/test-wide-int.c | 185 ++++++++++++++++++++++++++++++++
 1 file changed, 185 insertions(+)
 create mode 100644 gcc/testsuite/unittests/test-wide-int.c

diff --git a/gcc/testsuite/unittests/test-wide-int.c b/gcc/testsuite/unittests/test-wide-int.c
new file mode 100644
index 0000000..3f2e36e
--- /dev/null
+++ b/gcc/testsuite/unittests/test-wide-int.c
@@ -0,0 +1,185 @@
+/* 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
+<http://www.gnu.org/licenses/>.  */
+
+#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 "wide-int-print.h"
+
+namespace {
+
+/* The types we will run the testcases for.  */
+typedef ::testing::Types<wide_int, offset_int, widest_int> 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 "<type>":
+     [----------] 2 tests from wide_int_test/0, where TypeParam = <type>
+       (snip)
+     [----------] 2 tests from wide_int_test/0 (0 ms total)
+     [----------] 2 tests from wide_int_test/1, where TypeParam = <type>
+       (snip)
+     [----------] 2 tests from wide_int_test/1 (0 ms total)
+     [----------] 2 tests from wide_int_test/2, where TypeParam = <type>
+       (snip)
+     [----------] 2 tests from wide_int_test/2 (0 ms total)
+*/
+
+template <class VALUE_TYPE>
+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 <wide_int>::from_int (int i)
+{
+  return wi::shwi (i, 32);
+}
+
+template <>
+offset_int
+wide_int_test <offset_int>::from_int (int i)
+{
+  return offset_int (i);
+}
+
+template <>
+widest_int
+wide_int_test <widest_int>::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

