-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
186 lines (155 loc) · 6.42 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# *****************************************************************************
# YAML dumper for NICOS, the Networked Instrument Control System of the FRM-II
# Copyright (c) 2016-2023 by the NICOS contributors (see AUTHORS)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Georg Brandl <[email protected]>
#
# *****************************************************************************
"""Test suite for the quickyaml dumper."""
import io
import os
import sys
import glob
import random
import collections
import numpy
import yaml
import pytest
from pytest import raises
# Support running from checkout after setup.py build.
sys.path[0:0] = glob.glob('build/lib.*-%s*' % sys.implementation.cache_tag)
import quickyaml
FUZZ_TESTS = int(os.environ.get('FUZZ_TESTS', '1000'))
def dumps(obj, **kwds):
fp = io.BytesIO()
kwds.setdefault('array_handling', quickyaml.ARRAY_AS_SEQ)
quickyaml.Dumper(**kwds).dump(obj, fp)
return fp.getvalue()
def test_primitive():
assert dumps(None) == b'null\n'
assert dumps(True) == b'true\n'
assert dumps(False) == b'false\n'
assert dumps(127) == b'127\n'
assert dumps(2.5) == b'2.5\n'
assert dumps(float('inf')) == b'.inf\n'
assert dumps(float('-inf')) == b'-.inf\n'
assert dumps(float('nan')) == b'.nan\n'
def test_strings():
assert dumps("abc") == b'abc\n'
assert dumps("+abc") == b'"+abc"\n'
assert dumps("\\") == b'"\\\\"\n'
assert dumps(" space") == b'" space"\n'
assert dumps("space ") == b'"space "\n'
assert dumps("abc\ndef") == b'"abc\\ndef"\n'
assert dumps(u"\x85") == b'"\\x85"\n'
assert dumps(u"Käsefuß") == 'Käsefuß\n'.encode()
assert dumps(u"abc\u1234") == 'abc\u1234\n'.encode()
for special in ['no', 'on', 'yes', 'off',
'true', 'false', 'null']:
assert dumps(special) == f'"{special}"\n'.encode()
def test_structure():
assert dumps([1, 2, 3], indent=2) == b'- 1\n- 2\n- 3\n'
assert dumps((1, 2, 3), indent=2) == b'- 1\n- 2\n- 3\n'
assert dumps({1: 2, 3: 4}) in (b'1: 2\n3: 4\n', b'3: 4\n1: 2\n')
def test_odict():
odict = collections.OrderedDict([('a', 1), ('x', 2), (5, [1, 2])])
assert dumps(odict) == b'a: 1\nx: 2\n5:\n- 1\n- 2\n'
def test_flowlist():
flist = quickyaml.flowlist([1, 2.0, '+1', None, True])
assert dumps({'a': flist}) == b'a: [1, 2.0, "+1", null, true]\n'
flist = quickyaml.flowlist([0] * 8)
assert dumps({'a': flist}, width=10) == \
b'a: [0, 0, 0,\n 0, 0, 0,\n 0, 0]\n'
def test_numpy_as_seq():
arr = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
assert dumps(arr, indent=2) == \
b'- - [1, 2]\n - [3, 4]\n- - [5, 6]\n - [7, 8]\n'
arr = numpy.array([[0.0, float('nan')], [float('inf'), float('-inf')]])
assert dumps({'counts': arr}, indent=2) == \
b'counts:\n - [0.0, .nan]\n - [.inf, -.inf]\n'
assert raises(ValueError, dumps, numpy.array(["a"]))
arr = numpy.zeros((4, 4, 4, 4, 4)).astype(int)
assert yaml.safe_load(dumps(arr)) == arr.tolist()
obj = {'c': numpy.zeros((4, 4, 4, 4, 4)).astype(int)}
assert yaml.safe_load(dumps(obj)) == {'c': obj['c'].tolist()}
empty_dim = numpy.array([[], []])
assert yaml.safe_load(dumps(empty_dim)) == [[], []]
def test_numpy_as_map():
kw = {'array_handling': quickyaml.ARRAY_AS_MAP}
arr = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
assert dumps(arr, indent=2, **kw) == \
b'0:\n 0: [1, 2]\n 1: [3, 4]\n1:\n 0: [5, 6]\n 1: [7, 8]\n'
arr = numpy.array([[0.0, float('nan')], [float('inf'), float('-inf')]])
assert dumps({'counts': arr}, indent=2, **kw) == \
b'counts:\n 0: [0.0, .nan]\n 1: [.inf, -.inf]\n'
assert raises(ValueError, dumps, numpy.array(["a"]))
arr = numpy.zeros((4, 4, 4, 4, 4)).astype(int)
res = [0, 0, 0, 0]
for dim in range(4):
res = dict((i, res) for i in range(4))
assert yaml.safe_load(dumps(arr, **kw)) == res
empty_dim = numpy.array([[], []])
assert yaml.safe_load(dumps(empty_dim, **kw)) == {0: [], 1: []}
def test_callback():
def cb(tup):
if isinstance(tup, set):
return b'(...)'
raise TypeError("uh oh")
assert dumps({'a': set((1, 2, 3))}, callback=cb) == b'a: (...)\n'
# callback raised
assert raises(TypeError, dumps, {'a': Ellipsis}, callback=cb)
# no callback, unsupported type
assert raises(ValueError, dumps, {'a': Ellipsis})
def generate_structure(depth):
if depth == 0:
return random.randint(0, 10)
dis = random.random()
n = random.randint(0, 5)
if dis < 0.2:
return [generate_structure(depth - 1) for _ in range(n)]
elif dis < 0.4:
return quickyaml.flowlist(generate_structure(0) for _ in range(n))
elif dis < 0.6:
return dict((i, generate_structure(depth - 1)) for i in range(n))
elif dis < 0.7:
return n
elif dis < 0.75:
return float(n)
elif dis < 0.78:
return float('inf')
elif dis < 0.79:
return float('-inf')
elif dis < 0.8:
return None
elif dis < 0.82:
return True if n % 2 == 0 else False
elif dis < 0.9:
return ''.join(chr(random.randint(0, 0xff))
for x in range(random.randint(0, 100)))
elif dis < 0.95:
return ''.join(chr(random.randint(0, 0xd7ff))
for x in range(random.randint(0, 100)))
else:
return random.choice(['no', 'on', 'yes', 'off',
'true', 'false', 'null'])
testcases = [generate_structure(20) for i in range(FUZZ_TESTS)]
@pytest.mark.parametrize('structure', testcases)
def test_fuzz_structure(structure):
roundtripped = yaml.safe_load(dumps(structure).decode())
assert roundtripped == structure, \
'\nGenerated: %r\nRoundtripped: %r' % (structure, roundtripped)