1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
//////
// There should be a copyright thing here but I'm in too much of a hurry to add
// one right now. I don't care much what the copyright is though so if someone
// wants to put the appropriate one here, go right ahead (I think GPL probably
// unless that causes a problem with running on proprietory JVMs or testing
// proprietory class libraries or anything.
//////
import java.util.*;
/**
* Test of most of the methods in the Arrays class. The class prints out a
* single pass/fail message, and enumerates all failures. The message is in the
* PASS: or FAIL: format that dejagnu uses, but other than that I don't know
* enough to make it a "proper" testsuite.
*/
public class ArraysTest {
static int passed = 0;
static int failed = 0;
public static void main(String[] args) {
testBool();
testByte();
testChar();
testShort();
testInt();
testLong();
testFloat();
testDouble();
testObject();
if (failed != 0) {
System.out.println(" (" + failed + " fails and " + passed + " passes).");
} else {
System.out.println("PASSED: [Arrays] All " + passed + " tests.");
}
}
static void testBool() {
boolean[] a1 = new boolean[] {true, false, false, true, true, false, true};
boolean[] a2 = new boolean[] {false, false, false, true, true, true, true};
boolean[] a3 = new boolean[] {true, false, false, true, true, false, true};
passfail("boolean equals", !Arrays.equals(a1, a2) && Arrays.equals(a1, a3));
Arrays.fill(a1, false);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == false;
}
passfail("boolean fill", filled);
}
static void testByte() {
byte[] a1 = new byte[] {3, -2, 0, 1, 4, 0, -5};
byte[] a2 = new byte[] {-5, -2, 0, 0, 1, 3, 4};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("byte sort", sorted);
passfail("byte search", Arrays.binarySearch(a2, (byte)1) == 4 &&
Arrays.binarySearch(a2, (byte)-1) == -3);
passfail("byte equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, (byte)6);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == (byte)6;
}
passfail("byte fill", filled);
}
static void testChar() {
char[] a1 = new char[] {'i', 'd', 'f', 'g', 'j', 'f', 'a'};
char[] a2 = new char[] {'a', 'd', 'f', 'f', 'g', 'i', 'j'};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("char sort", sorted);
passfail("char search", Arrays.binarySearch(a2, 'i') == 5 &&
Arrays.binarySearch(a2, 'e') == -3);
passfail("char equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, 'Q');
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == 'Q';
}
passfail("char fill", filled);
}
static void testShort() {
short[] a1 = new short[] {3, -2, 0, 1, 4, 0, -5};
short[] a2 = new short[] {-5, -2, 0, 0, 1, 3, 4};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("short sort", sorted);
passfail("short search", Arrays.binarySearch(a2, (short)1) == 4 &&
Arrays.binarySearch(a2, (short)-1) == -3);
passfail("short equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, (short)6);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == (short)6;
}
passfail("short fill", filled);
}
static void testInt() {
int[] a1 = new int[] {3, -2, 0, 1, 4, 0, -5};
int[] a2 = new int[] {-5, -2, 0, 0, 1, 3, 4};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("int sort", sorted);
passfail("int search", Arrays.binarySearch(a2, 1) == 4 &&
Arrays.binarySearch(a2, -1) == -3);
passfail("int equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, 6);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == 6;
}
passfail("int fill", filled);
}
static void testLong() {
long[] a1 = new long[] {3, -2, 0, 1, 4, 0, -5};
long[] a2 = new long[] {-5, -2, 0, 0, 1, 3, 4};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("long sort", sorted);
passfail("long search", Arrays.binarySearch(a2, 1L) == 4 &&
Arrays.binarySearch(a2, -1L) == -3);
passfail("long equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, 6L);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == 6L;
}
passfail("long fill", filled);
}
static void testFloat() {
float[] a1 = new float[] {-4.0f, 75.3f, Float.POSITIVE_INFINITY, -0.0f,
-3324.342f, 0.0f, 3.14f, 2.5f, 1.0f, 1.0f};
float[] a2 = new float[] {-3324.342f, -4.0f, -0.0f, 0.0f, 1.0f, 1.0f, 2.5f,
3.14f, 75.3f, Float.POSITIVE_INFINITY};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("float sort", sorted);
passfail("float search", Arrays.binarySearch(a2, 3.14f) == 7 &&
Arrays.binarySearch(a2, -1.0f) == -3);
passfail("float equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, 27.0f);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == 27.0f;
}
passfail("float fill", filled);
}
static void testDouble() {
double[] a1 = new double[] {-4.0d, 75.3d, Double.POSITIVE_INFINITY, -0.0d,
-3324.342d, 0.0d, 3.14d, 2.5d, 1.0d, 1.0d};
double[] a2 = new double[] {-3324.342d, -4.0d, -0.0d, 0.0d, 1.0d, 1.0d,
2.5d, 3.14d, 75.3d, Double.POSITIVE_INFINITY};
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = !(a1[i] > a1[i+1]); // the odd way of writing <= is so that we
// aren't tooo mean to NaNs
}
passfail("double sort", sorted);
passfail("double search", Arrays.binarySearch(a2, 3.14d) == 7 &&
Arrays.binarySearch(a2, -1.0d) == -3);
passfail("double equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, 27.0d);
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i] == 27.0d;
}
passfail("double fill", filled);
}
static void testObject() {
String[] a1 = new String[] {"this", "is", "a", "string", "test", "which",
"will", "hopefully", "demonstrate", "that",
"sorting", "works"};
String[] a2 = new String[] {"a", "demonstrate", "hopefully", "is",
"sorting", "string", "test", "that", "this",
"which", "will", "works"};
String[] a3 = new String[] {"this", "is", "a", "reverse", "string", "test",
"which", "will", "hopefully", "demonstrate",
"that", "sorting", "works", "with",
"comparators"};
String[] a4 = new String[] {"works", "with", "will", "which", "this",
"that", "test", "string", "sorting", "reverse",
"is", "hopefully", "demonstrate", "comparators",
"a"};
final String list = "[works, with, will, which, this, that, test, string," +
" sorting, reverse, is, hopefully, demonstrate," +
" comparators, a]";
boolean firstEq = Arrays.equals(a1, a2);
Arrays.sort(a1);
boolean sorted = true;
for (int i = 0; sorted && i < a1.length - 1; i++) {
sorted = a1[i].compareTo(a1[i+1]) <= 0;
}
passfail("object sort", sorted);
passfail("object search", Arrays.binarySearch(a2, "hopefully") == 2 &&
Arrays.binarySearch(a2, "strange") == -6);
passfail("object equals", !firstEq && Arrays.equals(a1, a2));
Arrays.fill(a1, "blah");
boolean filled = true;
for (int i = 0; filled && i < a1.length; i++) {
filled = a1[i].equals("blah");
}
passfail("object fill", filled);
Comparator c = new ReverseOrder();
Arrays.sort(a3, c);
passfail("comparator sort", Arrays.equals(a3, a4));
passfail("comparator search", Arrays.binarySearch(a4, "sorting", c) == 8 &&
Arrays.binarySearch(a4, "nice", c) == -11);
// toList doesn't exist -gcb
// passfail("toList toString", Arrays.toList(a4).toString().equals(list));
}
static void passfail(String desc, boolean didpass) {
if (didpass) {
passed++;
} else {
if (failed++ != 0) {
System.out.print(", " + desc);
} else {
System.out.print("FAILED: [Arrays] " + desc);
}
}
}
}
class ReverseOrder implements Comparator {
public int compare(Object a, Object b) {
return -((Comparable)a).compareTo(b);
}
}
|