@@ -34,18 +34,22 @@ def test_memory_view_from_pointer
34
34
str = Marshal . load ( Marshal . dump ( "hello world" ) )
35
35
ptr = Pointer [ str ]
36
36
mview = MemoryView . new ( ptr )
37
- assert_same ( ptr , mview . obj )
38
- assert_equal ( str . bytesize , mview . byte_size )
39
- assert_equal ( true , mview . readonly? )
40
- assert_equal ( nil , mview . format )
41
- assert_equal ( 1 , mview . item_size )
42
- assert_equal ( 1 , mview . ndim )
43
- assert_equal ( nil , mview . shape )
44
- assert_equal ( nil , mview . strides )
45
- assert_equal ( nil , mview . sub_offsets )
37
+ begin
38
+ assert_same ( ptr , mview . obj )
39
+ assert_equal ( str . bytesize , mview . byte_size )
40
+ assert_equal ( true , mview . readonly? )
41
+ assert_equal ( nil , mview . format )
42
+ assert_equal ( 1 , mview . item_size )
43
+ assert_equal ( 1 , mview . ndim )
44
+ assert_equal ( nil , mview . shape )
45
+ assert_equal ( nil , mview . strides )
46
+ assert_equal ( nil , mview . sub_offsets )
46
47
47
- codes = str . codepoints
48
- assert_equal ( codes , ( 0 ...str . bytesize ) . map { |i | mview [ i ] } )
48
+ codes = str . codepoints
49
+ assert_equal ( codes , ( 0 ...str . bytesize ) . map { |i | mview [ i ] } )
50
+ ensure
51
+ mview . release
52
+ end
49
53
end
50
54
51
55
def test_memory_view_multi_dimensional
@@ -57,17 +61,21 @@ def test_memory_view_multi_dimensional
57
61
shape = [ 3 , 4 ]
58
62
md = MemoryViewTestUtils ::MultiDimensionalView . new ( buf , "l!" , shape , nil )
59
63
mview = Fiddle ::MemoryView . new ( md )
60
- assert_equal ( buf . bytesize , mview . byte_size )
61
- assert_equal ( "l!" , mview . format )
62
- assert_equal ( Fiddle ::SIZEOF_LONG , mview . item_size )
63
- assert_equal ( 2 , mview . ndim )
64
- assert_equal ( shape , mview . shape )
65
- assert_equal ( [ Fiddle ::SIZEOF_LONG *4 , Fiddle ::SIZEOF_LONG ] , mview . strides )
66
- assert_equal ( nil , mview . sub_offsets )
67
- assert_equal ( 1 , mview [ 0 , 0 ] )
68
- assert_equal ( 4 , mview [ 0 , 3 ] )
69
- assert_equal ( 6 , mview [ 1 , 1 ] )
70
- assert_equal ( 10 , mview [ 2 , 1 ] )
64
+ begin
65
+ assert_equal ( buf . bytesize , mview . byte_size )
66
+ assert_equal ( "l!" , mview . format )
67
+ assert_equal ( Fiddle ::SIZEOF_LONG , mview . item_size )
68
+ assert_equal ( 2 , mview . ndim )
69
+ assert_equal ( shape , mview . shape )
70
+ assert_equal ( [ Fiddle ::SIZEOF_LONG *4 , Fiddle ::SIZEOF_LONG ] , mview . strides )
71
+ assert_equal ( nil , mview . sub_offsets )
72
+ assert_equal ( 1 , mview [ 0 , 0 ] )
73
+ assert_equal ( 4 , mview [ 0 , 3 ] )
74
+ assert_equal ( 6 , mview [ 1 , 1 ] )
75
+ assert_equal ( 10 , mview [ 2 , 1 ] )
76
+ ensure
77
+ mview . release
78
+ end
71
79
end
72
80
73
81
def test_memory_view_multi_dimensional_with_strides
@@ -79,17 +87,21 @@ def test_memory_view_multi_dimensional_with_strides
79
87
strides = [ 4 *Fiddle ::SIZEOF_LONG *2 , Fiddle ::SIZEOF_LONG *2 ]
80
88
md = MemoryViewTestUtils ::MultiDimensionalView . new ( buf , "l!" , shape , strides )
81
89
mview = Fiddle ::MemoryView . new ( md )
82
- assert_equal ( "l!" , mview . format )
83
- assert_equal ( Fiddle ::SIZEOF_LONG , mview . item_size )
84
- assert_equal ( buf . bytesize , mview . byte_size )
85
- assert_equal ( 2 , mview . ndim )
86
- assert_equal ( shape , mview . shape )
87
- assert_equal ( strides , mview . strides )
88
- assert_equal ( nil , mview . sub_offsets )
89
- assert_equal ( 1 , mview [ 0 , 0 ] )
90
- assert_equal ( 5 , mview [ 0 , 2 ] )
91
- assert_equal ( 9 , mview [ 1 , 0 ] )
92
- assert_equal ( 15 , mview [ 1 , 3 ] )
90
+ begin
91
+ assert_equal ( "l!" , mview . format )
92
+ assert_equal ( Fiddle ::SIZEOF_LONG , mview . item_size )
93
+ assert_equal ( buf . bytesize , mview . byte_size )
94
+ assert_equal ( 2 , mview . ndim )
95
+ assert_equal ( shape , mview . shape )
96
+ assert_equal ( strides , mview . strides )
97
+ assert_equal ( nil , mview . sub_offsets )
98
+ assert_equal ( 1 , mview [ 0 , 0 ] )
99
+ assert_equal ( 5 , mview [ 0 , 2 ] )
100
+ assert_equal ( 9 , mview [ 1 , 0 ] )
101
+ assert_equal ( 15 , mview [ 1 , 3 ] )
102
+ ensure
103
+ mview . release
104
+ end
93
105
end
94
106
95
107
def test_memory_view_multi_dimensional_with_multiple_members
@@ -101,17 +113,21 @@ def test_memory_view_multi_dimensional_with_multiple_members
101
113
strides = [ 4 *Fiddle ::SIZEOF_SHORT *2 , Fiddle ::SIZEOF_SHORT *2 ]
102
114
md = MemoryViewTestUtils ::MultiDimensionalView . new ( buf , "ss" , shape , strides )
103
115
mview = Fiddle ::MemoryView . new ( md )
104
- assert_equal ( "ss" , mview . format )
105
- assert_equal ( Fiddle ::SIZEOF_SHORT *2 , mview . item_size )
106
- assert_equal ( buf . bytesize , mview . byte_size )
107
- assert_equal ( 2 , mview . ndim )
108
- assert_equal ( shape , mview . shape )
109
- assert_equal ( strides , mview . strides )
110
- assert_equal ( nil , mview . sub_offsets )
111
- assert_equal ( [ 1 , 2 ] , mview [ 0 , 0 ] )
112
- assert_equal ( [ 5 , 6 ] , mview [ 0 , 2 ] )
113
- assert_equal ( [ -1 , -2 ] , mview [ 1 , 0 ] )
114
- assert_equal ( [ -7 , -8 ] , mview [ 1 , 3 ] )
116
+ begin
117
+ assert_equal ( "ss" , mview . format )
118
+ assert_equal ( Fiddle ::SIZEOF_SHORT *2 , mview . item_size )
119
+ assert_equal ( buf . bytesize , mview . byte_size )
120
+ assert_equal ( 2 , mview . ndim )
121
+ assert_equal ( shape , mview . shape )
122
+ assert_equal ( strides , mview . strides )
123
+ assert_equal ( nil , mview . sub_offsets )
124
+ assert_equal ( [ 1 , 2 ] , mview [ 0 , 0 ] )
125
+ assert_equal ( [ 5 , 6 ] , mview [ 0 , 2 ] )
126
+ assert_equal ( [ -1 , -2 ] , mview [ 1 , 0 ] )
127
+ assert_equal ( [ -7 , -8 ] , mview [ 1 , 3 ] )
128
+ ensure
129
+ mview . release
130
+ end
115
131
end
116
132
117
133
def test_export
@@ -135,9 +151,13 @@ def test_to_s
135
151
data = "\u{3042} "
136
152
ptr = Pointer [ data ]
137
153
mview = MemoryView . new ( ptr )
138
- string = mview . to_s
139
- assert_equal ( [ data . b , true ] ,
140
- [ string , string . frozen? ] )
154
+ begin
155
+ string = mview . to_s
156
+ assert_equal ( [ data . b , true ] ,
157
+ [ string , string . frozen? ] )
158
+ ensure
159
+ mview . release
160
+ end
141
161
end
142
162
end
143
163
end
0 commit comments