Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Makes use of assertArraysAlmostEqual #1

Merged
merged 2 commits into from

2 participants

@saksham

The modified version of the test makes use of a method similar to assertAlmostEqual instead of explicitly rounding off expected and actual values for floating point comparison.

Based on the discussion here: http://www.udacity-forums.com/cs373/questions/1942/unit-tests-for-homework-14, probably the tests were written for python 2.6.

I have tested the new code with both python 2.6 and 2.7, and everything works!

saksham added some commits
@saksham saksham The test now works for Python 2.7 too 415422a
@saksham saksham Added a utility method that compares whether floating points in an array
The method is similar to array failUnlessAlmostEqual method of TestCase class from UnitTest library.

The code works with both Python 2.6 and 2.7
24a32e3
@fatroom fatroom merged commit e9a498d into fatroom:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2012
  1. @saksham
  2. @saksham

    Added a utility method that compares whether floating points in an array

    saksham authored
    The method is similar to array failUnlessAlmostEqual method of TestCase class from UnitTest library.
    
    The code works with both Python 2.6 and 2.7
This page is out of date. Refresh to see the latest.
Showing with 62 additions and 55 deletions.
  1. +62 −55 homework1.4/test.py
View
117 homework1.4/test.py
@@ -2,154 +2,166 @@
import task
class TestSequenceFunctions(unittest.TestCase):
+ def failUnlessArraysAlmostEqual(self, first, second, places=7, msg=None):
+ """Fail if the two arrays are unequal as determined by their
+ difference rounded to the given number of decimal places
+ (default 7) and comparing to zero.
+
+ Note that decimal places (from zero) are usually not the same
+ as significant digits (measured from the most signficant digit).
+ """
+ if (len(first) != len(second)):
+ raise self.failureException, \
+ (msg or '%r != %r because they have unequal lengths %d & %d', \
+ (first, second, len(first), len(second)))
+
+ for i in range(len(first)):
+ if isinstance(first[i], list):
+ self.failUnlessArraysAlmostEqual(first[i], second[i], places, msg)
+ elif round(abs(second[i]-first[i]), places) != 0:
+ raise self.failureException, \
+ (msg or '%r != %r within %r places' % (first, second, places))
+
+ # Synonym methods
+ assertArrayAlmostEqual = assertArrayAlmostEquals = failUnlessArraysAlmostEqual
def test_dataset1(self):
+ # ARRANGE
expected_result=[[0,0,0],[0,1,0],[0,0,0]]
task.colors = [['green','green','green'],
['green','red','green'],
- ['green','green','green']]
+ ['green','green','green']]
task.measurements=['red']
task.motions=[[0,0]]
task.sensor_right=1.0
task.p_move=1.0
+ # ACT
p = task.calculate()
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p)
def test_dataset2(self):
+ # ARRANGE
expected_result=[[0,0,0],[0,0.5,0.5],[0,0,0]]
task.colors = [['green','green','green'],
['green','red','red'],
- ['green','green','green']]
+ ['green','green','green']]
task.measurements=['red']
task.motions=[[0,0]]
task.sensor_right=1.0
task.p_move=1.0
+ # ACT
p = task.calculate()
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p)
def test_dataset3(self):
+ # ARRANGE
expected_result=[[0.06666,0.06666, 0.06666],[0.06666,0.26666,0.26666],[0.06666,0.06666,0.06666]]
task.colors = [['green','green','green'],
['green','red','red'],
- ['green','green','green']]
+ ['green','green','green']]
task.measurements=['red']
task.motions=[[0,0]]
task.sensor_right=0.8
task.p_move=1.0
+ # ACT
p = task.calculate()
- for i in range(len(p)):
- for j in range(len(p[1])):
- p[i][j] = round(p[i][j],4)
-
- for i in range(len(expected_result)):
- for j in range(len(expected_result[1])):
- expected_result[i][j] = round(expected_result[i][j],4)
-
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
def test_dataset4(self):
+ # ARRANGE
expected_result=[[0.03333, 0.03333, 0.03333],
[0.13333, 0.13333, 0.53333],
[0.03333, 0.03333, 0.03333]]
task.colors = [['green', 'green', 'green'],
['green', 'red', 'red'],
- ['green', 'green', 'green']]
+ ['green', 'green', 'green']]
task.measurements=['red', 'red']
task.motions=[[0,0], [0, 1]]
task.sensor_right=0.8
task.p_move=1.0
+ # ACT
p = task.calculate()
- for i in range(len(p)):
- for j in range(len(p[1])):
- p[i][j] = round(p[i][j],4)
-
- for i in range(len(expected_result)):
- for j in range(len(expected_result[1])):
- expected_result[i][j] = round(expected_result[i][j],4)
-
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
def test_dataset5(self):
+ # ARRANGE
expected_result=[[0.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 0.0]]
task.colors = [['green', 'green', 'green'],
['green', 'red', 'red'],
- ['green', 'green', 'green']]
+ ['green', 'green', 'green']]
task.measurements=['red', 'red']
task.motions=[[0,0], [0, 1]]
task.sensor_right=1.0
task.p_move=1.0
+ # ACT
p = task.calculate()
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
def test_dataset6(self):
+ # ARRANGE
expected_result=[[0.02898, 0.02898, 0.02898],
[0.07246, 0.28985, 0.46376],
[0.02898, 0.02898, 0.02898]]
task.colors = [['green', 'green', 'green'],
['green', 'red', 'red'],
- ['green', 'green', 'green']]
+ ['green', 'green', 'green']]
task.measurements=['red', 'red']
task.motions=[[0,0], [0, 1]]
task.sensor_right=.8
task.p_move=0.5
+ # ACT
p = task.calculate()
- for i in range(len(p)):
- for j in range(len(p[1])):
- p[i][j] = round(p[i][j],4)
-
- for i in range(len(expected_result)):
- for j in range(len(expected_result[1])):
- expected_result[i][j] = round(expected_result[i][j],4)
-
- self.assertEqual(expected_result, p)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
def test_dataset7(self):
+ # ARRANGE
expected_result=[[0.0, 0.0, 0.0],
[0.0, 0.33333, 0.66666],
[0.0, 0.0, 0.0]]
task.colors = [['green', 'green', 'green'],
['green', 'red', 'red'],
- ['green', 'green', 'green']]
+ ['green', 'green', 'green']]
task.measurements=['red', 'red']
task.motions=[[0,0], [0, 1]]
task.sensor_right=1.0
task.p_move=0.5
+ # ACT
p = task.calculate()
- for i in range(len(p)):
- for j in range(len(p[1])):
- p[i][j] = round(p[i][j],4)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
- for i in range(len(expected_result)):
- for j in range(len(expected_result[1])):
- expected_result[i][j] = round(expected_result[i][j],4)
- self.assertEqual(expected_result, p)
-
-
def test_dataset8(self):
+ # ARRANGE
expected_result=[[0.01105, 0.02464, 0.06799, 0.04472, 0.024651],
[0.00715, 0.01017, 0.08696, 0.07988, 0.00935],
[0.00739, 0.00894, 0.11272, 0.35350, 0.04065],
@@ -158,22 +170,17 @@ def test_dataset8(self):
task.colors = [['red','green', 'green', 'red','red'],
['red','red','green', 'red', 'red'],
['red','red','green', 'green', 'red'],
- ['red','red','red','red','red']]
+ ['red','red','red','red','red']]
task.measurements=['green','green','green','green','green']
task.motions=[[0,0], [0, 1], [1, 0], [1, 0], [0, 1]]
task.sensor_right=0.7
task.p_move=0.8
+ # ACT
p = task.calculate()
- for i in range(len(p)):
- for j in range(len(p[1])):
- p[i][j] = round(p[i][j],4)
-
- for i in range(len(expected_result)):
- for j in range(len(expected_result[1])):
- expected_result[i][j] = round(expected_result[i][j],4)
+ # ASSERT
+ self.assertArrayAlmostEquals(expected_result, p, 4)
- self.assertEqual(expected_result, p)
if __name__ == '__main__':
unittest.main()
Something went wrong with that request. Please try again.