<commit_before>import pytest

import taichi as ti
from tests import test_utils


@test_utils.test()
def test_return_without_type_hint():
    @ti.kernel
    def kernel():
        return 1

    with pytest.raises(ti.TaichiCompilationError):
        kernel()


def test_const_func_ret():
    ti.init()

    @ti.kernel
    def func1() -> ti.f32:
        return 3

    @ti.kernel
    def func2() -> ti.i32:
        return 3.3  # return type mismatch, will be auto-casted into ti.i32

    assert func1() == test_utils.approx(3)
    assert func2() == 3


@test_utils.test()
def _test_binary_func_ret(dt1, dt2, dt3, castor):
    @ti.kernel
    def func(a: dt1, b: dt2) -> dt3:
        return a * b

    if ti.types.is_integral(dt1):
        xs = list(range(4))
    else:
        xs = [0.2, 0.4, 0.8, 1.0]

    if ti.types.is_integral(dt2):
        ys = list(range(4))
    else:
        ys = [0.2, 0.4, 0.8, 1.0]

    for x, y in zip(xs, ys):
        assert func(x, y) == test_utils.approx(castor(x * y))


def test_binary_func_ret():
    _test_binary_func_ret(ti.i32, ti.f32, ti.f32, float)
    _test_binary_func_ret(ti.f32, ti.i32, ti.f32, float)
    _test_binary_func_ret(ti.i32, ti.f32, ti.i32, int)
    _test_binary_func_ret(ti.f32, ti.i32, ti.i32, int)


@test_utils.test()
def test_return_in_static_if():
    @ti.kernel
    def foo(a: ti.template()) -> ti.i32:
        if ti.static(a == 1):
            return 1<commit_msg>[test] Fix ill-formed test_binary_func_ret (#4587)<commit_after>import pytest

import taichi as ti
from tests import test_utils


@test_utils.test()
def test_return_without_type_hint():
    @ti.kernel
    def kernel():
        return 1

    with pytest.raises(ti.TaichiCompilationError):
        kernel()


def test_const_func_ret():
    ti.init()

    @ti.kernel
    def func1() -> ti.f32:
        return 3

    @ti.kernel
    def func2() -> ti.i32:
        return 3.3  # return type mismatch, will be auto-casted into ti.i32

    assert func1() == test_utils.approx(3)
    assert func2() == 3


@pytest.mark.parametrize("dt1,dt2,dt3,castor",
                         [(ti.i32, ti.f32, ti.f32, float),
                          (ti.f32, ti.i32, ti.f32, float),
                          (ti.i32, ti.f32, ti.i32, int),
                          (ti.f32, ti.i32, ti.i32, int)])
@test_utils.test()
def test_binary_func_ret(dt1, dt2, dt3, castor):
    @ti.kernel
    def func(a: dt1, b: dt2) -> dt3:
        return a * b

    if ti.types.is_integral(dt1):
        xs = list(range(4))
    else:
        xs = [0.2, 0.4, 0.8, 1.0]

    if ti.types.is_integral(dt2):
        ys = list(range(4))
    else:
        ys = [0.2, 0.4, 0.8, 1.0]

    for x, y in zip(xs, ys):
        assert func(x, y) == test_utils.approx(castor(x * y))


@test_utils.test()
def test_return_in_static_if():
    @ti.kernel
    def foo(a: ti.template()) -> ti.i32:
        if ti.static(a == 1):
            return 1
{'fraction_non_alphanumeric': 0.14359504132231404, 'fraction_numerical': 0.0506198347107438, 'mean_word_length': 2.2421875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093312', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1134, 'n_words': 258}