2020年7月6日月曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 15(Testing and Debugging)、Exercise 5の解答を求めてみる。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from typing import List, Tuple


class TestMinMax(TestCase):
    def test_one(self):
        self.assertEqual(find_min_max([1]), (1, 1))

    def test_two(self):
        self.assertEqual(find_min_max([1, 2]), (1, 2))

    def test_two_same(self):
        self.assertEqual(find_min_max([1, 1]), (1, 1))

    def test_five(self):
        self.assertEqual(find_min_max([-5, 5, 1, 5, 0]), (-5, 5))


def find_min_max(values: List[float]) -> Tuple[float, float]:
    if values == []:
        raise Exception("empty list")
    min_val = values[0]
    max_val = values[0]
    for value in values[1:]:
        if value > max_val:
            max_val = value
        elif value < min_val:
            min_val = value
    return (min_val, max_val)


if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

 % ./sample5.py 
EEEE
======================================================================
ERROR: test_five (__main__.TestMinMax)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 17, in test_five
    self.assertEqual(find_min_max([-5, 5, 1, 5, 0]), (-5, 10))
  File "./sample5.py", line 26, in find_min_max
    if value > max_val:
TypeError: '>' not supported between instances of 'int' and 'NoneType'

======================================================================
ERROR: test_one (__main__.TestMinMax)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in test_one
    self.assertEqual(find_min_max([1]), (1, 1))
  File "./sample5.py", line 26, in find_min_max
    if value > max_val:
TypeError: '>' not supported between instances of 'int' and 'NoneType'

======================================================================
ERROR: test_two (__main__.TestMinMax)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 11, in test_two
    self.assertEqual(find_min_max([1, 2]), (1, 2))
  File "./sample5.py", line 26, in find_min_max
    if value > max_val:
TypeError: '>' not supported between instances of 'int' and 'NoneType'

======================================================================
ERROR: test_two_same (__main__.TestMinMax)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 14, in test_two_same
    self.assertEqual(find_min_max([1, 1]), (1, 1))
  File "./sample5.py", line 26, in find_min_max
    if value > max_val:
TypeError: '>' not supported between instances of 'int' and 'NoneType'

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (errors=4)
% ./sample5.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% ./sample5.py -v
test_five (__main__.TestMinMax) ... ok
test_one (__main__.TestMinMax) ... ok
test_two (__main__.TestMinMax) ... ok
test_two_same (__main__.TestMinMax) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
% 

0 コメント:

コメントを投稿