10000 TST: property-test for unicode bug · Zac-HD/numpy@9217ab9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9217ab9

Browse files
committed
TST: property-test for unicode bug
The bug was reported in numpy#15363 and fixed in numpy#15385, before Numpy decided to allow Hypothesis in it's own test suite. Since it does now, I thought it would be nice to include the test that found the bug as well as the more specific regression test I wrote.
1 parent e8a3bfa commit 9217ab9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

numpy/core/tests/test_unicode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
from numpy.testing import assert_, assert_equal, assert_array_equal
33

4+
from hypothesis import example, given, strategies as st
5+
46
def buffer_length(arr):
57
if isinstance(arr, str):
68
if not arr:
@@ -360,3 +362,21 @@ class TestByteorder_1009_UCS4(ByteorderValues):
360362
"""Check the byteorder in unicode (size 1009, UCS4 values)"""
361363
ulen = 1009
362364
ucs_value = ucs4_value
365+
366+
367+
############################################################
368+
# Property-based tests
369+
############################################################
370+
371+
class TestEncodingProperties:
372+
"""Check that all strings round-trip as expected."""
373+
# This test found real bugs: see https://github.com/numpy/numpy/issues/15363
374+
375+
@example("\udd4a") # code point not in range(0x110000)
376+
@example("\ud82d") # code point in surrogate code point range(0xd800, 0xe000)
377+
@example("\ud800") # AssertionError: assert '\\ud800' == '\u2400'
378+
@given(st.text(st.characters()).map(lambda s: s.rstrip("\0")))
379+
def test_unicode_arrays_round_trip(self, s):
380+
arr = np.array([s]) # if we can construct an array like so...
381+
s2 = arr[0] # we should be able to access the element and
382+
assert_equal(s, s2) # it should equal the input (no trailing nulls)

0 commit comments

Comments
 (0)
0