Best Python code snippet using slash
test_type_check.py
Source:test_type_check.py  
...7from numpy.lib.type_check import (8    common_type, mintypecode, isreal, iscomplex, isposinf, isneginf,9    nan_to_num, isrealobj, iscomplexobj, asfarray, real_if_close10    )11def assert_all(x):12    assert_(np.all(x), x)13class TestCommonType(TestCase):14    def test_basic(self):15        ai32 = np.array([[1, 2], [3, 4]], dtype=np.int32)16        af16 = np.array([[1, 2], [3, 4]], dtype=np.float16)17        af32 = np.array([[1, 2], [3, 4]], dtype=np.float32)18        af64 = np.array([[1, 2], [3, 4]], dtype=np.float64)19        acs = np.array([[1+5j, 2+6j], [3+7j, 4+8j]], dtype=np.csingle)20        acd = np.array([[1+5j, 2+6j], [3+7j, 4+8j]], dtype=np.cdouble)21        assert_(common_type(ai32) == np.float64)22        assert_(common_type(af16) == np.float16)23        assert_(common_type(af32) == np.float32)24        assert_(common_type(af64) == np.float64)25        assert_(common_type(acs) == np.csingle)26        assert_(common_type(acd) == np.cdouble)27class TestMintypecode(TestCase):28    def test_default_1(self):29        for itype in '1bcsuwil':30            assert_equal(mintypecode(itype), 'd')31        assert_equal(mintypecode('f'), 'f')32        assert_equal(mintypecode('d'), 'd')33        assert_equal(mintypecode('F'), 'F')34        assert_equal(mintypecode('D'), 'D')35    def test_default_2(self):36        for itype in '1bcsuwil':37            assert_equal(mintypecode(itype+'f'), 'f')38            assert_equal(mintypecode(itype+'d'), 'd')39            assert_equal(mintypecode(itype+'F'), 'F')40            assert_equal(mintypecode(itype+'D'), 'D')41        assert_equal(mintypecode('ff'), 'f')42        assert_equal(mintypecode('fd'), 'd')43        assert_equal(mintypecode('fF'), 'F')44        assert_equal(mintypecode('fD'), 'D')45        assert_equal(mintypecode('df'), 'd')46        assert_equal(mintypecode('dd'), 'd')47        #assert_equal(mintypecode('dF',savespace=1),'F')48        assert_equal(mintypecode('dF'), 'D')49        assert_equal(mintypecode('dD'), 'D')50        assert_equal(mintypecode('Ff'), 'F')51        #assert_equal(mintypecode('Fd',savespace=1),'F')52        assert_equal(mintypecode('Fd'), 'D')53        assert_equal(mintypecode('FF'), 'F')54        assert_equal(mintypecode('FD'), 'D')55        assert_equal(mintypecode('Df'), 'D')56        assert_equal(mintypecode('Dd'), 'D')57        assert_equal(mintypecode('DF'), 'D')58        assert_equal(mintypecode('DD'), 'D')59    def test_default_3(self):60        assert_equal(mintypecode('fdF'), 'D')61        #assert_equal(mintypecode('fdF',savespace=1),'F')62        assert_equal(mintypecode('fdD'), 'D')63        assert_equal(mintypecode('fFD'), 'D')64        assert_equal(mintypecode('dFD'), 'D')65        assert_equal(mintypecode('ifd'), 'd')66        assert_equal(mintypecode('ifF'), 'F')67        assert_equal(mintypecode('ifD'), 'D')68        assert_equal(mintypecode('idF'), 'D')69        #assert_equal(mintypecode('idF',savespace=1),'F')70        assert_equal(mintypecode('idD'), 'D')71class TestIsscalar(TestCase):72    def test_basic(self):73        assert_(np.isscalar(3))74        assert_(not np.isscalar([3]))75        assert_(not np.isscalar((3,)))76        assert_(np.isscalar(3j))77        assert_(np.isscalar(long(10)))78        assert_(np.isscalar(4.0))79class TestReal(TestCase):80    def test_real(self):81        y = np.random.rand(10,)82        assert_array_equal(y, np.real(y))83    def test_cmplx(self):84        y = np.random.rand(10,)+1j*np.random.rand(10,)85        assert_array_equal(y.real, np.real(y))86class TestImag(TestCase):87    def test_real(self):88        y = np.random.rand(10,)89        assert_array_equal(0, np.imag(y))90    def test_cmplx(self):91        y = np.random.rand(10,)+1j*np.random.rand(10,)92        assert_array_equal(y.imag, np.imag(y))93class TestIscomplex(TestCase):94    def test_fail(self):95        z = np.array([-1, 0, 1])96        res = iscomplex(z)97        assert_(not np.sometrue(res, axis=0))98    def test_pass(self):99        z = np.array([-1j, 1, 0])100        res = iscomplex(z)101        assert_array_equal(res, [1, 0, 0])102class TestIsreal(TestCase):103    def test_pass(self):104        z = np.array([-1, 0, 1j])105        res = isreal(z)106        assert_array_equal(res, [1, 1, 0])107    def test_fail(self):108        z = np.array([-1j, 1, 0])109        res = isreal(z)110        assert_array_equal(res, [0, 1, 1])111class TestIscomplexobj(TestCase):112    def test_basic(self):113        z = np.array([-1, 0, 1])114        assert_(not iscomplexobj(z))115        z = np.array([-1j, 0, -1])116        assert_(iscomplexobj(z))117class TestIsrealobj(TestCase):118    def test_basic(self):119        z = np.array([-1, 0, 1])120        assert_(isrealobj(z))121        z = np.array([-1j, 0, -1])122        assert_(not isrealobj(z))123class TestIsnan(TestCase):124    def test_goodvalues(self):125        z = np.array((-1., 0., 1.))126        res = np.isnan(z) == 0127        assert_all(np.all(res, axis=0))128    def test_posinf(self):129        with np.errstate(divide='ignore'):130            assert_all(np.isnan(np.array((1.,))/0.) == 0)131    def test_neginf(self):132        with np.errstate(divide='ignore'):133            assert_all(np.isnan(np.array((-1.,))/0.) == 0)134    def test_ind(self):135        with np.errstate(divide='ignore', invalid='ignore'):136            assert_all(np.isnan(np.array((0.,))/0.) == 1)137    def test_integer(self):138        assert_all(np.isnan(1) == 0)139    def test_complex(self):140        assert_all(np.isnan(1+1j) == 0)141    def test_complex1(self):142        with np.errstate(divide='ignore', invalid='ignore'):143            assert_all(np.isnan(np.array(0+0j)/0.) == 1)144class TestIsfinite(TestCase):145    # Fixme, wrong place, isfinite now ufunc146    def test_goodvalues(self):147        z = np.array((-1., 0., 1.))148        res = np.isfinite(z) == 1149        assert_all(np.all(res, axis=0))150    def test_posinf(self):151        with np.errstate(divide='ignore', invalid='ignore'):152            assert_all(np.isfinite(np.array((1.,))/0.) == 0)153    def test_neginf(self):154        with np.errstate(divide='ignore', invalid='ignore'):155            assert_all(np.isfinite(np.array((-1.,))/0.) == 0)156    def test_ind(self):157        with np.errstate(divide='ignore', invalid='ignore'):158            assert_all(np.isfinite(np.array((0.,))/0.) == 0)159    def test_integer(self):160        assert_all(np.isfinite(1) == 1)161    def test_complex(self):162        assert_all(np.isfinite(1+1j) == 1)163    def test_complex1(self):164        with np.errstate(divide='ignore', invalid='ignore'):165            assert_all(np.isfinite(np.array(1+1j)/0.) == 0)166class TestIsinf(TestCase):167    # Fixme, wrong place, isinf now ufunc168    def test_goodvalues(self):169        z = np.array((-1., 0., 1.))170        res = np.isinf(z) == 0171        assert_all(np.all(res, axis=0))172    def test_posinf(self):173        with np.errstate(divide='ignore', invalid='ignore'):174            assert_all(np.isinf(np.array((1.,))/0.) == 1)175    def test_posinf_scalar(self):176        with np.errstate(divide='ignore', invalid='ignore'):177            assert_all(np.isinf(np.array(1.,)/0.) == 1)178    def test_neginf(self):179        with np.errstate(divide='ignore', invalid='ignore'):180            assert_all(np.isinf(np.array((-1.,))/0.) == 1)181    def test_neginf_scalar(self):182        with np.errstate(divide='ignore', invalid='ignore'):183            assert_all(np.isinf(np.array(-1.)/0.) == 1)184    def test_ind(self):185        with np.errstate(divide='ignore', invalid='ignore'):186            assert_all(np.isinf(np.array((0.,))/0.) == 0)187class TestIsposinf(TestCase):188    def test_generic(self):189        with np.errstate(divide='ignore', invalid='ignore'):190            vals = isposinf(np.array((-1., 0, 1))/0.)191        assert_(vals[0] == 0)192        assert_(vals[1] == 0)193        assert_(vals[2] == 1)194class TestIsneginf(TestCase):195    def test_generic(self):196        with np.errstate(divide='ignore', invalid='ignore'):197            vals = isneginf(np.array((-1., 0, 1))/0.)198        assert_(vals[0] == 1)199        assert_(vals[1] == 0)200        assert_(vals[2] == 0)201class TestNanToNum(TestCase):202    def test_generic(self):203        with np.errstate(divide='ignore', invalid='ignore'):204            vals = nan_to_num(np.array((-1., 0, 1))/0.)205        assert_all(vals[0] < -1e10) and assert_all(np.isfinite(vals[0]))206        assert_(vals[1] == 0)207        assert_all(vals[2] > 1e10) and assert_all(np.isfinite(vals[2]))208    def test_integer(self):209        vals = nan_to_num(1)210        assert_all(vals == 1)211        vals = nan_to_num([1])212        assert_array_equal(vals, np.array([1], np.int))213    def test_complex_good(self):214        vals = nan_to_num(1+1j)215        assert_all(vals == 1+1j)216    def test_complex_bad(self):217        with np.errstate(divide='ignore', invalid='ignore'):218            v = 1 + 1j219            v += np.array(0+1.j)/0.220        vals = nan_to_num(v)221        # !! This is actually (unexpectedly) zero222        assert_all(np.isfinite(vals))223    def test_complex_bad2(self):224        with np.errstate(divide='ignore', invalid='ignore'):225            v = 1 + 1j226            v += np.array(-1+1.j)/0.227        vals = nan_to_num(v)228        assert_all(np.isfinite(vals))229        # Fixme230        #assert_all(vals.imag > 1e10)  and assert_all(np.isfinite(vals))231        # !! This is actually (unexpectedly) positive232        # !! inf.  Comment out for now, and see if it233        # !! changes234        #assert_all(vals.real < -1e10) and assert_all(np.isfinite(vals))235class TestRealIfClose(TestCase):236    def test_basic(self):237        a = np.random.rand(10)238        b = real_if_close(a+1e-15j)239        assert_all(isrealobj(b))240        assert_array_equal(a, b)241        b = real_if_close(a+1e-7j)242        assert_all(iscomplexobj(b))243        b = real_if_close(a+1e-7j, tol=1e-6)244        assert_all(isrealobj(b))245class TestArrayConversion(TestCase):246    def test_asfarray(self):247        a = asfarray(np.array([1, 2, 3]))248        assert_equal(a.__class__, np.ndarray)249        assert_(np.issubdtype(a.dtype, np.float))250if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
