2
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
24
#include "precompiled.hpp"
25
#include "unittest.hpp"
26
#include "memory/allocation.hpp"
27
#include "memory/resourceArea.inline.hpp"
28
#include "runtime/thread.hpp"
30
TEST(absTest, sanity) {
31
// Simple integer cases
34
EXPECT_EQ(1, ABS(-1));
36
// Simple floating point cases, should be exactly representable
37
EXPECT_EQ(0.0f, ABS(0.0f));
38
EXPECT_EQ(1.0f, ABS(1.0f));
39
EXPECT_EQ(1.0f, ABS(-1.0f));
41
EXPECT_EQ(0.0, ABS(0.0));
42
EXPECT_EQ(1.0, ABS(1.0));
43
EXPECT_EQ(1.0, ABS(-1.0));
45
// Upper bounds for unsigned integers
46
EXPECT_EQ(max_jubyte, ABS(max_jubyte));
47
EXPECT_EQ(max_jushort, ABS(max_jushort));
48
EXPECT_EQ(max_juint, ABS(max_juint));
49
EXPECT_EQ(max_julong, ABS(max_julong));
51
// Upper bounds for signed integers
52
EXPECT_EQ(max_jbyte, ABS(max_jbyte));
53
EXPECT_EQ(max_jshort, ABS(max_jshort));
54
EXPECT_EQ(max_jint, ABS(max_jint));
55
EXPECT_EQ(max_jlong, ABS(max_jlong));
57
// Lower valid bounds for signed integers
58
EXPECT_EQ(max_jbyte, ABS(min_jbyte + 1));
59
EXPECT_EQ(max_jshort, ABS(min_jshort + 1));
60
EXPECT_EQ(max_jint, ABS(min_jint + 1));
61
EXPECT_EQ(max_jlong, ABS(min_jlong + 1));
63
// Lower bounds for signed integers after explicit FP cast
64
EXPECT_TRUE(ABS((float)min_jbyte) > 0);
65
EXPECT_TRUE(ABS((float)min_jshort) > 0);
66
EXPECT_TRUE(ABS((float)min_jint) > 0);
67
EXPECT_TRUE(ABS((float)min_jlong) > 0);
70
// Now check what happens when we feed invalid arguments.
74
// In release builds, ABS would return incorrect values.
76
TEST(absTest, release_sanity) {
77
EXPECT_EQ(min_jbyte, ABS(min_jbyte));
78
EXPECT_EQ(min_jshort, ABS(min_jshort));
79
EXPECT_EQ(min_jint, ABS(min_jint));
80
EXPECT_EQ(min_jlong, ABS(min_jlong));
85
// In debug builds, ABS would assert.
87
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jbyte,
88
"Error: ABS: argument should not allow overflow") {
90
jbyte r = ABS(min_jbyte); // should fail
91
EXPECT_TRUE(r > 0); // should not be normally reachable
94
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jshort,
95
"Error: ABS: argument should not allow overflow") {
97
jshort r = ABS(min_jshort); // should fail
98
EXPECT_TRUE(r > 0); // should not be normally reachable
101
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jint,
102
"Error: ABS: argument should not allow overflow") {
104
jint r = ABS(min_jint); // should fail
105
EXPECT_TRUE(r > 0); // should not be normally reachable
108
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jlong,
109
"Error: ABS: argument should not allow overflow") {
111
jlong r = ABS(min_jlong); // should fail
112
EXPECT_TRUE(r > 0); // should not be normally reachable