Created
January 28, 2018 21:27
-
-
Save jaredsburrows/32ee89f2834abac0c266fd42cf1d2dbd to your computer and use it in GitHub Desktop.
Unit test with System.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import api.TreeNode; | |
public final class BstPreOrder { | |
public static void printPreOrder(TreeNode<Integer> node) { | |
if (node == null) { | |
return; | |
} | |
System.out.print(node.value + " "); | |
printPreOrder(node.left); | |
printPreOrder(node.right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import api.TreeNode | |
import test.BaseSpec | |
final class BstPreOrderSpock extends BaseSpec { | |
def outContent = new ByteArrayOutputStream() | |
def errContent = new ByteArrayOutputStream() | |
def tree = new TreeNode<>(8) | |
// (8) | |
// / \ | |
// (2) (21) | |
// / \ / | |
// (1) (5) (13) | |
// / | |
// (3) | |
def "setup"() { | |
System.setOut(new PrintStream(outContent)) | |
System.setErr(new PrintStream(errContent)) | |
tree.right = new TreeNode<>(21) | |
tree.right.left = new TreeNode<>(13) | |
tree.left = new TreeNode<>(2) | |
tree.left.left = new TreeNode<>(1) | |
tree.left.right = new TreeNode<>(5) | |
tree.left.right.left = new TreeNode<>(3) | |
} | |
def "cleanup"() { | |
System.setOut(null) | |
System.setErr(null) | |
} | |
def "printPreOrder"() { | |
when: | |
BstPreOrder.printPreOrder(tree) | |
then: | |
outContent.toString().trim() == "8 2 1 5 3 21 13" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment