1
|
|
|
import six |
2
|
|
|
from hamcrest.library.number.iscloseto import * |
3
|
|
|
|
4
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
5
|
|
|
try: |
6
|
|
|
import unittest2 as unittest |
7
|
|
|
except ImportError: |
8
|
|
|
import unittest |
9
|
|
|
|
10
|
|
|
__author__ = "Jon Reid" |
11
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
12
|
|
|
__license__ = "BSD, see License.txt" |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class IsCloseToTest(MatcherTest): |
16
|
|
|
|
17
|
|
|
def testEvaluatesToTrueIfArgumentIsEqualToAValueWithinSomeError(self): |
18
|
|
|
matcher = close_to(1.0, 0.5) |
19
|
|
|
|
20
|
|
|
self.assert_matches('equal', matcher, 1.0) |
21
|
|
|
self.assert_matches('less but within delta', matcher, 0.5) |
22
|
|
|
self.assert_matches('greater but within delta', matcher, 1.5) |
23
|
|
|
|
24
|
|
|
self.assert_does_not_match('too small', matcher, 0.4) |
25
|
|
|
self.assert_does_not_match('too large', matcher, 1.6) |
26
|
|
|
|
27
|
|
|
def testMatcherCreationAcceptsOtherNumericTypes(self): |
28
|
|
|
for t in six.integer_types: |
29
|
|
|
close_to(t(5), t(1)) |
30
|
|
|
|
31
|
|
|
def testMatcherCreationRequiresNumbers(self): |
32
|
|
|
self.assertRaises(TypeError, close_to, 'a', 0.5) |
33
|
|
|
self.assertRaises(TypeError, close_to, 1.0, 'a') |
34
|
|
|
|
35
|
|
|
def testFailsIfMatchingAgainstNonNumber(self): |
36
|
|
|
self.assert_does_not_match('not a number', close_to(1.0, 0.5), 'a') |
37
|
|
|
|
38
|
|
|
def testHasAReadableDescription(self): |
39
|
|
|
self.assert_description('a numeric value within <0.5> of <1.0>', |
40
|
|
|
close_to(1.0, 0.5)) |
41
|
|
|
|
42
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
43
|
|
|
self.assert_no_mismatch_description(close_to(1.0, 0.5), 1.0) |
44
|
|
|
|
45
|
|
|
def testMismatchDescriptionShowsActualDeltaIfArgumentIsNumeric(self): |
46
|
|
|
self.assert_mismatch_description('<1.7> differed by <0.7>', |
47
|
|
|
close_to(1.0, 0.5), 1.7) |
48
|
|
|
|
49
|
|
|
def testMismatchDescriptionShowsActualArgumentIfNotNumeric(self): |
50
|
|
|
self.assert_mismatch_description("was 'bad'", close_to(1.0, 0.5), 'bad') |
51
|
|
|
|
52
|
|
|
def testDescribeMismatchShowsActualDeltaIfArgumentIsNumeric(self): |
53
|
|
|
self.assert_describe_mismatch('<1.7> differed by <0.7>', |
54
|
|
|
close_to(1.0, 0.5), 1.7) |
55
|
|
|
|
56
|
|
|
def testDescribeMismatchShowsActualArgumentIfNotNumeric(self): |
57
|
|
|
self.assert_describe_mismatch("was 'bad'", close_to(1.0, 0.5), 'bad') |
58
|
|
|
|
59
|
|
|
try: |
60
|
|
|
import numpy as np |
61
|
|
|
NUMPY_AVAILABLE = True |
62
|
|
|
except ImportError: |
63
|
|
|
NUMPY_AVAILABLE = False |
64
|
|
|
|
65
|
|
|
class IsNumericTest(unittest.TestCase): |
66
|
|
|
|
67
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
68
|
|
|
def test_numpy_numeric_type_int(self): |
69
|
|
|
self.assertTrue(isnumeric(np.int(1)), "Platform integer (normally either int32 or int64)") |
70
|
|
|
|
71
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
72
|
|
|
def test_numpy_numeric_type_int8(self): |
73
|
|
|
self.assertTrue(isnumeric(np.int8(1)), "Byte (-128 to 127)") |
74
|
|
|
|
75
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
76
|
|
|
def test_numpy_numeric_type_int16(self): |
77
|
|
|
self.assertTrue(isnumeric(np.int16(1)), "Integer (-32768 to 32767)") |
78
|
|
|
|
79
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
80
|
|
|
def test_numpy_numeric_type_int32(self): |
81
|
|
|
self.assertTrue(isnumeric(np.int32(1)), "Integer (-2147483648 to 2147483647)") |
82
|
|
|
|
83
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
84
|
|
|
def test_numpy_numeric_type_int64(self): |
85
|
|
|
self.assertTrue(isnumeric(np.int64(1)), "Integer (9223372036854775808 to 9223372036854775807)") |
86
|
|
|
|
87
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
88
|
|
|
def test_numpy_numeric_type_uint8(self): |
89
|
|
|
self.assertTrue(isnumeric(np.uint8(1)), "Unsigned integer (0 to 255)") |
90
|
|
|
|
91
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
92
|
|
|
def test_numpy_numeric_type_uint16(self): |
93
|
|
|
self.assertTrue(isnumeric(np.uint16(1)), "Unsigned integer (0 to 65535)") |
94
|
|
|
|
95
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
96
|
|
|
def test_numpy_numeric_type_uint32(self): |
97
|
|
|
self.assertTrue(isnumeric(np.uint32(1)), "Unsigned integer (0 to 4294967295)") |
98
|
|
|
|
99
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
100
|
|
|
def test_numpy_numeric_type_uint64(self): |
101
|
|
|
self.assertTrue(isnumeric(np.uint64(1)), "Unsigned integer (0 to 18446744073709551615)") |
102
|
|
|
|
103
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
104
|
|
|
def test_numpy_numeric_type_float(self): |
105
|
|
|
self.assertTrue(isnumeric(np.float(1)), "Shorthand for float64.") |
106
|
|
|
|
107
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
108
|
|
|
def test_numpy_numeric_type_float16(self): |
109
|
|
|
self.assertTrue(isnumeric(np.float16(1)), "Half precision float: sign bit, 5 bits exponent, 10 bits mantissa") |
110
|
|
|
|
111
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
112
|
|
|
def test_numpy_numeric_type_float32(self): |
113
|
|
|
self.assertTrue(isnumeric(np.float32(1)), "Single precision float: sign bit, 8 bits exponent, 23 bits mantissa") |
114
|
|
|
|
115
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
116
|
|
|
def test_numpy_numeric_type_float64(self): |
117
|
|
|
self.assertTrue(isnumeric(np.float64(1)), "Double precision float: sign bit, 11 bits exponent, 52 bits mantissa") |
118
|
|
|
|
119
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
120
|
|
|
def test_numpy_numeric_type_complex(self): |
121
|
|
|
self.assertTrue(isnumeric(np.complex(1)), "Shorthand for complex128.") |
122
|
|
|
|
123
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
124
|
|
|
def test_numpy_numeric_type_complex64(self): |
125
|
|
|
self.assertTrue(isnumeric(np.complex64(1)), "Complex number, represented by two 32-bit floats (real and imaginary components)") |
126
|
|
|
|
127
|
|
|
@unittest.skipUnless(NUMPY_AVAILABLE, "Skipped because it needs NumPy") |
128
|
|
|
def test_numpy_numeric_type_complex128(self): |
129
|
|
|
self.assertTrue(isnumeric(np.complex128(1)), "Complex number, represented by two 64-bit floats (real and imaginary components)") |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
if __name__ == '__main__': |
133
|
|
|
unittest.main() |
134
|
|
|
|