Debian Codegraph @

pypy/module/_pypyjson/test/test_simd.py

raw symbols SCIP
1 import sys
2 import pytest
3 from rpython.rtyper.lltypesystem import lltype, rffi
4 from rpython.rlib.rarithmetic import r_uint, intmask
5
6 from pypy.module._pypyjson.simd import USE_SIMD
7 from pypy.module._pypyjson.simd import find_end_of_string_slow
8 from pypy.module._pypyjson.simd import find_end_of_string_slow_no_hash
9 from pypy.module._pypyjson.simd import print_chars
10 from pypy.module._pypyjson.simd import find_end_of_string_simd_unaligned, WORD_SIZE
11 from pypy.module._pypyjson.simd import find_end_of_string_simd_unaligned_no_hash
12
13 try:
14 from hypothesis import example, given, strategies
15 except ImportError:
16 pytest.skip("missing hypothesis!")
17
18 if not USE_SIMD:
19 pytest.skip("only implemented for 64 bit for now")
20
21 def fill_to_word_size(res, ch=" "):
22 if len(res) % WORD_SIZE != 0:
23 res += ch * (WORD_SIZE - (len(res) % WORD_SIZE))
24 return res
25
26 def string_to_word(s):
27 assert len(s) == WORD_SIZE
28 ll_chars, llobj, flag = rffi.get_nonmovingbuffer_ll_final_null(s)
29 try:
30 wordarray = rffi.cast(rffi.UNSIGNEDP, ll_chars)
31 return wordarray[0]
32 finally:
33 rffi.free_nonmovingbuffer_ll(ll_chars, llobj, flag)
34
35 def ll(callable, string, *args):
36 ll_chars, llobj, flag = rffi.get_nonmovingbuffer_ll_final_null(string)
37 try:
38 return callable(ll_chars, *args)
39 finally:
40 rffi.free_nonmovingbuffer_ll(ll_chars, llobj, flag)
41
42 word = strategies.builds(
43 r_uint, strategies.integers(min_value=-sys.maxint-1, max_value=sys.maxint))
44
45 def build_string(prefix, content, end, suffix):
46 res = prefix + '"' + "".join([chr(x) for x in content]) + end + suffix
47 return fill_to_word_size(res), len(prefix) + 1
48
49 string_in_context_strategy = strategies.builds(
50 build_string, prefix=strategies.binary(),
51 content=strategies.lists(strategies.integers(1, 255), min_size=1),
52 end=strategies.sampled_from('"\\\x00\x01'),
53 suffix=strategies.binary())
54
55 def compare(string, res1, res2):
56 hash1, nonascii1, endindex1 = res1
57 hash2, nonascii2, endindex2 = res2
58 assert endindex1 == endindex2
59 if string[endindex1 - 1] == '"':
60 assert hash1 == hash2
61 assert nonascii1 == nonascii2
62
63
64 @example(('" \x80" ', 1))
65 @example(('"\x01" ', 1))
66 @example(('"aaaaaaaa"\x00\x00\x00\x00\x00\x00\x00 ', 1))
67 @example(('"aaaaaaaa" ', 1))
68 @example(('"12"', 1))
69 @example(('"1234567abcdefghAB"', 1))
70 @example(('"1234567abcdefgh"', 1))
71 @example((' "123456ABCDEF" \x00', 2))
72 @example((' "123456aaaaaaaaABCDEF"\x00', 2))
73 @given(string_in_context_strategy)
74 def test_find_end_of_string(a):
75 (string, startindex) = a
76 res = ll(find_end_of_string_slow, string, startindex, len(string))
77 hash, nonascii1, endposition1 = res
78 res2 = ll(find_end_of_string_slow_no_hash, string, startindex, len(string))
79 assert res2 == (nonascii1, endposition1)
80 ch = string[endposition1]
81 assert ch == '"' or ch == '\\' or ch < '\x20'
82 for ch in string[startindex:endposition1]:
83 assert not (ch == '"' or ch == '\\' or ch < '\x20')
84 compare(string, res, ll(find_end_of_string_simd_unaligned, string, startindex, len(string)))
85
86 nonascii2, endposition2 = ll(find_end_of_string_simd_unaligned_no_hash, string, startindex, len(string))
87 assert nonascii1 == nonascii2
88 assert endposition1 == endposition2
89
90 @given(string_in_context_strategy, strategies.binary(min_size=1))
91 def test_find_end_of_string_position_invariance(a, prefix):
92 fn = find_end_of_string_simd_unaligned
93 (string, startindex) = a
94 h1, nonascii1, i1 = ll(fn, string, startindex, len(string))
95 string2 = prefix + string
96 h2, nonascii2, i2 = ll(fn, string2, startindex + len(prefix), len(string) + len(prefix))
97 assert h1 == h2
98 assert nonascii1 == nonascii2
99 assert i1 + len(prefix) == i2
100
101 @given(string_in_context_strategy, strategies.binary(min_size=1))
102 def test_find_end_of_string_position_invariance_no_hash(a, prefix):
103 fn = find_end_of_string_simd_unaligned_no_hash
104 (string, startindex) = a
105 nonascii1, i1 = ll(fn, string, startindex, len(string))
106 string2 = prefix + string
107 nonascii2, i2 = ll(fn, string2, startindex + len(prefix), len(string) + len(prefix))
108 assert nonascii1 == nonascii2
109 assert i1 + len(prefix) == i2
110