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 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 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 _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 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_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 _set_item(note_nr, color): if not KSP.is_compiled(): color = color.id keys[note_nr].color = color