Skip to content

Commit

Permalink
Return 'inf' and '-inf' like Redis does, add Coder tests (apache#6658)
Browse files Browse the repository at this point in the history
Co-authored-by: Ray Ingles <[email protected]>
  • Loading branch information
ringles and ringles authored Jun 29, 2021
1 parent bc08aa0 commit cdc7fd8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ public static String bytesToString(byte[] bytes) {

public static String doubleToString(double d) {
if (d == Double.POSITIVE_INFINITY) {
return "Infinity";
return "inf";
}
if (d == Double.NEGATIVE_INFINITY) {
return "-Infinity";
return "-inf";
}

String stringValue = String.valueOf(d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/
package org.apache.geode.redis.internal.netty;

import static org.apache.geode.redis.internal.netty.Coder.bytesToDouble;
import static org.apache.geode.redis.internal.netty.Coder.bytesToString;
import static org.apache.geode.redis.internal.netty.Coder.doubleToString;
import static org.apache.geode.redis.internal.netty.Coder.equalsIgnoreCaseBytes;
import static org.apache.geode.redis.internal.netty.Coder.isInfinity;
import static org.apache.geode.redis.internal.netty.Coder.isNaN;
Expand Down Expand Up @@ -63,6 +65,13 @@ public void isInfinity_returnsCorrectly(String string, boolean isPositiveInfinit
assertThat(isNaN(bytes)).isEqualTo(isNaN);
}

@Test
@Parameters(method = "infinityReturnStrings")
public void doubleToString_processesLikeRedis(String inputString, String expectedString) {
byte[] bytes = stringToBytes(inputString);
assertThat(doubleToString(bytesToDouble(bytes))).isEqualTo(expectedString);
}

@SuppressWarnings("unused")
private Object[] stringPairs() {
// string1, string2
Expand Down Expand Up @@ -102,4 +111,18 @@ private Object[] infinityStrings() {
new Object[] {null, false, false, false}
};
}

@SuppressWarnings("unused")
private Object[] infinityReturnStrings() {
// string, expectedString
return new Object[] {
new Object[] {"inf", "inf"},
new Object[] {"+inf", "inf"},
new Object[] {"Infinity", "inf"},
new Object[] {"+Infinity", "inf"},
new Object[] {"-inf", "-inf"},
new Object[] {"-Infinity", "-inf"},
};
}

}

0 comments on commit cdc7fd8

Please sign in to comment.