1*9f3f6d7bSStella Laurenzo# RUN: %PYTHON %s | FileCheck %s 2*9f3f6d7bSStella Laurenzo 3*9f3f6d7bSStella Laurenzoimport gc 4*9f3f6d7bSStella Laurenzofrom mlir.ir import * 5*9f3f6d7bSStella Laurenzo 6*9f3f6d7bSStella Laurenzodef run(f): 7*9f3f6d7bSStella Laurenzo print("\nTEST:", f.__name__) 8*9f3f6d7bSStella Laurenzo f() 9*9f3f6d7bSStella Laurenzo gc.collect() 10*9f3f6d7bSStella Laurenzo assert Context._get_live_count() == 0 11*9f3f6d7bSStella Laurenzo 12*9f3f6d7bSStella Laurenzo 13*9f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testContextEnterExit 14*9f3f6d7bSStella Laurenzodef testContextEnterExit(): 15*9f3f6d7bSStella Laurenzo with Context() as ctx: 16*9f3f6d7bSStella Laurenzo assert Context.current is ctx 17*9f3f6d7bSStella Laurenzo try: 18*9f3f6d7bSStella Laurenzo _ = Context.current 19*9f3f6d7bSStella Laurenzo except ValueError as e: 20*9f3f6d7bSStella Laurenzo # CHECK: No current Context 21*9f3f6d7bSStella Laurenzo print(e) 22*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 23*9f3f6d7bSStella Laurenzo 24*9f3f6d7bSStella Laurenzorun(testContextEnterExit) 25*9f3f6d7bSStella Laurenzo 26*9f3f6d7bSStella Laurenzo 27*9f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testLocationEnterExit 28*9f3f6d7bSStella Laurenzodef testLocationEnterExit(): 29*9f3f6d7bSStella Laurenzo ctx1 = Context() 30*9f3f6d7bSStella Laurenzo with Location.unknown(ctx1) as loc1: 31*9f3f6d7bSStella Laurenzo assert Context.current is ctx1 32*9f3f6d7bSStella Laurenzo assert Location.current is loc1 33*9f3f6d7bSStella Laurenzo 34*9f3f6d7bSStella Laurenzo # Re-asserting the same context should not change the location. 35*9f3f6d7bSStella Laurenzo with ctx1: 36*9f3f6d7bSStella Laurenzo assert Context.current is ctx1 37*9f3f6d7bSStella Laurenzo assert Location.current is loc1 38*9f3f6d7bSStella Laurenzo # Asserting a different context should clear it. 39*9f3f6d7bSStella Laurenzo with Context() as ctx2: 40*9f3f6d7bSStella Laurenzo assert Context.current is ctx2 41*9f3f6d7bSStella Laurenzo try: 42*9f3f6d7bSStella Laurenzo _ = Location.current 43*9f3f6d7bSStella Laurenzo except ValueError: pass 44*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 45*9f3f6d7bSStella Laurenzo 46*9f3f6d7bSStella Laurenzo # And should restore. 47*9f3f6d7bSStella Laurenzo assert Context.current is ctx1 48*9f3f6d7bSStella Laurenzo assert Location.current is loc1 49*9f3f6d7bSStella Laurenzo 50*9f3f6d7bSStella Laurenzo # All should clear. 51*9f3f6d7bSStella Laurenzo try: 52*9f3f6d7bSStella Laurenzo _ = Location.current 53*9f3f6d7bSStella Laurenzo except ValueError as e: 54*9f3f6d7bSStella Laurenzo # CHECK: No current Location 55*9f3f6d7bSStella Laurenzo print(e) 56*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 57*9f3f6d7bSStella Laurenzo 58*9f3f6d7bSStella Laurenzorun(testLocationEnterExit) 59*9f3f6d7bSStella Laurenzo 60*9f3f6d7bSStella Laurenzo 61*9f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testInsertionPointEnterExit 62*9f3f6d7bSStella Laurenzodef testInsertionPointEnterExit(): 63*9f3f6d7bSStella Laurenzo ctx1 = Context() 64*9f3f6d7bSStella Laurenzo m = Module.create(Location.unknown(ctx1)) 65*9f3f6d7bSStella Laurenzo ip = InsertionPoint(m.body) 66*9f3f6d7bSStella Laurenzo 67*9f3f6d7bSStella Laurenzo with ip: 68*9f3f6d7bSStella Laurenzo assert InsertionPoint.current is ip 69*9f3f6d7bSStella Laurenzo # Asserting a location from the same context should preserve. 70*9f3f6d7bSStella Laurenzo with Location.unknown(ctx1) as loc1: 71*9f3f6d7bSStella Laurenzo assert InsertionPoint.current is ip 72*9f3f6d7bSStella Laurenzo assert Location.current is loc1 73*9f3f6d7bSStella Laurenzo # Location should clear. 74*9f3f6d7bSStella Laurenzo try: 75*9f3f6d7bSStella Laurenzo _ = Location.current 76*9f3f6d7bSStella Laurenzo except ValueError: pass 77*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 78*9f3f6d7bSStella Laurenzo 79*9f3f6d7bSStella Laurenzo # Asserting the same Context should preserve. 80*9f3f6d7bSStella Laurenzo with ctx1: 81*9f3f6d7bSStella Laurenzo assert InsertionPoint.current is ip 82*9f3f6d7bSStella Laurenzo 83*9f3f6d7bSStella Laurenzo # Asserting a different context should clear it. 84*9f3f6d7bSStella Laurenzo with Context() as ctx2: 85*9f3f6d7bSStella Laurenzo assert Context.current is ctx2 86*9f3f6d7bSStella Laurenzo try: 87*9f3f6d7bSStella Laurenzo _ = InsertionPoint.current 88*9f3f6d7bSStella Laurenzo except ValueError: pass 89*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 90*9f3f6d7bSStella Laurenzo 91*9f3f6d7bSStella Laurenzo # All should clear. 92*9f3f6d7bSStella Laurenzo try: 93*9f3f6d7bSStella Laurenzo _ = InsertionPoint.current 94*9f3f6d7bSStella Laurenzo except ValueError as e: 95*9f3f6d7bSStella Laurenzo # CHECK: No current InsertionPoint 96*9f3f6d7bSStella Laurenzo print(e) 97*9f3f6d7bSStella Laurenzo else: assert False, "Expected exception" 98*9f3f6d7bSStella Laurenzo 99*9f3f6d7bSStella Laurenzorun(testInsertionPointEnterExit) 100