2014年11月8日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 15(Testing and Debugging)、15.7(Exercises) 1.を解いてみる。

15.7(Exercises) 1.

コード(BBEdit)

sample1.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest

def doublePreceding(values):
    if values != []:
        temp = values[0]
        values[0] = 0
    for i in range(1, len(values)):
        values[i] = 2 * temp
        temp = values[i]

class TestDoublePreceding(unittest.TestCase):
    def test_default(self):
        values = [1, 2, 3]
        doublePreceding(values)
        expected = [0, 2, 4]
        self.assertEqual(expected, values, 'Test a [1, 2, 3].(before)')
        
    def test_empty(self):
        values = []
        doublePreceding(values)
        expected = []
        self.assertEqual(expected, values, 'Test a [].(before)')

    def test_other(self):
        values = [1, 10, 100]
        doublePreceding(values)
        expected = [0, 2, 20]
        self.assertEqual(expected, values, 'Test a [1, 10, 100].(before)')

def fixedDoublePreceding(values):
    if values != []:
        temp = values[0]
        values[0] = 0
    for i in range(1, len(values)):
        temp_1 = values[i]
        values[i] = temp * 2
        temp = temp_1

class TestFixedDoublePreceding(unittest.TestCase):
    def test_default(self):
        values = [1, 2, 3]
        fixedDoublePreceding(values)
        expected = [0, 2, 4]
        self.assertEqual(expected, values, 'Test a [1, 2, 3].(after)')
        
    def test_empty(self):
        values = []
        fixedDoublePreceding(values)
        expected = []
        self.assertEqual(expected, values, 'Test a [].(after)')


    def test_other(self):
        values = [1, 10, 100]
        fixedDoublePreceding(values)
        expected = [0, 2, 20]
        self.assertEqual(expected, values, 'Test a [1, 5, 10].(after)')

if __name__ == '__main__':
    unittest.main()

入出力結果(Terminal, IPython)

$ ./sample1.py
..F...
======================================================================
FAIL: test_other (__main__.TestDoublePreceding)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample1.py", line 31, in test_other
    self.assertEqual(expected, values, 'Test a [1, 10, 100].(before)')
AssertionError: Lists differ: [0, 2, 20] != [0, 2, 4]

First differing element 2:
20
4

- [0, 2, 20]
?        ^^

+ [0, 2, 4]
?        ^
 : Test a [1, 10, 100].(before)

----------------------------------------------------------------------
Ran 6 tests in 0.005s

FAILED (failures=1)
$

0 コメント:

コメントを投稿