Skip to content

Commit c75fb98

Browse files
committed
Unit tests for predecessor
1 parent 565d322 commit c75fb98

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

firebase-database/src/main/java/com/google/firebase/database/core/utilities/PushIdGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static final String predecessor(String key) {
7171
Validation.validateNullableKey(key);
7272
Integer num = tryParseInt(key);
7373
if (num != null) {
74-
if (Long.valueOf(num) - 1 < Integer.MIN_VALUE) {
74+
if (num == Integer.MIN_VALUE) {
7575
return ChildKey.MIN_KEY_NAME;
7676
}
7777
return String.valueOf(num - 1);
@@ -98,7 +98,7 @@ public static final String successor(String key) {
9898
Validation.validateNullableKey(key);
9999
Integer num = tryParseInt(key);
100100
if (num != null) {
101-
if (num + 1L > Integer.MAX_VALUE) {
101+
if (num == Integer.MAX_VALUE) {
102102
// See https://firebase.google.com/docs/database/web/lists-of-data#data-order
103103
return String.valueOf(MIN_PUSH_CHAR);
104104
}

firebase-database/src/test/java/com/google/firebase/database/PushIdGeneratorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.database;
1616

1717
import static com.google.firebase.database.snapshot.ChildKey.MAX_KEY_NAME;
18+
import static com.google.firebase.database.snapshot.ChildKey.MIN_KEY_NAME;
1819
import static org.junit.Assert.assertEquals;
1920

2021
import com.google.firebase.database.core.utilities.PushIdGenerator;
@@ -56,4 +57,21 @@ public void testSuccessorBasic() {
5657
assertEquals(
5758
PushIdGenerator.successor("abc" + MIN_PUSH_CHAR), "abc" + MIN_PUSH_CHAR + MIN_PUSH_CHAR);
5859
}
60+
61+
@Test
62+
public void testPredecessorSpecialValue() {
63+
assertEquals(
64+
PushIdGenerator.predecessor(String.valueOf(MIN_PUSH_CHAR)),
65+
String.valueOf(Integer.MAX_VALUE));
66+
assertEquals(PushIdGenerator.predecessor(String.valueOf(Integer.MIN_VALUE)), MIN_KEY_NAME);
67+
}
68+
69+
@Test
70+
public void testPredecessorBasicValue() {
71+
assertEquals(
72+
PushIdGenerator.predecessor("abc"),
73+
"abb"
74+
+ StringUtils.repeat(Character.toString(MAX_PUSH_CHAR), MAX_KEY_LEN - "abc".length()));
75+
assertEquals(PushIdGenerator.predecessor("abc" + MIN_PUSH_CHAR), "abc");
76+
}
5977
}

0 commit comments

Comments
 (0)