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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
require "-test-/memory_view"
require "rbconfig/sizeof"
class TestMemoryView < Test::Unit::TestCase
NATIVE_ENDIAN = MemoryViewTestUtils::NATIVE_ENDIAN
LITTLE_ENDIAN = :little_endian
BIG_ENDIAN = :big_endian
%I(SHORT INT INT16 INT32 INT64 INTPTR LONG LONG_LONG FLOAT DOUBLE).each do |type|
name = :"#{type}_ALIGNMENT"
const_set(name, MemoryViewTestUtils.const_get(name))
end
def test_rb_memory_view_register_duplicated
assert_warning(/Duplicated registration of memory view to/) do
MemoryViewTestUtils.register(MemoryViewTestUtils::ExportableString)
end
end
def test_rb_memory_view_register_nonclass
assert_raise(TypeError) do
MemoryViewTestUtils.register(Object.new)
end
end
def sizeof(type)
RbConfig::SIZEOF[type.to_s]
end
def test_rb_memory_view_item_size_from_format
[
[nil, 1], ['c', 1], ['C', 1],
['n', 2], ['v', 2],
['l', 4], ['L', 4], ['N', 4], ['V', 4], ['f', 4], ['e', 4], ['g', 4],
['q', 8], ['Q', 8], ['d', 8], ['E', 8], ['G', 8],
['s', sizeof(:short)], ['S', sizeof(:short)], ['s!', sizeof(:short)], ['S!', sizeof(:short)],
['i', sizeof(:int)], ['I', sizeof(:int)], ['i!', sizeof(:int)], ['I!', sizeof(:int)],
['l!', sizeof(:long)], ['L!', sizeof(:long)],
['q!', sizeof('long long')], ['Q!', sizeof('long long')],
['j', sizeof(:intptr_t)], ['J', sizeof(:intptr_t)],
].each do |format, expected|
actual, err = MemoryViewTestUtils.item_size_from_format(format)
assert_nil(err)
assert_equal(expected, actual, "rb_memory_view_item_size_from_format(#{format || 'NULL'}) == #{expected}")
end
end
def test_rb_memory_view_item_size_from_format_composed
actual, = MemoryViewTestUtils.item_size_from_format("ccc")
assert_equal(3, actual)
actual, = MemoryViewTestUtils.item_size_from_format("c3")
assert_equal(3, actual)
actual, = MemoryViewTestUtils.item_size_from_format("fd")
assert_equal(12, actual)
actual, = MemoryViewTestUtils.item_size_from_format("fx2d")
assert_equal(14, actual)
end
def test_rb_memory_view_item_size_from_format_with_spaces
# spaces should be ignored
actual, = MemoryViewTestUtils.item_size_from_format("f x2 d")
assert_equal(14, actual)
end
def test_rb_memory_view_item_size_from_format_error
assert_equal([-1, "a"], MemoryViewTestUtils.item_size_from_format("ccca"))
assert_equal([-1, "a"], MemoryViewTestUtils.item_size_from_format("ccc4a"))
end
def test_rb_memory_view_parse_item_format
total_size, members, err = MemoryViewTestUtils.parse_item_format("ccc2f3x2d4q!<")
assert_equal(58, total_size)
assert_nil(err)
assert_equal([
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 0, size: 1, repeat: 1},
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 1, size: 1, repeat: 1},
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 2, size: 1, repeat: 2},
{format: 'f', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 4, size: 4, repeat: 3},
{format: 'd', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 18, size: 8, repeat: 4},
{format: 'q', native_size_p: true, endianness: :little_endian, offset: 50, size: sizeof('long long'), repeat: 1}
],
members)
end
def test_rb_memory_view_parse_item_format_with_alignment_signle
[
["c", false, NATIVE_ENDIAN, 1, 1, 1],
["C", false, NATIVE_ENDIAN, 1, 1, 1],
["s", false, NATIVE_ENDIAN, SHORT_ALIGNMENT, sizeof(:short), 1],
["S", false, NATIVE_ENDIAN, SHORT_ALIGNMENT, sizeof(:short), 1],
["s!", true, NATIVE_ENDIAN, SHORT_ALIGNMENT, sizeof(:short), 1],
["S!", true, NATIVE_ENDIAN, SHORT_ALIGNMENT, sizeof(:short), 1],
["n", false, :big_endian, INT16_ALIGNMENT, sizeof(:int16_t), 1],
["v", false, :little_endian, INT16_ALIGNMENT, sizeof(:int16_t), 1],
["i", false, NATIVE_ENDIAN, INT_ALIGNMENT, sizeof(:int), 1],
["I", false, NATIVE_ENDIAN, INT_ALIGNMENT, sizeof(:int), 1],
["i!", true, NATIVE_ENDIAN, INT_ALIGNMENT, sizeof(:int), 1],
["I!", true, NATIVE_ENDIAN, INT_ALIGNMENT, sizeof(:int), 1],
["l", false, NATIVE_ENDIAN, INT32_ALIGNMENT, sizeof(:int32_t), 1],
["L", false, NATIVE_ENDIAN, INT32_ALIGNMENT, sizeof(:int32_t), 1],
["l!", true, NATIVE_ENDIAN, LONG_ALIGNMENT, sizeof(:long), 1],
["L!", true, NATIVE_ENDIAN, LONG_ALIGNMENT, sizeof(:long), 1],
["N", false, :big_endian, INT32_ALIGNMENT, sizeof(:int32_t), 1],
["V", false, :little_endian, INT32_ALIGNMENT, sizeof(:int32_t), 1],
["f", false, NATIVE_ENDIAN, FLOAT_ALIGNMENT, sizeof(:float), 1],
["e", false, :little_endian, FLOAT_ALIGNMENT, sizeof(:float), 1],
["g", false, :big_endian, FLOAT_ALIGNMENT, sizeof(:float), 1],
["q", false, NATIVE_ENDIAN, INT64_ALIGNMENT, sizeof(:int64_t), 1],
["Q", false, NATIVE_ENDIAN, INT64_ALIGNMENT, sizeof(:int64_t), 1],
["q!", true, NATIVE_ENDIAN, LONG_LONG_ALIGNMENT, sizeof("long long"), 1],
["Q!", true, NATIVE_ENDIAN, LONG_LONG_ALIGNMENT, sizeof("long long"), 1],
["d", false, NATIVE_ENDIAN, DOUBLE_ALIGNMENT, sizeof(:double), 1],
["E", false, :little_endian, DOUBLE_ALIGNMENT, sizeof(:double), 1],
["G", false, :big_endian, DOUBLE_ALIGNMENT, sizeof(:double), 1],
["j", false, NATIVE_ENDIAN, INTPTR_ALIGNMENT, sizeof(:intptr_t), 1],
["J", false, NATIVE_ENDIAN, INTPTR_ALIGNMENT, sizeof(:intptr_t), 1],
].each do |type, native_size_p, endianness, alignment, size, repeat, total_size|
total_size, members, err = MemoryViewTestUtils.parse_item_format("|c#{type}")
assert_nil(err)
padding_size = alignment - 1
expected_total_size = 1 + padding_size + size
assert_equal(expected_total_size, total_size)
expected_result = [
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 0, size: 1, repeat: 1},
{format: type[0], native_size_p: native_size_p, endianness: endianness, offset: alignment, size: size, repeat: repeat},
]
assert_equal(expected_result, members)
end
end
def alignment_padding(total_size, alignment)
res = total_size % alignment
if res > 0
alignment - res
else
0
end
end
def test_rb_memory_view_parse_item_format_with_alignment_total_size_with_tail_padding
total_size, _members, err = MemoryViewTestUtils.parse_item_format("|lqc")
assert_nil(err)
expected_total_size = sizeof(:int32_t)
expected_total_size += alignment_padding(expected_total_size, INT32_ALIGNMENT)
expected_total_size += sizeof(:int64_t)
expected_total_size += alignment_padding(expected_total_size, INT64_ALIGNMENT)
expected_total_size += 1
expected_total_size += alignment_padding(expected_total_size, INT64_ALIGNMENT)
assert_equal(expected_total_size, total_size)
end
def test_rb_memory_view_parse_item_format_with_alignment_compound
total_size, members, err = MemoryViewTestUtils.parse_item_format("|ccc2f3x2d4cq!<")
assert_nil(err)
expected_total_size = 1 + 1 + 1*2
expected_total_size += alignment_padding(expected_total_size, FLOAT_ALIGNMENT)
expected_total_size += sizeof(:float)*3 + 1*2
expected_total_size += alignment_padding(expected_total_size, DOUBLE_ALIGNMENT)
expected_total_size += sizeof(:double)*4 + 1
expected_total_size += alignment_padding(expected_total_size, LONG_LONG_ALIGNMENT)
expected_total_size += sizeof("long long")
assert_equal(expected_total_size, total_size)
expected_result = [
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 0, size: 1, repeat: 1},
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 1, size: 1, repeat: 1},
{format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: 2, size: 1, repeat: 2},
]
offset = 4
res = offset % FLOAT_ALIGNMENT
offset += FLOAT_ALIGNMENT - res if res > 0
expected_result << {format: 'f', native_size_p: false, endianness: NATIVE_ENDIAN, offset: offset, size: 4, repeat: 3}
offset += 12
offset += 2 # 2x
res = offset % DOUBLE_ALIGNMENT
offset += DOUBLE_ALIGNMENT - res if res > 0
expected_result << {format: 'd', native_size_p: false, endianness: NATIVE_ENDIAN, offset: offset, size: 8, repeat: 4}
offset += 32
expected_result << {format: 'c', native_size_p: false, endianness: NATIVE_ENDIAN, offset: offset, size: 1, repeat: 1}
offset += 1
res = offset % LONG_LONG_ALIGNMENT
offset += LONG_LONG_ALIGNMENT - res if res > 0
expected_result << {format: 'q', native_size_p: true, endianness: :little_endian, offset: offset, size: 8, repeat: 1}
assert_equal(expected_result, members)
end
def test_rb_memory_view_extract_item_members
m = MemoryViewTestUtils
assert_equal(1, m.extract_item_members([1].pack("c"), "c"))
assert_equal([1, 2], m.extract_item_members([1, 2].pack("ii"), "ii"))
assert_equal([1, 2, 3], m.extract_item_members([1, 2, 3].pack("cls"), "cls"))
end
def test_rb_memory_view_extract_item_members_endianness
m = MemoryViewTestUtils
assert_equal([0x0102, 0x0304], m.extract_item_members([1, 2, 3, 4].pack("c*"), "S>2"))
assert_equal([0x0102, 0x0304], m.extract_item_members([1, 2, 3, 4].pack("c*"), "n2"))
assert_equal([0x0201, 0x0403], m.extract_item_members([1, 2, 3, 4].pack("c*"), "S<2"))
assert_equal([0x0201, 0x0403], m.extract_item_members([1, 2, 3, 4].pack("c*"), "v2"))
assert_equal(0x01020304, m.extract_item_members([1, 2, 3, 4].pack("c*"), "L>"))
assert_equal(0x01020304, m.extract_item_members([1, 2, 3, 4].pack("c*"), "N"))
assert_equal(0x04030201, m.extract_item_members([1, 2, 3, 4].pack("c*"), "L<"))
assert_equal(0x04030201, m.extract_item_members([1, 2, 3, 4].pack("c*"), "V"))
assert_equal(0x0102030405060708, m.extract_item_members([1, 2, 3, 4, 5, 6, 7, 8].pack("c*"), "Q>"))
assert_equal(0x0807060504030201, m.extract_item_members([1, 2, 3, 4, 5, 6, 7, 8].pack("c*"), "Q<"))
end
def test_rb_memory_view_extract_item_members_float
m = MemoryViewTestUtils
packed = [1.23].pack("f")
assert_equal(packed.unpack("f")[0], m.extract_item_members(packed, "f"))
end
def test_rb_memory_view_extract_item_members_float_endianness
m = MemoryViewTestUtils
hi, lo = [1.23].pack("f").unpack("L")[0].divmod(0x10000)
packed = [lo, hi].pack("S*")
assert_equal(packed.unpack("e")[0], m.extract_item_members(packed, "e"))
packed = [hi, lo].pack("S*")
assert_equal(packed.unpack("g")[0], m.extract_item_members(packed, "g"))
end
def test_rb_memory_view_extract_item_members_doble
m = MemoryViewTestUtils
packed = [1.23].pack("d")
assert_equal(1.23, m.extract_item_members(packed, "d"))
end
def test_rb_memory_view_extract_item_members_doble_endianness
m = MemoryViewTestUtils
hi, lo = [1.23].pack("d").unpack("Q")[0].divmod(0x10000)
packed = [lo, hi].pack("L*")
assert_equal(packed.unpack("E")[0], m.extract_item_members(packed, "E"))
packed = [hi, lo].pack("L*")
assert_equal(packed.unpack("G")[0], m.extract_item_members(packed, "G"))
end
def test_rb_memory_view_available_p
es = MemoryViewTestUtils::ExportableString.new("ruby")
assert_equal(true, MemoryViewTestUtils.available?(es))
es = MemoryViewTestUtils::ExportableString.new(nil)
assert_equal(false, MemoryViewTestUtils.available?(es))
end
def test_ref_count_with_exported_object
es = MemoryViewTestUtils::ExportableString.new("ruby")
assert_equal(1, MemoryViewTestUtils.ref_count_while_exporting(es, 1))
assert_equal(2, MemoryViewTestUtils.ref_count_while_exporting(es, 2))
assert_equal(10, MemoryViewTestUtils.ref_count_while_exporting(es, 10))
assert_nil(MemoryViewTestUtils.ref_count_while_exporting(es, 0))
end
def test_rb_memory_view_init_as_byte_array
# ExportableString's memory view is initialized by rb_memory_view_init_as_byte_array
es = MemoryViewTestUtils::ExportableString.new("ruby")
memory_view_info = MemoryViewTestUtils.get_memory_view_info(es)
assert_equal({
obj: es,
byte_size: 4,
readonly: true,
format: nil,
item_size: 1,
ndim: 1,
shape: nil,
strides: nil,
sub_offsets: nil
},
memory_view_info)
end
def test_rb_memory_view_get_with_memory_view_unavailable_object
es = MemoryViewTestUtils::ExportableString.new(nil)
memory_view_info = MemoryViewTestUtils.get_memory_view_info(es)
assert_nil(memory_view_info)
end
def test_rb_memory_view_fill_contiguous_strides
row_major_strides = MemoryViewTestUtils.fill_contiguous_strides(3, 8, [2, 3, 4], true)
assert_equal([96, 32, 8],
row_major_strides)
column_major_strides = MemoryViewTestUtils.fill_contiguous_strides(3, 8, [2, 3, 4], false)
assert_equal([8, 16, 48],
column_major_strides)
end
def test_rb_memory_view_get_item_pointer_single_member
buf = [ 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 ].pack("l!*")
shape = [3, 4]
mv = MemoryViewTestUtils::MultiDimensionalView.new(buf, "l!", shape, nil)
assert_equal(1, mv[[0, 0]])
assert_equal(4, mv[[0, 3]])
assert_equal(6, mv[[1, 1]])
assert_equal(10, mv[[2, 1]])
end
def test_rb_memory_view_get_item_pointer_multiple_members
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
-1, -2, -3, -4, -5, -6, -7, -8].pack("s*")
shape = [2, 4]
strides = [4*sizeof(:short)*2, sizeof(:short)*2]
mv = MemoryViewTestUtils::MultiDimensionalView.new(buf, "ss", shape, strides)
assert_equal([1, 2], mv[[0, 0]])
assert_equal([5, 6], mv[[0, 2]])
assert_equal([-1, -2], mv[[1, 0]])
assert_equal([-7, -8], mv[[1, 3]])
end
def test_ractor
assert_in_out_err([], <<-"end;", ["[5, 6]", "[-7, -8]"], [])
require "-test-/memory_view"
require "rbconfig/sizeof"
$VERBOSE = nil
r = Ractor.new RbConfig::SIZEOF["short"] do |sizeof_short|
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
-1, -2, -3, -4, -5, -6, -7, -8].pack("s*")
shape = [2, 4]
strides = [4*sizeof_short*2, sizeof_short*2]
mv = MemoryViewTestUtils::MultiDimensionalView.new(buf, "ss", shape, strides)
p mv[[0, 2]]
mv[[1, 3]]
end
p r.take
end;
end
end
|