Reworked tree dfs unit tests.

This commit is contained in:
Donne Martin 2015-08-04 19:35:19 -04:00
parent db798ed6a7
commit 706ccd3256
3 changed files with 67 additions and 61 deletions

View File

@ -97,12 +97,15 @@
"source": [
"def in_order_traversal(node, visit_func):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"def pre_order_traversal(node, visit_func):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"def post_order_traversal(node, visit_func):\n",
" # TODO: Implement me"
" # TODO: Implement me\n",
" pass"
]
},
{
@ -120,7 +123,7 @@
},
"outputs": [],
"source": [
"%run ../utils/captured_output.py"
"%run ../utils/results.py"
]
},
{
@ -132,12 +135,14 @@
"outputs": [],
"source": [
"# %load test_dfs.py\n",
"from __future__ import print_function\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestDfs(object):\n",
"\n",
" def __init__(self):\n",
" self.results = Results()\n",
"\n",
" def test_dfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
@ -145,17 +150,17 @@
" 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",
" with captured_output() as (out, err):\n",
" pre_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '52138')\n",
" pre_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" post_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '13285')\n",
" post_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" insert(node, 2)\n",
@ -163,17 +168,16 @@
" 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",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" pre_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
" pre_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" post_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '54321')\n",
" post_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
"\n",
" print('Success: test_dfs')\n",
"\n",

View File

@ -163,11 +163,11 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
"%run ../utils/captured_output.py"
"%run ../utils/results.py"
]
},
{
@ -187,12 +187,14 @@
],
"source": [
"%%writefile test_dfs.py\n",
"from __future__ import print_function\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestDfs(object):\n",
"\n",
" def __init__(self):\n",
" self.results = Results()\n",
"\n",
" def test_dfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
@ -200,17 +202,17 @@
" 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",
" with captured_output() as (out, err):\n",
" pre_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '52138')\n",
" pre_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" post_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '13285')\n",
" post_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" insert(node, 2)\n",
@ -218,17 +220,16 @@
" 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",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" pre_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
" pre_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" with captured_output() as (out, err):\n",
" post_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '54321')\n",
" post_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
"\n",
" print('Success: test_dfs')\n",
"\n",

View File

@ -1,9 +1,11 @@
from __future__ import print_function
from nose.tools import assert_equal
class TestDfs(object):
def __init__(self):
self.results = Results()
def test_dfs(self):
node = Node(5)
insert(node, 2)
@ -11,17 +13,17 @@ class TestDfs(object):
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()
with captured_output() as (out, err):
pre_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '52138')
pre_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), "[5, 2, 1, 3, 8]")
self.results.clear_results()
with captured_output() as (out, err):
post_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '13285')
post_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), "[1, 3, 2, 8, 5]")
self.results.clear_results()
node = Node(1)
insert(node, 2)
@ -29,17 +31,16 @@ class TestDfs(object):
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]")
self.results.clear_results()
with captured_output() as (out, err):
pre_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '12345')
pre_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
self.results.clear_results()
with captured_output() as (out, err):
post_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '54321')
post_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), "[5, 4, 3, 2, 1]")
print('Success: test_dfs')