@@ -1744,135 +1744,6 @@ def _naive_prod(iterable, start=1):
1744
1744
self .assertEqual (type (prod ([1 , decimal .Decimal (2.0 ), 3 , 4 , 5 , 6 ])),
1745
1745
decimal .Decimal )
1746
1746
1747
- # Custom assertions.
1748
-
1749
- def assertIsNaN (self , value ):
1750
- if not math .isnan (value ):
1751
- self .fail ("Expected a NaN, got {!r}." .format (value ))
1752
-
1753
-
1754
- class IsCloseTests (unittest .TestCase ):
1755
- isclose = math .isclose # subclasses should override this
1756
-
1757
- def assertIsClose (self , a , b , * args , ** kwargs ):
1758
- self .assertTrue (self .isclose (a , b , * args , ** kwargs ),
1759
- msg = "%s and %s should be close!" % (a , b ))
1760
-
1761
- def assertIsNotClose (self , a , b , * args , ** kwargs ):
1762
- self .assertFalse (self .isclose (a , b , * args , ** kwargs ),
1763
- msg = "%s and %s should not be close!" % (a , b ))
1764
-
1765
- def assertAllClose (self , examples , * args , ** kwargs ):
1766
- for a , b in examples :
1767
- self .assertIsClose (a , b , * args , ** kwargs )
1768
-
1769
- def assertAllNotClose (self , examples , * args , ** kwargs ):
1770
- for a , b in examples :
1771
- self .assertIsNotClose (a , b , * args , ** kwargs )
1772
-
1773
- def test_negative_tolerances (self ):
1774
- # ValueError should be raised if either tolerance is less than zero
1775
- with self .assertRaises (ValueError ):
1776
- self .assertIsClose (1 , 1 , rel_tol = - 1e-100 )
1777
- with self .assertRaises (ValueError ):
1778
- self .assertIsClose (1 , 1 , rel_tol = 1e-100 , abs_tol = - 1e10 )
1779
-
1780
- def test_identical (self ):
1781
- # identical values must test as close
1782
- identical_examples = [(2.0 , 2.0 ),
1783
- (0.1e200 , 0.1e200 ),
1784
- (1.123e-300 , 1.123e-300 ),
1785
- (12345 , 12345.0 ),
1786
- (0.0 , - 0.0 ),
1787
- (345678 , 345678 )]
1788
- self .assertAllClose (identical_examples , rel_tol = 0.0 , abs_tol = 0.0 )
1789
-
1790
- def test_eight_decimal_places (self ):
1791
- # examples that are close to 1e-8, but not 1e-9
1792
- eight_decimal_places_examples = [(1e8 , 1e8 + 1 ),
1793
- (- 1e-8 , - 1.000000009e-8 ),
1794
- (1.12345678 , 1.12345679 )]
1795
- self .assertAllClose (eight_decimal_places_examples , rel_tol = 1e-8 )
1796
- self .assertAllNotClose (eight_decimal_places_examples , rel_tol = 1e-9 )
1797
-
1798
- def test_near_zero (self ):
1799
- # values close to zero
1800
- near_zero_examples = [(1e-9 , 0.0 ),
1801
- (- 1e-9 , 0.0 ),
1802
- (- 1e-150 , 0.0 )]
1803
- # these should not be close to any rel_tol
1804
- self .assertAllNotClose (near_zero_examples , rel_tol = 0.9 )
1805
- # these should be close to abs_tol=1e-8
1806
- self .assertAllClose (near_zero_examples , abs_tol = 1e-8 )
1807
-
1808
- def test_identical_infinite (self ):
1809
- # these are close regardless of tolerance -- i.e. they are equal
1810
- self .assertIsClose (INF , INF )
1811
- self .assertIsClose (INF , INF , abs_tol = 0.0 )
1812
- self .assertIsClose (NINF , NINF )
1813
- self .assertIsClose (NINF , NINF , abs_tol = 0.0 )
1814
-
1815
- def test_inf_ninf_nan (self ):
1816
- # these should never be close (following IEEE 754 rules for equality)
1817
- not_close_examples = [(NAN , NAN ),
1818
- (NAN , 1e-100 ),
1819
- (1e-100 , NAN ),
1820
- (INF , NAN ),
1821
- (NAN , INF ),
1822
- (INF , NINF ),
1823
- (INF , 1.0 ),
1824
- (1.0 , INF ),
1825
- (INF , 1e308 ),
1826
- (1e308 , INF )]
1827
- # use largest reasonable tolerance
1828
- self .assertAllNotClose (not_close_examples , abs_tol = 0.999999999999999 )
1829
-
1830
- def test_zero_tolerance (self ):
1831
- # test with zero tolerance
1832
- zero_tolerance_close_examples = [(1.0 , 1.0 ),
1833
- (- 3.4 , - 3.4 ),
1834
- (- 1e-300 , - 1e-300 )]
1835
- self .assertAllClose (zero_tolerance_close_examples , rel_tol = 0.0 )
1836
-
1837
- zero_tolerance_not_close_examples = [(1.0 , 1.000000000000001 ),
1838
- (0.99999999999999 , 1.0 ),
1839
- (1.0e200 , .999999999999999e200 )]
1840
- self .assertAllNotClose (zero_tolerance_not_close_examples , rel_tol = 0.0 )
1841
-
1842
- def test_asymmetry (self ):
1843
- # test the asymmetry example from PEP 485
1844
- self .assertAllClose ([(9 , 10 ), (10 , 9 )], rel_tol = 0.1 )
1845
-
1846
- def test_integers (self ):
1847
- # test with integer values
1848
- integer_examples = [(100000001 , 100000000 ),
1849
- (123456789 , 123456788 )]
1850
-
1851
- self .assertAllClose (integer_examples , rel_tol = 1e-8 )
1852
- self .assertAllNotClose (integer_examples , rel_tol = 1e-9 )
1853
-
1854
- def test_decimals (self ):
1855
- # test with Decimal values
1856
- from decimal import Decimal
1857
-
1858
- decimal_examples = [(Decimal ('1.00000001' ), Decimal ('1.0' )),
1859
- (Decimal ('1.00000001e-20' ), Decimal ('1.0e-20' )),
1860
- (Decimal ('1.00000001e-100' ), Decimal ('1.0e-100' )),
1861
- (Decimal ('1.00000001e20' ), Decimal ('1.0e20' ))]
1862
- self .assertAllClose (decimal_examples , rel_tol = 1e-8 )
1863
- self .assertAllNotClose (decimal_examples , rel_tol = 1e-9 )
1864
-
1865
- def test_fractions (self ):
1866
- # test with Fraction values
1867
- from fractions import Fraction
1868
-
1869
- fraction_examples = [
1870
- (Fraction (1 , 100000000 ) + 1 , Fraction (1 )),
1871
- (Fraction (100000001 ), Fraction (100000000 )),
1872
- (Fraction (10 ** 8 + 1 , 10 ** 28 ), Fraction (1 , 10 ** 20 ))]
1873
- self .assertAllClose (fraction_examples , rel_tol = 1e-8 )
1874
- self .assertAllNotClose (fraction_examples , rel_tol = 1e-9 )
1875
-
1876
1747
def testPerm (self ):
1877
1748
perm = math .perm
1878
1749
factorial = math .factorial
@@ -2007,6 +1878,135 @@ def testComb(self):
2007
1878
self .assertIs (type (comb (IntSubclass (5 ), IntSubclass (k ))), int )
2008
1879
self .assertIs (type (comb (MyIndexable (5 ), MyIndexable (k ))), int )
2009
1880
1881
+ # Custom assertions.
1882
+
1883
+ def assertIsNaN (self , value ):
1884
+ if not math .isnan (value ):
1885
+ self .fail ("Expected a NaN, got {!r}." .format (value ))
1886
+
1887
+
1888
+ class IsCloseTests (unittest .TestCase ):
1889
+ isclose = math .isclose # subclasses should override this
1890
+
1891
+ def assertIsClose (self , a , b , * args , ** kwargs ):
1892
+ self .assertTrue (self .isclose (a , b , * args , ** kwargs ),
1893
+ msg = "%s and %s should be close!" % (a , b ))
1894
+
1895
+ def assertIsNotClose (self , a , b , * args , ** kwargs ):
1896
+ self .assertFalse (self .isclose (a , b , * args , ** kwargs ),
1897
+ msg = "%s and %s should not be close!" % (a , b ))
1898
+
1899
+ def assertAllClose (self , examples , * args , ** kwargs ):
1900
+ for a , b in examples :
1901
+ self .assertIsClose (a , b , * args , ** kwargs )
1902
+
1903
+ def assertAllNotClose (self , examples , * args , ** kwargs ):
1904
+ for a , b in examples :
1905
+ self .assertIsNotClose (a , b , * args , ** kwargs )
1906
+
1907
+ def test_negative_tolerances (self ):
1908
+ # ValueError should be raised if either tolerance is less than zero
1909
+ with self .assertRaises (ValueError ):
1910
+ self .assertIsClose (1 , 1 , rel_tol = - 1e-100 )
1911
+ with self .assertRaises (ValueError ):
1912
+ self .assertIsClose (1 , 1 , rel_tol = 1e-100 , abs_tol = - 1e10 )
1913
+
1914
+ def test_identical (self ):
1915
+ # identical values must test as close
1916
+ identical_examples = [(2.0 , 2.0 ),
1917
+ (0.1e200 , 0.1e200 ),
1918
+ (1.123e-300 , 1.123e-300 ),
1919
+ (12345 , 12345.0 ),
1920
+ (0.0 , - 0.0 ),
1921
+ (345678 , 345678 )]
1922
+ self .assertAllClose (identical_examples , rel_tol = 0.0 , abs_tol = 0.0 )
1923
+
1924
+ def test_eight_decimal_places (self ):
1925
+ # examples that are close to 1e-8, but not 1e-9
1926
+ eight_decimal_places_examples = [(1e8 , 1e8 + 1 ),
1927
+ (- 1e-8 , - 1.000000009e-8 ),
1928
+ (1.12345678 , 1.12345679 )]
1929
+ self .assertAllClose (eight_decimal_places_examples , rel_tol = 1e-8 )
1930
+ self .assertAllNotClose (eight_decimal_places_examples , rel_tol = 1e-9 )
1931
+
1932
+ def test_near_zero (self ):
1933
+ # values close to zero
1934
+ near_zero_examples = [(1e-9 , 0.0 ),
1935
+ (- 1e-9 , 0.0 ),
1936
+ (- 1e-150 , 0.0 )]
1937
+ # these should not be close to any rel_tol
1938
+ self .assertAllNotClose (near_zero_examples , rel_tol = 0.9 )
1939
+ # these should be close to abs_tol=1e-8
1940
+ self .assertAllClose (near_zero_examples , abs_tol = 1e-8 )
1941
+
1942
+ def test_identical_infinite (self ):
1943
+ # these are close regardless of tolerance -- i.e. they are equal
1944
+ self .assertIsClose (INF , INF )
1945
+ self .assertIsClose (INF , INF , abs_tol = 0.0 )
1946
+ self .assertIsClose (NINF , NINF )
1947
+ self .assertIsClose (NINF , NINF , abs_tol = 0.0 )
1948
+
1949
+ def test_inf_ninf_nan (self ):
1950
+ # these should never be close (following IEEE 754 rules for equality)
1951
+ not_close_examples = [(NAN , NAN ),
1952
+ (NAN , 1e-100 ),
1953
+ (1e-100 , NAN ),
1954
+ (INF , NAN ),
1955
+ (NAN , INF ),
1956
+ (INF , NINF ),
1957
+ (INF , 1.0 ),
1958
+ (1.0 , INF ),
1959
+ (INF , 1e308 ),
1960
+ (1e308 , INF )]
1961
+ # use largest reasonable tolerance
1962
+ self .assertAllNotClose (not_close_examples , abs_tol = 0.999999999999999 )
1963
+
1964
+ def test_zero_tolerance (self ):
1965
+ # test with zero tolerance
1966
+ zero_tolerance_close_examples = [(1.0 , 1.0 ),
1967
+ (- 3.4 , - 3.4 ),
1968
+ (- 1e-300 , - 1e-300 )]
1969
+ self .assertAllClose (zero_tolerance_close_examples , rel_tol = 0.0 )
1970
+
1971
+ zero_tolerance_not_close_examples = [(1.0 , 1.000000000000001 ),
1972
+ (0.99999999999999 , 1.0 ),
1973
+ (1.0e200 , .999999999999999e200 )]
1974
+ self .assertAllNotClose (zero_tolerance_not_close_examples , rel_tol = 0.0 )
1975
+
1976
+ def test_asymmetry (self ):
1977
+ # test the asymmetry example from PEP 485
1978
+ self .assertAllClose ([(9 , 10 ), (10 , 9 )], rel_tol = 0.1 )
1979
+
1980
+ def test_integers (self ):
1981
+ # test with integer values
1982
+ integer_examples = [(100000001 , 100000000 ),
1983
+ (123456789 , 123456788 )]
1984
+
1985
+ self .assertAllClose (integer_examples , rel_tol = 1e-8 )
1986
+ self .assertAllNotClose (integer_examples , rel_tol = 1e-9 )
1987
+
1988
+ def test_decimals (self ):
1989
+ # test with Decimal values
1990
+ from decimal import Decimal
1991
+
1992
+ decimal_examples = [(Decimal ('1.00000001' ), Decimal ('1.0' )),
1993
+ (Decimal ('1.00000001e-20' ), Decimal ('1.0e-20' )),
1994
+ (Decimal ('1.00000001e-100' ), Decimal ('1.0e-100' )),
1995
+ (Decimal ('1.00000001e20' ), Decimal ('1.0e20' ))]
1996
+ self .assertAllClose (decimal_examples , rel_tol = 1e-8 )
1997
+ self .assertAllNotClose (decimal_examples , rel_tol = 1e-9 )
1998
+
1999
+ def test_fractions (self ):
2000
+ # test with Fraction values
2001
+ from fractions import Fraction
2002
+
2003
+ fraction_examples = [
2004
+ (Fraction (1 , 100000000 ) + 1 , Fraction (1 )),
2005
+ (Fraction (100000001 ), Fraction (100000000 )),
2006
+ (Fraction (10 ** 8 + 1 , 10 ** 28 ), Fraction (1 , 10 ** 20 ))]
2007
+ self .assertAllClose (fraction_examples , rel_tol = 1e-8 )
2008
+ self .assertAllNotClose (fraction_examples , rel_tol = 1e-9 )
2009
+
2010
2010
2011
2011
def test_main ():
2012
2012
from doctest import DocFileSuite
0 commit comments