Beispiel #1
0
    def in_bounds(cls, type_str, value):
        # TODO: fix this circular import
        from vyper.codegen.types import parse_decimal_info, parse_integer_typeinfo

        assert isinstance(type_str, str)
        if type_str == "decimal":
            info = parse_decimal_info(type_str)
        else:
            info = parse_integer_typeinfo(type_str)

        (lo, hi) = int_bounds(info.is_signed, info.bits)
        return lo <= value <= hi
Beispiel #2
0
def test_minmax_value_decimal(get_contract, op, typ):
    code = f"""
@external
def foo() -> {typ}:
    return {op}({typ})
    """
    c = get_contract(code)

    typ_info = parse_decimal_info(typ)
    (lo, hi) = typ_info.decimal_bounds
    if op == "min_value":
        assert c.foo() == lo
    elif op == "max_value":
        assert c.foo() == hi
Beispiel #3
0
def _cases_for_decimal(typ):
    info = parse_decimal_info(typ)

    lo, hi = info.decimal_bounds

    ret = [Decimal(i) for i in [-1, 0, 1]]
    ret.extend([lo - 1, lo, lo + 1, hi - 1, hi, hi + 1])

    ret.extend([
        lo - DECIMAL_EPSILON, lo + DECIMAL_EPSILON, hi - DECIMAL_EPSILON,
        hi + DECIMAL_EPSILON
    ])

    # random cases cause reproducibility issues. TODO fixme
    # (use int values because randrange can't generate fractional decimals)
    # int_lo, int_hi = info.bounds  # e.g. -(2**167)
    # NUM_RANDOM_CASES = 10  # more than int, just for paranoia's sake
    # DIVISOR = info.divisor
    # ret.extend(random.randrange(int_lo, int_hi) / DIVISOR for _ in range(NUM_RANDOM_CASES))

    return ret
Beispiel #4
0
def _parse_type(typename):
    if typename.startswith(("uint", "int")):
        info = parse_integer_typeinfo(typename)
        assert info.bits % 8 == 0
        return TestType(typename, info.bits // 8, "int", info)
    elif typename == "decimal":
        info = parse_decimal_info(typename)
        assert info.bits % 8 == 0
        return TestType(typename, info.bits // 8, "decimal", info)
    elif typename.startswith("bytes"):
        info = parse_bytes_m_info(typename)
        return TestType(typename, info.m, "bytes", info)
    elif typename.startswith("Bytes"):
        assert typename == "Bytes[32]"  # TODO test others
        return TestType(typename, 32, "Bytes", None)
    elif typename.startswith("String"):
        assert typename == "String[32]"  # TODO test others
        return TestType(typename, 32, "String", None)
    elif typename == "address":
        return TestType(typename, 20, "address", None)
    elif typename == "bool":
        return TestType(typename, 1, "bool", None)

    raise AssertionError(f"no info {typename}")