blob: 996db6d01786462c639c5b5e4715f9aff911b514 [file] [log] [blame]
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package io.v.todos.persistence.syncbase;
import org.junit.Test;
import java.util.Random;
import java.util.regex.Pattern;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class IdGeneratorTest {
private static final Pattern COLLECTION_ID_PATTERN = Pattern.compile("[0-9A-Za-z][0-9A-Za-z_]*");
private static final Random RANDOM = new Random();
@Test
public void testLongToPaddedIdentifier() {
IdGenerator
testGeneratorA = new IdGenerator(IdAlphabets.COLLECTION_ID, true),
testGeneratorB = new IdGenerator(IdAlphabets.ROW_NAME, true);
long[] sortedInputs = new long[]{
Long.MIN_VALUE,
Long.MIN_VALUE + 1,
Long.MIN_VALUE + 2,
-1,
0,
1,
Long.MAX_VALUE - 2,
Long.MAX_VALUE - 1,
Long.MAX_VALUE
};
String lastIdA = null, lastIdB = null;
for (long input : sortedInputs) {
String testId = testGeneratorA.longToPaddedIdentifier(input);
if (lastIdA != null) {
assertTrue("IdGenerator.longToPaddedIdentifier generates IDs consistent with " +
"input ordering", testId.compareTo(lastIdA) > 0);
}
lastIdA = testId;
testId = testGeneratorB.longToPaddedIdentifier(input);
if (lastIdB != null) {
assertTrue("IdGenerator.longToPaddedIdentifier generates IDs consistent with " +
"input ordering", testId.compareTo(lastIdB) > 0);
}
lastIdB = testId;
}
for (int i = 0; i < 50; i++) {
// Smoke fuzz test
testGeneratorA.longToPaddedIdentifier(RANDOM.nextLong());
testGeneratorB.longToPaddedIdentifier(RANDOM.nextLong());
}
}
@Test
public void testDecodeChar() {
for (int i = 0; i < IdAlphabets.COLLECTION_ID.radix(); i++) {
char testChar = IdAlphabets.COLLECTION_ID.encodeDigit(i);
assertEquals("IdGenerator.decodeChar('" + testChar + "')",
i, IdAlphabets.COLLECTION_ID.decodeDigit(testChar));
}
}
private void verifyIncrement(String input, String incremented) {
String diagnostic = input + " + 1 = " + incremented;
assertTrue("IdGenerator.increment generates larger IDs than the input (" + diagnostic + ")",
incremented.compareTo(input) > 0);
assertTrue("IdGenerator.increment generates legal IDs (" + diagnostic + ")",
COLLECTION_ID_PATTERN.matcher(incremented).matches());
}
@Test
public void testIncrement() {
IdGenerator testGenerator = new IdGenerator(IdAlphabets.COLLECTION_ID, true);
String[] inputs = new String[]{
"0000",
"AZ00",
"Az00",
"Zz00",
"zz00",
"zzz0",
"zzzz"
};
for (String input : inputs) {
verifyIncrement(input, testGenerator.increment(input, 2));
}
for (int i = 0; i < 50; i++) {
String input = testGenerator.longToPaddedIdentifier(RANDOM.nextLong());
verifyIncrement(input, testGenerator.increment(input, RANDOM.nextInt(input.length())));
}
}
}