diff options
author | Martin Liska <mliska@suse.cz> | 2016-07-20 09:00:50 +0200 |
---|---|---|
committer | Martin Liska <marxin@gcc.gnu.org> | 2016-07-20 07:00:50 +0000 |
commit | dcbdb17aebb7fac8b3e455774728c044dd711e37 (patch) | |
tree | 88225b8bb402d7d64ffddd07144958b5588a22da /gcc/sreal.c | |
parent | 43694e8bbb002aac45baaf35cc70861c546a1a40 (diff) | |
download | gcc-dcbdb17aebb7fac8b3e455774728c044dd711e37.zip gcc-dcbdb17aebb7fac8b3e455774728c044dd711e37.tar.gz gcc-dcbdb17aebb7fac8b3e455774728c044dd711e37.tar.bz2 |
Add sreal to selftests
* selftest-run-tests.c (selftest::run_tests): New function.
* selftest.h (sreal_c_tests): Declare.
* sreal.c (sreal_verify_basics): New function.
(verify_aritmetics): Likewise.
(sreal_verify_arithmetics): Likewise.
(verify_shifting): Likewise.
(sreal_verify_shifting): Likewise.
(void sreal_c_tests): Likewise.
* gcc.dg/plugin/plugin.exp: Remove sreal test.
* gcc.dg/plugin/sreal-test-1.c: Remove.
* gcc.dg/plugin/sreal_plugin.c: Remove.
From-SVN: r238508
Diffstat (limited to 'gcc/sreal.c')
-rw-r--r-- | gcc/sreal.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/gcc/sreal.c b/gcc/sreal.c index a7c9c12..9c43b4e 100644 --- a/gcc/sreal.c +++ b/gcc/sreal.c @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see #include <math.h> #include "coretypes.h" #include "sreal.h" +#include "selftest.h" /* Print the content of struct sreal. */ @@ -233,3 +234,114 @@ sreal::operator/ (const sreal &other) const r.normalize (); return r; } + +#if CHECKING_P + +namespace selftest { + +/* Selftests for sreals. */ + +/* Verify basic sreal operations. */ + +static void +sreal_verify_basics (void) +{ + sreal minimum = INT_MIN; + sreal maximum = INT_MAX; + + sreal seven = 7; + sreal minus_two = -2; + sreal minus_nine = -9; + + ASSERT_EQ (INT_MIN, minimum.to_int ()); + ASSERT_EQ (INT_MAX, maximum.to_int ()); + + ASSERT_FALSE (minus_two < minus_two); + ASSERT_FALSE (seven < seven); + ASSERT_TRUE (seven > minus_two); + ASSERT_TRUE (minus_two < seven); + ASSERT_TRUE (minus_two != seven); + ASSERT_EQ (minus_two, -2); + ASSERT_EQ (seven, 7); + ASSERT_EQ ((seven << 10) >> 10, 7); + ASSERT_EQ (seven + minus_nine, -2); +} + +/* Helper function that performs basic arithmetics and comparison + of given arguments A and B. */ + +static void +verify_aritmetics (int64_t a, int64_t b) +{ + ASSERT_EQ (a, -(-(sreal (a))).to_int ()); + ASSERT_EQ (a < b, sreal (a) < sreal (b)); + ASSERT_EQ (a <= b, sreal (a) <= sreal (b)); + ASSERT_EQ (a == b, sreal (a) == sreal (b)); + ASSERT_EQ (a != b, sreal (a) != sreal (b)); + ASSERT_EQ (a > b, sreal (a) > sreal (b)); + ASSERT_EQ (a >= b, sreal (a) >= sreal (b)); + ASSERT_EQ (a + b, (sreal (a) + sreal (b)).to_int ()); + ASSERT_EQ (a - b, (sreal (a) - sreal (b)).to_int ()); + ASSERT_EQ (b + a, (sreal (b) + sreal (a)).to_int ()); + ASSERT_EQ (b - a, (sreal (b) - sreal (a)).to_int ()); +} + +/* Verify arithmetics for interesting numbers. */ + +static void +sreal_verify_arithmetics (void) +{ + int values[] = {-14123413, -7777, -17, -10, -2, 0, 17, 139, 1234123}; + unsigned c = sizeof (values) / sizeof (int); + + for (unsigned i = 0; i < c; i++) + for (unsigned j = 0; j < c; j++) + { + int a = values[i]; + int b = values[j]; + + verify_aritmetics (a, b); + } +} + +/* Helper function that performs various shifting test of a given + argument A. */ + +static void +verify_shifting (int64_t a) +{ + sreal v = a; + + for (unsigned i = 0; i < 16; i++) + ASSERT_EQ (a << i, (v << i).to_int()); + + a = a << 16; + v = v << 16; + + for (unsigned i = 0; i < 16; i++) + ASSERT_EQ (a >> i, (v >> i).to_int()); +} + +/* Verify shifting for interesting numbers. */ + +static void +sreal_verify_shifting (void) +{ + int values[] = {0, 17, 32, 139, 1024, 55555, 1234123}; + unsigned c = sizeof (values) / sizeof (int); + + for (unsigned i = 0; i < c; i++) + verify_shifting (values[i]); +} + +/* Run all of the selftests within this file. */ + +void sreal_c_tests () +{ + sreal_verify_basics (); + sreal_verify_arithmetics (); + sreal_verify_shifting (); +} + +} // namespace selftest +#endif /* CHECKING_P */ |