File: array.py

package info (click to toggle)
python-jpype 0.5.4.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,200 kB
  • ctags: 1,827
  • sloc: cpp: 11,011; xml: 1,998; python: 1,677; java: 447; makefile: 4; sh: 3
file content (114 lines) | stat: -rw-r--r-- 3,633 bytes parent folder | download | duplicates (3)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#*****************************************************************************
#   Copyright 2004-2008 Steve Menard
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#   
#*****************************************************************************
import jpype
from jpype import JPackage, JArray, JByte, java
import unittest, common

VALUES = (12234,1234,234,1324,424,234,234,142,5,251,242,35,235,62,1235,46,245132,51, 2, 3, 4)

def suite() :
    return unittest.makeSuite(ArrayTestCase)

class ArrayTestCase(common.JPypeTestCase) :
    def __arrayEquals(self, a1, a2) :
        assert len(a1) == len(a2)
        
        for i in range(len(a1)) :
            assert a1[i] == a2[i]
    
    def testReadArray(self) :
        t = JPackage("jpype").array.TestArray()
        assert not isinstance(t, JPackage)
        
        self.__arrayEquals(VALUES, t.i)
        
        assert t.i[0] == VALUES[0]
        
        self.__arrayEquals(VALUES[1:-2], t.i[1:-2])

    def testStangeBehavior(self) :
        ''' Test for stange crash reported in bug #1089302'''
        Test2 = jpype.JPackage('jpype.array').Test2
        test = Test2()
        test.test(test.getValue())

    def testWriteArray(self) :
        t = JPackage("jpype").array.TestArray()
        assert not isinstance(t, JPackage)

        t.i[0] = 32
        assert t.i[0] == 32
        
        t.i[1:3] = (33, 34)
        assert t.i[1] == 33   
        assert t.i[2] == 34
        
        self.__arrayEquals(t.i[:5], (32, 33, 34 ,1324, 424) )
        
    def testObjectArraySimple(self) :
        a = JArray(java.lang.String, 1)(2)
        a[1] = "Foo"
        assert "Foo" == a[1]
        
    def testByteArraySimple(self) :
        a = JArray(JByte)(2)
        a[1] = 2
        assert a[1] == 2
        
    def testIterateArray(self):
        t = JPackage("jpype").array.TestArray()
        assert not isinstance(t, JPackage)

        for i in t.i :
            assert i != 0 

    def testGetSubclass(self) :
        t = JPackage("jpype").array.TestArray()
        v = t.getSubClassArray()
        
        assert isinstance(v[0], unicode)
        
    def testGetArrayAsObject(self) :
        t = JPackage("jpype").array.TestArray()
        v = t.getArrayAsObject()        

    def testCharArrayAsString(self) :
        t = JPackage("jpype").array.TestArray()
        v = t.charArray
        assert str(v) == 'avcd'
        assert unicode(v) == u'avcd'
        
    def testByteArrayAsString(self) :
        t = JPackage("jpype").array.TestArray()
        v = t.byteArray
        assert str(v) == 'avcd'
        
    def testByteArrayIntoVector(self):
    	ba = jpype.JArray(jpype.JByte)('123')
    	v = jpype.java.util.Vector(1)
    	v.add(ba)
    	assert len(v) == 1
    	assert v[0] is not None

if __name__ == '__main__' :
    import os.path
    root = os.path.abspath(os.path.dirname(__file__))    
    common.CLASSPATH= "%s/../../build/test/java" % root
    
    runner = unittest.TextTestRunner()
    runner.run(suite())