s.add(new Random());
return s;
}
public static void testPySetAsJavaSet() {
PySet s = new PySet();
String v = "value";
check(s.add(v));// Add a String as it should be wrapped in PyString
check(!s.add(v));
String[] asArray = (String[])s.toArray(new String[0]);// The array type should be the same
// and it should be resized properly
check(asArray.length == 1);
check(asArray[0] == v);
Object[] naiveArray = s.toArray();
check(naiveArray.length == 1);
check(naiveArray[0] == v);
// Add a Random as it should be wrapped in a generic PyObject; go through addAll to give it
// a little exercise
Random rand = new Random();
check(s.addAll(Generic.list(rand)));
check(!s.addAll(Generic.list(rand, v)));
naiveArray = s.toArray();
check(naiveArray.length == 2);
for (Object object : naiveArray) {
if (object instanceof String) {
check(object == v);
} else {
check(object == rand, "Should be 'value' or rand, not " + object);
}
}
check(!s.remove(new Random()), "The Random in the set shouldn't match a new Random");
check(s.remove(rand));
check(s.removeAll(Generic.list(rand, v)),
"The set should contain v and indicate it removed it");
check(s.isEmpty());
check(s.addAll(Generic.list(rand, v)));
check(2 == s.size(), "There should be 2 items, not " + s.size());
check(s.containsAll(Generic.list(rand, v)));
check(!s.containsAll(Generic.list(rand, v, "other")));
check(s.retainAll(Generic.list(rand)));
check(!s.retainAll(Generic.list(rand)));
check(s.addAll(Generic.list(rand, v)));
check(2 == s.size(), "There should be 2 items, not " + s.size());
check(!s.addAll(Generic.list(rand, v)));
check(2 == s.size(), "There should be 2 items, not " + s.size());
}