Debian Codegraph @

test/jpypetest/test_comparable.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 sys
19 import jpype
20 import common
21
22
23 class ComparableTestCase(common.JPypeTestCase):
24
25 def setUp(self):
26 common.JPypeTestCase.setUp(self)
27
28 def testComparable(self):
29 a = jpype.java.time.Instant.ofEpochSecond(10000000)
30 b = jpype.java.time.Instant.ofEpochSecond(10000001)
31 self.assertFalse(a < a)
32 self.assertFalse(a > a)
33 self.assertTrue(a >= a)
34 self.assertTrue(a <= a)
35 self.assertTrue(a == a)
36 self.assertFalse(a != a)
37 self.assertTrue(a < b)
38 self.assertFalse(a > b)
39 self.assertFalse(a >= b)
40 self.assertTrue(a <= b)
41 self.assertFalse(a == b)
42 self.assertTrue(a != b)
43
44 def testComparableHash(self):
45 i = jpype.java.math.BigInteger("1000000000000")
46 self.assertIsInstance(hash(i), int)
47
48 def testComparableNull(self):
49 Instant = jpype.JClass("java.time.Instant")
50 i1 = Instant.parse("1970-01-01T00:00:00Z")
51 i3 = jpype.JObject(None, Instant)
52
53 self.assertTrue(i1 == i1)
54 self.assertFalse(i1 == i3)
55 self.assertFalse(i3 == i1)
56 self.assertTrue(i1 != i3)
57 self.assertTrue(i3 != i1)
58 with self.assertRaises(ValueError):
59 print(i1 < i3)
60 with self.assertRaises(ValueError):
61 print(i1 <= i3)
62 with self.assertRaises(ValueError):
63 print(i1 > i3)
64 with self.assertRaises(ValueError):
65 print(i1 >= i3)
66 with self.assertRaises(ValueError):
67 print(i3 < i1)
68 with self.assertRaises(ValueError):
69 print(i3 <= i1)
70 with self.assertRaises(ValueError):
71 print(i3 > i1)
72 with self.assertRaises(ValueError):
73 print(i3 >= i1)
74 with self.assertRaises(ValueError):
75 print(i3 < i3)
76 with self.assertRaises(ValueError):
77 print(i3 <= i3)
78 with self.assertRaises(ValueError):
79 print(i3 > i3)
80 with self.assertRaises(ValueError):
81 print(i3 >= i3)
82
83 def testComparableObj(self):
84 C1 = jpype.JClass("java.time.temporal.ChronoUnit")
85 C2 = jpype.JClass("java.util.concurrent.TimeUnit")
86 O1 = C1.SECONDS
87 O2 = C2.SECONDS
88 N1 = jpype.JObject(None, C1)
89 N2 = jpype.JObject(None, C2)
90 V = jpype.JInt(1)
91 # Test dissimilar objects
92 self.assertTrue(O1 != O2)
93 self.assertFalse(O1 == O2)
94 # test Nulls
95 self.assertTrue(N1 == N2)
96 self.assertFalse(N1 != N2)
97 self.assertTrue(N1 == None)
98 self.assertFalse(N1 != None)
99 # test primitives
100 self.assertFalse(O1 == V)
101 self.assertFalse(V == O1)
102 # test null to primitives
103 self.assertFalse(N1 == V)
104 self.assertFalse(V == N1)
105
106 self.assertFalse(1 == O1)
107 self.assertFalse("M" == O1)
108 self.assertFalse(O1 == 1)
109 self.assertFalse(O1 == "M")
110
111 self.assertTrue(1 != O1)
112 self.assertTrue("M" != O1)
113 self.assertTrue(O1 != 1)
114 self.assertTrue(O1 != "M")
115
116 with self.assertRaises(TypeError):
117 self.assertTrue(O1 > 1)
118 with self.assertRaises(TypeError):
119 self.assertTrue(1 > O1)
120 with self.assertRaises(TypeError):
121 self.assertTrue(O1 > O2)