Skip to content

Commit 934e3eb

Browse files
committed
Merge remote-tracking branch 'origin/master' into tim/sklearn-linreg
2 parents 8ed30a2 + b68b8f5 commit 934e3eb

25 files changed

Lines changed: 2182 additions & 277 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the
16+
# Software is contributed by such licensors),
17+
#
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
#
26+
# The above copyright notice and either this complete permission notice or at a
27+
# minimum a reference to the UPL must be included in all copies or substantial
28+
# portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
import unittest
39+
40+
class CompareTest(unittest.TestCase):
41+
42+
def comp_eq(self, left, right):
43+
self.assertFalse(left < right)
44+
self.assertTrue(left<=right)
45+
self.assertTrue(left==right)
46+
self.assertFalse(left!=right)
47+
self.assertFalse(left>right)
48+
self.assertTrue(left>=right)
49+
50+
def comp_ne(self, left, right):
51+
self.assertFalse(left==right)
52+
self.assertTrue(left!=right)
53+
54+
def comp_gt(self, left, right):
55+
self.assertFalse(left < right)
56+
self.assertFalse(left <= right)
57+
self.assertTrue(left>right)
58+
self.assertTrue(left>=right)
59+
self.assertFalse(left==right)
60+
self.assertTrue(left!=right)
61+
62+
def comp_lt(self, left, right):
63+
self.assertTrue(left < right)
64+
self.assertTrue(left <= right)
65+
self.assertFalse(left>right)
66+
self.assertFalse(left>=right)
67+
self.assertFalse(left==right)
68+
self.assertTrue(left!=right)
69+
70+
def comp_ge(self, left, right, isEQ):
71+
self.assertFalse(left < right)
72+
self.assertTrue(left>=right)
73+
if (isEQ):
74+
self.assertTrue(left<=right)
75+
self.assertTrue(left==right)
76+
self.assertFalse(left!=right)
77+
self.assertFalse(left>right)
78+
else:
79+
self.assertFalse(left<=right)
80+
self.assertFalse(left==right)
81+
self.assertTrue(left!=right)
82+
self.assertTrue(left>right)
83+
84+
def comp_le(self, left, right, isEQ):
85+
self.assertFalse(left > right)
86+
self.assertTrue(left<=right)
87+
if (isEQ):
88+
self.assertTrue(left==right)
89+
self.assertFalse(left!=right)
90+
self.assertFalse(left<right)
91+
self.assertTrue(left>=right)
92+
else:
93+
self.assertFalse(left==right)
94+
self.assertTrue(left!=right)
95+
self.assertTrue(left<right)
96+
self.assertFalse(left>=right)
97+

‎graalpython/com.oracle.graal.python.test/src/tests/test_list.py‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
LONG_NUMBER = 6227020800;
1111

1212
import list_tests
13+
from compare import CompareTest
1314

1415

1516
class ListTest(list_tests.CommonTest):
@@ -532,4 +533,97 @@ def __index__(self):
532533
ob = My(10)
533534
self.assertRaises(TypeError, l.__imul__, ob)
534535

535-
536+
class ListCompareTest(CompareTest):
537+
538+
def test_compare(self):
539+
l1 = [1, 2, 3]
540+
l2 = [1,2,3,0]
541+
l3 = [1,2,3,4]
542+
543+
self.comp_eq(l1, l1)
544+
545+
self.comp_ne(l1, l2)
546+
self.comp_ne(l2, l3)
547+
548+
self.comp_ge(l1, l1, True)
549+
self.comp_ge(l2, l1, False)
550+
self.comp_ge(l3, l2, False)
551+
self.comp_ge(l3, l1, False)
552+
553+
self.comp_le(l1, l1, True)
554+
self.comp_le(l1, l2, False)
555+
self.comp_le(l2, l3, False)
556+
self.comp_le(l1, l3, False)
557+
558+
self.comp_lt(l1, l2)
559+
self.comp_lt(l2, l3)
560+
self.comp_lt(l1, l3)
561+
562+
self.comp_gt(l2, l1)
563+
self.comp_gt(l3, l2)
564+
self.comp_gt(l3, l1)
565+
566+
def test_equal_other(self):
567+
def tryWithOtherType(left, right):
568+
self.assertFalse(left == right, "Operation {} == {} should be False".format(left, right))
569+
self.assertTrue(left != right, "Operation {} != {} should be True".format(left, right))
570+
571+
l1 = [1, 2, 3]
572+
tryWithOtherType(l1, 1)
573+
tryWithOtherType(l1, 'hello')
574+
tryWithOtherType(l1, False)
575+
tryWithOtherType(l1, (1, 2, 3))
576+
tryWithOtherType(l1, {1, 2, 3})
577+
tryWithOtherType(l1, {'one':1, 'two':2, 'three':3})
578+
579+
def test_raiseTypeError(self):
580+
def tryWithOtherType(left, right):
581+
def raiseTypeError(left, op, right):
582+
try:
583+
if op == "<":
584+
left < right
585+
elif op == ">":
586+
left > right
587+
elif op == "<=":
588+
left <= right
589+
elif op == ">=":
590+
left >= right
591+
self.assertTrue(False, "Operation {} {} {} should raise TypeError".format(left, op, right))
592+
except TypeError:
593+
pass
594+
595+
raiseTypeError(left, "<", right)
596+
raiseTypeError(left, ">", right)
597+
raiseTypeError(left, "<=", right)
598+
raiseTypeError(left, ">=", right)
599+
600+
l1 = [1, 2, 3]
601+
tryWithOtherType(l1, 1)
602+
tryWithOtherType(l1, True)
603+
tryWithOtherType(l1, 'hello')
604+
605+
def test_extendingClass(self):
606+
class MyList(list):
607+
def __eq__(self, value):
608+
return 'eq'
609+
def __ne__(self, value):
610+
return value;
611+
def __gt__(self, value):
612+
return 10
613+
def __lt__(self, value):
614+
return 11.11
615+
def __ge__(self, value):
616+
return value + 5
617+
def __le__(self, value):
618+
r = super().__le__(value)
619+
return "OK:" + str(r)
620+
621+
l1 = MyList([1, 10])
622+
self.assertEqual(l1 == 1, 'eq')
623+
self.assertEqual(l1 != 'ne', 'ne')
624+
self.assertEqual(l1 > 'ne', 10)
625+
self.assertEqual(l1 < 1, 11.11)
626+
self.assertEqual(l1 >= 6, 11)
627+
self.assertEqual(l1 <= [1, 1], 'OK:False')
628+
self.assertEqual(l1 <= [1, 10], 'OK:True')
629+
self.assertEqual(l1 <= [1, 10, 0], 'OK:True')
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
# Copyright (C) 1996-2017 Python Software Foundation
3+
#
4+
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
6+
import unittest
7+
import random
8+
import time
9+
10+
11+
class TestBasicOps:
12+
def __init__(self):
13+
super().__init__()
14+
self.gen = random.Random()
15+
16+
def randomlist(self, n):
17+
"""Helper function to make a list of random numbers"""
18+
return [self.gen.random() for i in range(n)]
19+
20+
def test_autoseed(self):
21+
self.gen.seed()
22+
state1 = self.gen.getstate()
23+
time.sleep(0.1)
24+
self.gen.seed() # diffent seeds at different times
25+
state2 = self.gen.getstate()
26+
self.assertNotEqual(state1, state2)
27+
28+
def test_saverestore(self):
29+
N = 1000
30+
self.gen.seed()
31+
state = self.gen.getstate()
32+
randseq = self.randomlist(N)
33+
self.gen.setstate(state) # should regenerate the same sequence
34+
self.assertEqual(randseq, self.randomlist(N))
35+
36+
def test_seedargs(self):
37+
# Seed value with a negative hash.
38+
class MySeed(object):
39+
def __hash__(self):
40+
return -1729
41+
for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
42+
3.14, 1+2j, 'a', tuple('abc'), MySeed()]:
43+
self.gen.seed(arg)
44+
for arg in [list(range(3)), dict(one=1)]:
45+
self.assertRaises(TypeError, self.gen.seed, arg)
46+
self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
47+
self.assertRaises(TypeError, type(self.gen), [])
48+
49+
def test_shuffle(self):
50+
shuffle = self.gen.shuffle
51+
lst = []
52+
shuffle(lst)
53+
self.assertEqual(lst, [])
54+
lst = [37]
55+
shuffle(lst)
56+
self.assertEqual(lst, [37])
57+
seqs = [list(range(n)) for n in range(10)]
58+
shuffled_seqs = [list(range(n)) for n in range(10)]
59+
for shuffled_seq in shuffled_seqs:
60+
shuffle(shuffled_seq)
61+
for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
62+
self.assertEqual(len(seq), len(shuffled_seq))
63+
self.assertEqual(set(seq), set(shuffled_seq))
64+
# The above tests all would pass if the shuffle was a
65+
# no-op. The following non-deterministic test covers that. It
66+
# asserts that the shuffled sequence of 1000 distinct elements
67+
# must be different from the original one. Although there is
68+
# mathematically a non-zero probability that this could
69+
# actually happen in a genuinely random shuffle, it is
70+
# completely negligible, given that the number of possible
71+
# permutations of 1000 objects is 1000! (factorial of 1000),
72+
# which is considerably larger than the number of atoms in the
73+
# universe...
74+
lst = list(range(1000))
75+
shuffled_lst = list(range(1000))
76+
shuffle(shuffled_lst)
77+
self.assertTrue(lst != shuffled_lst)
78+
shuffle(lst)
79+
self.assertTrue(lst != shuffled_lst)
80+
81+
# def test_choice(self):
82+
# choice = self.gen.choice
83+
# with self.assertRaises(IndexError):
84+
# choice([])
85+
# self.assertEqual(choice([50]), 50)
86+
# self.assertIn(choice([25, 75]), [25, 75])
87+
88+
def test_sample(self):
89+
# For the entire allowable range of 0 <= k <= N, validate that
90+
# the sample is of the correct length and contains only unique items
91+
N = 100
92+
population = range(N)
93+
for k in range(N+1):
94+
s = self.gen.sample(population, k)
95+
self.assertEqual(len(s), k)
96+
uniq = set(s)
97+
self.assertEqual(len(uniq), k)
98+
self.assertTrue(uniq <= set(population))
99+
self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
100+
# Exception raised if size of sample exceeds that of population
101+
self.assertRaises(ValueError, self.gen.sample, population, N+1)
102+
self.assertRaises(ValueError, self.gen.sample, [], -1)
103+
104+
def test_sample_distribution(self):
105+
# For the entire allowable range of 0 <= k <= N, validate that
106+
# sample generates all possible permutations
107+
n = 5
108+
pop = range(n)
109+
trials = 10000 # large num prevents false negatives without slowing normal case
110+
def factorial(n):
111+
if n == 0:
112+
return 1
113+
return n * factorial(n - 1)
114+
for k in range(n):
115+
expected = factorial(n) // factorial(n-k)
116+
perms = {}
117+
for i in range(trials):
118+
perms[tuple(self.gen.sample(pop, k))] = None
119+
if len(perms) == expected:
120+
break
121+
else:
122+
self.fail()
123+
124+
def test_sample_inputs(self):
125+
# SF bug #801342 -- population can be any iterable defining __len__()
126+
self.gen.sample(set(range(20)), 2)
127+
self.gen.sample(range(20), 2)
128+
self.gen.sample(range(20), 2)
129+
self.gen.sample(str('abcdefghijklmnopqrst'), 2)
130+
self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
131+
132+
def test_sample_on_dicts(self):
133+
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
134+
135+
def test_choices(self):
136+
import sys
137+
if sys.version_info.minor < 6:
138+
return
139+
140+
choices = self.gen.choices
141+
data = ['red', 'green', 'blue', 'yellow']
142+
str_data = 'abcd'
143+
range_data = range(4)
144+
set_data = set(range(4))
145+
146+
# basic functionality
147+
for sample in [
148+
choices(data, k=5),
149+
choices(data, range(4), k=5),
150+
choices(k=5, population=data, weights=range(4)),
151+
]:
152+
self.assertEqual(len(sample), 5)
153+
self.assertEqual(type(sample), list)
154+
self.assertTrue(set(sample) <= set(data))
155+
156+
def test_gauss(self):
157+
# Ensure that the seed() method initializes all the hidden state. In
158+
# particular, through 2.2.1 it failed to reset a piece of state used
159+
# by (and only by) the .gauss() method.
160+
161+
for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
162+
self.gen.seed(seed)
163+
x1 = self.gen.random()
164+
y1 = self.gen.gauss(0, 1)
165+
166+
self.gen.seed(seed)
167+
x2 = self.gen.random()
168+
y2 = self.gen.gauss(0, 1)
169+
170+
self.assertEqual(x1, x2)
171+
self.assertEqual(y1, y2)
172+
173+
def test_bug_9025(self):
174+
# Had problem with an uneven distribution in int(n*random())
175+
# Verify the fix by checking that distributions fall within expectations.
176+
n = 100000
177+
randrange = self.gen.randrange
178+
k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
179+
self.assertTrue(0.30 < k/n < .37, (k/n))

0 commit comments

Comments
 (0)