def test_instantiate_eval_define_values(): l = make_linklet("(linklet () ((x x15)) (define-values (x) 4) (+ x x))") t = inst(make_linklet("(linklet () ((x x16)) (define-values (x) 1000))")) _, t = eval_fixnum(l, t) assert defines(t, "x15") and defines(t, "x16") and not defines(t, "x") assert check_val(t, "x15", 4) assert check_val(t, "x16", 1000)
def test_instantiate_eval_define_values(): l = make_linklet("(linklet () ((x x15)) (define-values (x) 4) (+ x x))") t = make_instance("(linklet () ((x x16)) (define-values (x) 1000))") _, t = eval_fixnum(l, t) assert defines(t, "x15") and defines(t, "x16") and not defines(t, "x") assert check_val(t, "x15", 4) assert check_val(t, "x16", 1000)
def test_instantiate_defs_export_names(): l = make_instance("(linklet () ((x x15)) (define-values (x) 4))") assert not defines(l, "x") assert defines(l, "x15") # LinkletVars will be referred by the external name (e.g. (+ x15 x15) l = make_linklet("(linklet () ((x x15)) (define-values (x) 4) (+ x x))") result, t = eval_fixnum(l, empty_target()) assert result == 8 assert not defines(t, "x") assert defines(t, "x15")
def test_instantiate_defs_export_names(): l = inst(make_linklet("(linklet () ((x x15)) (define-values (x) 4))")) assert not defines(l, "x") assert defines(l, "x15") # LinkletVars will be referred by the external name (e.g. (+ x15 x15) l = make_linklet("(linklet () ((x x15)) (define-values (x) 4) (+ x x))") result, t = eval_fixnum(l, empty_target()) assert result == 8 assert not defines(t, "x") assert defines(t, "x15")
def test_instantiate_discarding_defs(): l = inst(make_linklet("(linklet () ((x x15)) (define-values (x) 4) (define-values (x15) 75))")) assert not defines(l, "x") assert check_val(l, "x15", 4) #### Not 75! #k,v = get_var_val(l, "x15.1") # uninterned #assert v.value == 75 l = inst(make_linklet("(linklet () ((x x15) k) (define-values (x) 4) (define-values (x15) 75) (define-values (k) x15))")) assert not defines(l, "x") assert check_val(l, "x15", 4) #### Not 75! assert check_val(l, "k", 75) #### Not 4!
def test_instantiate_target_def_stays_the_same(): # if linklet doesn't export, then target's def stay the same l = make_linklet("(linklet () () (define-values (x) 4) (+ x x))") t = make_instance( "(linklet () () (define-values (x) 1) (define-values (y) 2))") result, t = eval_fixnum(l, t) assert result == 8 assert check_val(t, "x", 1) assert check_val(t, "y", 2) # use the local var, don't change target's var if you don't export l = make_linklet("(linklet () () (define-values (x) 4) (+ x x))") t1 = make_instance("(linklet () () (define-values (x) 10))") t2 = make_instance("(linklet () (x) (define-values (x) 10))") result1, t1 = eval_fixnum(l, t1) result2, t2 = eval_fixnum(l, t2) assert result1 == 8 and result2 == 8 assert check_val(t1, "x", 10) assert check_val(t2, "x", 10) # imported variables doesn't get into target at all ... l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet ((x)) () (+ x x))") result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 assert not defines(t, "x") # ... let alone overwrite any var inside the target l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet ((x)) () (+ x x))") t = make_instance("(linklet () () (define-values (x) 1))") result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1)
def test_instantiate_target_def_stays_the_same(): # if linklet doesn't export, then target's def stay the same l = make_linklet("(linklet () () (define-values (x) 4) (+ x x))") t = make_instance({'x':1,'y':2}) result, t = eval_fixnum(l, t) assert result == 8 assert check_val(t, "x", 1) assert check_val(t, "y", 2) # use the local var, don't change target's var if you don't export l = make_linklet("(linklet () () (define-values (x) 4) (+ x x))") t1 = inst(make_linklet("(linklet () (x) (define-values (x) 10))")) t2 = inst(make_linklet("(linklet () (x) (define-values (x) 10))")) result1, t1 = eval_fixnum(l, t1) result2, t2 = eval_fixnum(l, t2) assert result1 == 8 and result2 == 8 assert check_val(t1, "x", 10) assert check_val(t2, "x", 10) # imported variables doesn't get into target at all ... l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet ((x)) () (+ x x))") result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 assert not defines(t, "x") # ... let alone overwrite any var inside the target l1 = make_instance({'x':4}) l2 = make_linklet("(linklet ((x)) () (+ x x))") t = make_instance({'x':1}) result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1)
def test_instantiate_discarding_defs(): l = make_instance( "(linklet () ((x x15)) (define-values (x) 4) (define-values (x15) 75))" ) assert not defines(l, "x") assert check_val(l, "x15", 4) #### Not 75! k, v = get_var_val(l, "x15.1") # uninterned assert v.value == 75 l = make_instance( "(linklet () ((x x15) k) (define-values (x) 4) (define-values (x15) 75) (define-values (k) x15))" ) assert not defines(l, "x") assert check_val(l, "x15", 4) #### Not 75! assert check_val(l, "k", 75) #### Not 4! k, v = get_var_val(l, "x15.1") assert v.value == 75
def test_instantiate_closures_and_variables(): l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet ((x)) (g) (define-values (g) (lambda (y) x)))") _, t = eval_fixnum(l2, empty_target(), [l1]) assert defines(t, "g") and not defines(t, "x") # use the modified target l3 = make_linklet("(linklet () (g) (g 5))") result, t = eval_fixnum(l3, t) assert result == 4 # import the closure l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = inst(make_linklet("(linklet ((x)) (g) (define-values (g) (lambda (y) x)))"), [l1]) l4 = make_linklet("(linklet ((g)) () (g 3))") result, _ = eval_fixnum(l4, empty_target(), [l2]) assert result == 4
def test_instantiate_export_rename(): l1 = inst(make_linklet("(linklet () ((x1 x)) (define-values (x1) 4))")) l2 = make_linklet("(linklet ((x)) ((y1 y)) (define-values (y1) x) (+ x y1))") assert check_val(l1, "x", 4) result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 assert check_val(t, "y", 4) assert not defines(t, "x")
def test_instantiate_closures_and_variables(): l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet ((x)) (g) (define-values (g) (lambda (y) x)))") _, t = eval_fixnum(l2, empty_target(), [l1]) assert defines(t, "g") and not defines(t, "x") # use the modified target l3 = make_linklet("(linklet () (g) (g 5))") result, t = eval_fixnum(l3, t) assert result == 4 # import the closure l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_instance( "(linklet ((x)) (g) (define-values (g) (lambda (y) x)))", [l1]) l4 = make_linklet("(linklet ((g)) () (g 3))") result, _ = eval_fixnum(l4, empty_target(), [l2]) assert result == 4
def test_instantiate_export_rename(): l1 = make_instance("(linklet () ((x1 x)) (define-values (x1) 4))") l2 = make_linklet( "(linklet ((x)) ((y1 y)) (define-values (y1) x) (+ x y1))") assert check_val(l1, "x", 4) result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 assert check_val(t, "y", 4) assert not defines(t, "x")
def test_instantiate_set_bang(): l = make_linklet("(linklet () () (define-values (x) 3) (set! x 5) (+ x x))") t = make_instance({'x':6}) result, t = eval_fixnum(l, t) assert defines(t, "x") assert check_val(t, "x", 6) assert result == 10 l = make_linklet("(linklet () (x) (set! x 5) (+ x x))") t = make_instance({'x':3}) result, t = eval_fixnum(l, t) assert check_val(t, "x", 5) assert result == 10
def test_instantiate_set_bang(): l = make_linklet( "(linklet () () (define-values (x) 3) (set! x 5) (+ x x))") t = make_instance("(linklet () () (define-values (x) 6))") result, t = eval_fixnum(l, t) assert defines(t, "x") assert check_val(t, "x", 6) assert result == 10 l = make_linklet("(linklet () (x) (set! x 5) (+ x x))") t = make_instance("(linklet () () (define-values (x) 3))") result, t = eval_fixnum(l, t) assert check_val(t, "x", 5) assert result == 10
def test_instantiate_uninitialize_undefined_exports(): l = make_linklet("(linklet () (x))") _, t = eval_fixnum(l, empty_target()) assert t.is_var_uninitialized(W_Symbol.make("x")) # don't touch if target has it l = make_linklet("(linklet () (x))") t = make_instance("(linklet () () (define-values (x) 10))") _, t = eval_fixnum(l, t) assert not t.is_var_uninitialized(W_Symbol.make("x")) # target exports the same var with another external name l = make_linklet("(linklet () (x2) (+ x2 x2))") t = make_instance("(linklet () ((x x2)) (define-values (x) 10))") result, t = eval_fixnum(l, t) assert result == 20 assert check_val(t, "x2", 10) assert not defines(t, "x")
def test_instantiate_uninitialize_undefined_exports(): l = make_linklet("(linklet () (x))") _, t = eval_fixnum(l, empty_target()) assert t.vars[W_Symbol.make("x")].val is w_uninitialized # don't touch if target has it l = make_linklet("(linklet () (x))") t = make_instance({'x':10}) _, t = eval_fixnum(l, t) assert t.vars[W_Symbol.make("x")].val is not w_uninitialized # target exports the same var with another external name l = make_linklet("(linklet () (x2) (+ x2 x2))") t = inst(make_linklet("(linklet () ((x x2)) (define-values (x) 10))")) result, t = eval_fixnum(l, t) assert result == 20 assert check_val(t, "x2", 10) assert not defines(t, "x")
def test_instantiate_basic_import(): l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet ((x)) () (+ x x))") result, _ = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet( "(linklet ((x)) (y) (define-values (y) (+ x x)) (+ y y))") result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 16 assert check_val(t, "y", 8) assert not defines(t, "x") # target's defs are overwritten only if the linklet has a definition # not with an imported variable l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet ((x)) () (+ x x))") t = make_instance("(linklet () () (define-values (x) 1000))") result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1000) ## same thing with the import renaming l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet("(linklet (((x x2))) () (+ x2 x2))") t = make_instance( "(linklet () () (define-values (x) 1000) (define-values (x2) 2000))") result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1000) assert check_val(t, "x2", 2000) ## slightly trickier l1 = make_instance("(linklet () (x) (define-values (x) 4))") l2 = make_linklet( "(linklet (((x x2))) () (define-values (x) 14) (+ x2 x))") t = make_instance( "(linklet () () (define-values (x) 1000) (define-values (x2) 2000))") result, t = eval_fixnum(l2, t, [l1]) assert result == 18 assert check_val(t, "x", 1000) assert check_val(t, "x2", 2000)
def test_instantiate_basic_import(): l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet ((x)) () (+ x x))") result, _ = eval_fixnum(l2, empty_target(), [l1]) assert result == 8 l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet ((x)) (y) (define-values (y) (+ x x)) (+ y y))") result, t = eval_fixnum(l2, empty_target(), [l1]) assert result == 16 assert check_val(t, "y", 8) assert not defines(t, "x") # target's defs are overwritten only if the linklet has a definition # not with an imported variable l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet ((x)) () (+ x x))") t = make_instance({'x':1000}) result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1000) ## same thing with the import renaming l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet (((x x2))) () (+ x2 x2))") t = make_instance({'x':1000, 'x2':2000}) result, t = eval_fixnum(l2, t, [l1]) assert result == 8 assert check_val(t, "x", 1000) assert check_val(t, "x2", 2000) ## slightly trickier l1 = inst(make_linklet("(linklet () (x) (define-values (x) 4))")) l2 = make_linklet("(linklet (((x x2))) () (define-values (x) 14) (+ x2 x))") t = make_instance({'x':1000, 'x2':2000}) result, t = eval_fixnum(l2, t, [l1]) assert result == 18 assert check_val(t, "x", 1000) assert check_val(t, "x2", 2000)