Skip to content

Instantly share code, notes, and snippets.

@Icaro-Lima
Last active December 5, 2017 00:11
Show Gist options
  • Select an option

  • Save Icaro-Lima/5084a0996bbc804dcd543226205c114d to your computer and use it in GitHub Desktop.

Select an option

Save Icaro-Lima/5084a0996bbc804dcd543226205c114d to your computer and use it in GitHub Desktop.
@Test
public void testTDG() throws StackOverflowException, StackUnderflowException {
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);
int stackSize = rand.nextInt(50);
Stack<Integer> stack = new StackRecursiveDoubleLinkedListImpl<>(stackSize);
java.util.Stack<Integer> stackJava = new java.util.Stack<>();
for (int i = 0; i < operations; i++) {
int ch = rand.nextInt(5);
if (ch == 0) {
Integer element = rand.nextInt(MAX_INTEGER);
if (stack.isFull()) {
assertEquals(stackJava.size() == stackSize, stack.isFull());
try {
stack.push(element);
Assert.fail();
} catch(Exception ex) {
}
} else {
stack.push(element);
stackJava.push(element);
}
if (DEBUG) {
System.out.println("Inserindo " + element + ".");
}
} else if (ch == 1) {
if (stackJava.size() == 0) {
assertNull(stack.top());
} else {
assertEquals(stackJava.peek(), stack.top());
}
} else if (ch == 2) {
Assert.assertEquals(stackJava.isEmpty(), stack.isEmpty());
} else if (ch == 3) {
assertEquals(stackJava.size() == stackSize, stack.isFull());
} else if (ch == 4 && !stackJava.isEmpty()) {
assertEquals(stackJava.pop(), stack.pop());
if (DEBUG) {
System.out.println("Dando pop.");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment