Best Python code snippet using hypothesis
test_conveniences.py
Source:test_conveniences.py  
...148            st.dictionaries(st.text(), st.floats(allow_infinity=False, allow_nan=False), min_size=10),149            float,150            dict,151        ),152        value_with_type(xps.arrays(xps.floating_dtypes(), 10, unique=True), float, list),153        value_with_type(xps.arrays(xps.integer_dtypes(), 10, unique=True), int, list),154        value_with_type(xps.arrays(xps.boolean_dtypes(), 10, unique=True), int, list),155    ),156)157def test_conv_sequences(value_and_types: tuple[Any, type, type]):158    values, type_, container_type = value_and_types159    assume(inf not in values)160    converted_list = conv(values)161    assert isinstance(converted_list, container_type)162    # Check if size of converted value doesn't change163    assert len(converted_list) == len(values)164    # Check if type of each item from converted value is correct165    if isinstance(converted_list, dict):166        iterator = converted_list.values()167        original = values.values()168    else:169        iterator = converted_list170        original = values171    for converted_value, value in zip(sorted(iterator), sorted(original)):172        assert isinstance(converted_value, type_)173        conv_value = conv(value)174        # check if converted values are the same or both are nan175        assert converted_value == conv_value or (converted_value != converted_value and conv_value != conv_value)176@pytest.mark.unit177@pytest.mark.parametrize("EPSILON", [0.1])178@given(value=numpy_value(xps.floating_dtypes(), allow_infinity=False, allow_nan=False))179def test_epsilon_on_fp_conv(value, EPSILON):180    converted_value = conv(value)181    assert value - converted_value < EPSILON182    assert converted_value - value < EPSILON183@pytest.mark.unit184@given(185    value_and_type=st.one_of(186        value_with_type(187            elements=numpy_value(xps.floating_dtypes(), allow_infinity=False, allow_nan=False),188            expected_type=float,189        ),190        value_with_type(191            elements=numpy_value(xps.integer_dtypes(), allow_infinity=False, allow_nan=False),192            expected_type=int,193        ),194        value_with_type(195            elements=st.datetimes(timezones=xptz.timezones()),196            expected_type=int,197        ),198    ),199)200def test_conv(value_and_type):201    value, expected_type = value_and_type202    converted_value = conv(value)203    assert isinstance(converted_value, expected_type)204@pytest.mark.unit205@pytest.mark.parametrize(206    "sysmax, maxint",207    [208        (numpy.int64(sys.maxsize), numpy.int64(9223372036854775807)),209    ],210)211def test_maxint_conv(sysmax, maxint):212    # Robustness213    assert conv(sysmax) == maxint214@pytest.mark.unit215@given(216    value_and_types=st.one_of(217        value_with_type(218            st.integers(min_value=numpy.iinfo("int64").min, max_value=numpy.iinfo("int64").max),219            int,220            xps.integer_dtypes(endianness="=", sizes=(64,)),221        ),222        value_with_type(223            st.integers(min_value=numpy.iinfo("int32").min, max_value=numpy.iinfo("int32").max),224            int,225            xps.integer_dtypes(endianness="=", sizes=(32,)),226        ),227        value_with_type(228            st.integers(min_value=numpy.iinfo("int16").min, max_value=numpy.iinfo("int16").max),229            int,230            xps.integer_dtypes(endianness="=", sizes=(16,)),231        ),232        value_with_type(233            st.integers(min_value=numpy.iinfo("int8").min, max_value=numpy.iinfo("int8").max),234            int,235            xps.integer_dtypes(endianness="=", sizes=(8,)),236        ),237        value_with_type(st.floats(width=16), float, xps.floating_dtypes(endianness="=", sizes=(16,))),238        value_with_type(st.floats(width=32), float, xps.floating_dtypes(endianness="=", sizes=(32,))),239        value_with_type(st.floats(width=64), float, xps.floating_dtypes(endianness="=", sizes=(64,))),240    ),241)242def test_reverse_conv(value_and_types):243    value, current_type, data_type = value_and_types244    # verify if the current data type is as expected (int or float)245    assert isinstance(value, current_type)246    # convert value to given data type (int64, int32, float64 etc .. )247    converted_value = reverse_conv(data_type, value)248    # check if conversion is performed according to given data (int -> numpy.int64, float -> numpy.float64)249    assert numpy.issubdtype(type(converted_value), data_type)250    # check if converted data type is changed and not match with old one251    assert type(converted_value) != current_type252@pytest.mark.unit253@given(254    value_and_type=st.one_of(255        value_with_type(256            elements=numpy_value(xps.floating_dtypes(), allow_infinity=False, allow_nan=False),257            expected_type=float,258        ),259        value_with_type(260            elements=numpy_value(xps.integer_dtypes(), allow_infinity=False, allow_nan=False),261            expected_type=int,262        ),263        value_with_type(264            elements=st.datetimes(),265            expected_type=int,266        ),267    ),268)269@settings(suppress_health_check=(HealthCheck.function_scoped_fixture,))270def test_hypotesis_with_dask(dask_client_all, value_and_type):...test_operator.py
Source:test_operator.py  
...40@pytest.mark.parametrize("toep_cls", OPERATOR_LIST)41@given(42    arrays(43        shared(44            floating_dtypes(sizes=FLOAT_SIZES, endianness="="),45            key="dtype"46        ),47        shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"),48        elements=floats(allow_infinity=False, allow_nan=False, width=32)49    ),50    arrays(51        shared(52            floating_dtypes(sizes=FLOAT_SIZES, endianness="="),53            key="dtype"54        ),55        shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),56        elements=floats(allow_infinity=False, allow_nan=False, width=32)57    ),58    arrays(59        shared(60            floating_dtypes(sizes=FLOAT_SIZES, endianness="="),61            key="dtype"62        ),63        tuples(64            shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),65            integers(min_value=1, max_value=MAX_ARRAY)66        ),67        elements=floats(allow_infinity=False, allow_nan=False, width=32)68    ),69)70def test_toeplitz_real_mat(toep_cls, first_col, first_row, test):71    """Test toeplitz for real inputs."""72    full_mat = toeplitz(first_col, first_row)73    toeplitz_op = toep_cls(first_col, first_row)74    if first_col.dtype == np.float16:75        atol_frac = 1e-276    elif first_col.dtype == np.float32:77        atol_frac = 1e-578    elif first_col.dtype == np.float64:79        atol_frac = 1e-1480    elif first_col.dtype == np.float128:81        atol_frac = 1.1e-1582        if toep_cls == FFTToeplitz:83            atol_frac = 1e-1484    max_el = np.max(np.abs(first_col))85    if len(first_row) > 1:86        max_el = max(max_el, np.max(np.abs(first_row[1:])))87    max_test = np.max(np.abs(test))88    if max_el != 0 and max_test != 0:89        max_el *= max_test90    mat_result = full_mat.dot(test)91    if first_col.dtype == np.float32:92        # Apparently `np.dot` uses an extended-precision accumulator93        assume(np.all(np.isfinite(mat_result)))94    op_result = toeplitz_op.dot(test)95    if toep_cls == FFTToeplitz:96        assume(np.all(np.isfinite(op_result)))97    np_tst.assert_allclose(98        op_result,99        mat_result,100        atol=(atol_frac * max_el +101              ATOL_MIN * (len(test) + toeplitz_op.shape[0])),102        rtol=atol_frac103    )104@pytest.mark.parametrize("toep_cls", OPERATOR_LIST)105@given(106    arrays(107        shared(108            integer_dtypes(sizes=INTEGER_SIZES, endianness="="),109            key="dtype"110        ),111        shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"),112    ),113    arrays(114        shared(115            integer_dtypes(sizes=INTEGER_SIZES, endianness="="),116            key="dtype"117        ),118        shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),119    ),120    arrays(121        shared(122            integer_dtypes(sizes=INTEGER_SIZES, endianness="="),123            key="dtype"124        ),125        tuples(126            shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),127            integers(min_value=1, max_value=MAX_ARRAY)128        ),129    ),130)131def test_toeplitz_int_mat(toep_cls, first_col, first_row, test):132    """Test toeplitz for integer inputs."""133    full_mat = toeplitz(first_col, first_row)134    toeplitz_op = toep_cls(first_col, first_row)135    mat_result = full_mat.dot(test)136    if toep_cls in (ConvolveToeplitz, FFTToeplitz):137        rtol = 2e-6138        max_el = np.max(np.abs(first_col))139        if len(first_row) > 1:140            max_el = max(max_el, np.max(np.abs(first_row[1:])))141        # if max_el != 0:142        #     max_el *= np.max(np.abs(test))143        # assume(np.array(max_el, first_col.dtype)  == max_el)144        atol = abs(rtol * max_el * np.max(np.abs(test)))145        mat_result_long = toeplitz(146            first_col.astype(float),147            first_row.astype(float)148        ).dot(149            test.astype(float)150        )151        assume(np.allclose(152            mat_result,153            mat_result_long.astype(first_col.dtype)154        ))155    else:156        rtol = 0157        atol = 0158    np_tst.assert_allclose(159        toeplitz_op.dot(test),160        mat_result,161        rtol=rtol, atol=atol162    )163@pytest.mark.parametrize("toep_cls", OPERATOR_LIST)164@given(165    arrays(166        shared(167            complex_number_dtypes(sizes=COMPLEX_SIZES, endianness="="),168            key="dtype"169        ),170        shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"),171        elements=builds(172            complex,173            floats(allow_infinity=False, allow_nan=False, width=32),174            floats(allow_infinity=False, allow_nan=False, width=32),175        ),176    ).filter(lambda x: np.all(np.isfinite(x))),177    arrays(178        shared(179            complex_number_dtypes(sizes=COMPLEX_SIZES, endianness="="),180            key="dtype"181        ),182        shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),183        elements=builds(184            complex,185            floats(allow_infinity=False, allow_nan=False, width=32),186            floats(allow_infinity=False, allow_nan=False, width=32),187        ),188    ).filter(lambda x: np.all(np.isfinite(x))),189    arrays(190        shared(191            complex_number_dtypes(sizes=COMPLEX_SIZES, endianness="="),192            key="dtype"193        ),194        tuples(195            shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),196            integers(min_value=1, max_value=MAX_ARRAY)197        ),198        elements=builds(199            complex,200            floats(allow_infinity=False, allow_nan=False, width=32),201            floats(allow_infinity=False, allow_nan=False, width=32),202        ),203    ).filter(lambda x: np.all(np.isfinite(x))),204)205def test_toeplitz_complex_mat(toep_cls, first_col, first_row, test):206    """Test toeplitz for complex inputs."""207    full_mat = toeplitz(first_col, first_row)208    toeplitz_op = toep_cls(first_col, first_row)209    if first_col.dtype == np.complex64:210        atol_frac = 1e-5211    elif first_col.dtype == np.complex128:212        atol_frac = 1e-14213    elif first_col.dtype == np.complex256:214        atol_frac = 1e-15215        if toep_cls == FFTToeplitz:216            atol_frac = 1e-14217    max_el = np.max(np.abs(first_col))218    if len(first_row) > 1:219        max_el = max(max_el, np.max(np.abs(first_row[1:])))220    max_test = np.max(np.abs(test))221    if max_el != 0 and max_test != 0:222        max_el *= max_test223    mat_result = full_mat.dot(test)224    # Apparently `np.dot` uses an extended-precision accumulator225    assume(np.all(np.isfinite(mat_result)))226    op_result = toeplitz_op.dot(test)227    # np.dot may give nan or zero depending on array rank.228    assume(~np.any(np.isnan(op_result)))229    assume(np.all(np.isfinite(np.abs(op_result))))230    atol = atol_frac * max_el + ATOL_MIN * (len(test) + toeplitz_op.shape[0])231    assume(atol < np.inf)232    assume(atol != np.inf)233    np_tst.assert_allclose(234        op_result,235        mat_result,236        atol=atol,237        rtol=atol_frac238    )239@pytest.mark.parametrize("toep_cls", OPERATOR_LIST)240@given(241    arrays(242        shared(243            floating_dtypes(sizes=FLOAT_SIZES, endianness="="),244            key="dtype"245        ),246        shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"),247        elements=floats(allow_infinity=False, allow_nan=False, width=32)248    ),249    arrays(250        shared(251            floating_dtypes(sizes=FLOAT_SIZES, endianness="="),252            key="dtype"253        ),254        tuples(255            shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"),256            integers(min_value=1, max_value=MAX_ARRAY)257        ),258        elements=floats(allow_infinity=False, allow_nan=False, width=32)259    ),260)261def test_toeplitz_only_col(toep_cls, first_col, test):262    """Test toeplitz for real inputs."""263    full_mat = toeplitz(first_col)264    toeplitz_op = toep_cls(first_col)265    if first_col.dtype == np.float16:...common.py
Source:common.py  
2from hypothesis.extra.numpy import arrays as hy_arrays3from hypothesis.extra.numpy import floating_dtypes, integer_dtypes4from hypothesis.strategies import one_of5hy_array_gen = hy_arrays(6    dtype=one_of(integer_dtypes(sizes=(32, 64)), floating_dtypes(sizes=(32, 64))),7    shape=array_shapes(),8)9hy_int_array_gen = hy_arrays(10    dtype=integer_dtypes(sizes=(32, 64)),11    shape=array_shapes(),...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!!
