aboutsummaryrefslogtreecommitdiff
path: root/gcc/wide-int.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-06-06 17:11:30 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-06-06 17:11:30 +0000
commitd9b950dd440fe2029a111cda56add2c9e91123b9 (patch)
tree63fc70ab85f16020285e16504429ce186042a4da /gcc/wide-int.cc
parentdbc6221fe5aa38017bb5818aa28257360b15f3b6 (diff)
downloadgcc-d9b950dd440fe2029a111cda56add2c9e91123b9.zip
gcc-d9b950dd440fe2029a111cda56add2c9e91123b9.tar.gz
gcc-d9b950dd440fe2029a111cda56add2c9e91123b9.tar.bz2
Selftest framework
gcc/ChangeLog: * Makefile.in (OBJS): Add function-tests.o, hash-map-tests.o, hash-set-tests.o, rtl-tests.o, selftest-run-tests.o. (OBJS-libcommon): Add selftest.o. (OBJS-libcommon-target): Add selftest.o. (all.internal): Add "selftest". (all.cross): Likewise. (selftest): New phony target. (s-selftest): New target. (selftest-gdb): New phony target. (COLLECT2_OBJS): Add selftest.o. * bitmap.c: Include "selftest.h". (selftest::test_gc_alloc): New function. (selftest::test_set_range): New function. (selftest::test_clear_bit_in_middle): New function. (selftest::test_copying): New function. (selftest::test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. * common.opt (fself-test): New. * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. * et-forest.c: Include "selftest.h". (selftest::test_single_node): New function. (selftest::test_simple_tree): New function. (selftest::test_disconnected_nodes): New function. (selftest::et_forest_c_tests): New function. * fold-const.c: Include "selftest.h". (selftest::assert_binop_folds_to_const): New function. (selftest::assert_binop_folds_to_nonlvalue): New function. (selftest::test_arithmetic_folding): New function. (selftest::fold_const_c_tests): New function. * function-tests.c: New file. * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (selftest::verify_gimple_pp): New function. (selftest::test_assign_single): New function. (selftest::test_assign_binop): New function. (selftest::test_nop_stmt): New function. (selftest::test_return_stmt): New function. (selftest::test_return_without_value): New function. (selftest::gimple_c_tests): New function. * hash-map-tests.c: New file. * hash-set-tests.c: New file. * input.c: Include "selftest.h". (selftest::assert_loceq): New function. (selftest::test_accessing_ordinary_linemaps): New function. (selftest::test_unknown_location): New function. (selftest::test_builtins): New function. (selftest::test_reading_source_line): New function. (selftest::input_c_tests): New function. * rtl-tests.c: New file. * selftest-run-tests.c: New file. * selftest.c: New file. * selftest.h: New file. * spellcheck.c: Include "selftest.h". (selftest::levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (selftest::levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. * toplev.c: Include selftest.h. (toplev::run_self_tests): New. (toplev::main): Handle -fself-test. * toplev.h (toplev::run_self_tests): New. * tree.c: Include "selftest.h". (selftest::test_integer_constants): New function. (selftest::test_identifiers): New function. (selftest::test_labels): New function. (selftest::tree_c_tests): New function. * tree-cfg.c: Include "selftest.h". (selftest::push_fndecl): New function. (selftest::test_linear_chain): New function. (selftest::test_diamond): New function. (selftest::test_fully_connected): New function. (selftest::tree_cfg_c_tests): New function. * vec.c: Include "selftest.h". (selftest::safe_push_range): New function. (selftest::test_quick_push): New function. (selftest::test_safe_push): New function. (selftest::test_truncate): New function. (selftest::test_safe_grow_cleared): New function. (selftest::test_pop): New function. (selftest::test_safe_insert): New function. (selftest::test_ordered_remove): New function. (selftest::test_unordered_remove): New function. (selftest::test_block_remove): New function. (selftest::reverse_cmp): New function. (selftest::test_qsort): New function. (selftest::vec_c_tests): New function.c. * wide-int.cc: Include selftest.h and wide-int-print.h. (selftest::from_int <wide_int>): New function. (selftest::from_int <offset_int>): New function. (selftest::from_int <widest_int>): New function. (selftest::assert_deceq): New function. (selftest::assert_hexeq): New function. (selftest::test_printing <VALUE_TYPE>): New function template. (selftest::test_ops <VALUE_TYPE>): New function template. (selftest::test_comparisons <VALUE_TYPE>): New function template. (selftest::run_all_wide_int_tests <VALUE_TYPE>): New function template. (selftest::wide_int_cc_tests): New function. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. From-SVN: r237144
Diffstat (limited to 'gcc/wide-int.cc')
-rw-r--r--gcc/wide-int.cc170
1 files changed, 170 insertions, 0 deletions
diff --git a/gcc/wide-int.cc b/gcc/wide-int.cc
index 8648e7d..30d355f 100644
--- a/gcc/wide-int.cc
+++ b/gcc/wide-int.cc
@@ -23,6 +23,8 @@ along with GCC; see the file COPYING3. If not see
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
+#include "selftest.h"
+#include "wide-int-print.h"
#define HOST_BITS_PER_HALF_WIDE_INT 32
@@ -2144,3 +2146,171 @@ template void generic_wide_int <wide_int_ref_storage <false> >::dump () const;
template void generic_wide_int <wide_int_ref_storage <true> >::dump () const;
template void offset_int::dump () const;
template void widest_int::dump () const;
+
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* Selftests for wide ints. We run these multiple times, once per type. */
+
+/* Helper function for building a test value. */
+
+template <class VALUE_TYPE>
+static VALUE_TYPE
+from_int (int i);
+
+/* Specializations of the fixture for each wide-int type. */
+
+/* Specialization for VALUE_TYPE == wide_int. */
+
+template <>
+wide_int
+from_int (int i)
+{
+ return wi::shwi (i, 32);
+}
+
+/* Specialization for VALUE_TYPE == offset_int. */
+
+template <>
+offset_int
+from_int (int i)
+{
+ return offset_int (i);
+}
+
+/* Specialization for VALUE_TYPE == widest_int. */
+
+template <>
+widest_int
+from_int (int i)
+{
+ return widest_int (i);
+}
+
+/* Verify that print_dec (WI, ..., SGN) gives the expected string
+ representation (using base 10). */
+
+static void
+assert_deceq (const char *expected, const wide_int_ref &wi, signop sgn)
+{
+ char buf[WIDE_INT_PRINT_BUFFER_SIZE];
+ print_dec (wi, buf, sgn);
+ ASSERT_STREQ (expected, buf);
+}
+
+/* Likewise for base 16. */
+
+static void
+assert_hexeq (const char *expected, const wide_int_ref &wi)
+{
+ char buf[WIDE_INT_PRINT_BUFFER_SIZE];
+ print_hex (wi, buf);
+ ASSERT_STREQ (expected, buf);
+}
+
+/* Test cases. */
+
+/* Verify that print_dec and print_hex work for VALUE_TYPE. */
+
+template <class VALUE_TYPE>
+static void
+test_printing ()
+{
+ VALUE_TYPE a = from_int<VALUE_TYPE> (42);
+ assert_deceq ("42", a, SIGNED);
+ assert_hexeq ("0x2a", a);
+}
+
+/* Verify that various operations work correctly for VALUE_TYPE,
+ unary and binary, using both function syntax, and
+ overloaded-operators. */
+
+template <class VALUE_TYPE>
+static void
+test_ops ()
+{
+ VALUE_TYPE a = from_int<VALUE_TYPE> (7);
+ VALUE_TYPE b = from_int<VALUE_TYPE> (3);
+
+ /* Using functions. */
+ assert_deceq ("-7", wi::neg (a), SIGNED);
+ assert_deceq ("10", wi::add (a, b), SIGNED);
+ assert_deceq ("4", wi::sub (a, b), SIGNED);
+ assert_deceq ("-4", wi::sub (b, a), SIGNED);
+ assert_deceq ("21", wi::mul (a, b), SIGNED);
+
+ /* Using operators. */
+ assert_deceq ("-7", -a, SIGNED);
+ assert_deceq ("10", a + b, SIGNED);
+ assert_deceq ("4", a - b, SIGNED);
+ assert_deceq ("-4", b - a, SIGNED);
+ assert_deceq ("21", a * b, SIGNED);
+}
+
+/* Verify that various comparisons work correctly for VALUE_TYPE. */
+
+template <class VALUE_TYPE>
+static void
+test_comparisons ()
+{
+ VALUE_TYPE a = from_int<VALUE_TYPE> (7);
+ VALUE_TYPE b = from_int<VALUE_TYPE> (3);
+
+ /* == */
+ ASSERT_TRUE (wi::eq_p (a, a));
+ ASSERT_FALSE (wi::eq_p (a, b));
+
+ /* != */
+ ASSERT_TRUE (wi::ne_p (a, b));
+ ASSERT_FALSE (wi::ne_p (a, a));
+
+ /* < */
+ ASSERT_FALSE (wi::lts_p (a, a));
+ ASSERT_FALSE (wi::lts_p (a, b));
+ ASSERT_TRUE (wi::lts_p (b, a));
+
+ /* <= */
+ ASSERT_TRUE (wi::les_p (a, a));
+ ASSERT_FALSE (wi::les_p (a, b));
+ ASSERT_TRUE (wi::les_p (b, a));
+
+ /* > */
+ ASSERT_FALSE (wi::gts_p (a, a));
+ ASSERT_TRUE (wi::gts_p (a, b));
+ ASSERT_FALSE (wi::gts_p (b, a));
+
+ /* >= */
+ ASSERT_TRUE (wi::ges_p (a, a));
+ ASSERT_TRUE (wi::ges_p (a, b));
+ ASSERT_FALSE (wi::ges_p (b, a));
+
+ /* comparison */
+ ASSERT_EQ (-1, wi::cmps (b, a));
+ ASSERT_EQ (0, wi::cmps (a, a));
+ ASSERT_EQ (1, wi::cmps (a, b));
+}
+
+/* Run all of the selftests, using the given VALUE_TYPE. */
+
+template <class VALUE_TYPE>
+static void run_all_wide_int_tests ()
+{
+ test_printing <VALUE_TYPE> ();
+ test_ops <VALUE_TYPE> ();
+ test_comparisons <VALUE_TYPE> ();
+}
+
+/* Run all of the selftests within this file, for all value types. */
+
+void
+wide_int_cc_tests ()
+{
+ run_all_wide_int_tests <wide_int> ();
+ run_all_wide_int_tests <offset_int> ();
+ run_all_wide_int_tests <widest_int> ();
+}
+
+} // namespace selftest
+#endif /* CHECKING_P */