def test_ne( self ): def main_ne(x, y): return x != y self.assertEqual( True, rpy.run( main_ne, MIN_INT, MAX_INT ) ) self.assertEqual( True, rpy.run( main_ne, MAX_INT, MIN_INT ) ) self.assertEqual( False, rpy.run( main_ne, MAX_INT, MAX_INT ) ) self.assertEqual( False, rpy.run( main_ne, MIN_INT, MIN_INT ) )
def test_lt(self): def main_lt(x, y): return x < y self.assertEqual(True, rpy.run(main_lt, MIN_INT, MAX_INT)) self.assertEqual(False, rpy.run(main_lt, MAX_INT, MIN_INT)) self.assertEqual(False, rpy.run(main_lt, MAX_INT, MAX_INT))
def test_gt( self ): def main_gt(x, y): return x > y self.assertEqual( False, rpy.run( main_gt, MIN_INT, MAX_INT ) ) self.assertEqual( True, rpy.run( main_gt, MAX_INT, MIN_INT ) ) self.assertEqual( False, rpy.run( main_gt, MAX_INT, MAX_INT ) ) self.assertEqual( False, rpy.run( main_gt, MIN_INT, MIN_INT ) )
def test_eq( self ): def main_eq(x, y): return x == y self.assertEqual( False, rpy.run( main_eq, MIN_INT, MAX_INT ) ) self.assertEqual( False, rpy.run( main_eq, MAX_INT, MIN_INT ) ) self.assertEqual( True, rpy.run( main_eq, MAX_INT, MAX_INT ) ) self.assertEqual( True, rpy.run( main_eq, MIN_INT, MIN_INT ) )
def test_ge(self): def main_ge(x, y): return x >= y self.assertEqual(False, rpy.run(main_ge, MIN_INT, MAX_INT)) self.assertEqual(True, rpy.run(main_ge, MAX_INT, MIN_INT)) self.assertEqual(True, rpy.run(main_ge, MAX_INT, MAX_INT)) self.assertEqual(True, rpy.run(main_ge, MIN_INT, MIN_INT))
def test_eq(self): def main_eq(x, y): return x == y self.assertEqual(False, rpy.run(main_eq, MIN_INT, MAX_INT)) self.assertEqual(False, rpy.run(main_eq, MAX_INT, MIN_INT)) self.assertEqual(True, rpy.run(main_eq, MAX_INT, MAX_INT)) self.assertEqual(True, rpy.run(main_eq, MIN_INT, MIN_INT))
def test_if2( self ): def main_if(x, y): if x < y: z = 2 * x else: z = 2 * y return z self.assertEqual( 20, rpy.run( main_if, 10, 30 ) ) self.assertEqual( 20, rpy.run( main_if, 30, 10 ) )
def test_while_break_else( self ): def main_while( x ): count = x while count < 20: if count % 5 == 0: break count += 1 else: count *= 2 return count self.assertEqual( 40, rpy.run( main_while, 16 ) ) self.assertEqual( 5, rpy.run( main_while, 1 ) )
def test_nested_if1( self ): def main_if(x, y, z): if x < y: if x < z: min_value = x else: min_value = z elif y < z: min_value = y else: min_value = z return min_value self.assertEqual( 1, rpy.run( main_if, 1, 2, 3 ) ) self.assertEqual( 1, rpy.run( main_if, 2, 1, 3 ) ) self.assertEqual( 1, rpy.run( main_if, 2, 3, 1 ) ) self.assertEqual( 1, rpy.run( main_if, 3, 2, 1 ) ) self.assertEqual( 1, rpy.run( main_if, 3, 1, 2 ) )
def test_while_break( self ): def main_while( x ): count = x while True: if count % 5 == 0: break count += 1 return count self.assertEqual( 5, rpy.run( main_while, 3 ) )
def test_while1( self ): def main_while( x ): count = x value = 0 while count > 0: value += count count -= 1 return value self.assertEqual( 1+2, rpy.run( main_while, 2 ) )
def test_while_break_continue( self ): def main_while( x ): count = x while True: count += 1 if count % 10 == 0: count *= 2 continue if count % 5 == 0: break return count self.assertEqual( 25, rpy.run( main_while, 9 ) )
def test_nested_while_break1( self ): def main_while( x ): count = x while True: if count % 5 == 0: while count < 50: if count % 10 == 0: break count += 1 break count += 1 return count self.assertEqual( 10, rpy.run( main_while, 7 ) )
def test_div_param( self ): def main(x, y): x //= y return x self.assertEqual( -4, rpy.run( main, 20, -5 ) )
def test_reminder_param( self ): def main(x, y): x %= y return x self.assertEqual( 2, rpy.run( main, 22, 5 ) )
def test_mul_param( self ): def main(x, y): x *= y return x self.assertEqual( -6, rpy.run( main, 2, -3 ) )
def test_add_param( self ): def main(x, y): return x + y self.assertEqual( 3, rpy.run( main, 1, 2 ) )
def test_misc1_param( self ): def main(a, b, c): return a + b*b + c*c*c self.assertEqual( 2 + 3*3 + 5*5*5, rpy.run( main, 2, 3, 5 ) )
def test_sub_param( self ): def main(x, y): x -= y return x self.assertEqual( -1, rpy.run( main, 1, 2 ) )
import sys import unittest here = os.path.dirname(os.path.abspath(__file__)) def test_suite(): suite = suite = unittest.TestSuite() loader = unittest.TestLoader() for fn in os.listdir(here): if fn.startswith("test") and fn.endswith(".py"): modname = "rpybuiltin.tests." + fn[:-3] __import__(modname) module = sys.modules[modname] suite.addTests(loader.loadTestsFromModule(module)) return suite MAIN_SUITE = test_suite() def main(): runner = unittest.TextTestRunner(verbosity=2) runner.run(MAIN_SUITE) if __name__ == '__main__': import rpy rpy.run(main) #main()
def test_class_decl(self): def main_class_decl(x, y): p = Point(x, y) return p.x + p.y self.assertEqual(5, rpy.run(main_class_decl, 2, 3))
def test_if1( self ): def main_if(x, y): if x < y: return x return y self.assertEqual( 1234, rpy.run( main_if, 1234, 4567 ) )
def test_fn_call( self ): def main_fn_call(x, y): return mul2i(x, y ) self.assertEqual( 12, rpy.run( main_fn_call, 6, 2 ) )
def test_fn_call(self): def main_fn_call(x, y): return mul2i(x, y) self.assertEqual(12, rpy.run(main_fn_call, 6, 2))
import os import sys import unittest here = os.path.dirname(os.path.abspath(__file__)) def test_suite(): suite = suite = unittest.TestSuite() loader = unittest.TestLoader() for fn in os.listdir(here): if fn.startswith("test") and fn.endswith(".py"): modname = "rpybuiltin.tests." + fn[:-3] __import__(modname) module = sys.modules[modname] suite.addTests(loader.loadTestsFromModule(module)) return suite MAIN_SUITE = test_suite() def main(): runner = unittest.TextTestRunner(verbosity=2) runner.run(MAIN_SUITE) if __name__ == '__main__': import rpy rpy.run( main ) #main()
def test_class_decl( self ): def main_class_decl(x, y): p = Point(x,y) return p.x + p.y self.assertEqual( 5, rpy.run( main_class_decl, 2, 3 ) )