From 72e133a96c86329df11fed22476d108f38114398 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 4 Aug 2015 19:33:42 -0400 Subject: [PATCH] Reworked bst unit tests. --- graphs_trees/bst/bst_challenge.ipynb | 26 ++++++++++++++------------ graphs_trees/bst/bst_solution.ipynb | 26 +++++++++++++------------- graphs_trees/bst/test_bst.py | 17 ++++++++--------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/graphs_trees/bst/bst_challenge.ipynb b/graphs_trees/bst/bst_challenge.ipynb index 7699f15..5110969 100644 --- a/graphs_trees/bst/bst_challenge.ipynb +++ b/graphs_trees/bst/bst_challenge.ipynb @@ -85,9 +85,12 @@ "\n", " def __init__(self, data):\n", " # TODO: Implement me\n", + " pass\n", + "\n", "\n", "def insert(root, data):\n", - " # TODO: Implement me" + " # TODO: Implement me\n", + " pass" ] }, { @@ -112,7 +115,7 @@ }, "outputs": [], "source": [ - "%run ../utils/captured_output.py" + "%run dfs.py" ] }, { @@ -123,7 +126,7 @@ }, "outputs": [], "source": [ - "%run dfs.py" + "%run ../utils/results.py" ] }, { @@ -135,32 +138,31 @@ "outputs": [], "source": [ "# %load test_bst.py\n", - "from __future__ import print_function\n", "from nose.tools import assert_equal\n", "\n", "\n", "class TestTree(object):\n", "\n", + " def __init__(self):\n", + " self.results = Results()\n", + "\n", " def test_tree(self):\n", " node = Node(5)\n", " insert(node, 2)\n", " insert(node, 8)\n", " insert(node, 1)\n", " insert(node, 3)\n", - "\n", - " with captured_output() as (out, err):\n", - " in_order_traversal(node, sys.stdout.write)\n", - " assert_equal(out.getvalue().strip(), '12358')\n", + " in_order_traversal(node, self.results.add_result)\n", + " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", + " self.results.clear_results()\n", "\n", " node = Node(1)\n", " insert(node, 2)\n", " insert(node, 3)\n", " insert(node, 4)\n", " insert(node, 5)\n", - "\n", - " with captured_output() as (out, err):\n", - " in_order_traversal(node, sys.stdout.write)\n", - " assert_equal(out.getvalue().strip(), '12345')\n", + " in_order_traversal(node, self.results.add_result)\n", + " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n", "\n", " print('Success: test_tree')\n", "\n", diff --git a/graphs_trees/bst/bst_solution.ipynb b/graphs_trees/bst/bst_solution.ipynb index 598266a..6b471b5 100644 --- a/graphs_trees/bst/bst_solution.ipynb +++ b/graphs_trees/bst/bst_solution.ipynb @@ -100,6 +100,7 @@ " self.left = None\n", " self.right = None\n", "\n", + "\n", "def insert(root, data):\n", " if data <= root.data:\n", " if root.left is None:\n", @@ -124,22 +125,22 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "%run ../utils/captured_output.py" + "%run dfs.py" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ - "%run dfs.py" + "%run ../utils/results.py" ] }, { @@ -159,32 +160,31 @@ ], "source": [ "%%writefile test_bst.py\n", - "from __future__ import print_function\n", "from nose.tools import assert_equal\n", "\n", "\n", "class TestTree(object):\n", "\n", + " def __init__(self):\n", + " self.results = Results()\n", + "\n", " def test_tree(self):\n", " node = Node(5)\n", " insert(node, 2)\n", " insert(node, 8)\n", " insert(node, 1)\n", " insert(node, 3)\n", - "\n", - " with captured_output() as (out, err):\n", - " in_order_traversal(node, sys.stdout.write)\n", - " assert_equal(out.getvalue().strip(), '12358')\n", + " in_order_traversal(node, self.results.add_result)\n", + " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", + " self.results.clear_results()\n", "\n", " node = Node(1)\n", " insert(node, 2)\n", " insert(node, 3)\n", " insert(node, 4)\n", " insert(node, 5)\n", - "\n", - " with captured_output() as (out, err):\n", - " in_order_traversal(node, sys.stdout.write)\n", - " assert_equal(out.getvalue().strip(), '12345')\n", + " in_order_traversal(node, self.results.add_result)\n", + " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n", "\n", " print('Success: test_tree')\n", "\n", diff --git a/graphs_trees/bst/test_bst.py b/graphs_trees/bst/test_bst.py index fd37060..24f9f43 100644 --- a/graphs_trees/bst/test_bst.py +++ b/graphs_trees/bst/test_bst.py @@ -1,29 +1,28 @@ -from __future__ import print_function from nose.tools import assert_equal class TestTree(object): + def __init__(self): + self.results = Results() + def test_tree(self): node = Node(5) insert(node, 2) insert(node, 8) insert(node, 1) insert(node, 3) - - with captured_output() as (out, err): - in_order_traversal(node, sys.stdout.write) - assert_equal(out.getvalue().strip(), '12358') + in_order_traversal(node, self.results.add_result) + assert_equal(str(self.results), '[1, 2, 3, 5, 8]') + self.results.clear_results() node = Node(1) insert(node, 2) insert(node, 3) insert(node, 4) insert(node, 5) - - with captured_output() as (out, err): - in_order_traversal(node, sys.stdout.write) - assert_equal(out.getvalue().strip(), '12345') + in_order_traversal(node, self.results.add_result) + assert_equal(str(self.results), '[1, 2, 3, 4, 5]') print('Success: test_tree')