Skip to content

Commit 4ab2901

Browse files
SONARJAVA-5522 test LiteralUtils.doubleLiteralValue
1 parent 318205b commit 4ab2901

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

java-frontend/src/main/java/org/sonar/java/model/LiteralUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ public static Long longLiteralValue(ExpressionTree tree) {
8787

8888
@CheckForNull
8989
public static Double doubleLiteralValue(ExpressionTree expression) {
90+
int sign = 1;
91+
if (expression.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) {
92+
sign = expression.is(Kind.UNARY_MINUS) ? -1 : 1;
93+
expression = ((UnaryExpressionTree) expression).expression();
94+
}
95+
9096
if (expression.is(Kind.FLOAT_LITERAL, Kind.DOUBLE_LITERAL)) {
9197
String value = ((LiteralTree) expression).value().replace("_", "");
9298

93-
return Double.parseDouble(value);
99+
return sign*Double.parseDouble(value);
94100
}
95101
return null;
96102
}

java-frontend/src/test/java/org/sonar/java/model/LiteralUtilsTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ static void beforeAll() {
8585
long y17 = 0XFFL;
8686
long y18 = 0B1100110L;
8787

88+
float f1 = 0f;
89+
float f2 = 0.f;
90+
float f3 = 123.45f;
91+
float f4 = 1_000_000F;
92+
float f5 = +1_000_000_000_000_000_000f;
93+
float f6 = -1_000_000F;
94+
float f7 = -.9f;
95+
float f8 = 0x1.2p3f;
96+
float f9 = 123_456e-7f;
97+
float f10 = (float)0;
98+
float f11 = 0;
99+
float f13 = f1 + 0;
100+
double d1 = 0.;
101+
double d2 = 1.d;
102+
double d3 = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;
103+
double d4 = -1_000_000D;
104+
double d5 = +1_000_000_000_000_000_000d;
105+
double d6 = 23_456e-7d;
106+
double d7 = 0x1.2p3;
107+
double d8 = 0;
108+
double d9 = d1 + 0;
109+
88110
String s1 = "";
89111
String s2 = " ";
90112
String s3 = "not_empty";
@@ -116,6 +138,30 @@ void test_int_and_long_value() {
116138
}
117139
}
118140

141+
@Test
142+
void test_float_value() {
143+
Double[] expectedValues = {0d, 0.d, 123.45d, 1_000_000D, 1_000_000_000_000_000_000d, -1_000_000d, -.9d, 0x1.2p3d, 123_456e-7d, null, null, null};
144+
int idx = 0;
145+
146+
for (VariableTree variableTree : variables) {
147+
if (variableTree.simpleName().name().startsWith("f")) {
148+
assertThat(LiteralUtils.doubleLiteralValue(variableTree.initializer())).isEqualTo(expectedValues[idx++]);
149+
}
150+
}
151+
}
152+
153+
@Test
154+
void test_double_value() {
155+
Double[] expectedValues = {0., 1.d, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, -1_000_000D, +1_000_000_000_000_000_000d, 23_456e-7d, 0x1.2p3, null , null};
156+
int idx = 0;
157+
158+
for (VariableTree variableTree : variables) {
159+
if (variableTree.simpleName().name().startsWith("d")) {
160+
assertThat(LiteralUtils.doubleLiteralValue(variableTree.initializer())).isEqualTo(expectedValues[idx++]);
161+
}
162+
}
163+
}
164+
119165
/**
120166
* Binary, hex and octal int literals are allowed when they fit into 32-bits (jls11 - §3.10.1)
121167
*/

0 commit comments

Comments
 (0)