Initial commit

This commit is contained in:
2020-07-29 12:30:34 +03:00
commit e96a30c275
108 changed files with 7929 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package ru.windcorp.optica.util;
import static org.junit.Assert.assertEquals;
import java.util.Random;
import org.junit.Test;
import ru.windcorp.optica.common.util.CoordinatePacker;
public class CoordinatePackerTest {
@Test
public void cornerCases() {
check(0, 0, 0);
check(0, 0, 42);
check(0, 42, 0);
check(42, 0, 0);
check(1, 1, 1);
check(-1, -1, -1);
check(1 << 19, 1 << 19, 1 << 19);
check((1 << 20) - 1, (1 << 20) - 1, (1 << 20) - 1);
check(-(1 << 19), -(1 << 19), -(1 << 19));
}
@Test
public void randomValues() {
Random random = new Random(0);
int bound = 1 << 20;
for (int i = 0; i < 1000000; ++i) {
check(
random.nextInt(bound) * (random.nextBoolean() ? 1 : -1),
random.nextInt(bound) * (random.nextBoolean() ? 1 : -1),
random.nextInt(bound) * (random.nextBoolean() ? 1 : -1)
);
}
}
private void check(int a, int b, int c) {
long packed = CoordinatePacker.pack3IntsIntoLong(a, b, c);
int unpackedA = CoordinatePacker.unpack3IntsFromLong(packed, 0);
int unpackedB = CoordinatePacker.unpack3IntsFromLong(packed, 1);
int unpackedC = CoordinatePacker.unpack3IntsFromLong(packed, 2);
assertEquals(a, unpackedA);
assertEquals(b, unpackedB);
assertEquals(c, unpackedC);
}
}

View File

@@ -0,0 +1,153 @@
package ru.windcorp.optica.util;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import org.junit.Test;
import junit.framework.AssertionFailedError;
import ru.windcorp.optica.common.util.Namespaced;
public class NamespacedTest {
class TestNamespaced extends Namespaced {
public TestNamespaced(String namespace, String name) {
super(namespace, name);
}
}
void shouldReject(String a, String b) {
try {
new TestNamespaced(a, b);
} catch (IllegalArgumentException | NullPointerException e) {
try {
new TestNamespaced(b, a);
} catch (IllegalArgumentException | NullPointerException e1) {
return;
}
}
throw new AssertionFailedError("Expected NPE or IAE for: \"" + a + "\":\"" + b + "\"");
}
@Test
public void shouldAllow() {
new TestNamespaced("Something", "Usual");
new TestNamespaced("Vry", "Sml");
new TestNamespaced("ALL", "CAPS");
new TestNamespaced("WithDigits12345", "MoreDigits67890");
}
@Test
public void shouldRejectNulls() {
shouldReject(null, "Normal");
shouldReject(null, null);
}
@Test
public void shouldRejectInvalid() {
shouldReject("Contains-hyphens", "Normal");
shouldReject("Contains_underscores", "Normal");
shouldReject("ALL_CAPS_WITH_UNDERSCORES", "Normal");
shouldReject("Contains whitespace", "Normal");
shouldReject("0StartsWithDigit", "Normal");
shouldReject("lowerCamelCase", "Normal");
shouldReject("XS", "Normal");
shouldReject("", "Normal");
shouldReject("Contains:separators", "Normal");
shouldReject("СодержитРусский", "Normal");
}
@Test
public void shouldRejectGarbage() {
Random random = new Random(0);
byte[] bytes = new byte[1024];
for (int attempt = 0; attempt < 10000; ++attempt) {
random.nextBytes(bytes);
bytes[0] = 'a'; // Make sure it is invalid
shouldReject(new String(bytes), "ContainsUtterGarbage");
}
}
@Test
public void testHashCodeAndEquals() {
HashSet<TestNamespaced> hashSet = new HashSet<>();
Collection<TestNamespaced> contains = new ArrayList<>();
Collection<TestNamespaced> doesNotContain = new ArrayList<>();
Random random = new Random(0);
for (int i = 0; i < 256; ++i) {
String namespace = getRandomValidString(random);
String name = getRandomValidString(random);
TestNamespaced a = new TestNamespaced(namespace, name);
TestNamespaced b = new TestNamespaced(namespace, name);
contains.add(a);
hashSet.add(b);
}
for (int i = 0; i < 256; ++i) {
String namespace = getRandomValidString(random);
String name = getRandomValidString(random);
TestNamespaced c = new TestNamespaced(namespace, name);
doesNotContain.add(c);
}
for (TestNamespaced x : contains) {
Iterator<TestNamespaced> it = doesNotContain.iterator();
while (it.hasNext()) {
TestNamespaced next = it.next();
if (next.getName().equals(x.getName()) && next.getNamespace().equals(x.getNamespace())) {
it.remove();
}
}
}
for (TestNamespaced test : contains) {
assertTrue(hashSet.contains(test));
}
for (TestNamespaced test : doesNotContain) {
assertFalse(hashSet.contains(test));
}
}
String getRandomValidString(Random random) {
char[] chars = new char[random.nextInt(100) + 3];
for (int i = 0; i < chars.length; ++i) {
switch (random.nextInt(3)) {
case 0:
if (i != 0) {
chars[i] = (char) ('a' + random.nextInt('z' - 'a'));
break;
}
case 1:
if (i != 0) {
chars[i] = (char) ('0' + random.nextInt('9' - '0'));
break;
}
case 2:
chars[i] = (char) ('A' + random.nextInt('Z' - 'A'));
break;
}
}
return new String(chars);
}
}

View File

@@ -0,0 +1,83 @@
package ru.windcorp.optica.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Deque;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Random;
import org.junit.Test;
import com.google.common.collect.Iterators;
import ru.windcorp.optica.common.util.StashingStack;
public class StashingStackTest {
@Test
public void normalOperation() {
final int size = 256;
final int operations = 2 << 16;
final long seed = 0;
Random random = new Random(seed);
Deque<String> spares = new LinkedList<>();
for (int i = 0; i < size; ++i) {
spares.add(Integer.toString(i));
}
StashingStack<String> stashing = new StashingStack<>(spares);
Deque<String> reference = new LinkedList<>();
for (int i = 0; i < operations; ++i) {
boolean isFull = stashing.isFull();
boolean isEmpty = stashing.isEmpty();
assertTrue("isFull", isFull == (reference.size() == size));
assertTrue("isEmpty", isEmpty == reference.isEmpty());
assertEquals("size", reference.size(), stashing.getSize());
if (isFull || (!isEmpty && random.nextBoolean())) {
if (random.nextBoolean()) {
String popped = reference.pop();
assertEquals("pop", popped, stashing.pop());
spares.push(popped);
} else {
String peeked = reference.peek();
assertEquals("peek", peeked, stashing.peek());
}
} else {
reference.push(spares.pop());
stashing.push();
}
}
assertTrue("remaining", Iterators.elementsEqual(
reference.descendingIterator(),
stashing.iterator()
));
}
@Test
public void cornerCases() {
StashingStack<Object> stack = new StashingStack<>(10);
assertNull(stack.peek());
assertNull(stack.pop());
assertNull(stack.push());
}
@Test(expected = NoSuchElementException.class)
public void noSuchElementWhenGetHead() {
new StashingStack<>(10).getHead();
}
@Test(expected = NoSuchElementException.class)
public void noSuchElementWhenRemoveHead() {
new StashingStack<>(10).removeHead();
}
}