def test_func(self): x = self.MyFunc('my_func', args=OrderedDict(arg1=int, arg2=BuiltInIntVar)) self.assertEqual(x.id, 0) bi = BuiltInIntVar('BI', def_val=1) val = x(2, bi) self.assertEqual(val._get_compiled(), 'my_func(2, $BI)') self.assertEqual(val._get_runtime(), 3) val = x(val, bi) self.assertEqual(val._get_compiled(), 'my_func(my_func(2, $BI), $BI)') self.assertEqual(val._get_runtime(), 4) y = self.MyFuncInt('y_func', args=OrderedDict(arg1=int, arg2=BuiltInIntVar)) self.assertEqual(y.id, 2) val = y(x(1, bi), bi) self.assertEqual(val._get_compiled(), 'y_func(my_func(1, $BI), $BI)') self.assertEqual(val._get_runtime(), 3) KSP.set_compiled(True) self.assertEqual((y(1, bi) + x(2, bi)).expand(), 'y_func(1, $BI) + my_func(2, $BI)') Output().refresh() y(1, bi) self.assertEqual(Output().get()[-1], 'y_func(1, $BI)') Output().refresh() y(x(1, bi), bi) self.assertEqual(unpack_lines(Output().get()), 'y_func(my_func(1, $BI), $BI)')
def test_real(self): KSP.set_compiled(True) x = kArrReal() x.append(2.0) self.assertEqual(Output().pop(), '?kArrReal0[0] := 2.0') self.assertEqual(x[0].val, '?kArrReal0[0]') self.assertEqual(x._get_compiled(), '?kArrReal0') x.append(3.0) self.assertEqual(Output().pop(), '?kArrReal0[1] := 3.0') self.assertEqual(x[1].val, '?kArrReal0[1]') self.assertEqual(x._get_runtime(), [2.0, 3.0]) self.assertEqual(x._generate_init(), ['declare ?kArrReal0[2]']) y = kArrReal([1.0, 2.0, 3.0]) y.append(4.0) self.assertEqual(Output().pop(), '?kArrReal1[3] := 4.0') self.assertEqual(y._get_runtime(), [1.0, 2.0, 3.0, 4.0]) self.assertEqual(y._generate_init(), ['declare ?kArrReal1[4] := (1.0, 2.0, 3.0)']) IName.set_compact(True) z = kArrReal([1.0, 2.0], 'z') self.assertEqual(z.name(), '?z3yxf') self.assertEqual(z[1].val, '?z3yxf[1]') self.assertEqual(z[1]._get_runtime(), 2.0) z.read() self.assertEqual(Output().pop(), 'read_persistent_var(?z3yxf)') self.assertEqual( z._generate_init(), ['declare ?z3yxf[2] := (1.0, 2.0)', 'make_persistent(?z3yxf)']) preserved = kArrReal(name='preserved', preserve=True) self.assertEqual(preserved.name(), '?preserved')
def test_arrays(self): KSP.set_compiled(True) stack = Stack('test', kArrInt, 10) arr = kArrInt([1, 2, 3, 4, 5]) returned = stack.push(arr)[0] self.assertIsInstance(returned, StackFrameArray) self.assertEqual( returned[1].val, '%_stack_test_arr[1 + (0 + ' + '%_stack_test_idx[$_stack_test_pointer])]') idx = kInt(4) self.assertEqual( returned[idx].val, '%_stack_test_arr[$kInt0 + (0 + ' + '%_stack_test_idx[$_stack_test_pointer])]') with self.assertRaises(IndexError): returned[idx + 1].val stack.pop() returned = stack.push(kLoc(int, 5))[0] self.assertIsInstance(returned, StackFrameArray) self.assertEqual( returned[1].val, '%_stack_test_arr[1 + (0 + ' + '%_stack_test_idx[$_stack_test_pointer])]') idx = kInt(4) self.assertEqual(returned[idx].val, '%_stack_test_arr[$kInt1 + (0 + ' + '%_stack_test_idx[$_stack_test_pointer])]') with self.assertRaises(IndexError): returned[idx + 1].val
def simple_for(self): with For(arr=self.arrX) as seq: for idx, val in enumerate(seq): self.x <<= val if not KSP.is_compiled(): with self.subTest(): self.assertEqual(self.x.val, self.arrX[idx]) if KSP.is_compiled(): out = unpack_lines(self.code) self.assertEqual(out, default_for_string)
def test_pop(self): KSP.set_compiled(True) stack = Stack('test', kArrInt, 10) with self.assertRaises(IndexError): stack.pop() stack.push(kLoc(int), kLoc(int, 2), kLoc(int)) stack.pop() self.assertEqual(Output().pop(), 'dec($_stack_test_pointer)')
def tearDown(self): KspObject.refresh() Output().refresh() Callback.refresh() # Function.refresh() BuiltIn.refresh() refresh_names_count() IName.refresh() gui_refresh() For.refresh() KSP.refresh()
def step(self): x = kInt(name='x') with For(2, 20, 2) as name: for idx, i in enumerate(name): x <<= i if not KSP.is_compiled(): with self.subTest(): self.assertEqual(i, (idx) * 2 + 2) if not KSP.is_compiled(): self.assertEqual(x.val, 18) else: out = unpack_lines(self.code) self.assertEqual(out, step_string)
def runTest(self): KSP.set_compiled(True) x = kInt(name='x') y = kInt(name='y') self.assertEqual((x + y).expand(), '$x + $y') self.assertEqual((x + y + x).expand(), '$x + $y + $x') self.assertEqual((x + (y + x)).expand(), '$x + ($y + $x)') self.assertEqual((x + y * x).expand(), '$x + $y * $x') self.assertEqual(((x + y) * x).expand(), '($x + $y) * $x') self.assertEqual((x * (y * x)).expand(), '$x * ($y * $x)') self.assertEqual((x * x * y | y * x).expand(), '$x * $x * $y .or. $y * $x') self.assertEqual((x * (x * y | y * x)).expand(), '$x * ($x * $y .or. $y * $x)')
def code(self): x = kInt(2, 'x') y = kInt(1, 'y') code = list() Output().set(code) with Select(x): with Case(1): check() y += 1 with Case(2): check() y += 2 with Select(y): with Case(2): check() y += 1 with Case(3): check() CondFalse() with self.assertRaises(KspCondError): with Case(3): pass if not KSP.is_compiled(): self.assertEqual(y.val, 3) else: code = unpack_lines(code) self.assertEqual(code, select_string)
def runTest(self): x = kInt(2, 'x') y = kLoc(int) z = kLoc(int, 5) arr = kArrInt([0] * 10, 'arr', size=10) with self.assertRaises(TypeError): StackFrame(1, 1, 1) with self.assertRaises(TypeError): StackFrame(x, (y, 1), z) KSP.set_compiled(True) frame = StackFrame(arr, (x, y), kInt(2, 'idx', is_local=True)) x, y = frame.vars self.assertEqual(x._get_compiled(), '%arr[0 + $idx]') self.assertEqual(x._get_runtime(), 2) self.assertEqual(y._get_compiled(), '%arr[1 + $idx]') self.assertEqual(y._get_runtime(), 0)
def _check_callback(self): if self._callbacks is all_callbacks: return if self.in_init() and InitCallback in self._callbacks: return if KSP.callback() not in self._callbacks: raise RuntimeError( f'can be used only in {self._callbacks} callbacks')
def _get_item(note_nr): if hasattr(note_nr, '_get_runtime'): note_nr = note_nr._get_runtime() if hasattr(note_nr, 'get_value'): note_nr = note_nr.get_value() if not KSP.is_compiled(): color = keys[note_nr].color return color return keys[note_nr].color
def test_compiled(self): KSP.set_compiled(True) x = kInt(1, 'x') y = kInt(2, 'y') with If((x < y) | (x == 1)): check() x <<= y with If(y == 2): check() y += 1 with Else((x == 1) & (y == 2)): check() y += 1 with Else(): check() x -= 1 self.assertEqual(unpack_lines(Output().get()), if_lines)
def runTest(self): KSP.set_compiled(True) arr = kArrInt([1, 2, 3, 4, 5]) f_arr = StackFrameArray(arr, 2, 5) idx = kInt(2) self.assertEqual(f_arr[idx].val, '%kArrInt0[$kInt0 + 2]') self.assertEqual(f_arr[idx]._get_runtime(), 5) f_arr[idx] <<= 3 self.assertEqual(f_arr[idx].val, '%kArrInt0[$kInt0 + 2]') self.assertEqual(f_arr[idx]._get_runtime(), 3) with For(arr=f_arr) as s: for idx, i in enumerate(s): self.assertEqual( i.val, '%kArrInt0[%_for_loop_idx[$_for_loop_curr_idx] + 2]') for idx, val in enumerate(f_arr.iter_runtime()): self.assertEqual(val, arr[idx + 2]._get_runtime())
def refresh_all(): '''clears the Output() calls refresh methods of: BuiltIn IName For KSP calls native_types.refresh_names_count() and bi_ui_controls.refresh() ''' KspObject.refresh() # Callback.refresh() # Function.refresh() BuiltIn.refresh() refresh_names_count() IName.refresh() gui_refresh() For.refresh() Output().refresh() KSP.refresh()
def main(self): self.assertEqual(self.x._get_runtime(), 0) with While() as w: while w(lambda x=self.x, y=self.y: x != y): with If(self.y != 10): check() self.y <<= 10 self.x += 1 self.assertEqual(self.x._get_runtime(), self.y._get_runtime()) if KSP.is_compiled(): out = unpack_lines(self.code) self.assertEqual(out, while_string)
def folded_for(self): arrY = kArrInt(name='arrY', sequence=[1, 2, 3, 6]) y = kInt(name='y') break_indicies = [0, 2, 3, 3, 3] with For(arr=self.arrX) as seq: for idx, val in enumerate(seq): self.x <<= val with For(arr=arrY) as seq_y: for idx2, val2 in enumerate(seq_y): with If(self.x == val2): check() y <<= val2 with self.subTest(): if not KSP.is_compiled(): self.assertEqual(idx2, break_indicies[idx]) with Else(): check() Break() if KSP.is_compiled(): out = unpack_lines(self.code) self.assertEqual(out, folded_for_string)
def test_kVar(self): x = kVar() x <<= 1 self.assertEqual(x.val, 1) arr_x = kVar(size=2) arr_x <<= [1, 2] self.assertIsInstance(arr_x, kArrInt) self.assertEqual(arr_x[0].val, 1) KSP.set_compiled(True) y = kVar(name='y') with self.assertRaises(AttributeError): y.val y <<= 'string' self.assertIsInstance(y, kStr) self.assertEqual(y.val, '@y') self.assertEqual(y._get_runtime(), 'string') z = kVar(1.0, 'z') self.assertIsInstance(z, kReal) self.assertEqual(z.val, '~z') self.assertEqual(z._get_runtime(), 1.0) IName.set_compact(True) myvar = kVar(1) self.assertEqual(myvar.val, '$nyxgq') preserved = kVar(1, 'preserved', preserve=True) self.assertEqual(preserved.val, '$preserved') preserved.read() self.assertEqual(Output().pop(), 'read_persistent_var($preserved)') self.assertEqual( preserved._generate_init(), ['declare $preserved := 1', 'make_persistent($preserved)']) test = kVar(myvar) self.assertEqual(test._get_runtime(), 1)
def test_str(self): KSP.set_compiled(True) x = kArrStr() x.append('2') self.assertEqual(Output().pop(), '!kArrStr0[0] := "2"') self.assertEqual(x[0].val, '!kArrStr0[0]') self.assertEqual(x._get_compiled(), '!kArrStr0') x.append('3') self.assertEqual(Output().pop(), '!kArrStr0[1] := "3"') self.assertEqual(x[1].val, '!kArrStr0[1]') self.assertEqual(x._get_runtime(), ['2', '3']) self.assertEqual(x._generate_init(), ['declare !kArrStr0[2]']) y = kArrStr(['1', '2', '3']) y.append('4') self.assertEqual(Output().pop(), '!kArrStr1[3] := "4"') self.assertEqual(y._get_runtime(), ['1', '2', '3', '4']) self.assertEqual(y._generate_init(), [ 'declare !kArrStr1[4]', '!kArrStr1[0] := "1"', '!kArrStr1[1] := "2"', '!kArrStr1[2] := "3"' ]) IName.set_compact(True) string = kStr('string', 'str_var', preserve=True) z = kArrStr(['1', string], 'z') self.assertEqual(z.name(), '!z3yxf') self.assertEqual(z[1].val, '!z3yxf[1]') self.assertEqual(z[1]._get_runtime(), 'string') z.read() self.assertEqual(Output().pop(), 'read_persistent_var(!z3yxf)') self.assertEqual(z._generate_init(), [ 'declare !z3yxf[2]', '!z3yxf[0] := "1"', '!z3yxf[1] := @str_var', 'make_persistent(!z3yxf)' ]) preserved = kArrStr(name='preserved', preserve=True) self.assertEqual(preserved.name(), '!preserved')
def test_int(self): KSP.set_compiled(True) x = kArrInt() x.append(2) self.assertEqual(Output().pop(), '%kArrInt0[0] := 2') self.assertEqual(x[0].val, '%kArrInt0[0]') self.assertEqual(x._get_compiled(), '%kArrInt0') x.append(3) self.assertEqual(Output().pop(), '%kArrInt0[1] := 3') self.assertEqual(x[1].val, '%kArrInt0[1]') self.assertEqual(x._get_runtime(), [2, 3]) self.assertEqual(x._generate_init(), ['declare %kArrInt0[2]']) y = kArrInt([1, 2, 3]) y.append(4) self.assertEqual(Output().pop(), '%kArrInt1[3] := 4') self.assertEqual(y._get_runtime(), [1, 2, 3, 4]) self.assertEqual(y._generate_init(), ['declare %kArrInt1[4] := (1, 2, 3)']) IName.set_compact(True) z = kArrInt([1, 2], 'z') self.assertEqual(z.name(), '%z3yxf') self.assertEqual(z[1].val, '%z3yxf[1]') self.assertEqual(z[1]._get_runtime(), 2) z.read() self.assertEqual(Output().pop(), 'read_persistent_var(%z3yxf)') self.assertEqual( z._generate_init(), ['declare %z3yxf[2] := (1, 2)', 'make_persistent(%z3yxf)']) preserved = kArrInt(name='preserved', preserve=True) self.assertEqual(preserved.name(), '%preserved') big = list(range(0, 1000000)) big_arr = kArrInt(big) self.assertEqual(big_arr[3]._get_runtime(), 3)
def test_calls(self): KSP.set_compiled(True) self.method() self.assertEqual( unpack_lines(Output().get()), call_out.format(method=Function.get_func_name(self.method))) Output().refresh() x = kInt(name='x') self.method(x) self.assertEqual(x._get_compiled(), '$x') self.assertEqual(x._get_runtime(), 3) self.assertEqual( unpack_lines(Output().get()), call_with_out.format(method=Function.get_func_name(self.method))) Output().refresh() self.inlined = True self.method(x, inline=True) self.assertEqual(unpack_lines(Output().get()), inlined_out) Output().refresh() arr = kArrInt([1, 2, 3, 4, 5]) @func def foo(loc: kArg(int, 5) = arr): with For(arr=loc) as seq: for item in seq: self.method(item) foo() # test for excluding from generation without # "calling" it @func def bar(var: int): self.method() bar(1, inline=True) generated_exec = KspObject.generate_all_executables() self.maxDiff = None self.assertEqual( unpack_lines(generated_exec), executables.format(method=Function.get_func_name(self.method), foo=Function.get_func_name(foo))) Output().refresh() @func def foobar(arg=kLoc(int)): self.method(arg) KSP.in_init(True) foobar() KSP.in_init(False) self.assertEqual(unpack_lines(Output().get()), invoked_in_init)
def wrapper(*a, inline=False, **kv): f_self, maped, outs = fargs.map(*a, *kv) odict = OrderedDict(**maped) args = list() for arg, val in odict.items(): args.append(val) args = _func_stack.push(*args) passed = dict() for arg, name in zip(args, odict): passed[name] = arg _f_obj.cash_args((args, passed)) if f_self: passed['self'] = f_self blocked = None if not inline and not KSP.in_init(): FuncCallsStack.put(_f_obj) FuncCallsStack.append(_f_obj._call_stack) _f_obj.called = True Output().put(f'call {_f_obj.name()}') FunctionCallback.open() if not Output().blocked: Output().blocked = True blocked = True out = f(**passed) if not inline and not KSP.in_init(): if _f_obj in _f_obj._call_stack: raise Exception(f'recursive call of {_f_obj} detected') FuncCallsStack.pop() FunctionCallback.close() if blocked: Output().blocked = False if outs: for name, val in outs.items(): maped[name] <<= passed[name] _func_stack.pop() return out
def test_push(self): self.maxDiff = None KSP.set_compiled(True) stack = Stack('test', kArrInt, 10) x = kInt(1, 'x') y = kInt(2, 'y') arr = kArrInt([3, 4, 5]) ret_x, ret_y, ret_arr = stack.push(x, y, arr) self.assertEqual( ret_x._get_compiled(), '%_stack_test_arr[0 +' + ' %_stack_test_idx[$_stack_test_pointer]]') self.assertEqual( ret_y._get_compiled(), '%_stack_test_arr[1 +' + ' %_stack_test_idx[$_stack_test_pointer]]') self.assertEqual(ret_x._get_runtime(), 1) self.assertEqual(ret_y._get_runtime(), 2) # print(ret_arr._get_runtime()) with For(arr=ret_arr) as s: for idx, var in enumerate(s): self.assertEqual( var._get_compiled(), '%_stack_test_arr[%_for_loop_idx[' + f'$_for_loop_curr_idx] + (2 + ' + '%_stack_test_idx[$_stack_test_pointer])]') self.assertEqual(var._get_runtime(), arr[idx]._get_runtime()) if KSP.is_compiled(): # print(unpack_lines(Output().get())) self.assertEqual(unpack_lines(Output().get()), push_output) Output().refresh() stack.push(kLoc(int), kLoc(int, 3), kLoc(int)) self.assertEqual(unpack_lines(Output().get()), push_lvl2_output)
def open(self): super().open() KSP.in_init(True)
def refresh(): for cb in Callback.__callbacks: cb._refresh() KSP.set_callback(None)
def test_return(self): KSP.set_compiled(False) self.main()
def test_generator(self): KSP.set_compiled(True) self.main()
def close(self): super().close() KSP.in_init(False)
def test_step_return(self): KSP.set_compiled(True) self.step()
def runTest(self): self.setUp() self.code() self.setUp() KSP.set_compiled(True) self.code()