Reworked bst unit tests.

This commit is contained in:
Donne Martin 2015-08-04 19:33:42 -04:00
parent 9dc4116256
commit 72e133a96c
3 changed files with 35 additions and 34 deletions

View File

@ -85,9 +85,12 @@
"\n", "\n",
" def __init__(self, data):\n", " def __init__(self, data):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass\n",
"\n",
"\n", "\n",
"def insert(root, data):\n", "def insert(root, data):\n",
" # TODO: Implement me" " # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -112,7 +115,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../utils/captured_output.py" "%run dfs.py"
] ]
}, },
{ {
@ -123,7 +126,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%run dfs.py" "%run ../utils/results.py"
] ]
}, },
{ {
@ -135,32 +138,31 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_bst.py\n", "# %load test_bst.py\n",
"from __future__ import print_function\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"\n", "\n",
"\n", "\n",
"class TestTree(object):\n", "class TestTree(object):\n",
"\n", "\n",
" def __init__(self):\n",
" self.results = Results()\n",
"\n",
" def test_tree(self):\n", " def test_tree(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" insert(node, 2)\n", " insert(node, 2)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
" insert(node, 1)\n", " insert(node, 1)\n",
" insert(node, 3)\n", " insert(node, 3)\n",
"\n", " in_order_traversal(node, self.results.add_result)\n",
" with captured_output() as (out, err):\n", " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" in_order_traversal(node, sys.stdout.write)\n", " self.results.clear_results()\n",
" assert_equal(out.getvalue().strip(), '12358')\n",
"\n", "\n",
" node = Node(1)\n", " node = Node(1)\n",
" insert(node, 2)\n", " insert(node, 2)\n",
" insert(node, 3)\n", " insert(node, 3)\n",
" insert(node, 4)\n", " insert(node, 4)\n",
" insert(node, 5)\n", " insert(node, 5)\n",
"\n", " in_order_traversal(node, self.results.add_result)\n",
" with captured_output() as (out, err):\n", " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
"\n", "\n",
" print('Success: test_tree')\n", " print('Success: test_tree')\n",
"\n", "\n",

View File

@ -100,6 +100,7 @@
" self.left = None\n", " self.left = None\n",
" self.right = None\n", " self.right = None\n",
"\n", "\n",
"\n",
"def insert(root, data):\n", "def insert(root, data):\n",
" if data <= root.data:\n", " if data <= root.data:\n",
" if root.left is None:\n", " if root.left is None:\n",
@ -124,22 +125,22 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": true
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../utils/captured_output.py" "%run dfs.py"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": true "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%run dfs.py" "%run ../utils/results.py"
] ]
}, },
{ {
@ -159,32 +160,31 @@
], ],
"source": [ "source": [
"%%writefile test_bst.py\n", "%%writefile test_bst.py\n",
"from __future__ import print_function\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"\n", "\n",
"\n", "\n",
"class TestTree(object):\n", "class TestTree(object):\n",
"\n", "\n",
" def __init__(self):\n",
" self.results = Results()\n",
"\n",
" def test_tree(self):\n", " def test_tree(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" insert(node, 2)\n", " insert(node, 2)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
" insert(node, 1)\n", " insert(node, 1)\n",
" insert(node, 3)\n", " insert(node, 3)\n",
"\n", " in_order_traversal(node, self.results.add_result)\n",
" with captured_output() as (out, err):\n", " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" in_order_traversal(node, sys.stdout.write)\n", " self.results.clear_results()\n",
" assert_equal(out.getvalue().strip(), '12358')\n",
"\n", "\n",
" node = Node(1)\n", " node = Node(1)\n",
" insert(node, 2)\n", " insert(node, 2)\n",
" insert(node, 3)\n", " insert(node, 3)\n",
" insert(node, 4)\n", " insert(node, 4)\n",
" insert(node, 5)\n", " insert(node, 5)\n",
"\n", " in_order_traversal(node, self.results.add_result)\n",
" with captured_output() as (out, err):\n", " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
"\n", "\n",
" print('Success: test_tree')\n", " print('Success: test_tree')\n",
"\n", "\n",

View File

@ -1,29 +1,28 @@
from __future__ import print_function
from nose.tools import assert_equal from nose.tools import assert_equal
class TestTree(object): class TestTree(object):
def __init__(self):
self.results = Results()
def test_tree(self): def test_tree(self):
node = Node(5) node = Node(5)
insert(node, 2) insert(node, 2)
insert(node, 8) insert(node, 8)
insert(node, 1) insert(node, 1)
insert(node, 3) insert(node, 3)
in_order_traversal(node, self.results.add_result)
with captured_output() as (out, err): assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
in_order_traversal(node, sys.stdout.write) self.results.clear_results()
assert_equal(out.getvalue().strip(), '12358')
node = Node(1) node = Node(1)
insert(node, 2) insert(node, 2)
insert(node, 3) insert(node, 3)
insert(node, 4) insert(node, 4)
insert(node, 5) insert(node, 5)
in_order_traversal(node, self.results.add_result)
with captured_output() as (out, err): assert_equal(str(self.results), '[1, 2, 3, 4, 5]')
in_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '12345')
print('Success: test_tree') print('Success: test_tree')