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

View File

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

View File

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