corda/sgx-jvm/avian/test/Collections.java
Andras Slemmer 9bb3d6b972 Add 'sgx-jvm/avian/' from commit '09e4fe60d01f4f4bfb6b2976973bb4913ef61edc'
git-subtree-dir: sgx-jvm/avian
git-subtree-mainline: f978eab8d1
git-subtree-split: 09e4fe60d0
2017-03-13 12:18:24 +00:00

69 lines
1.7 KiB
Java

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Collections {
public static void main(String[] args) {
testValues();
testSort();
}
@SuppressWarnings("rawtypes")
private static void testValues() {
Map testMap = java.util.Collections.unmodifiableMap(java.util.Collections.emptyMap());
Collection values = testMap.values();
if (values == null) {
throw new NullPointerException();
}
try {
values.clear();
throw new IllegalStateException("Object should be immutable, exception should have thrown");
} catch (Exception e) {
// expected
}
}
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
private static <T extends Comparable<T>> void expectSorted(List<T> list) {
for (int i = 1; i < list.size(); ++i) {
expect(list.get(i - 1).compareTo(list.get(i)) <= 0);
}
}
private static int pseudoRandom(int seed) {
return 3170425 * seed + 132102;
}
private static <T extends Comparable<T>> int shuffle(List<T> list, int seed) {
for (int i = list.size(); i > 1; --i) {
int i2 = (seed < 0 ? -seed : seed) % i;
T value = list.get(i - 1);
list.set(i - 1, list.get(i2));
list.set(i2, value);
seed = pseudoRandom(seed);
}
return seed;
}
public static void testSort() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 64; ++i) {
list.add(Integer.valueOf(i + 1));
}
;
int random = 12345;
for (int i = 0; i < 32; ++i) {
random = shuffle(list, random);
java.util.Collections.sort(list);
expectSorted(list);
}
}
}