xnd 0.2.0dev3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +42 -0
  3. data/Gemfile +3 -0
  4. data/History.md +0 -0
  5. data/README.md +7 -0
  6. data/Rakefile +135 -0
  7. data/ext/ruby_xnd/extconf.rb +70 -0
  8. data/ext/ruby_xnd/float_pack_unpack.c +277 -0
  9. data/ext/ruby_xnd/float_pack_unpack.h +39 -0
  10. data/ext/ruby_xnd/gc_guard.c +36 -0
  11. data/ext/ruby_xnd/gc_guard.h +12 -0
  12. data/ext/ruby_xnd/include/xnd.h +449 -0
  13. data/ext/ruby_xnd/lib/libxnd.a +0 -0
  14. data/ext/ruby_xnd/lib/libxnd.so +1 -0
  15. data/ext/ruby_xnd/lib/libxnd.so.0 +1 -0
  16. data/ext/ruby_xnd/lib/libxnd.so.0.2.0dev3 +0 -0
  17. data/ext/ruby_xnd/memory_block_object.c +32 -0
  18. data/ext/ruby_xnd/memory_block_object.h +33 -0
  19. data/ext/ruby_xnd/ruby_xnd.c +1953 -0
  20. data/ext/ruby_xnd/ruby_xnd.h +61 -0
  21. data/ext/ruby_xnd/ruby_xnd_internal.h +85 -0
  22. data/ext/ruby_xnd/util.h +170 -0
  23. data/ext/ruby_xnd/xnd/AUTHORS.txt +5 -0
  24. data/ext/ruby_xnd/xnd/INSTALL.txt +134 -0
  25. data/ext/ruby_xnd/xnd/LICENSE.txt +29 -0
  26. data/ext/ruby_xnd/xnd/MANIFEST.in +3 -0
  27. data/ext/ruby_xnd/xnd/Makefile.in +80 -0
  28. data/ext/ruby_xnd/xnd/README.rst +44 -0
  29. data/ext/ruby_xnd/xnd/config.guess +1530 -0
  30. data/ext/ruby_xnd/xnd/config.h.in +22 -0
  31. data/ext/ruby_xnd/xnd/config.sub +1782 -0
  32. data/ext/ruby_xnd/xnd/configure +4867 -0
  33. data/ext/ruby_xnd/xnd/configure.ac +164 -0
  34. data/ext/ruby_xnd/xnd/doc/Makefile +14 -0
  35. data/ext/ruby_xnd/xnd/doc/_static/copybutton.js +66 -0
  36. data/ext/ruby_xnd/xnd/doc/conf.py +26 -0
  37. data/ext/ruby_xnd/xnd/doc/index.rst +44 -0
  38. data/ext/ruby_xnd/xnd/doc/libxnd/data-structures.rst +186 -0
  39. data/ext/ruby_xnd/xnd/doc/libxnd/functions.rst +148 -0
  40. data/ext/ruby_xnd/xnd/doc/libxnd/index.rst +25 -0
  41. data/ext/ruby_xnd/xnd/doc/releases/index.rst +34 -0
  42. data/ext/ruby_xnd/xnd/doc/xnd/align-pack.rst +96 -0
  43. data/ext/ruby_xnd/xnd/doc/xnd/buffer-protocol.rst +42 -0
  44. data/ext/ruby_xnd/xnd/doc/xnd/index.rst +30 -0
  45. data/ext/ruby_xnd/xnd/doc/xnd/quickstart.rst +62 -0
  46. data/ext/ruby_xnd/xnd/doc/xnd/types.rst +674 -0
  47. data/ext/ruby_xnd/xnd/install-sh +527 -0
  48. data/ext/ruby_xnd/xnd/libxnd/Makefile.in +102 -0
  49. data/ext/ruby_xnd/xnd/libxnd/Makefile.vc +112 -0
  50. data/ext/ruby_xnd/xnd/libxnd/bitmaps.c +345 -0
  51. data/ext/ruby_xnd/xnd/libxnd/contrib.h +313 -0
  52. data/ext/ruby_xnd/xnd/libxnd/copy.c +944 -0
  53. data/ext/ruby_xnd/xnd/libxnd/equal.c +1216 -0
  54. data/ext/ruby_xnd/xnd/libxnd/inline.h +154 -0
  55. data/ext/ruby_xnd/xnd/libxnd/overflow.h +147 -0
  56. data/ext/ruby_xnd/xnd/libxnd/split.c +286 -0
  57. data/ext/ruby_xnd/xnd/libxnd/tests/Makefile.in +39 -0
  58. data/ext/ruby_xnd/xnd/libxnd/tests/Makefile.vc +44 -0
  59. data/ext/ruby_xnd/xnd/libxnd/tests/README.txt +2 -0
  60. data/ext/ruby_xnd/xnd/libxnd/tests/runtest.c +101 -0
  61. data/ext/ruby_xnd/xnd/libxnd/tests/test.h +48 -0
  62. data/ext/ruby_xnd/xnd/libxnd/tests/test_fixed.c +108 -0
  63. data/ext/ruby_xnd/xnd/libxnd/xnd.c +1304 -0
  64. data/ext/ruby_xnd/xnd/libxnd/xnd.h +449 -0
  65. data/ext/ruby_xnd/xnd/python/test_xnd.py +3144 -0
  66. data/ext/ruby_xnd/xnd/python/xnd/__init__.py +290 -0
  67. data/ext/ruby_xnd/xnd/python/xnd/_xnd.c +2822 -0
  68. data/ext/ruby_xnd/xnd/python/xnd/contrib/pretty.py +850 -0
  69. data/ext/ruby_xnd/xnd/python/xnd/docstrings.h +129 -0
  70. data/ext/ruby_xnd/xnd/python/xnd/pyxnd.h +200 -0
  71. data/ext/ruby_xnd/xnd/python/xnd/util.h +182 -0
  72. data/ext/ruby_xnd/xnd/python/xnd_randvalue.py +1121 -0
  73. data/ext/ruby_xnd/xnd/python/xnd_support.py +106 -0
  74. data/ext/ruby_xnd/xnd/setup.py +303 -0
  75. data/ext/ruby_xnd/xnd/vcbuild/INSTALL.txt +42 -0
  76. data/ext/ruby_xnd/xnd/vcbuild/runtest32.bat +16 -0
  77. data/ext/ruby_xnd/xnd/vcbuild/runtest64.bat +14 -0
  78. data/ext/ruby_xnd/xnd/vcbuild/vcbuild32.bat +29 -0
  79. data/ext/ruby_xnd/xnd/vcbuild/vcbuild64.bat +29 -0
  80. data/ext/ruby_xnd/xnd/vcbuild/vcclean.bat +13 -0
  81. data/ext/ruby_xnd/xnd/vcbuild/vcdistclean.bat +14 -0
  82. data/lib/ruby_xnd.so +0 -0
  83. data/lib/xnd.rb +306 -0
  84. data/lib/xnd/monkeys.rb +29 -0
  85. data/lib/xnd/version.rb +6 -0
  86. data/spec/debug_spec.rb +9 -0
  87. data/spec/gc_guard_spec.rb +10 -0
  88. data/spec/leakcheck.rb +9 -0
  89. data/spec/spec_helper.rb +877 -0
  90. data/spec/type_inference_spec.rb +81 -0
  91. data/spec/xnd_spec.rb +2921 -0
  92. data/xnd.gemspec +47 -0
  93. metadata +215 -0
@@ -0,0 +1,1121 @@
1
+ #
2
+ # BSD 3-Clause License
3
+ #
4
+ # Copyright (c) 2017-2018, plures
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # 1. Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ #
13
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ #
17
+ # 3. Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from
19
+ # this software without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ # Python NDarray and functions for generating test cases.
34
+
35
+ from itertools import accumulate, count, product
36
+ from random import randrange
37
+ from collections import namedtuple
38
+ from xnd_support import R
39
+
40
+
41
+ # ======================================================================
42
+ # Primitive types
43
+ # ======================================================================
44
+
45
+ PRIMITIVE = [
46
+ 'bool',
47
+ 'int8', 'int16', 'int32', 'int64',
48
+ 'uint8', 'uint16', 'uint32', 'uint64',
49
+ 'float32', 'float64',
50
+ 'complex64', 'complex128'
51
+ ]
52
+
53
+ EMPTY_TEST_CASES = [
54
+ (0, "%s"),
55
+ ([], "0 * %s"),
56
+ ([0], "1 * %s"),
57
+ ([0, 0], "var(offsets=[0, 2]) * %s"),
58
+ (3 * [{"a": 0, "b": 0}], "3 * {a: int64, b: %s}")
59
+ ]
60
+
61
+
62
+ # ======================================================================
63
+ # Typed values
64
+ # ======================================================================
65
+
66
+ DTYPE_EMPTY_TEST_CASES = [
67
+ # Tuples
68
+ ((), "()"),
69
+ ((0,), "(int8)"),
70
+ ((0, 0), "(int8, int64)"),
71
+ ((0, (0+0j,)), "(uint16, (complex64))"),
72
+ ((0, (0+0j,)), "(uint16, (complex64), pack=1)"),
73
+ ((0, (0+0j,)), "(uint16, (complex64), pack=2)"),
74
+ ((0, (0+0j,)), "(uint16, (complex64), pack=4)"),
75
+ ((0, (0+0j,)), "(uint16, (complex64), pack=8)"),
76
+ ((0, (0+0j,)), "(uint16, (complex64), align=16)"),
77
+ (([],), "(0 * bytes)"),
78
+ (([], []), "(0 * bytes, 0 * string)"),
79
+ (([b''], 2 * [0.0j], 3 * [""]), "(1 * bytes, 2 * complex128, 3 * string)"),
80
+ (([b''], 2 * [(0.0j, 10 * [2 * [""]])], 3 * [""]), "(1 * bytes, 2 * (complex128, 10 * 2 * string), 3 * string)"),
81
+ ((0, 2 * [3 * [4 * [5 * [0.0]]]]), "(int64, 2 * 3 * Some(4 * 5 * float64))"),
82
+ ((0, 2 * [3 * [4 * [5 * [0.0]]]]), "(int64, 2 * 3 * ref(4 * 5 * float64))"),
83
+
84
+ # Optional tuples
85
+ (None, "?()"),
86
+ (None, "?(int8)"),
87
+ (None, "?(int8, int64)"),
88
+ (None, "?(uint16, (complex64))"),
89
+ (None, "?(uint16, (complex64), pack=1)"),
90
+ (None, "?(uint16, (complex64), pack=2)"),
91
+ (None, "?(uint16, (complex64), pack=4)"),
92
+ (None, "?(uint16, (complex64), pack=8)"),
93
+ (None, "?(uint16, (complex64), align=16)"),
94
+ (None, "?(0 * bytes)"),
95
+ (None, "?(0 * bytes, 0 * string)"),
96
+ (None, "?(1 * bytes, 2 * complex128, 3 * string)"),
97
+ (None, "?(1 * bytes, 2 * (complex128, 10 * 2 * string), 3 * string)"),
98
+
99
+ # Tuples with optional elements
100
+ ((None,), "(?int8)"),
101
+ ((None, 0), "(?int8, int64)"),
102
+ ((0, None), "(int8, ?int64)"),
103
+ ((None, None), "(?int8, ?int64)"),
104
+ (None, "?(?int8, ?int64)"),
105
+
106
+ ((0, None), "(uint16, ?(complex64))"),
107
+ ((0, (None,)), "(uint16, (?complex64))"),
108
+ ((0, None), "(uint16, ?(?complex64))"),
109
+
110
+ ((None, (0+0j,)), "(?uint16, (complex64), pack=1)"),
111
+ ((0, None), "(uint16, ?(complex64), pack=1)"),
112
+ ((0, (None,)), "(uint16, (?complex64), pack=1)"),
113
+
114
+ (([],), "(0 * ?bytes)"),
115
+ (([None],), "(1 * ?bytes)"),
116
+ ((10 * [None],), "(10 * ?bytes)"),
117
+ (([], []), "(0 * ?bytes, 0 * ?string)"),
118
+ ((5 * [None], 2 * [""]), "(5 * ?bytes, 2 * string)"),
119
+ ((5 * [b''], 2 * [None]), "(5 * bytes, 2 * ?string)"),
120
+ ((5 * [None], 2 * [None]), "(5 * ?bytes, 2 * ?string)"),
121
+
122
+ (([None], 2 * [None], 3 * [None]), "(1 * ?bytes, 2 * ?complex128, 3 * ?string)"),
123
+
124
+ (([None], 2 * [(0.0j, 10 * [2 * [""]])], 3 * [""]), "(1 * ?bytes, 2 * (complex128, 10 * 2 * string), 3 * string)"),
125
+ (([b''], 2 * [None], 3 * [""]), "(1 * bytes, 2 * ?(complex128, 10 * 2 * string), 3 * string)"),
126
+ (([b''], 2 * [(0.0j, 10 * [2 * [""]])], 3 * [None]), "(1 * bytes, 2 * (complex128, 10 * 2 * string), 3 * ?string)"),
127
+ (([None], 2 * [None], 3 * [""]), "(1 * ?bytes, 2 * ?(complex128, 10 * 2 * string), 3 * string)"),
128
+ (([None], 2 * [(0.0j, 10 * [2 * [""]])], 3 * [None]), "(1 * ?bytes, 2 * (complex128, 10 * 2 * string), 3 * ?string)"),
129
+ (([None], 2 * [None], 3 * [None]), "(1 * ?bytes, 2 * ?(complex128, 10 * 2 * string), 3 * ?string)"),
130
+
131
+ (([b''], 2 * [(0.0j, 10 * [2 * [None]])], 3 * [""]), "(1 * bytes, 2 * (complex128, 10 * 2 * ?string), 3 * string)"),
132
+ (([None], 2 * [(0.0j, 10 * [2 * [None]])], 3 * [""]), "(1 * ?bytes, 2 * (complex128, 10 * 2 * ?string), 3 * string)"),
133
+ (([None], 2 * [(None, 10 * [2 * [None]])], 3 * [""]), "(1 * ?bytes, 2 * (?complex128, 10 * 2 * ?string), 3 * string)"),
134
+ (([b''], 2 * [(None, 10 * [2 * [None]])], 3 * [None]), "(1 * bytes, 2 * (?complex128, 10 * 2 * ?string), 3 * ?string)"),
135
+
136
+ ((0, 2 * [3 * [4 * [5 * [None]]]]), "(int64, 2 * 3 * Some(4 * 5 * ?float64))"),
137
+ ((0, 2 * [3 * [4 * [5 * [None]]]]), "(int64, 2 * 3 * ref(4 * 5 * ?float64))"),
138
+
139
+ # Records
140
+ ({}, "{}"),
141
+ (R['x': 0], "{x: int8}"),
142
+ (R['x': 0, 'y': 0], "{x: int8, y: int64}"),
143
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}}"),
144
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}, pack=1}"),
145
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}, pack=2}"),
146
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}, pack=4}"),
147
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}, pack=8}"),
148
+ (R['x': 0, 'y': R['z': 0+0j]], "{x: uint16, y: {z: complex64}, align=16}"),
149
+ (R['x': []], "{x: 0 * bytes}"),
150
+ (R['x': [], 'y': []], "{x: 0 * bytes, y: 0 * string}"),
151
+ (R['x': [b''], 'y': 2 * [0.0j], 'z': 3 * [""]], "{x: 1 * bytes, y: 2 * complex128, z: 3 * string}"),
152
+ (R['x': [b''], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [""]]]], 'z': 3 * [""]], "{x: 1 * bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * string}"),
153
+ (R['x': 0, 'y': 2 * [3 * [4 * [5 * [0.0]]]]], "{x: int64, y: 2 * 3 * Some(4 * 5 * float64)}"),
154
+ (R['x': 0, 'y': 2 * [3 * [4 * [5 * [0.0]]]]], "{x: int64, y: 2 * 3 * ref(4 * 5 * float64)}"),
155
+
156
+ # Optional records
157
+ (None, "?{}"),
158
+ (None, "?{x: int8}"),
159
+ (None, "?{x: int8, y: int64}"),
160
+ (None, "?{x: uint16, y: {z: complex64}}"),
161
+ (None, "?{x: uint16, y: {z: complex64}, pack=1}"),
162
+ (None, "?{x: uint16, y: {z: complex64}, pack=2}"),
163
+ (None, "?{x: uint16, y: {z: complex64}, pack=4}"),
164
+ (None, "?{x: uint16, y: {z: complex64}, pack=8}"),
165
+ (None, "?{x: uint16, y: {z: complex64}, align=16}"),
166
+ (None, "?{x: 0 * bytes}"),
167
+ (None, "?{x: 0 * bytes, y: 0 * string}"),
168
+ (None, "?{x: 1 * bytes, y: 2 * complex128, z: 3 * string}"),
169
+ (None, "?{x: 1 * bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * string}"),
170
+
171
+ # Records with optional elements
172
+ (R['x': None], "{x: ?int8}"),
173
+ (R['x': None, 'y': 0], "{x: ?int8, y: int64}"),
174
+ (R['x': 0, 'y': None], "{x: int8, y: ?int64}"),
175
+ (R['x': None, 'y': None], "{x: ?int8, y: ?int64}"),
176
+ (None, "?{x: ?int8, y: ?int64}"),
177
+
178
+ (R['x': 0, 'y': None], " {x: uint16, y: ?{z: complex64}}"),
179
+ (R['x': 0, 'y': R['z': None]], "{x: uint16, y: {z: ?complex64}}"),
180
+ (R['x': 0, 'y': None], "{x: uint16, y: ?{z: ?complex64}}"),
181
+
182
+ (R['x': None, 'y': R['z': 0+0j]], "{x: ?uint16, y: {z: complex64}, pack=1}"),
183
+ (R['x': 0, 'y': None], "{x: uint16, y: ?{z: complex64}, pack=1}"),
184
+ (R['x': 0, 'y': R['z': None]], "{x: uint16, y: {z: ?complex64}, pack=1}"),
185
+
186
+ (R['x': []], "{x: 0 * ?bytes}"),
187
+ (R['x': 1 * [None]], "{x: 1 * ?bytes}"),
188
+ (R['x': 10 * [None]], "{x: 10 * ?bytes}"),
189
+ (R['x': [], 'y': []], "{x: 0 * ?bytes, y: 0 * ?string}"),
190
+ (R['x': 5 * [None], 'y': 2 * [""]], "{x: 5 * ?bytes, y: 2 * string}"),
191
+ (R['x': 5 * [b''], 'y': 2 * [None]], "{x: 5 * bytes, y: 2 * ?string}"),
192
+ (R['x': 5 * [None], 'y': 2 * [None]], "{x: 5 * ?bytes, y: 2 * ?string}"),
193
+
194
+ (R['x': [None], 'y': 2 * [None], 'z': 3 * [None]], "{x: 1 * ?bytes, y: 2 * ?complex128, z: 3 * ?string}"),
195
+ (R['x': [None], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [""]]]], 'z': 3 * [""]], "{x: 1 * ?bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * string}"),
196
+ (R['x': [b''], 'y': 2 * [None], 'z': 3 * [""]], "{x: 1 * bytes, y: 2 * ?{a: complex128, b: 10 * 2 * string}, z: 3 * string}"),
197
+ (R['x': [b''], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [""]]]], 'z': 3 * [None]], "{x: 1 * bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * ?string}"),
198
+ (R['x': [None], 'y': 2 * [None], 'z': 3 * [""]], "{x: 1 * ?bytes, y: 2 * ?{a: complex128, b: 10 * 2 * string}, z: 3 * string}"),
199
+ (R['x': [None], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [""]]]], 'z': 3 * [None]], "{x: 1 * ?bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * ?string}"),
200
+ (R['x': [None], 'y': 2 * [None], 'z': 3 * [None]], "{x: 1 * ?bytes, y: 2 * ?{a: complex128, b: 10 * 2 * string}, z: 3 * ?string}"),
201
+
202
+ (R['x': [b''], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [None]]]], 'z': 3 * [""]], "{x: 1 * bytes, y: 2 * {a: complex128, b: 10 * 2 * ?string}, z: 3 * string}"),
203
+ (R['x': [None], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * [None]]]], 'z': 3 * [""]], "{x: 1 * ?bytes, y: 2 * {a: complex128, b: 10 * 2 * ?string}, z: 3 * string}"),
204
+ (R['x': [None], 'y': 2 * [R['a': None, 'b': 10 * [2 * [None]]]], 'z': 3 * [""]], "{x: 1 * ?bytes, y: 2 * {a: ?complex128, b: 10 * 2 * ?string}, z: 3 * string}"),
205
+ (R['x': [b''], 'y': 2 * [R['a': None, 'b': 10 * [2 * [None]]]], 'z': 3 * [None]], "{x: 1 * bytes, y: 2 * {a: ?complex128, b: 10 * 2 * ?string}, z: 3 * ?string}"),
206
+
207
+ (R['x': 0, 'y': 2 * [3 * [4 * [5 * [None]]]]], "{x: int64, y: 2 * 3 * Some(4 * 5 * ?float64)}"),
208
+ (R['x': 0, 'y': 2 * [3 * [4 * [5 * [None]]]]], "{x: int64, y: 2 * 3 * ref(4 * 5 * ?float64)}"),
209
+
210
+ # Primitive types
211
+ (False, "bool"),
212
+ (0, "bool"),
213
+
214
+ (0, "int8"),
215
+ (0, "int16"),
216
+ (0, "int32"),
217
+ (0, "int64"),
218
+
219
+ (0, "uint8"),
220
+ (0, "uint16"),
221
+ (0, "uint32"),
222
+ (0, "uint64"),
223
+
224
+ (0.0, "float32"),
225
+ (0.0, "float64"),
226
+
227
+ (0+0j, "complex64"),
228
+ (0+0j, "complex128"),
229
+
230
+ (0+0j, "complex64"),
231
+ (0+0j, "complex128"),
232
+
233
+ # Optional primitive types
234
+ (None, "?bool"),
235
+
236
+ (None, "?int8"),
237
+ (None, "?int16"),
238
+ (None, "?int32"),
239
+ (None, "?int64"),
240
+
241
+ (None, "?uint8"),
242
+ (None, "?uint16"),
243
+ (None, "?uint32"),
244
+ (None, "?uint64"),
245
+
246
+ (None, "?float32"),
247
+ (None, "?float64"),
248
+
249
+ (None, "?complex64"),
250
+ (None, "?complex128"),
251
+
252
+ (None, "?complex64"),
253
+ (None, "?complex128"),
254
+
255
+ # References
256
+ (False, "&bool"),
257
+ (0, "&bool"),
258
+
259
+ (0, "&int8"),
260
+ (0, "&int16"),
261
+ (0, "&int32"),
262
+ (0, "&int64"),
263
+
264
+ (0, "ref(uint8)"),
265
+ (0, "ref(uint16)"),
266
+ (0, "ref(uint32)"),
267
+ (0, "ref(uint64)"),
268
+
269
+ (0, "ref(ref(uint8))"),
270
+ (0, "ref(ref(uint16))"),
271
+ (0, "ref(ref(uint32))"),
272
+ (0, "ref(ref(uint64))"),
273
+
274
+ (0.0, "ref(float32)"),
275
+ (0.0, "ref(float64)"),
276
+
277
+ (0+0j, "ref(complex64)"),
278
+ (0+0j, "ref(complex128)"),
279
+
280
+ ([], "ref(0 * bool)"),
281
+ ([0], "ref(1 * int16)"),
282
+ (2 * [0], "ref(2 * int32)"),
283
+ (2 * [3 * [0]], "ref(2 * 3 * int8)"),
284
+
285
+ ([], "ref(ref(0 * bool))"),
286
+ ([0], "ref(ref(1 * int16))"),
287
+ (2 * [0], "ref(ref(2 * int32))"),
288
+ (2 * [3 * [0]], "ref(ref(2 * 3 * int8))"),
289
+
290
+ ([], "ref(!0 * bool)"),
291
+ ([0], "ref(!1 * int16)"),
292
+ (2 * [0], "ref(!2 * int32)"),
293
+ (2 * [3 * [0]], "ref(!2 * 3 * int8)"),
294
+
295
+ ([], "ref(ref(!0 * bool))"),
296
+ ([0], "ref(ref(!1 * int16))"),
297
+ (2 * [0], "ref(ref(!2 * int32))"),
298
+ (2 * [3 * [0]], "ref(ref(!2 * 3 * int8))"),
299
+
300
+ # Optional references
301
+ (None, "?&bool"),
302
+
303
+ (None, "?&int8"),
304
+ (None, "?&int16"),
305
+ (None, "?&int32"),
306
+ (None, "?&int64"),
307
+
308
+ (None, "?ref(uint8)"),
309
+ (None, "?ref(uint16)"),
310
+ (None, "?ref(uint32)"),
311
+ (None, "?ref(uint64)"),
312
+
313
+ (None, "?ref(ref(uint8))"),
314
+ (None, "?ref(ref(uint16))"),
315
+ (None, "?ref(ref(uint32))"),
316
+ (None, "?ref(ref(uint64))"),
317
+
318
+ (None, "?ref(float32)"),
319
+ (None, "?ref(float64)"),
320
+
321
+ (None, "?ref(complex64)"),
322
+ (None, "?ref(complex128)"),
323
+
324
+ (None, "?ref(0 * bool)"),
325
+ (None, "?ref(1 * int16)"),
326
+ (None, "?ref(2 * int32)"),
327
+ (None, "?ref(2 * 3 * int8)"),
328
+
329
+ (None, "?ref(ref(0 * bool))"),
330
+ (None, "ref(?ref(0 * bool))"),
331
+ (None, "?ref(?ref(0 * bool))"),
332
+ (None, "?ref(ref(1 * int16))"),
333
+ (None, "ref(?ref(1 * int16))"),
334
+ (None, "?ref(?ref(1 * int16))"),
335
+ (None, "?ref(ref(2 * int32))"),
336
+
337
+ (None, "?ref(!2 * 3 * int8)"),
338
+ (None, "?ref(ref(!2 * 3 * int32))"),
339
+
340
+ # References to types with optional data
341
+ (None, "&?bool"),
342
+
343
+ (None, "&?int8"),
344
+ (None, "&?int16"),
345
+ (None, "&?int32"),
346
+ (None, "&?int64"),
347
+
348
+ (None, "ref(?uint8)"),
349
+ (None, "ref(?uint16)"),
350
+ (None, "ref(?uint32)"),
351
+ (None, "ref(?uint64)"),
352
+
353
+ (None, "ref(ref(?uint8))"),
354
+ (None, "ref(ref(?uint16))"),
355
+ (None, "ref(ref(?uint32))"),
356
+ (None, "ref(ref(?uint64))"),
357
+
358
+ (None, "ref(?float32)"),
359
+ (None, "ref(?float64)"),
360
+
361
+ (None, "ref(?complex64)"),
362
+ (None, "ref(?complex128)"),
363
+
364
+ ([], "ref(0 * ?bool)"),
365
+ ([None], "ref(1 * ?int16)"),
366
+ (2 * [None], "ref(2 * ?int32)"),
367
+ (2 * [3 * [None]], "ref(2 * 3 * ?int8)"),
368
+
369
+ ([], "ref(ref(0 * ?bool))"),
370
+ ([None], "ref(ref(1 * ?int16))"),
371
+ (2 * [None], "ref(ref(2 * ?int32))"),
372
+
373
+ (2 * [3 * [None]], "ref(!2 * 3 * ?int8)"),
374
+ (2 * [3 * [None]], "ref(ref(!2 * 3 * ?int8))"),
375
+
376
+ # Constructors
377
+ (False, "Some(bool)"),
378
+ (0, "Some(bool)"),
379
+
380
+ (0, "Some(int8)"),
381
+ (0, "Some(int16)"),
382
+ (0, "Some(int32)"),
383
+ (0, "Some(int64)"),
384
+
385
+ (0, "Some(uint8)"),
386
+ (0, "Some(uint16)"),
387
+ (0, "Some(uint32)"),
388
+ (0, "Some(uint64)"),
389
+
390
+ (0.0, "Some(float32)"),
391
+ (0.0, "Some(float64)"),
392
+
393
+ (0+0j, "Some(complex64)"),
394
+ (0+0j, "Some(complex128)"),
395
+
396
+ ([0], "ThisGuy(1 * int16)"),
397
+ (2 * [0], "ThisGuy(2 * int32)"),
398
+ (2 * [3 * [0.0]], "ThisGuy(2 * 3 * float32)"),
399
+
400
+ (2 * [3 * [0.0]], "ThisGuy(!2 * 3 * float32)"),
401
+
402
+ # Optional constructors
403
+ (None, "?Some(bool)"),
404
+
405
+ (None, "?Some(int8)"),
406
+ (None, "?Some(int16)"),
407
+ (None, "?Some(int32)"),
408
+ (None, "?Some(int64)"),
409
+
410
+ (None, "?Some(uint8)"),
411
+ (None, "?Some(uint16)"),
412
+ (None, "?Some(uint32)"),
413
+ (None, "?Some(uint64)"),
414
+
415
+ (None, "?Some(float32)"),
416
+ (None, "?Some(float64)"),
417
+
418
+ (None, "?Some(complex64)"),
419
+ (None, "?Some(complex128)"),
420
+
421
+ (None, "?ThisGuy(0 * int16)"),
422
+ (None, "?ThisGuy(1 * int16)"),
423
+ (None, "?ThisGuy(2 * int32)"),
424
+ (None, "?ThisGuy(2 * 3 * float32)"),
425
+
426
+ (None, "?ThisGuy(!2 * 3 * float32)"),
427
+
428
+ # Constructors with an optional data type argument
429
+ (None, "Some(?bool)"),
430
+
431
+ (None, "Some(?int8)"),
432
+ (None, "Some(?int16)"),
433
+ (None, "Some(?int32)"),
434
+ (None, "Some(?int64)"),
435
+
436
+ (None, "Some(?uint8)"),
437
+ (None, "Some(?uint16)"),
438
+ (None, "Some(?uint32)"),
439
+ (None, "Some(?uint64)"),
440
+
441
+ (None, "Some(?float32)"),
442
+ (None, "Some(?float64)"),
443
+
444
+ (None, "Some(?complex64)"),
445
+ (None, "Some(?complex128)"),
446
+
447
+ ([], "ThisGuy(0 * ?int16)"),
448
+ ([None], "ThisGuy(1 * ?int16)"),
449
+ (2 * [None], "ThisGuy(2 * ?int32)"),
450
+ (2 * [3 * [None]], "ThisGuy(2 * 3 * ?float32)"),
451
+
452
+ (2 * [3 * [None]], "ThisGuy(!2 * 3 * ?float32)"),
453
+ ]
454
+
455
+ #
456
+ # Test case for richcompare:
457
+ # v: value
458
+ # t: type of v
459
+ # u: equivalent type for v
460
+ # w: value different from v
461
+ # eq: expected comparison result
462
+ #
463
+ T = namedtuple('T', 'v t u w eq')
464
+
465
+ EQUAL_TEST_CASES = [
466
+ # Tuples
467
+ T(v=(),
468
+ t="()",
469
+ u=None,
470
+ w=None,
471
+ eq=True),
472
+
473
+ T(v=(100,),
474
+ t="(int8)",
475
+ u="?(int8)",
476
+ w=(101,),
477
+ eq=True),
478
+
479
+ T(v=(2**7-1, 2**63-1),
480
+ t="(int8, int64)",
481
+ u="(int8, ?int64) ",
482
+ w=(2**7-2, 2**63-1),
483
+ eq=True),
484
+
485
+ T(v=(2**16-1, (1.2312222+28j,)),
486
+ t="(uint16, (complex64))",
487
+ u="(uint16, ?(complex64))",
488
+ w=(2**16-1, (1.23122+28j,)),
489
+ eq=True),
490
+
491
+ T(v=(1, (1e22+2j,)),
492
+ t="(uint32, (complex64), pack=1)",
493
+ u="(uint32, (complex64), align=16)",
494
+ w=(1, (1e22+3j,)),
495
+ eq=True),
496
+
497
+ T(v=([],),
498
+ t="(0 * bytes)",
499
+ u=None,
500
+ w=None,
501
+ eq=True),
502
+
503
+ T(v=([], []),
504
+ t="(0 * bytes, 0 * string)",
505
+ u=None,
506
+ w=None,
507
+ eq=True),
508
+
509
+ T(v=([b'x'], 2 * [1.2j], 3 * ["abc"]),
510
+ t="(1 * bytes, 2 * complex128, 3 * string)",
511
+ u="(1 * bytes, 2 * ?complex128, 3 * string)",
512
+ w=([b'x'], 2 * [1.2j], 3 * ["ab"]),
513
+ eq=True),
514
+
515
+ T(v=([b'123'], 2 * [(3e22+0.2j, 10 * [2 * ["xyz"]])], 3 * ["1"]),
516
+ t="(1 * bytes, 2 * (complex128, 10 * 2 * string), 3 * string)",
517
+ u="(1 * bytes, 2 * ?(complex128, 10 * 2 * string), 3 * string)",
518
+ w=([b'1234'], 2 * [(3e22+0.2j, 10 * [2 * ["xyz"]])], 3 * ["1"]),
519
+ eq=True),
520
+
521
+ T(v=(10001, 2 * [3 * [4 * [5 * [2.250]]]]),
522
+ t="(int64, 2 * 3 * Some(4 * 5 * float64))",
523
+ u="(int64, 2 * 3 * Some(4 * 5 * ?float64))",
524
+ w=(10001, 2 * [3 * [4 * [5 * [2.251]]]]),
525
+ eq=True),
526
+
527
+ T(v=(-2**63, 2 * [3 * [4 * [5 * [10.1]]]]),
528
+ t="(int64, 2 * 3 * ref(4 * 5 * float64))",
529
+ u="(int64, 2 * 3 * ?ref(4 * 5 * float64))",
530
+ w=(-2**63+1, 2 * [3 * [4 * [5 * [10.1]]]]),
531
+ eq=True),
532
+
533
+ # Optional tuples
534
+ T(v=None,
535
+ t="?()",
536
+ u=None,
537
+ w=None,
538
+ eq=False),
539
+
540
+ T(v=None,
541
+ t="?(int8)",
542
+ u=None,
543
+ w=None,
544
+ eq=False),
545
+
546
+ T(v=None,
547
+ t="?(1 * bytes, 2 * (complex128, 10 * 2 * string), 3 * string)",
548
+ u=None,
549
+ w=None,
550
+ eq=False),
551
+
552
+ # Tuples with optional elements
553
+ T(v=(None,),
554
+ t="(?int8)",
555
+ u=None,
556
+ w=None,
557
+ eq=False),
558
+
559
+ T(v=(None, 0),
560
+ t="(?int8, int64)",
561
+ u=None,
562
+ w=None,
563
+ eq=False),
564
+
565
+ T(v=(0, None),
566
+ t="(int8, ?int64)",
567
+ u=None,
568
+ w=None,
569
+ eq=False),
570
+
571
+ T(v=(None, None),
572
+ t="(?int8, ?int64)",
573
+ u=None,
574
+ w=None,
575
+ eq=False),
576
+
577
+ T(v=None,
578
+ t="?(?int8, ?int64)",
579
+ u=None,
580
+ w=None,
581
+ eq=False),
582
+
583
+ T(v=(0, None),
584
+ t="(uint16, ?(complex64))",
585
+ u=None,
586
+ w=None,
587
+ eq=False),
588
+
589
+ T(v=(0, (None,)),
590
+ t="(uint16, (?complex64))",
591
+ u=None,
592
+ w=None,
593
+ eq=False),
594
+
595
+ T(v=(0, None),
596
+ t="(uint16, ?(?complex64))",
597
+ u=None,
598
+ w=None,
599
+ eq=False),
600
+
601
+ T(v=(None, (0+0j,)),
602
+ t="(?uint16, (complex64), pack=1)",
603
+ u=None,
604
+ w=None,
605
+ eq=False),
606
+
607
+ T(v=(0, None),
608
+ t="(uint16, ?(complex64), pack=1)",
609
+ u=None,
610
+ w=None,
611
+ eq=False),
612
+
613
+ T(v=(0, (None,)),
614
+ t="(uint16, (?complex64), pack=1)",
615
+ u=None,
616
+ w=None,
617
+ eq=False),
618
+
619
+ # Records
620
+ T(v={},
621
+ t="{}",
622
+ u=None,
623
+ w=None,
624
+ eq=True),
625
+
626
+ T(v=R['x': 2**31-1],
627
+ t="{x: int32}",
628
+ u="{x: ?int32}",
629
+ w=R['x': 2**31-2],
630
+ eq=True),
631
+
632
+ T(v=R['x': -128, 'y': -1],
633
+ t="{x: int8, y: int64}",
634
+ u="{x: int8, y: int64, pack=1}",
635
+ w=R['x': -127, 'y': -1],
636
+ eq=True),
637
+
638
+ T(v=R['x': 2**32-1, 'y': R['z': 10000001e3+36.1e7j]],
639
+ t="{x: uint32, y: {z: complex64}}",
640
+ u="{x: uint32, y: {z: complex64}}",
641
+ w=R['x': 2**32-2, 'y': R['z': 10000001e3+36.1e7j]],
642
+ eq=True),
643
+
644
+ T(v=R['x': 255, 'y': R['z': 2+3j]],
645
+ t="{x: uint8, y: {z: complex64}, pack=1}",
646
+ u="{x: uint8, y: {z: complex64}, pack=4}",
647
+ w=R['x': 255, 'y': R['z': 3+3j]],
648
+ eq=True),
649
+
650
+ T(v=R['x': 10001, 'y': R['z': "abc"]],
651
+ t="{x: uint16, y: {z: fixed_string(100)}, pack=2}",
652
+ u="{x: uint16, y: {z: fixed_string(100)}, align=8}",
653
+ w=R['x': 10001, 'y': R['z': "abcd"]],
654
+ eq=True),
655
+
656
+ T(v=R['x': []],
657
+ t="{x: 0 * bytes}",
658
+ u=None,
659
+ w=None,
660
+ eq=True),
661
+
662
+ T(v=R['x': [], 'y': []],
663
+ t="{x: 0 * bytes, y: 0 * string}",
664
+ u=None,
665
+ w=None,
666
+ eq=True),
667
+
668
+ T(v=R['x': [b''], 'y': 2 * [2.0j], 'z': 3 * ["y"]],
669
+ t="{x: 1 * fixed_bytes(size=512), y: 2 * complex128, z: 3 * string}",
670
+ u="{x: 1 * fixed_bytes(size=512, align=256), y: 2 * complex128, z: 3 * string}",
671
+ w=None,
672
+ eq=True),
673
+
674
+ T(v=R['x': 100, 'y': 2 * [3 * [4 * [5 * [301.0]]]]],
675
+ t="{x: int64, y: 2 * 3 * Some(4 * 5 * ?float64)}",
676
+ u="{x: int64, y: 2 * 3 * ?Some(4 * 5 * ?float64)}",
677
+ w=R['x': 100, 'y': 2 * [3 * [4 * [5 * [None]]]]],
678
+ eq=True),
679
+
680
+ # Optional records
681
+ T(v=None,
682
+ t="?{}",
683
+ u="?{}",
684
+ w=None,
685
+ eq=False),
686
+
687
+ T(v=None,
688
+ t="?{x: int8}",
689
+ u="?{x: int8}",
690
+ w=None,
691
+ eq=False),
692
+
693
+ T(v=R['x': 101],
694
+ t="?{x: int8}",
695
+ u="{x: int8}",
696
+ w=R['x': 100],
697
+ eq=True),
698
+
699
+ T(v=None,
700
+ t="?{x: 0 * bytes}",
701
+ u="?{x: 0 * bytes}",
702
+ w=None,
703
+ eq=False),
704
+
705
+ T(v=None,
706
+ t="?{x: 0 * bytes, y: 0 * string}",
707
+ u="?{x: 0 * bytes, y: 0 * string}",
708
+ w=None,
709
+ eq=False),
710
+
711
+ # Records with optional elements
712
+ T(v=R['x': None],
713
+ t="{x: ?int8}",
714
+ u="{x: ?int8}",
715
+ w=None,
716
+ eq=False),
717
+
718
+ T(v=R['x': None, 'y': 0],
719
+ t="{x: ?int8, y: int64}",
720
+ u="{x: ?int8, y: int64}",
721
+ w=R['x': 0, 'y': 0],
722
+ eq=False),
723
+
724
+ T(v=R['x': 100, 'y': None],
725
+ t="{x: int8, y: ?int64}",
726
+ u="{x: int8, y: ?int64}",
727
+ w=R['x': 100, 'y': 10],
728
+ eq=False),
729
+
730
+ T(v=R['x': None, 'y': None],
731
+ t="{x: ?int8, y: ?int64}",
732
+ u="{x: ?int8, y: ?int64}",
733
+ w=R['x': 1, 'y': 1],
734
+ eq=False),
735
+
736
+ T(v=None,
737
+ t="?{x: ?int8, y: ?int64}",
738
+ u="?{x: ?int8, y: ?int64}",
739
+ w=None,
740
+ eq=False),
741
+
742
+ T(v=R['x': 0, 'y': None],
743
+ t="{x: uint16, y: ?{z: complex64}}",
744
+ u="{x: uint16, y: ?{z: complex64}}",
745
+ w=R['x': 1, 'y': None],
746
+ eq=False),
747
+
748
+ T(v=R['x': 0, 'y': R['z': None]],
749
+ t="{x: uint16, y: {z: ?complex64}}",
750
+ u="{x: uint16, y: {z: ?complex64}}",
751
+ w=R['x': 0, 'y': R['z': 2]],
752
+ eq=False),
753
+
754
+ T(v=R['x': 0, 'y': None],
755
+ t="{x: uint16, y: ?{z: ?complex64}}",
756
+ u="{x: uint16, y: ?{z: ?complex64}}",
757
+ w=R['x': 0, 'y': R['z': 1+10j]],
758
+ eq=False),
759
+
760
+ T(v=R['x': None, 'y': R['z': 0+0j]],
761
+ t="{x: ?uint16, y: {z: complex64}, pack=1}",
762
+ u="{x: ?uint16, y: {z: complex64}, align=16}",
763
+ w=R['x': 256, 'y': R['z': 0+0j]],
764
+ eq=False),
765
+
766
+ T(v=R['x': 0, 'y': None],
767
+ t="{x: uint16, y: ?{z: complex64}, pack=1}",
768
+ u="{x: uint16, y: ?{z: complex64}, pack=1}",
769
+ w=R['x': 0, 'y': R['z': 0+1j]],
770
+ eq=False),
771
+
772
+ T(v=R['x': 0, 'y': R['z': None]],
773
+ t="{x: ?uint16, y: {z: ?complex64}, pack=1}",
774
+ u="{x: ?uint16, y: {z: ?complex64}, pack=1}",
775
+ w=R['x': None, 'y': R['z': None]],
776
+ eq=False),
777
+
778
+ T(v=R['x': []],
779
+ t="{x: 0 * ?bytes}",
780
+ u="{x: 0 * ?bytes}",
781
+ w=None,
782
+ eq=True),
783
+
784
+ T(v=R['x': 1 * [None]],
785
+ t="{x: 1 * ?bytes}",
786
+ u="{x: 1 * ?bytes}",
787
+ w=None,
788
+ eq=False),
789
+
790
+ T(v=R['x': 10 * [None]],
791
+ t="{x: 10 * ?bytes}",
792
+ u="{x: 10 * ?bytes}",
793
+ w=R['x': 10 * [b"123"]],
794
+ eq=False),
795
+
796
+ T(v=R['x': [], 'y': []],
797
+ t="{x: 0 * ?bytes, y: 0 * ?string}",
798
+ u="{x: 0 * ?bytes, y: 0 * ?string}",
799
+ w=None,
800
+ eq=True),
801
+
802
+ T(v=R['x': 5 * [b"123"], 'y': 2 * ["abc"]],
803
+ t="{x: 5 * ?bytes, y: 2 * string}",
804
+ u="{x: 5 * bytes, y: 2 * string}",
805
+ w=R['x': 5 * [b"12345"], 'y': 2 * ["abc"]],
806
+ eq=True),
807
+
808
+ T(v=R['x': [b"-123"], 'y': 2 * [1e200+10j], 'z': 3 * [100*"t"]],
809
+ t="{x: 1 * ?bytes, y: 2 * ?complex128, z: 3 * ?string}",
810
+ u="{x: 1 * ?bytes, y: 2 * ?complex128, z: 3 * ?string}",
811
+ w=R['x': [b"-123"], 'y': 2 * [1e200+10j], 'z': 3 * [100*"t" + "u"]],
812
+ eq=True),
813
+
814
+ T(v=R['x': [b"x"], 'y': 2 * [R['a': 0.0j, 'b': 10 * [2 * ["c"]]]], 'z': 3 * ["a"]],
815
+ t="{x: 1 * ?bytes, y: 2 * {a: complex128, b: 10 * 2 * string}, z: 3 * string}",
816
+ u="{x: 1 * ?bytes, y: 2 * {a: complex128, b: 10 * 2 * ?string}, z: 3 * string}",
817
+ w=R['x': [b"x"], 'y': 2 * [R['a': 1.0j, 'b': 10 * [2 * ["c"]]]], 'z': 3 * ["a"]],
818
+ eq=True),
819
+
820
+ # Primitive types
821
+ T(v=False, t="bool", u="?bool", w=True, eq=True),
822
+ T(v=0, t="?bool", u="bool", w=1, eq=True),
823
+
824
+ T(v=127, t="int8", u="?int8", w=100, eq=True),
825
+ T(v=127, t="?int8", u="int8", w=100, eq=True),
826
+
827
+ T(v=-127, t="int16", u="?int16", w=100, eq=True),
828
+ T(v=-127, t="?int16", u="int16", w=100, eq=True),
829
+
830
+ T(v=127, t="int32", u="?int32", w=100, eq=True),
831
+ T(v=127, t="?int32", u="int32", w=100, eq=True),
832
+
833
+ T(v=127, t="int64", u="?int64", w=100, eq=True),
834
+ T(v=127, t="?int64", u="int64", w=100, eq=True),
835
+
836
+ T(v=127, t="uint8", u="?uint8", w=100, eq=True),
837
+ T(v=127, t="?uint8", u="uint8", w=100, eq=True),
838
+
839
+ T(v=127, t="uint16", u="?uint16", w=100, eq=True),
840
+ T(v=127, t="?uint16", u="uint16", w=100, eq=True),
841
+
842
+ T(v=127, t="uint32", u="?uint32", w=100, eq=True),
843
+ T(v=127, t="?uint32", u="uint32", w=100, eq=True),
844
+
845
+ T(v=127, t="uint64", u="?uint64", w=100, eq=True),
846
+ T(v=127, t="?uint64", u="uint64", w=100, eq=True),
847
+
848
+ T(v=1.122e11, t="float32", u="?float32", w=2.111, eq=True),
849
+ T(v=1.233e10, t="?float32", u="float32", w=3.111, eq=True),
850
+
851
+ T(v=1.122e11, t="float64", u="?float64", w=2.111, eq=True),
852
+ T(v=1.233e10, t="?float64", u="float64", w=3.111, eq=True),
853
+
854
+ T(v=1.122e11+1j, t="complex64", u="?complex64", w=1.122e11+2j, eq=True),
855
+ T(v=1.122e11+1j, t="?complex64", u="complex64", w=1.1e11+1j, eq=True),
856
+
857
+ T(v=1.122e11-100j, t="complex128", u="?complex128", w=1.122e11-101j, eq=True),
858
+ T(v=1.122e11-100j, t="?complex128", u="complex128", w=1.122e10-100j, eq=True),
859
+ ]
860
+
861
+
862
+ # ======================================================================
863
+ # Definition of generalized slicing and indexing
864
+ # ======================================================================
865
+
866
+ def maxlevel(lst):
867
+ """Return maximum nesting depth"""
868
+ maxlev = 0
869
+ def f(lst, level):
870
+ nonlocal maxlev
871
+ if isinstance(lst, list):
872
+ level += 1
873
+ maxlev = max(level, maxlev)
874
+ for item in lst:
875
+ f(item, level)
876
+ f(lst, 0)
877
+ return maxlev
878
+
879
+ def getitem(lst, indices):
880
+ """Definition for multidimensional slicing and indexing on arbitrarily
881
+ shaped nested lists.
882
+ """
883
+ if not indices:
884
+ return lst
885
+
886
+ i, indices = indices[0], indices[1:]
887
+ item = list.__getitem__(lst, i)
888
+
889
+ if isinstance(i, int):
890
+ return getitem(item, indices)
891
+
892
+ if not item:
893
+ # Empty slice: check if all subsequent indices are in range for the
894
+ # full slice, raise IndexError otherwise. This is NumPy's behavior.
895
+ _ = [getitem(x, indices) for x in lst]
896
+ return []
897
+
898
+ return [getitem(x, indices) for x in item]
899
+
900
+ class NDArray(list):
901
+ """A simple wrapper for using generalized slicing/indexing on a list."""
902
+ def __init__(self, value):
903
+ list.__init__(self, value)
904
+ self.maxlevel = maxlevel(value)
905
+
906
+ def __getitem__(self, indices):
907
+ if not isinstance(indices, tuple):
908
+ indices = (indices,)
909
+
910
+ if len(indices) > self.maxlevel: # NumPy
911
+ raise IndexError("too many indices")
912
+
913
+ if not all(isinstance(i, (int, slice)) for i in indices):
914
+ raise TypeError(
915
+ "index must be int or slice or a tuple of integers and slices")
916
+
917
+ result = getitem(self, indices)
918
+ return NDArray(result) if isinstance(result, list) else result
919
+
920
+
921
+ # ======================================================================
922
+ # Generate test cases
923
+ # ======================================================================
924
+
925
+ SUBSCRIPT_FIXED_TEST_CASES = [
926
+ [],
927
+ [[]],
928
+ [[], []],
929
+ [[0], [1]],
930
+ [[0], [1], [2]],
931
+ [[0, 1], [1, 2], [2 ,3]],
932
+ [[[]]],
933
+ [[[0]]],
934
+ [[[], []]],
935
+ [[[0], [1]]],
936
+ [[[0, 1], [2, 3]]],
937
+ [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
938
+ [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
939
+ ]
940
+
941
+ SUBSCRIPT_VAR_TEST_CASES = [
942
+ [[[0, 1], [2, 3]], [[4, 5, 6], [7]], [[8, 9]]],
943
+ [[[0, 1], [2, 3]], [[4, 5, None], [None], [7]], [[], [None, 8]], [[9, 10]]],
944
+ [[[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10]], [[11, 12, 13, 14], [15, 16, 17], [18, 19]]],
945
+ [[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]], [[10, 11]]]
946
+ ]
947
+
948
+ def single_fixed(max_ndim=4, min_shape=1, max_shape=10):
949
+ nat = count()
950
+ shape = [randrange(min_shape, max_shape+1) for _ in range(max_ndim)]
951
+
952
+ def f(ndim):
953
+ if ndim == 0:
954
+ return next(nat)
955
+ return [f(ndim-1) for _ in range(shape[ndim-1])]
956
+
957
+ return f(max_ndim)
958
+
959
+ def gen_fixed(max_ndim=4, min_shape=1, max_shape=10):
960
+ assert max_ndim >=0 and min_shape >=0 and min_shape <= max_shape
961
+
962
+ for _ in range(30):
963
+ yield single_fixed(max_ndim, min_shape, max_shape)
964
+
965
+ def single_var(max_ndim=4, min_shape=1, max_shape=10):
966
+ nat = count()
967
+
968
+ def f(ndim):
969
+ if ndim == 0:
970
+ return next(nat)
971
+ if ndim == 1:
972
+ shape = randrange(min_shape, max_shape+1)
973
+ else:
974
+ n = 1 if min_shape == 0 else min_shape
975
+ shape = randrange(n, max_shape+1)
976
+ return [f(ndim-1) for _ in range(shape)]
977
+
978
+ return f(max_ndim)
979
+
980
+ def gen_var(max_ndim=4, min_shape=1, max_shape=10):
981
+ assert max_ndim >=0 and min_shape >=0 and min_shape <= max_shape
982
+
983
+ for _ in range(30):
984
+ yield single_var(max_ndim, min_shape, max_shape)
985
+
986
+
987
+ def genindices():
988
+ for i in range(4):
989
+ yield (i,)
990
+ for i in range(4):
991
+ for j in range(4):
992
+ yield (i, j)
993
+ for i in range(4):
994
+ for j in range(4):
995
+ for k in range(4):
996
+ yield (i, j, k)
997
+
998
+ def rslice(ndim):
999
+ start = randrange(0, ndim+1)
1000
+ stop = randrange(0, ndim+1)
1001
+ step = 0
1002
+ while step == 0:
1003
+ step = randrange(-ndim-1, ndim+1)
1004
+ start = None if randrange(5) == 4 else start
1005
+ stop = None if randrange(5) == 4 else stop
1006
+ step = None if randrange(5) == 4 else step
1007
+ return slice(start, stop, step)
1008
+
1009
+ def multislice(ndim):
1010
+ return tuple(rslice(ndim) for _ in range(randrange(1, ndim+1)))
1011
+
1012
+ def randslices(ndim):
1013
+ for i in range(5):
1014
+ yield multislice(ndim)
1015
+
1016
+ def gen_indices_or_slices():
1017
+ for i in range(5):
1018
+ if randrange(2):
1019
+ yield (randrange(4), randrange(4), randrange(4))
1020
+ else:
1021
+ yield multislice(3)
1022
+
1023
+ def genslices(n):
1024
+ """Generate all possible slices for a single dimension."""
1025
+ def range_with_none():
1026
+ yield None
1027
+ yield from range(-n, n+1)
1028
+
1029
+ for t in product(range_with_none(), range_with_none(), range_with_none()):
1030
+ s = slice(*t)
1031
+ if s.step != 0:
1032
+ yield s
1033
+
1034
+ def genslices_ndim(ndim, shape):
1035
+ """Generate all possible slice tuples for 'shape'."""
1036
+ iterables = [genslices(shape[n]) for n in range(ndim)]
1037
+ yield from product(*iterables)
1038
+
1039
+ def mixed_index(max_ndim):
1040
+ ndim = randrange(1, max_ndim+1)
1041
+ indices = []
1042
+ for i in range(1, ndim+1):
1043
+ if randrange(2):
1044
+ indices.append(randrange(max_ndim))
1045
+ else:
1046
+ indices.append(rslice(ndim))
1047
+ return tuple(indices)
1048
+
1049
+ def mixed_indices(max_ndim):
1050
+ for i in range(5):
1051
+ yield mixed_index(max_ndim)
1052
+
1053
+ def itos(indices):
1054
+ return ", ".join(str(i) if isinstance(i, int) else "%s:%s:%s" %
1055
+ (i.start, i.stop, i.step) for i in indices)
1056
+
1057
+
1058
+ # ======================================================================
1059
+ # Split a shape into N almost equal slices
1060
+ # ======================================================================
1061
+
1062
+ def start(i, r, q):
1063
+ return i*(q+1) if i < r else r+i*q
1064
+
1065
+ def stop(i, r, q):
1066
+ return (i+1)*(q+1) if i < r else r+(i+1)*q
1067
+
1068
+ def step(i, r, q):
1069
+ return q+1 if i < r else q
1070
+
1071
+ def sl(i, r, q):
1072
+ return slice(start(i, r, q), stop(i, r, q))
1073
+
1074
+ def prepend(x, xs):
1075
+ return [(x,) + t for t in xs]
1076
+
1077
+ def last_column(i, r, q, n):
1078
+ return [(sl(i, r, q),) for i in range(n)]
1079
+
1080
+ def schedule(n, shape):
1081
+ assert isinstance(n, int) and isinstance(shape, list)
1082
+ if (n <= 0):
1083
+ raise ValueError("n must be greater than zero")
1084
+ if shape == []:
1085
+ return [()]
1086
+ m, ms = shape[0], shape[1:]
1087
+ if (m <= 0):
1088
+ raise ValueError("shape must be greater than zero")
1089
+ if n <= m:
1090
+ q, r = divmod(m, n)
1091
+ return last_column(0, r, q, n)
1092
+ else:
1093
+ q, r = divmod(n, m)
1094
+ return column(0, r, q, m, ms)
1095
+
1096
+ def column(i, r, q, m, ms):
1097
+ if i == m: return []
1098
+ return prepend(slice(i, i+1),
1099
+ schedule(step(i, r, q), ms)) + \
1100
+ column(i+1, r, q, m, ms)
1101
+
1102
+ # ======================================================================
1103
+ # Split an xnd object into N subtrees
1104
+ # ======================================================================
1105
+
1106
+ def zero_in_shape(shape):
1107
+ for i in shape:
1108
+ if i == 0:
1109
+ return True
1110
+ return False
1111
+
1112
+ def split_xnd(x, n, max_outer=None):
1113
+ shape = list(x.type.shape)
1114
+ if zero_in_shape(shape):
1115
+ raise ValueError("split does not support zeros in shape")
1116
+ if max_outer is not None:
1117
+ shape = shape[:max_outer]
1118
+ indices_list = schedule(n, shape)
1119
+ return [x[i] for i in indices_list]
1120
+
1121
+