def test_narrow_with_collapsed_type(): from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_DOUBLE Int = SLEEPY_INT RefInt = ReferenceType(Int) RefRefInt = ReferenceType(RefInt) Int_RefInt = UnionType.from_types([Int, RefInt]) Double = SLEEPY_DOUBLE RefDouble = ReferenceType(Double) Int_Double = UnionType.from_types([Int, Double]) RefDouble_RefInt = UnionType.from_types([RefDouble, RefInt]) assert_equal(narrow_with_collapsed_type(Int, Int), Int) assert_equal(narrow_with_collapsed_type(RefInt, Int), RefInt) assert_equal(narrow_with_collapsed_type(RefRefInt, Int), RefRefInt) assert_equal(narrow_with_collapsed_type(Int_RefInt, Int), Int_RefInt) assert_equal(narrow_with_collapsed_type(RefInt, RefInt), RefInt) assert_equal(narrow_with_collapsed_type(Int, RefInt), SLEEPY_NEVER) assert_equal(narrow_with_collapsed_type(Int_RefInt, RefInt), narrow_type(Int_RefInt, RefInt)) assert_equal(narrow_with_collapsed_type(Int_Double, Int), narrow_type(Int_Double, Int)) assert_equal(narrow_with_collapsed_type(RefDouble_RefInt, Int), narrow_type(RefDouble_RefInt, RefInt)) assert_equal(narrow_with_collapsed_type(ReferenceType(Int_Double), Int), ReferenceType(narrow_type(Int_Double, Int))) assert_equal( narrow_with_collapsed_type(ReferenceType(ReferenceType(Int_Double)), Int), ReferenceType(ReferenceType(narrow_type(Int_Double, Int)))) assert_equal( narrow_with_collapsed_type(ReferenceType(ReferenceType(Int_Double)), ReferenceType(Int)), ReferenceType(ReferenceType(narrow_type(Int_Double, Int))))
def test_union_replace_types(): from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_CHAR, SLEEPY_DOUBLE union = UnionType.from_types(possible_types=[SLEEPY_DOUBLE]) replaced_union = union.replace_types( {SLEEPY_DOUBLE: UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])}) assert_equal( replaced_union, UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR], val_size=union.val_size))
def test_narrow_type_templates(): from sleepy.types import narrow_type, UnionType, PlaceholderTemplateType, StructType from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_BOOL context = make_test_context() Int, Bool = SLEEPY_INT, SLEEPY_BOOL T = PlaceholderTemplateType('T') List = StructType(identity=StructIdentity('List', context=context), template_param_or_arg=[T], member_identifiers=[], member_types=[]) # narrow(0:List[Int]|1:List[Bool], List[Int]) = 0:List[Int] assert_equal( narrow_type( UnionType.from_types( [List.replace_types({T: Int}), List.replace_types({T: Bool})]), List.replace_types({T: Int})), UnionType.from_types([List.replace_types({T: Int})])) # narrow(List[Int|Bool], List[Int]) = List[Int|Bool] assert_equal( narrow_type(List.replace_types({T: UnionType.from_types([Int, Bool])}), List.replace_types({T: Int})), List.replace_types({T: UnionType.from_types([Int, Bool])})) # narrow(List[Int|Bool]|List[Int], List[Int]) = List[Int|Bool]|List[Int] assert_equal( narrow_type( UnionType.from_types([ List.replace_types({T: UnionType.from_types([Int, Bool])}), List.replace_types({T: Int}) ]), List.replace_types({T: Int})), UnionType.from_types([ List.replace_types({T: UnionType.from_types([Int, Bool])}), List.replace_types({T: Int}) ]))
def test_exclude_type(): from sleepy.types import exclude_type, UnionType, ReferenceType from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_BOOL, SLEEPY_DOUBLE assert_equal(exclude_type(SLEEPY_INT, SLEEPY_NEVER), SLEEPY_INT) assert_equal(exclude_type(SLEEPY_INT, SLEEPY_INT), SLEEPY_NEVER) assert_equal( exclude_type(UnionType({ SLEEPY_INT: 0, SLEEPY_DOUBLE: 1 }, 8), SLEEPY_DOUBLE), UnionType({SLEEPY_INT: 0}, 8)) assert_equal( exclude_type(UnionType({ SLEEPY_INT: 0, SLEEPY_DOUBLE: 1 }, 8), SLEEPY_INT), UnionType({SLEEPY_DOUBLE: 1}, 8)) assert_equal( exclude_type(UnionType({ SLEEPY_INT: 0, SLEEPY_DOUBLE: 1 }, 8), SLEEPY_BOOL), UnionType({ SLEEPY_INT: 0, SLEEPY_DOUBLE: 1 }, 8)) assert_equal( exclude_type(ReferenceType(SLEEPY_INT), ReferenceType(SLEEPY_INT)), SLEEPY_NEVER) assert_equal(exclude_type(ReferenceType(SLEEPY_INT), SLEEPY_NEVER), ReferenceType(SLEEPY_INT)) assert_equal( exclude_type( ReferenceType(UnionType({ SLEEPY_INT: 0, SLEEPY_BOOL: 1 }, 4)), ReferenceType(SLEEPY_INT)), ReferenceType(UnionType({SLEEPY_BOOL: 1}, 4))) assert_equal( exclude_type( ReferenceType(UnionType({ SLEEPY_INT: 0, SLEEPY_BOOL: 1 }, 4)), ReferenceType(SLEEPY_BOOL)), ReferenceType(UnionType({SLEEPY_INT: 0}, 4)))
def test_copy_collapse(): from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_DOUBLE Int = TypedValue(typ=SLEEPY_INT, num_unbindings=0, ir_val=None) RefInt = TypedValue(typ=ReferenceType(SLEEPY_INT), num_unbindings=0, ir_val=None) Int_RefInt = TypedValue(typ=UnionType.from_types([Int.type, RefInt.type]), num_unbindings=0, ir_val=None) RefInt_Int = TypedValue(typ=UnionType.from_types([RefInt.type, Int.type]), num_unbindings=0, ir_val=None) RefInt_Double = TypedValue(typ=UnionType.from_types( [RefInt.type, SLEEPY_DOUBLE]), num_unbindings=0, ir_val=None) assert_equal(RefInt.copy_collapse(context=None, name='a'), Int) assert_equal(Int.copy_collapse(context=None, name='a'), Int) assert_equal(RefInt_Int.copy_collapse(context=None, name='a'), Int) assert_equal(Int_RefInt.copy_collapse(context=None, name='a'), Int)
def test_get_common_type(): from sleepy.types import get_common_type, UnionType from sleepy.builtin_symbols import SLEEPY_INT from sleepy.builtin_symbols import SLEEPY_BOOL from sleepy.builtin_symbols import SLEEPY_DOUBLE assert_equal(get_common_type([SLEEPY_INT]), SLEEPY_INT) assert_equal(get_common_type([SLEEPY_INT, SLEEPY_INT]), SLEEPY_INT) int_bool_union = UnionType({SLEEPY_INT: 0, SLEEPY_BOOL: 1}, 4) bool_int_union = UnionType({SLEEPY_INT: 1, SLEEPY_BOOL: 0}, 4) assert_equal(get_common_type([SLEEPY_INT, SLEEPY_BOOL]), int_bool_union) assert_equal(get_common_type([SLEEPY_INT, SLEEPY_BOOL, int_bool_union]), int_bool_union) assert_equal(get_common_type([int_bool_union, bool_int_union]), int_bool_union) assert_equal( get_common_type([int_bool_union, SLEEPY_DOUBLE]), UnionType({ SLEEPY_INT: 0, SLEEPY_BOOL: 1, SLEEPY_DOUBLE: 2 }, 8))
def test_try_infer_templ_types_union(): from sleepy.types import try_infer_template_arguments from sleepy.builtin_symbols import SLEEPY_CHAR from sleepy.builtin_symbols import SLEEPY_INT assert_equal( try_infer_template_arguments( calling_types=[SLEEPY_INT], signature_types=[UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])], template_parameters=[]), []) assert_equal( try_infer_template_arguments( calling_types=[SLEEPY_CHAR], signature_types=[UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])], template_parameters=[]), []) assert_equal( try_infer_template_arguments( calling_types=[UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])], signature_types=[UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])], template_parameters=[]), []) assert_equal( try_infer_template_arguments( calling_types=[UnionType.from_types([SLEEPY_INT, SLEEPY_CHAR])], signature_types=[UnionType.from_types([SLEEPY_CHAR, SLEEPY_INT])], template_parameters=[]), [])
def test_narrow_type(): from sleepy.types import narrow_type, UnionType from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_BOOL assert_equal(narrow_type(SLEEPY_INT, SLEEPY_INT), SLEEPY_INT) assert_equal(narrow_type(UnionType.from_types([SLEEPY_INT]), SLEEPY_INT), UnionType.from_types([SLEEPY_INT])) assert_equal(narrow_type(SLEEPY_INT, UnionType.from_types([SLEEPY_INT])), SLEEPY_INT) assert_equal( narrow_type(UnionType.from_types([SLEEPY_INT, SLEEPY_BOOL]), UnionType.from_types([SLEEPY_INT])), UnionType.from_types([SLEEPY_INT])) assert_equal( narrow_type(UnionType.from_types([SLEEPY_INT, SLEEPY_BOOL]), SLEEPY_BOOL), UnionType({SLEEPY_BOOL: 1}, val_size=4)) assert_equal( narrow_type(UnionType.from_types([SLEEPY_INT, SLEEPY_BOOL]), UnionType.from_types([SLEEPY_BOOL])), UnionType({SLEEPY_BOOL: 1}, val_size=4)) assert_equal(narrow_type(SLEEPY_INT, SLEEPY_BOOL), SLEEPY_NEVER)
def test_can_implicit_cast_to(): from sleepy.types import can_implicit_cast_to, ReferenceType, UnionType, StructType, PlaceholderTemplateType from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_DOUBLE context = make_test_context() assert_equal(can_implicit_cast_to(SLEEPY_INT, SLEEPY_DOUBLE), False) assert_equal(can_implicit_cast_to(SLEEPY_INT, SLEEPY_INT), True) assert_equal( can_implicit_cast_to(UnionType.from_types([SLEEPY_INT]), SLEEPY_INT), True) assert_equal( can_implicit_cast_to(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE]), SLEEPY_DOUBLE), False) assert_equal( can_implicit_cast_to(SLEEPY_DOUBLE, UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])), True) assert_equal( can_implicit_cast_to(ReferenceType(SLEEPY_INT), ReferenceType(SLEEPY_DOUBLE)), False) assert_equal( can_implicit_cast_to(ReferenceType(SLEEPY_INT), ReferenceType(SLEEPY_INT)), True) assert_equal( can_implicit_cast_to( ReferenceType(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])), ReferenceType(SLEEPY_INT)), False) assert_equal( can_implicit_cast_to( ReferenceType(SLEEPY_INT), ReferenceType(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE]))), False) T = PlaceholderTemplateType('T') List = StructType(identity=StructIdentity('List', context=context), template_param_or_arg=[T], member_identifiers=[], member_types=[]) assert_equal( can_implicit_cast_to( ReferenceType(SLEEPY_INT), ReferenceType(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE]))), False) assert_equal( can_implicit_cast_to( ReferenceType(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])), ReferenceType(SLEEPY_INT)), False) assert_equal( can_implicit_cast_to( ReferenceType(UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])), UnionType.from_types( [ReferenceType(SLEEPY_INT), ReferenceType(SLEEPY_DOUBLE)])), False) assert_equal( can_implicit_cast_to( List.replace_types( {T: UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])}), List.replace_types({T: SLEEPY_INT})), False) assert_equal( can_implicit_cast_to( List.replace_types({T: SLEEPY_INT}), List.replace_types( {T: UnionType.from_types([SLEEPY_INT, SLEEPY_DOUBLE])})), False)
def test_narrow_type_references(): from sleepy.types import narrow_type, UnionType, ReferenceType from sleepy.builtin_symbols import SLEEPY_INT, SLEEPY_BOOL Int, Bool = SLEEPY_INT, SLEEPY_BOOL Ref = ReferenceType # narrow(Ref[A], Ref[A]) = Ref[A] assert_equal(narrow_type(Ref(Int), Ref(Int)), Ref(Int)) # narrow(Ref[0:A|1:B], Ref[A]) = Ref[0:A] assert_equal(narrow_type(Ref(UnionType({ Int: 0, Bool: 1 }, 4)), Ref(Int)), Ref(UnionType.from_types([Int]))) # narrow(0:Ref[0:A|1:B]|1:Ref[A], Ref[B]) = 0:Ref[1:B] assert_equal( narrow_type( UnionType.from_types( [Ref(UnionType({ Int: 0, Bool: 1 }, 4)), Ref(Int)]), Ref(Bool)), UnionType({Ref(UnionType({Bool: 1}, 4)): 0}, 8)) # narrow(Ref[0:A|1:B], Ref[A]|Ref[B]) = Ref[0:A|1:B] assert_equal( narrow_type(Ref(UnionType({ Int: 0, Bool: 1 }, 8)), UnionType({ Ref(Int): 0, Ref(Bool): 1 }, 8)), Ref(UnionType({ Int: 0, Bool: 1 }, 8))) # narrow(Ref[A]|Ref[B], Ref[A|B]) = Ref[A]|Ref[B] assert_equal( narrow_type(UnionType.from_types([Ref(Int), Ref(Bool)]), Ref(UnionType.from_types([Int, Bool]))), UnionType.from_types([Ref(Int), Ref(Bool)])) # narrow(Ref[0:A|1:B]|Ref[A], Ref[A]) = Ref[0:A]|Ref[A] assert_equal( narrow_type( UnionType.from_types( [Ref(UnionType.from_types([Int, Bool])), Ref(Int)]), Ref(Int)), UnionType.from_types([Ref(UnionType.from_types([Int])), Ref(Int)])) # narrow(Ref[0:A], Ref[0:A|1:B]) = Ref[0:A] assert_equal( narrow_type(Ref(UnionType.from_types([Int])), Ref(UnionType.from_types([Int, Bool]))), Ref(UnionType.from_types([Int])))