mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Make numerous improvements
- Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
This commit is contained in:
parent
fa7b4f5bd1
commit
39bf41f4eb
806 changed files with 77494 additions and 63859 deletions
157
third_party/python/Lib/test/test_heapq.py
vendored
157
third_party/python/Lib/test/test_heapq.py
vendored
|
@ -1,5 +1,6 @@
|
|||
"""Unittests for heapq."""
|
||||
|
||||
import heapq
|
||||
import random
|
||||
import unittest
|
||||
|
||||
|
@ -7,26 +8,12 @@ from test import support
|
|||
from unittest import TestCase, skipUnless
|
||||
from operator import itemgetter
|
||||
|
||||
py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
|
||||
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
|
||||
|
||||
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
|
||||
# _heapq is imported, so check them there
|
||||
# heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
|
||||
# heapq is imported, so check them there
|
||||
func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace',
|
||||
'_heappop_max', '_heapreplace_max', '_heapify_max']
|
||||
|
||||
class TestModules(TestCase):
|
||||
def test_py_functions(self):
|
||||
for fname in func_names:
|
||||
self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
|
||||
|
||||
@skipUnless(c_heapq, 'requires _heapq')
|
||||
def test_c_functions(self):
|
||||
for fname in func_names:
|
||||
self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
|
||||
|
||||
|
||||
class TestHeap:
|
||||
class TestHeap(TestCase):
|
||||
|
||||
def test_push_pop(self):
|
||||
# 1) Push 256 random numbers and pop them off, verifying all's OK.
|
||||
|
@ -36,11 +23,11 @@ class TestHeap:
|
|||
for i in range(256):
|
||||
item = random.random()
|
||||
data.append(item)
|
||||
self.module.heappush(heap, item)
|
||||
heapq.heappush(heap, item)
|
||||
self.check_invariant(heap)
|
||||
results = []
|
||||
while heap:
|
||||
item = self.module.heappop(heap)
|
||||
item = heapq.heappop(heap)
|
||||
self.check_invariant(heap)
|
||||
results.append(item)
|
||||
data_sorted = data[:]
|
||||
|
@ -49,10 +36,10 @@ class TestHeap:
|
|||
# 2) Check that the invariant holds for a sorted array
|
||||
self.check_invariant(results)
|
||||
|
||||
self.assertRaises(TypeError, self.module.heappush, [])
|
||||
self.assertRaises(TypeError, heapq.heappush, [])
|
||||
try:
|
||||
self.assertRaises(TypeError, self.module.heappush, None, None)
|
||||
self.assertRaises(TypeError, self.module.heappop, None)
|
||||
self.assertRaises(TypeError, heapq.heappush, None, None)
|
||||
self.assertRaises(TypeError, heapq.heappop, None)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
@ -66,18 +53,18 @@ class TestHeap:
|
|||
def test_heapify(self):
|
||||
for size in list(range(30)) + [20000]:
|
||||
heap = [random.random() for dummy in range(size)]
|
||||
self.module.heapify(heap)
|
||||
heapq.heapify(heap)
|
||||
self.check_invariant(heap)
|
||||
|
||||
self.assertRaises(TypeError, self.module.heapify, None)
|
||||
self.assertRaises(TypeError, heapq.heapify, None)
|
||||
|
||||
def test_naive_nbest(self):
|
||||
data = [random.randrange(2000) for i in range(1000)]
|
||||
heap = []
|
||||
for item in data:
|
||||
self.module.heappush(heap, item)
|
||||
heapq.heappush(heap, item)
|
||||
if len(heap) > 10:
|
||||
self.module.heappop(heap)
|
||||
heapq.heappop(heap)
|
||||
heap.sort()
|
||||
self.assertEqual(heap, sorted(data)[-10:])
|
||||
|
||||
|
@ -85,7 +72,7 @@ class TestHeap:
|
|||
# An iterator returning a heap's elements, smallest-first.
|
||||
try:
|
||||
while 1:
|
||||
yield self.module.heappop(heap)
|
||||
yield heapq.heappop(heap)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
|
@ -97,42 +84,42 @@ class TestHeap:
|
|||
# (10 log-time steps).
|
||||
data = [random.randrange(2000) for i in range(1000)]
|
||||
heap = data[:10]
|
||||
self.module.heapify(heap)
|
||||
heapq.heapify(heap)
|
||||
for item in data[10:]:
|
||||
if item > heap[0]: # this gets rarer the longer we run
|
||||
self.module.heapreplace(heap, item)
|
||||
heapq.heapreplace(heap, item)
|
||||
self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
|
||||
|
||||
self.assertRaises(TypeError, self.module.heapreplace, None)
|
||||
self.assertRaises(TypeError, self.module.heapreplace, None, None)
|
||||
self.assertRaises(IndexError, self.module.heapreplace, [], None)
|
||||
self.assertRaises(TypeError, heapq.heapreplace, None)
|
||||
self.assertRaises(TypeError, heapq.heapreplace, None, None)
|
||||
self.assertRaises(IndexError, heapq.heapreplace, [], None)
|
||||
|
||||
def test_nbest_with_pushpop(self):
|
||||
data = [random.randrange(2000) for i in range(1000)]
|
||||
heap = data[:10]
|
||||
self.module.heapify(heap)
|
||||
heapq.heapify(heap)
|
||||
for item in data[10:]:
|
||||
self.module.heappushpop(heap, item)
|
||||
heapq.heappushpop(heap, item)
|
||||
self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
|
||||
self.assertEqual(self.module.heappushpop([], 'x'), 'x')
|
||||
self.assertEqual(heapq.heappushpop([], 'x'), 'x')
|
||||
|
||||
def test_heappushpop(self):
|
||||
h = []
|
||||
x = self.module.heappushpop(h, 10)
|
||||
x = heapq.heappushpop(h, 10)
|
||||
self.assertEqual((h, x), ([], 10))
|
||||
|
||||
h = [10]
|
||||
x = self.module.heappushpop(h, 10.0)
|
||||
x = heapq.heappushpop(h, 10.0)
|
||||
self.assertEqual((h, x), ([10], 10.0))
|
||||
self.assertEqual(type(h[0]), int)
|
||||
self.assertEqual(type(x), float)
|
||||
|
||||
h = [10];
|
||||
x = self.module.heappushpop(h, 9)
|
||||
x = heapq.heappushpop(h, 9)
|
||||
self.assertEqual((h, x), ([10], 9))
|
||||
|
||||
h = [10];
|
||||
x = self.module.heappushpop(h, 11)
|
||||
x = heapq.heappushpop(h, 11)
|
||||
self.assertEqual((h, x), ([11], 10))
|
||||
|
||||
def test_heapsort(self):
|
||||
|
@ -142,12 +129,12 @@ class TestHeap:
|
|||
data = [random.randrange(25) for i in range(size)]
|
||||
if trial & 1: # Half of the time, use heapify
|
||||
heap = data[:]
|
||||
self.module.heapify(heap)
|
||||
heapq.heapify(heap)
|
||||
else: # The rest of the time, use heappush
|
||||
heap = []
|
||||
for item in data:
|
||||
self.module.heappush(heap, item)
|
||||
heap_sorted = [self.module.heappop(heap) for i in range(size)]
|
||||
heapq.heappush(heap, item)
|
||||
heap_sorted = [heapq.heappop(heap) for i in range(size)]
|
||||
self.assertEqual(heap_sorted, sorted(data))
|
||||
|
||||
def test_merge(self):
|
||||
|
@ -165,8 +152,8 @@ class TestHeap:
|
|||
for seq in inputs:
|
||||
seqs.append(sorted(seq, key=key, reverse=reverse))
|
||||
self.assertEqual(sorted(chain(*inputs), key=key, reverse=reverse),
|
||||
list(self.module.merge(*seqs, key=key, reverse=reverse)))
|
||||
self.assertEqual(list(self.module.merge()), [])
|
||||
list(heapq.merge(*seqs, key=key, reverse=reverse)))
|
||||
self.assertEqual(list(heapq.merge()), [])
|
||||
|
||||
def test_merge_does_not_suppress_index_error(self):
|
||||
# Issue 19018: Heapq.merge suppresses IndexError from user generator
|
||||
|
@ -175,7 +162,7 @@ class TestHeap:
|
|||
for i in range(20):
|
||||
yield s[i] # IndexError when i > 10
|
||||
with self.assertRaises(IndexError):
|
||||
list(self.module.merge(iterable(), iterable()))
|
||||
list(heapq.merge(iterable(), iterable()))
|
||||
|
||||
def test_merge_stability(self):
|
||||
class Int(int):
|
||||
|
@ -189,25 +176,25 @@ class TestHeap:
|
|||
inputs[stream].append(obj)
|
||||
for stream in inputs:
|
||||
stream.sort()
|
||||
result = [i.pair for i in self.module.merge(*inputs)]
|
||||
result = [i.pair for i in heapq.merge(*inputs)]
|
||||
self.assertEqual(result, sorted(result))
|
||||
|
||||
def test_nsmallest(self):
|
||||
data = [(random.randrange(2000), i) for i in range(1000)]
|
||||
for f in (None, lambda x: x[0] * 547 % 2000):
|
||||
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(list(self.module.nsmallest(n, data)),
|
||||
self.assertEqual(list(heapq.nsmallest(n, data)),
|
||||
sorted(data)[:n])
|
||||
self.assertEqual(list(self.module.nsmallest(n, data, key=f)),
|
||||
self.assertEqual(list(heapq.nsmallest(n, data, key=f)),
|
||||
sorted(data, key=f)[:n])
|
||||
|
||||
def test_nlargest(self):
|
||||
data = [(random.randrange(2000), i) for i in range(1000)]
|
||||
for f in (None, lambda x: x[0] * 547 % 2000):
|
||||
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(list(self.module.nlargest(n, data)),
|
||||
self.assertEqual(list(heapq.nlargest(n, data)),
|
||||
sorted(data, reverse=True)[:n])
|
||||
self.assertEqual(list(self.module.nlargest(n, data, key=f)),
|
||||
self.assertEqual(list(heapq.nlargest(n, data, key=f)),
|
||||
sorted(data, key=f, reverse=True)[:n])
|
||||
|
||||
def test_comparison_operator(self):
|
||||
|
@ -215,8 +202,8 @@ class TestHeap:
|
|||
# For python 3.0, __le__ alone is not enough
|
||||
def hsort(data, comp):
|
||||
data = [comp(x) for x in data]
|
||||
self.module.heapify(data)
|
||||
return [self.module.heappop(data).x for i in range(len(data))]
|
||||
heapq.heapify(data)
|
||||
return [heapq.heappop(data).x for i in range(len(data))]
|
||||
class LT:
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
|
@ -233,15 +220,6 @@ class TestHeap:
|
|||
self.assertRaises(TypeError, data, LE)
|
||||
|
||||
|
||||
class TestHeapPython(TestHeap, TestCase):
|
||||
module = py_heapq
|
||||
|
||||
|
||||
@skipUnless(c_heapq, 'requires _heapq')
|
||||
class TestHeapC(TestHeap, TestCase):
|
||||
module = c_heapq
|
||||
|
||||
|
||||
#==============================================================================
|
||||
|
||||
class LenOnly:
|
||||
|
@ -348,48 +326,48 @@ class SideEffectLT:
|
|||
return self.value < other.value
|
||||
|
||||
|
||||
class TestErrorHandling:
|
||||
class TestErrorHandling(TestCase):
|
||||
|
||||
def test_non_sequence(self):
|
||||
for f in (self.module.heapify, self.module.heappop):
|
||||
for f in (heapq.heapify, heapq.heappop):
|
||||
self.assertRaises((TypeError, AttributeError), f, 10)
|
||||
for f in (self.module.heappush, self.module.heapreplace,
|
||||
self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.heappush, heapq.heapreplace,
|
||||
heapq.nlargest, heapq.nsmallest):
|
||||
self.assertRaises((TypeError, AttributeError), f, 10, 10)
|
||||
|
||||
def test_len_only(self):
|
||||
for f in (self.module.heapify, self.module.heappop):
|
||||
for f in (heapq.heapify, heapq.heappop):
|
||||
self.assertRaises((TypeError, AttributeError), f, LenOnly())
|
||||
for f in (self.module.heappush, self.module.heapreplace):
|
||||
for f in (heapq.heappush, heapq.heapreplace):
|
||||
self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10)
|
||||
for f in (self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.nlargest, heapq.nsmallest):
|
||||
self.assertRaises(TypeError, f, 2, LenOnly())
|
||||
|
||||
def test_get_only(self):
|
||||
for f in (self.module.heapify, self.module.heappop):
|
||||
for f in (heapq.heapify, heapq.heappop):
|
||||
self.assertRaises(TypeError, f, GetOnly())
|
||||
for f in (self.module.heappush, self.module.heapreplace):
|
||||
for f in (heapq.heappush, heapq.heapreplace):
|
||||
self.assertRaises(TypeError, f, GetOnly(), 10)
|
||||
for f in (self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.nlargest, heapq.nsmallest):
|
||||
self.assertRaises(TypeError, f, 2, GetOnly())
|
||||
|
||||
def test_get_only(self):
|
||||
seq = [CmpErr(), CmpErr(), CmpErr()]
|
||||
for f in (self.module.heapify, self.module.heappop):
|
||||
for f in (heapq.heapify, heapq.heappop):
|
||||
self.assertRaises(ZeroDivisionError, f, seq)
|
||||
for f in (self.module.heappush, self.module.heapreplace):
|
||||
for f in (heapq.heappush, heapq.heapreplace):
|
||||
self.assertRaises(ZeroDivisionError, f, seq, 10)
|
||||
for f in (self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.nlargest, heapq.nsmallest):
|
||||
self.assertRaises(ZeroDivisionError, f, 2, seq)
|
||||
|
||||
def test_arg_parsing(self):
|
||||
for f in (self.module.heapify, self.module.heappop,
|
||||
self.module.heappush, self.module.heapreplace,
|
||||
self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.heapify, heapq.heappop,
|
||||
heapq.heappush, heapq.heapreplace,
|
||||
heapq.nlargest, heapq.nsmallest):
|
||||
self.assertRaises((TypeError, AttributeError), f, 10)
|
||||
|
||||
def test_iterable_args(self):
|
||||
for f in (self.module.nlargest, self.module.nsmallest):
|
||||
for f in (heapq.nlargest, heapq.nsmallest):
|
||||
for s in ("123", "", range(1000), (1, 1.2), range(2000,2200,5)):
|
||||
for g in (G, I, Ig, L, R):
|
||||
self.assertEqual(list(f(2, g(s))), list(f(2,s)))
|
||||
|
@ -405,14 +383,14 @@ class TestErrorHandling:
|
|||
heap.extend(SideEffectLT(i, heap) for i in range(200))
|
||||
# Python version raises IndexError, C version RuntimeError
|
||||
with self.assertRaises((IndexError, RuntimeError)):
|
||||
self.module.heappush(heap, SideEffectLT(5, heap))
|
||||
heapq.heappush(heap, SideEffectLT(5, heap))
|
||||
|
||||
def test_heappop_mutating_heap(self):
|
||||
heap = []
|
||||
heap.extend(SideEffectLT(i, heap) for i in range(200))
|
||||
# Python version raises IndexError, C version RuntimeError
|
||||
with self.assertRaises((IndexError, RuntimeError)):
|
||||
self.module.heappop(heap)
|
||||
heapq.heappop(heap)
|
||||
|
||||
def test_comparison_operator_modifiying_heap(self):
|
||||
# See bpo-39421: Strong references need to be taken
|
||||
|
@ -423,8 +401,8 @@ class TestErrorHandling:
|
|||
return NotImplemented
|
||||
|
||||
heap = []
|
||||
self.module.heappush(heap, EvilClass(0))
|
||||
self.assertRaises(IndexError, self.module.heappushpop, heap, 1)
|
||||
heapq.heappush(heap, EvilClass(0))
|
||||
self.assertRaises(IndexError, heapq.heappushpop, heap, 1)
|
||||
|
||||
def test_comparison_operator_modifiying_heap_two_heaps(self):
|
||||
|
||||
|
@ -440,18 +418,11 @@ class TestErrorHandling:
|
|||
|
||||
list1, list2 = [], []
|
||||
|
||||
self.module.heappush(list1, h(0))
|
||||
self.module.heappush(list2, g(0))
|
||||
heapq.heappush(list1, h(0))
|
||||
heapq.heappush(list2, g(0))
|
||||
|
||||
self.assertRaises((IndexError, RuntimeError), self.module.heappush, list1, g(1))
|
||||
self.assertRaises((IndexError, RuntimeError), self.module.heappush, list2, h(1))
|
||||
|
||||
class TestErrorHandlingPython(TestErrorHandling, TestCase):
|
||||
module = py_heapq
|
||||
|
||||
@skipUnless(c_heapq, 'requires _heapq')
|
||||
class TestErrorHandlingC(TestErrorHandling, TestCase):
|
||||
module = c_heapq
|
||||
self.assertRaises((IndexError, RuntimeError), heapq.heappush, list1, g(1))
|
||||
self.assertRaises((IndexError, RuntimeError), heapq.heappush, list2, h(1))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue