jdk

Форк
0
/
test_abs.cpp 
115 строк · 3.6 Кб
1
/*
2
 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
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.
8
 *
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).
14
 *
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.
18
 *
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
21
 * questions.
22
 */
23

24
#include "precompiled.hpp"
25
#include "unittest.hpp"
26
#include "memory/allocation.hpp"
27
#include "memory/resourceArea.inline.hpp"
28
#include "runtime/thread.hpp"
29

30
TEST(absTest, sanity) {
31
  // Simple integer cases
32
  EXPECT_EQ(0, ABS(0));
33
  EXPECT_EQ(1, ABS(1));
34
  EXPECT_EQ(1, ABS(-1));
35

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));
40

41
  EXPECT_EQ(0.0, ABS(0.0));
42
  EXPECT_EQ(1.0, ABS(1.0));
43
  EXPECT_EQ(1.0, ABS(-1.0));
44

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));
50

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));
56

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));
62

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);
68
}
69

70
// Now check what happens when we feed invalid arguments.
71

72
#ifndef ASSERT
73

74
// In release builds, ABS would return incorrect values.
75

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));
81
}
82

83
#else
84

85
// In debug builds, ABS would assert.
86

87
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jbyte,
88
  "Error: ABS: argument should not allow overflow") {
89

90
  jbyte r = ABS(min_jbyte); // should fail
91
  EXPECT_TRUE(r > 0); // should not be normally reachable
92
}
93

94
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jshort,
95
  "Error: ABS: argument should not allow overflow") {
96

97
  jshort r = ABS(min_jshort); // should fail
98
  EXPECT_TRUE(r > 0); // should not be normally reachable
99
}
100

101
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jint,
102
  "Error: ABS: argument should not allow overflow") {
103

104
  jint r = ABS(min_jint); // should fail
105
  EXPECT_TRUE(r > 0); // should not be normally reachable
106
}
107

108
TEST_VM_ASSERT_MSG(absTest, debug_sanity_min_jlong,
109
  "Error: ABS: argument should not allow overflow") {
110

111
  jlong r = ABS(min_jlong); // should fail
112
  EXPECT_TRUE(r > 0); // should not be normally reachable
113
}
114

115
#endif
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.