class ArrayTest(unittest.TestCase): def setUp(self): self.jvm = Machine() load_stdlib_classes(self.jvm) self.jvm.load_class_file('example/ArrayTest.class') def test_insertion_sort(self): l = [4, 3, 5, 1] r = self.jvm.call_function('jvmtest/ArrayTest/insertionSort', l) self.assertEqual(r, None) self.assertEqual(l, [1, 3, 4, 5]) def test_cocktail_sort(self): l = [4, 3, 5, 1] r = self.jvm.call_function('jvmtest/ArrayTest/cocktailSort', l) self.assertEqual(r, None) self.assertEqual(l, [1, 3, 4, 5]) def test_sum(self): l = [5, 5, 14, 3] r = self.jvm.call_function('jvmtest/ArrayTest/sum', l) self.assertEqual(r, sum(l)) def test_loop_multiple(self): r = self.jvm.call_function('jvmtest/ArrayTest/loopMultipleArray') self.assertEqual(r, 'aA1\nbB2\ncC3\n')
class Rot13Test(unittest.TestCase): def setUp(self): self.jvm = Machine() load_stdlib_classes(self.jvm) self.jvm.load_class_file('example/Rot13.class') def test_rot13_hello_world(self): self.assertEqual(self.jvm .call_function('jvmtest/Rot13/rot13', 'Hello World!') , 'Uryyb Jbeyq!')
class JVMTest(unittest.TestCase): def setUp(self): self.jvm = Machine() load_stdlib_classes(self.jvm) self.jvm.load_class_file('example/IntegerTest.class') def test_return_0(self): self.assertEqual(self.jvm.call_function('jvmtest/IntegerTest/return0'), 0) def test_return_1(self): self.assertEqual(self.jvm.call_function('jvmtest/IntegerTest/return1'), 1) def test_return_5000(self): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/return5000'), 5000) def test_passthrough(self): for i in range(500): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/passthrough', i), i) def test_passthrough_loop(self): for i in range(500, 550): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/passthrough_loop', i), i) def test_double(self): for i in range(500): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/doubleNum', i), i * 2) def test_power(self): for i in range(500): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/power', i), i * i) def test_iterative_fibonacci(self): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/iterativeFibonacci', 20), 6765) def test_recursive_fibonacci(self): self.assertEqual( self.jvm.call_function('jvmtest/IntegerTest/recursiveFibonacci', 7), 13)
class StringTest(unittest.TestCase): def setUp(self): self.jvm = Machine() load_stdlib_classes(self.jvm) self.jvm.load_class_file('example/StringTest.class') def test_rot13_hello_world(self): self.assertEqual( self.jvm.call_function('jvmtest/StringTest/rot13', 'Hello World!'), 'Uryyb Jbeyq!') def test_reverse_string(self): r = self.jvm.call_function('jvmtest/StringTest/reverseString', 'Test message!') self.assertEqual(r, '!egassem tseT')
class InstanceTest(unittest.TestCase): def setUp(self): self.jvm = Machine() load_stdlib_classes(self.jvm) self.jvm.load_class_file('example/InstanceTest.class') def test_single_instance(self): self.assertEqual(self.jvm .call_function('jvmtest/InstanceTest/test_single'), 12345) def test_multiple_instance_1(self): self.assertEqual(self.jvm .call_function('jvmtest/InstanceTest/test_multiple_1'), 12345) def test_multiple_instance_2(self): self.assertEqual(self.jvm .call_function('jvmtest/InstanceTest/test_multiple_2'), 54321)
#!/usr/bin/env python3 from pyjvm.Machine import Machine from pyjvm.jstdlib.StdlibLoader import load_stdlib_classes m = Machine() # Load stdlib load_stdlib_classes(m) # Load local classes m.load_class_file('example/Hello.class') #m.load_class_file('example/TestImport.class') #m.load_class_file('example/IntegerTest.class') #m.load_class_file('example/Rot13.class') #m.load_class_file('example/InstanceTest.class') # Dump machine state #m.dump() print(m.call_function('com/gkbrk/JVMTest/Hello/monteCarloPi', 5000))
except subprocess.CalledProcessError: log.error(f'Failed to compile {java_file}') exit(1) with open(java_file, 'r') as f: code = f.readlines() if 'package' in code[0]: package = code[0].split()[-1].strip(';') class_name = f'{package}/{class_name}' method = f'{class_name}/{method_name}' # load the class file jvm = Machine() load_stdlib_classes(jvm) jvm.load_class_file(class_file) # create layout for constant pool view frames = [ [ Label(text=f'{i+1}:', width=5) for i, _ in enumerate(jvm.class_files[class_name].const_pool) ], [ Label(text=f'{c.tag.name if c.tag else "?"}', width=15) for c in jvm.class_files[class_name].const_pool ], [ Label(text=', '.join( [f'{k}:{v}' for k, v in c.__dict__.items() if k != 'tag'])) for c in jvm.class_files[class_name].const_pool