19f3f6d7bSStella Laurenzo# RUN: %PYTHON %s | FileCheck %s 29f3f6d7bSStella Laurenzo 39f3f6d7bSStella Laurenzoimport gc 49f3f6d7bSStella Laurenzofrom mlir.ir import * 59f3f6d7bSStella Laurenzo 69f3f6d7bSStella Laurenzodef run(f): 79f3f6d7bSStella Laurenzo print("\nTEST:", f.__name__) 89f3f6d7bSStella Laurenzo f() 99f3f6d7bSStella Laurenzo gc.collect() 109f3f6d7bSStella Laurenzo assert Context._get_live_count() == 0 11fc7594ccSAlex Zinenko return f 129f3f6d7bSStella Laurenzo 139f3f6d7bSStella Laurenzo 149f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testIntegerSetCapsule 15fc7594ccSAlex Zinenko@run 169f3f6d7bSStella Laurenzodef testIntegerSetCapsule(): 179f3f6d7bSStella Laurenzo with Context() as ctx: 189f3f6d7bSStella Laurenzo is1 = IntegerSet.get_empty(1, 1, ctx) 199f3f6d7bSStella Laurenzo capsule = is1._CAPIPtr 209f3f6d7bSStella Laurenzo # CHECK: mlir.ir.IntegerSet._CAPIPtr 219f3f6d7bSStella Laurenzo print(capsule) 229f3f6d7bSStella Laurenzo is2 = IntegerSet._CAPICreate(capsule) 239f3f6d7bSStella Laurenzo assert is1 == is2 249f3f6d7bSStella Laurenzo assert is2.context is ctx 259f3f6d7bSStella Laurenzo 269f3f6d7bSStella Laurenzo 279f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testIntegerSetGet 28fc7594ccSAlex Zinenko@run 299f3f6d7bSStella Laurenzodef testIntegerSetGet(): 309f3f6d7bSStella Laurenzo with Context(): 319f3f6d7bSStella Laurenzo d0 = AffineDimExpr.get(0) 329f3f6d7bSStella Laurenzo d1 = AffineDimExpr.get(1) 339f3f6d7bSStella Laurenzo s0 = AffineSymbolExpr.get(0) 349f3f6d7bSStella Laurenzo c42 = AffineConstantExpr.get(42) 359f3f6d7bSStella Laurenzo 369f3f6d7bSStella Laurenzo # CHECK: (d0, d1)[s0] : (d0 - d1 == 0, s0 - 42 >= 0) 379f3f6d7bSStella Laurenzo set0 = IntegerSet.get(2, 1, [d0 - d1, s0 - c42], [True, False]) 389f3f6d7bSStella Laurenzo print(set0) 399f3f6d7bSStella Laurenzo 409f3f6d7bSStella Laurenzo # CHECK: (d0)[s0] : (1 == 0) 419f3f6d7bSStella Laurenzo set1 = IntegerSet.get_empty(1, 1) 429f3f6d7bSStella Laurenzo print(set1) 439f3f6d7bSStella Laurenzo 449f3f6d7bSStella Laurenzo # CHECK: (d0)[s0, s1] : (d0 - s1 == 0, s0 - 42 >= 0) 459f3f6d7bSStella Laurenzo set2 = set0.get_replaced([d0, AffineSymbolExpr.get(1)], [s0], 1, 2) 469f3f6d7bSStella Laurenzo print(set2) 479f3f6d7bSStella Laurenzo 489f3f6d7bSStella Laurenzo try: 499f3f6d7bSStella Laurenzo IntegerSet.get(2, 1, [], []) 509f3f6d7bSStella Laurenzo except ValueError as e: 519f3f6d7bSStella Laurenzo # CHECK: Expected non-empty list of constraints 529f3f6d7bSStella Laurenzo print(e) 539f3f6d7bSStella Laurenzo 549f3f6d7bSStella Laurenzo try: 559f3f6d7bSStella Laurenzo IntegerSet.get(2, 1, [d0 - d1], [True, False]) 569f3f6d7bSStella Laurenzo except ValueError as e: 579f3f6d7bSStella Laurenzo # CHECK: Expected the number of constraints to match that of equality flags 589f3f6d7bSStella Laurenzo print(e) 599f3f6d7bSStella Laurenzo 609f3f6d7bSStella Laurenzo try: 619f3f6d7bSStella Laurenzo IntegerSet.get(2, 1, [0], [True]) 629f3f6d7bSStella Laurenzo except RuntimeError as e: 639f3f6d7bSStella Laurenzo # CHECK: Invalid expression when attempting to create an IntegerSet 649f3f6d7bSStella Laurenzo print(e) 659f3f6d7bSStella Laurenzo 669f3f6d7bSStella Laurenzo try: 679f3f6d7bSStella Laurenzo IntegerSet.get(2, 1, [None], [True]) 689f3f6d7bSStella Laurenzo except RuntimeError as e: 699f3f6d7bSStella Laurenzo # CHECK: Invalid expression (None?) when attempting to create an IntegerSet 709f3f6d7bSStella Laurenzo print(e) 719f3f6d7bSStella Laurenzo 729f3f6d7bSStella Laurenzo try: 739f3f6d7bSStella Laurenzo set0.get_replaced([d0], [s0], 1, 1) 749f3f6d7bSStella Laurenzo except ValueError as e: 759f3f6d7bSStella Laurenzo # CHECK: Expected the number of dimension replacement expressions to match that of dimensions 769f3f6d7bSStella Laurenzo print(e) 779f3f6d7bSStella Laurenzo 789f3f6d7bSStella Laurenzo try: 799f3f6d7bSStella Laurenzo set0.get_replaced([d0, d1], [s0, s0], 1, 1) 809f3f6d7bSStella Laurenzo except ValueError as e: 819f3f6d7bSStella Laurenzo # CHECK: Expected the number of symbol replacement expressions to match that of symbols 829f3f6d7bSStella Laurenzo print(e) 839f3f6d7bSStella Laurenzo 849f3f6d7bSStella Laurenzo try: 859f3f6d7bSStella Laurenzo set0.get_replaced([d0, 1], [s0], 1, 1) 869f3f6d7bSStella Laurenzo except RuntimeError as e: 879f3f6d7bSStella Laurenzo # CHECK: Invalid expression when attempting to create an IntegerSet by replacing dimensions 889f3f6d7bSStella Laurenzo print(e) 899f3f6d7bSStella Laurenzo 909f3f6d7bSStella Laurenzo try: 919f3f6d7bSStella Laurenzo set0.get_replaced([d0, d1], [None], 1, 1) 929f3f6d7bSStella Laurenzo except RuntimeError as e: 939f3f6d7bSStella Laurenzo # CHECK: Invalid expression (None?) when attempting to create an IntegerSet by replacing symbols 949f3f6d7bSStella Laurenzo print(e) 959f3f6d7bSStella Laurenzo 969f3f6d7bSStella Laurenzo 979f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testIntegerSetProperties 98fc7594ccSAlex Zinenko@run 999f3f6d7bSStella Laurenzodef testIntegerSetProperties(): 1009f3f6d7bSStella Laurenzo with Context(): 1019f3f6d7bSStella Laurenzo d0 = AffineDimExpr.get(0) 1029f3f6d7bSStella Laurenzo d1 = AffineDimExpr.get(1) 1039f3f6d7bSStella Laurenzo s0 = AffineSymbolExpr.get(0) 1049f3f6d7bSStella Laurenzo c42 = AffineConstantExpr.get(42) 1059f3f6d7bSStella Laurenzo 1069f3f6d7bSStella Laurenzo set0 = IntegerSet.get(2, 1, [d0 - d1, s0 - c42, s0 - d0], [True, False, False]) 1079f3f6d7bSStella Laurenzo # CHECK: 2 1089f3f6d7bSStella Laurenzo print(set0.n_dims) 1099f3f6d7bSStella Laurenzo # CHECK: 1 1109f3f6d7bSStella Laurenzo print(set0.n_symbols) 1119f3f6d7bSStella Laurenzo # CHECK: 3 1129f3f6d7bSStella Laurenzo print(set0.n_inputs) 1139f3f6d7bSStella Laurenzo # CHECK: 1 1149f3f6d7bSStella Laurenzo print(set0.n_equalities) 1159f3f6d7bSStella Laurenzo # CHECK: 2 1169f3f6d7bSStella Laurenzo print(set0.n_inequalities) 1179f3f6d7bSStella Laurenzo 1189f3f6d7bSStella Laurenzo # CHECK: 3 1199f3f6d7bSStella Laurenzo print(len(set0.constraints)) 1209f3f6d7bSStella Laurenzo 1219f3f6d7bSStella Laurenzo # CHECK-DAG: d0 - d1 == 0 1229f3f6d7bSStella Laurenzo # CHECK-DAG: s0 - 42 >= 0 1239f3f6d7bSStella Laurenzo # CHECK-DAG: -d0 + s0 >= 0 1249f3f6d7bSStella Laurenzo for cstr in set0.constraints: 1259f3f6d7bSStella Laurenzo print(cstr.expr, end='') 1269f3f6d7bSStella Laurenzo print(" == 0" if cstr.is_eq else " >= 0") 1279f3f6d7bSStella Laurenzo 128fc7594ccSAlex Zinenko 129*8894c05bSIvan Kosarev# TODO-LABEL: TEST: testHash 130fc7594ccSAlex Zinenko@run 131fc7594ccSAlex Zinenkodef testHash(): 132fc7594ccSAlex Zinenko with Context(): 133fc7594ccSAlex Zinenko d0 = AffineDimExpr.get(0) 134fc7594ccSAlex Zinenko d1 = AffineDimExpr.get(1) 135fc7594ccSAlex Zinenko set = IntegerSet.get(2, 0, [d0 + d1], [True]) 136fc7594ccSAlex Zinenko 137fc7594ccSAlex Zinenko assert hash(set) == hash(IntegerSet.get(2, 0, [d0 + d1], [True])) 138fc7594ccSAlex Zinenko 139fc7594ccSAlex Zinenko dictionary = dict() 140fc7594ccSAlex Zinenko dictionary[set] = 42 141fc7594ccSAlex Zinenko assert set in dictionary 142