java_array_test.py
2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Created on Mar 24, 2010
@author: Barthelemy Dagenais
"""
from __future__ import unicode_literals, absolute_import
import time
import unittest
from py4j.java_gateway import JavaGateway
from py4j.tests.java_gateway_test import (
start_example_app_process)
class ArrayTest(unittest.TestCase):
def setUp(self):
self.p = start_example_app_process()
time.sleep(0.5)
self.gateway = JavaGateway()
def tearDown(self):
self.p.terminate()
self.gateway.shutdown()
time.sleep(0.5)
def testArray(self):
example = self.gateway.entry_point.getNewExample()
array1 = example.getStringArray()
array2 = example.getIntArray()
self.assertEqual(3, len(array1))
self.assertEqual(4, len(array2))
self.assertEqual("333", array1[2])
self.assertEqual(5, array2[1])
array1[2] = "aaa"
array2[1] = 6
self.assertEqual("aaa", array1[2])
self.assertEqual(6, array2[1])
new_array = array2[1:3]
self.assertEqual(2, len(new_array))
self.assertEqual(1, new_array[1])
def testCreateArray(self):
int_class = self.gateway.jvm.int
string_class = self.gateway.jvm.java.lang.String
int_array = self.gateway.new_array(int_class, 2)
string_array = self.gateway.new_array(string_class, 3, 5)
self.assertEqual(2, len(int_array))
self.assertEqual(3, len(string_array))
self.assertEqual(5, len(string_array[0]))
def testDoubleArray(self):
double_class = self.gateway.jvm.double
double_array = self.gateway.new_array(double_class, 2)
double_array[0] = 2.2
self.assertAlmostEqual(double_array[0], 2.2)
def testFloatArray(self):
float_class = self.gateway.jvm.float
float_array = self.gateway.new_array(float_class, 2)
float_array[0] = 2.2
self.assertAlmostEqual(float_array[0], 2.2)
def testCharArray(self):
char_class = self.gateway.jvm.char
char_array = self.gateway.new_array(char_class, 2)
char_array[0] = "a"
self.assertEqual(char_array[0], "a")
if __name__ == "__main__":
unittest.main()