mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Reworked bst unit tests.
This commit is contained in:
parent
9dc4116256
commit
72e133a96c
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user