Formatted source, added/updated copyright headers
- Also added formatting settings for Eclipse IDE
This commit is contained in:
@@ -1,3 +1,21 @@
|
||||
/*
|
||||
* JPUtil
|
||||
* Copyright (C) 2019-2021 OLEGSHA/Javapony and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.jputil.chars.stringUtil;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
@@ -9,49 +27,50 @@ import org.junit.Test;
|
||||
import ru.windcorp.jputil.chars.StringUtil;
|
||||
|
||||
public class SplitAtTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testExamplesFromDocs() {
|
||||
test("a.b.c", new int[] {1, 3}, new String[] {"a", "b", "c"});
|
||||
test("a..b", new int[] {1, 2}, new String[] {"a", "", "b"});
|
||||
test(".b.", new int[] {0, 2}, new String[] {"", "b", ""});
|
||||
test("a.b", new int[] {1, 1, 1}, new String[] {"a", "", "", "b"});
|
||||
test("a.b.c", new int[] { 1, 3 }, new String[] { "a", "b", "c" });
|
||||
test("a..b", new int[] { 1, 2 }, new String[] { "a", "", "b" });
|
||||
test(".b.", new int[] { 0, 2 }, new String[] { "", "b", "" });
|
||||
test("a.b", new int[] { 1, 1, 1 }, new String[] { "a", "", "", "b" });
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIndexPermutations() {
|
||||
Random random = new Random(0);
|
||||
|
||||
|
||||
int stringLength = 1000;
|
||||
char[] chars = new char[stringLength];
|
||||
|
||||
|
||||
for (int i = 0; i < stringLength; ++i) {
|
||||
chars[i] = (char) ('a' + random.nextInt('z' - 'a'));
|
||||
}
|
||||
|
||||
|
||||
String src = new String(chars);
|
||||
|
||||
|
||||
int[] indices = new int[100];
|
||||
|
||||
|
||||
for (int i = 0; i < indices.length; ++i) {
|
||||
indices[i] = random.nextInt(stringLength);
|
||||
}
|
||||
|
||||
|
||||
String[] expected = StringUtil.splitAt(src, indices);
|
||||
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
shuffleArray(indices, random);
|
||||
|
||||
|
||||
int[] copy = indices.clone();
|
||||
test(src, indices, expected);
|
||||
assertArrayEquals(indices, copy); // Make sure indices array hasn't changed
|
||||
assertArrayEquals(indices, copy); // Make sure indices array hasn't
|
||||
// changed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Shamelessly copied from
|
||||
// https://stackoverflow.com/a/1520212/4463352
|
||||
// https://stackoverflow.com/a/1520212/4463352
|
||||
// Thanks, https://stackoverflow.com/users/15459/philho!
|
||||
|
||||
|
||||
// Implementing Fisher–Yates shuffle
|
||||
private static void shuffleArray(int[] ar, Random random) {
|
||||
for (int i = ar.length - 1; i > 0; i--) {
|
||||
|
@@ -1,72 +1,73 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.BinUtil;
|
||||
|
||||
public class BinUtilIsPowerOf2Test {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
test(-1);
|
||||
test(0);
|
||||
test(1);
|
||||
|
||||
test(15);
|
||||
test(16);
|
||||
test(17);
|
||||
|
||||
test(1 << 30);
|
||||
test(Integer.MAX_VALUE);
|
||||
test(Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void random() {
|
||||
Random random = new Random(0);
|
||||
|
||||
for (int x = 0; x < 10000; ++x) {
|
||||
test(x);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
test(random.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
void test(int x) {
|
||||
assertEquals("Round, x = " + x, referenceIsPowerOf2(x), BinUtil.isPowerOf2(x));
|
||||
}
|
||||
|
||||
boolean referenceIsPowerOf2(int x) {
|
||||
for (int power = 1; power > 0; power *= 2) {
|
||||
if (x == power) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.BinUtil;
|
||||
|
||||
public class BinUtilIsPowerOf2Test {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
test(-1);
|
||||
test(0);
|
||||
test(1);
|
||||
|
||||
test(15);
|
||||
test(16);
|
||||
test(17);
|
||||
|
||||
test(1 << 30);
|
||||
test(Integer.MAX_VALUE);
|
||||
test(Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void random() {
|
||||
Random random = new Random(0);
|
||||
|
||||
for (int x = 0; x < 10000; ++x) {
|
||||
test(x);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
test(random.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
void test(int x) {
|
||||
assertEquals("Round, x = " + x, referenceIsPowerOf2(x), BinUtil.isPowerOf2(x));
|
||||
}
|
||||
|
||||
boolean referenceIsPowerOf2(int x) {
|
||||
for (int power = 1; power > 0; power *= 2) {
|
||||
if (x == power) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,75 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.BinUtil;
|
||||
|
||||
public class BinUtilRoundTest {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
test(1);
|
||||
test(2);
|
||||
test(3);
|
||||
test(4);
|
||||
test(7);
|
||||
test(8);
|
||||
test(9);
|
||||
|
||||
test(1023);
|
||||
test(1024);
|
||||
test(1025);
|
||||
|
||||
test((1 << 16) - 1);
|
||||
test(1 << 16);
|
||||
test((1 << 16) + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void random() {
|
||||
Random random = new Random(0);
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
test(random.nextInt((1 << 30) - 2) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void test(int x) {
|
||||
assertEquals("Round, x = " + x, referenceRound(x), BinUtil.roundToGreaterPowerOf2(x));
|
||||
assertEquals("Greater, x = " + x, referenceGreater(x), BinUtil.closestGreaterPowerOf2(x));
|
||||
}
|
||||
|
||||
int referenceGreater(int x) {
|
||||
int p;
|
||||
for (p = 1; p <= x; p *= 2);
|
||||
return p;
|
||||
}
|
||||
|
||||
int referenceRound(int x) {
|
||||
int p;
|
||||
for (p = 1; p < x; p *= 2);
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.BinUtil;
|
||||
|
||||
public class BinUtilRoundTest {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
test(1);
|
||||
test(2);
|
||||
test(3);
|
||||
test(4);
|
||||
test(7);
|
||||
test(8);
|
||||
test(9);
|
||||
|
||||
test(1023);
|
||||
test(1024);
|
||||
test(1025);
|
||||
|
||||
test((1 << 16) - 1);
|
||||
test(1 << 16);
|
||||
test((1 << 16) + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void random() {
|
||||
Random random = new Random(0);
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
test(random.nextInt((1 << 30) - 2) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void test(int x) {
|
||||
assertEquals("Round, x = " + x, referenceRound(x), BinUtil.roundToGreaterPowerOf2(x));
|
||||
assertEquals("Greater, x = " + x, referenceGreater(x), BinUtil.closestGreaterPowerOf2(x));
|
||||
}
|
||||
|
||||
int referenceGreater(int x) {
|
||||
int p;
|
||||
for (p = 1; p <= x; p *= 2)
|
||||
;
|
||||
return p;
|
||||
}
|
||||
|
||||
int referenceRound(int x) {
|
||||
int p;
|
||||
for (p = 1; p < x; p *= 2)
|
||||
;
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,69 +1,71 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.CoordinatePacker;
|
||||
|
||||
public class CoordinatePacker2Test {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
check(0, 0);
|
||||
check(0, 42);
|
||||
check(42, 0);
|
||||
check(1, 1);
|
||||
check(-1, -1);
|
||||
|
||||
for (int a : new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}) {
|
||||
for (int b : new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}) {
|
||||
check(a, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomValues() {
|
||||
Random random = new Random(0);;
|
||||
|
||||
for (int i = 0; i < 1000000; ++i) {
|
||||
check(
|
||||
random.nextInt(),
|
||||
random.nextInt()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void check(int a, int b) {
|
||||
|
||||
long packed = CoordinatePacker.pack2IntsIntoLong(a, b);
|
||||
|
||||
int unpackedA = CoordinatePacker.unpack2IntsFromLong(packed, 0);
|
||||
int unpackedB = CoordinatePacker.unpack2IntsFromLong(packed, 1);
|
||||
|
||||
assertEquals(a, unpackedA);
|
||||
assertEquals(b, unpackedB);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.CoordinatePacker;
|
||||
|
||||
public class CoordinatePacker2Test {
|
||||
|
||||
@Test
|
||||
public void cornerCases() {
|
||||
check(0, 0);
|
||||
check(0, 42);
|
||||
check(42, 0);
|
||||
check(1, 1);
|
||||
check(-1, -1);
|
||||
|
||||
for (int a : new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE, 0 }) {
|
||||
for (int b : new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE, 0 }) {
|
||||
check(a, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomValues() {
|
||||
Random random = new Random(0);
|
||||
;
|
||||
|
||||
for (int i = 0; i < 1000000; ++i) {
|
||||
check(
|
||||
random.nextInt(),
|
||||
random.nextInt()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void check(int a, int b) {
|
||||
|
||||
long packed = CoordinatePacker.pack2IntsIntoLong(a, b);
|
||||
|
||||
int unpackedA = CoordinatePacker.unpack2IntsFromLong(packed, 0);
|
||||
int unpackedB = CoordinatePacker.unpack2IntsFromLong(packed, 1);
|
||||
|
||||
assertEquals(a, unpackedA);
|
||||
assertEquals(b, unpackedB);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,71 +1,72 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.CoordinatePacker;
|
||||
|
||||
public class CoordinatePacker3Test {
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.windcorp.progressia.common.util.CoordinatePacker;
|
||||
|
||||
public class CoordinatePacker3Test {
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,172 +1,173 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.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.progressia.common.util.namespaces.IllegalIdException;
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedUtil;
|
||||
|
||||
public class NamespacedTest {
|
||||
|
||||
class TestNamespaced extends Namespaced {
|
||||
|
||||
public TestNamespaced(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void shouldReject(String a, String b) {
|
||||
try {
|
||||
new TestNamespaced(NamespacedUtil.getId(a, b));
|
||||
} catch (IllegalIdException | NullPointerException e) {
|
||||
try {
|
||||
new TestNamespaced(NamespacedUtil.getId(b, a));
|
||||
} catch (IllegalIdException | NullPointerException e1) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new AssertionFailedError("Expected NPE or IllegalIdException 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[NamespacedUtil.MAX_NAME_LENGTH];
|
||||
for (int attempt = 0; attempt < 10000; ++attempt) {
|
||||
random.nextBytes(bytes);
|
||||
bytes[bytes.length - 1] = '!'; // 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(NamespacedUtil.getId(namespace, name));
|
||||
TestNamespaced b = new TestNamespaced(NamespacedUtil.getId(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(NamespacedUtil.getId(namespace, name));
|
||||
|
||||
doesNotContain.add(c);
|
||||
}
|
||||
|
||||
for (TestNamespaced x : contains) {
|
||||
Iterator<TestNamespaced> it = doesNotContain.iterator();
|
||||
while (it.hasNext()) {
|
||||
TestNamespaced next = it.next();
|
||||
if (next.getId().equals(x.getId())) {
|
||||
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(NamespacedUtil.MAX_NAME_LENGTH - 3) + 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);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.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.progressia.common.util.namespaces.IllegalIdException;
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedUtil;
|
||||
|
||||
public class NamespacedTest {
|
||||
|
||||
class TestNamespaced extends Namespaced {
|
||||
|
||||
public TestNamespaced(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void shouldReject(String a, String b) {
|
||||
try {
|
||||
new TestNamespaced(NamespacedUtil.getId(a, b));
|
||||
} catch (IllegalIdException | NullPointerException e) {
|
||||
try {
|
||||
new TestNamespaced(NamespacedUtil.getId(b, a));
|
||||
} catch (IllegalIdException | NullPointerException e1) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new AssertionFailedError("Expected NPE or IllegalIdException 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[NamespacedUtil.MAX_NAME_LENGTH];
|
||||
for (int attempt = 0; attempt < 10000; ++attempt) {
|
||||
random.nextBytes(bytes);
|
||||
bytes[bytes.length - 1] = '!'; // 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(NamespacedUtil.getId(namespace, name));
|
||||
TestNamespaced b = new TestNamespaced(NamespacedUtil.getId(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(NamespacedUtil.getId(namespace, name));
|
||||
|
||||
doesNotContain.add(c);
|
||||
}
|
||||
|
||||
for (TestNamespaced x : contains) {
|
||||
Iterator<TestNamespaced> it = doesNotContain.iterator();
|
||||
while (it.hasNext()) {
|
||||
TestNamespaced next = it.next();
|
||||
if (next.getId().equals(x.getId())) {
|
||||
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(NamespacedUtil.MAX_NAME_LENGTH - 3) + 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,100 +1,104 @@
|
||||
/*******************************************************************************
|
||||
* Progressia
|
||||
* Copyright (C) 2020 Wind Corporation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.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.progressia.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();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package ru.windcorp.progressia.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.progressia.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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user