Debian Codegraph @

test/jpypetest/test_bytebuffer.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import jpype
19 import _jpype
20 import common
21
22
23 class ByteBufferCase(common.JPypeTestCase):
24 def setUp(self):
25 common.JPypeTestCase.setUp(self)
26
27 def testConvertToDirectBuffer(self):
28 a = bytearray([0, 0, 0, 0])
29 bb = jpype.nio.convertToDirectBuffer(a)
30 self.assertIsInstance(bb, jpype.JClass("java.nio.DirectByteBuffer"))
31 self.assertFalse(bb.isReadOnly())
32 bb.put(1)
33 bb.put(2)
34 bb.put(3)
35 bb.put(4)
36 self.assertEqual(a, bytearray([1, 2, 3, 4]))
37 with self.assertRaises(jpype.JException):
38 bb.put(5)
39
40 def testConvertToReadonlyBuffer(self):
41 a = bytes([1, 2, 3, 4])
42 bb = jpype.nio.convertToDirectBuffer(a)
43 self.assertIsInstance(bb, jpype.JClass("java.nio.DirectByteBuffer"))
44 self.assertTrue(bb.isReadOnly())
45 self.assertEqual(a, bytearray([1, 2, 3, 4]))
46 with self.assertRaises(jpype.JException):
47 bb.put(0, 1)
48
49 def testConvertMemoryViewToReadonlyBuffer(self):
50 a = bytearray([1, 2, 3, 4])
51 a = memoryview(a).toreadonly()
52 bb = jpype.nio.convertToDirectBuffer(a)
53 self.assertIsInstance(bb, jpype.JClass("java.nio.DirectByteBuffer"))
54 self.assertTrue(bb.isReadOnly())
55 self.assertEqual(a, bytearray([1, 2, 3, 4]))
56 with self.assertRaises(jpype.JException):
57 bb.put(0, 1)
58
59 def testRepr(self):
60 a = bytearray([0, 0, 0, 0])
61 bb = jpype.nio.convertToDirectBuffer(a)
62 self.assertIsInstance(repr(bb), str)
63 self.assertEqual(repr(bb), "<java buffer 'java.nio.DirectByteBuffer'>")
64
65 def testMemoryView(self):
66 self.assertEqual(memoryview(jpype.java.nio.ByteBuffer.allocateDirect(100)).nbytes, 100)