Skip to content

Instantly share code, notes, and snippets.

@Icaro-Lima
Last active December 4, 2017 23:05
Show Gist options
  • Save Icaro-Lima/9fc095f30973a8e242ed2ae1f6150e7b to your computer and use it in GitHub Desktop.
Save Icaro-Lima/9fc095f30973a8e242ed2ae1f6150e7b to your computer and use it in GitHub Desktop.
@Override
@Test
public void testTDG() {
Random rand = new Random();
final boolean DEBUG = false;
final int COUNT_TESTS = 1000;
final int MAX_OPERATIONS = 1000;
final int MAX_INTEGER = 20;
for (int t = 0; t < COUNT_TESTS; t++) {
int operations = rand.nextInt(MAX_OPERATIONS);
ArrayList<Integer> arrayList = new ArrayList<>();
RecursiveDoubleLinkedListImpl<Integer> linkedList = new RecursiveDoubleLinkedListImpl<>();
for (int i = 0; i < operations; i++) {
int ch = rand.nextInt(9);
if (ch == 0) {
Integer element = rand.nextInt(20);
arrayList.add(element);
linkedList.insert(element);
if (DEBUG) {
System.out.println("Inserindo " + element + " no fim.");
}
} else if (ch == 1) {
Assert.assertEquals(arrayList.size(), linkedList.size());
} else if (ch == 2) {
Assert.assertEquals(arrayList.isEmpty(), linkedList.isEmpty());
} else if (ch == 3) {
Integer element = rand.nextInt(MAX_INTEGER);
int index = arrayList.indexOf(element);
if (index == -1) {
Assert.assertNull(linkedList.search(element));
} else {
Assert.assertEquals(arrayList.get(index), linkedList.search(element));
}
} else if (ch == 4) {
Integer element = rand.nextInt(MAX_INTEGER);
arrayList.remove(element);
linkedList.remove(element);
if (DEBUG) {
System.out.println("Removendo " + element + ".");
}
} else if (ch == 5) {
Assert.assertArrayEquals(arrayList.toArray(), linkedList.toArray());
} else if (ch == 6) {
Integer element = rand.nextInt(MAX_INTEGER);
arrayList.add(0, element);
linkedList.insertFirst(element);
if (DEBUG) {
System.out.println("Inserindo " + element + " no começo.");
}
} else if (ch == 7) {
if (arrayList.size() > 0) {
arrayList.remove(arrayList.size() - 1);
linkedList.removeLast();
} else {
linkedList.removeLast();
}
if (DEBUG) {
System.out.println("Removendo o último elemento.");
}
} else if (ch == 8) {
if (arrayList.size() > 0) {
arrayList.remove(0);
linkedList.removeFirst();
} else {
linkedList.removeFirst();
}
if (DEBUG) {
System.out.println("Removendo o primeiro elemento.");
}
}
if (DEBUG) {
System.out.println(Arrays.toString(arrayList.toArray()) + " : " + Arrays.toString(linkedList.toArray()));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment