@@ -57,35 +57,56 @@ def test_ensure_call_dlload
57
57
def test_struct_memory_access ( )
58
58
# check memory operations performed directly on struct
59
59
my_struct = Fiddle ::Importer . struct ( [ 'int id' ] ) . malloc
60
- my_struct [ 0 , Fiddle ::SIZEOF_INT ] = "\x01 " . b * Fiddle ::SIZEOF_INT
61
- assert_equal 0x01010101 , my_struct . id
62
-
63
- my_struct . id = 0
64
- assert_equal "\x00 " . b * Fiddle ::SIZEOF_INT , my_struct [ 0 , Fiddle ::SIZEOF_INT ]
60
+ begin
61
+ my_struct [ 0 , Fiddle ::SIZEOF_INT ] = "\x01 " . b * Fiddle ::SIZEOF_INT
62
+ assert_equal 0x01010101 , my_struct . id
63
+
64
+ my_struct . id = 0
65
+ assert_equal "\x00 " . b * Fiddle ::SIZEOF_INT , my_struct [ 0 , Fiddle ::SIZEOF_INT ]
66
+ ensure
67
+ Fiddle . free my_struct . to_ptr
68
+ end
65
69
end
66
70
67
71
def test_struct_ptr_array_subscript_multiarg ( )
68
72
# check memory operations performed on struct#to_ptr
69
73
struct = Fiddle ::Importer . struct ( [ 'int x' ] ) . malloc
70
- ptr = struct . to_ptr
74
+ begin
75
+ ptr = struct . to_ptr
71
76
72
- struct . x = 0x02020202
73
- assert_equal ( "\x02 " . b * Fiddle ::SIZEOF_INT , ptr [ 0 , Fiddle ::SIZEOF_INT ] )
77
+ struct . x = 0x02020202
78
+ assert_equal ( "\x02 " . b * Fiddle ::SIZEOF_INT , ptr [ 0 , Fiddle ::SIZEOF_INT ] )
74
79
75
- ptr [ 0 , Fiddle ::SIZEOF_INT ] = "\x01 " . b * Fiddle ::SIZEOF_INT
76
- assert_equal 0x01010101 , struct . x
80
+ ptr [ 0 , Fiddle ::SIZEOF_INT ] = "\x01 " . b * Fiddle ::SIZEOF_INT
81
+ assert_equal 0x01010101 , struct . x
82
+ ensure
83
+ Fiddle . free struct . to_ptr
84
+ end
77
85
end
78
86
79
87
def test_malloc ( )
80
88
s1 = LIBC ::Timeval . malloc ( )
81
- s2 = LIBC ::Timeval . malloc ( )
82
- refute_equal ( s1 . to_ptr . to_i , s2 . to_ptr . to_i )
89
+ begin
90
+ s2 = LIBC ::Timeval . malloc ( )
91
+ begin
92
+ refute_equal ( s1 . to_ptr . to_i , s2 . to_ptr . to_i )
93
+ ensure
94
+ Fiddle . free s2 . to_ptr
95
+ end
96
+ ensure
97
+ Fiddle . free s1 . to_ptr
98
+ end
83
99
end
84
100
85
101
def test_sizeof ( )
86
102
assert_equal ( SIZEOF_VOIDP , LIBC . sizeof ( "FILE*" ) )
87
103
assert_equal ( LIBC ::MyStruct . size ( ) , LIBC . sizeof ( LIBC ::MyStruct ) )
88
- assert_equal ( LIBC ::MyStruct . size ( ) , LIBC . sizeof ( LIBC ::MyStruct . malloc ( ) ) )
104
+ my_struct = LIBC ::MyStruct . malloc ( )
105
+ begin
106
+ assert_equal ( LIBC ::MyStruct . size ( ) , LIBC . sizeof ( my_struct ) )
107
+ ensure
108
+ Fiddle . free my_struct . to_ptr
109
+ end
89
110
assert_equal ( SIZEOF_LONG_LONG , LIBC . sizeof ( "long long" ) ) if defined? ( SIZEOF_LONG_LONG )
90
111
end
91
112
@@ -131,35 +152,51 @@ def test_value()
131
152
132
153
def test_struct_array_assignment ( )
133
154
instance = Fiddle ::Importer . struct ( [ "unsigned int stages[3]" ] ) . malloc
134
- instance . stages [ 0 ] = 1024
135
- instance . stages [ 1 ] = 10
136
- instance . stages [ 2 ] = 100
137
- assert_equal 1024 , instance . stages [ 0 ]
138
- assert_equal 10 , instance . stages [ 1 ]
139
- assert_equal 100 , instance . stages [ 2 ]
140
- assert_equal [ 1024 , 10 , 100 ] . pack ( Fiddle ::PackInfo ::PACK_MAP [ -Fiddle ::TYPE_INT ] * 3 ) ,
141
- instance . to_ptr [ 0 , 3 * Fiddle ::SIZEOF_INT ]
142
- assert_raise ( IndexError ) { instance . stages [ -1 ] = 5 }
143
- assert_raise ( IndexError ) { instance . stages [ 3 ] = 5 }
155
+ begin
156
+ instance . stages [ 0 ] = 1024
157
+ instance . stages [ 1 ] = 10
158
+ instance . stages [ 2 ] = 100
159
+ assert_equal 1024 , instance . stages [ 0 ]
160
+ assert_equal 10 , instance . stages [ 1 ]
161
+ assert_equal 100 , instance . stages [ 2 ]
162
+ assert_equal [ 1024 , 10 , 100 ] . pack ( Fiddle ::PackInfo ::PACK_MAP [ -Fiddle ::TYPE_INT ] * 3 ) ,
163
+ instance . to_ptr [ 0 , 3 * Fiddle ::SIZEOF_INT ]
164
+ assert_raise ( IndexError ) { instance . stages [ -1 ] = 5 }
165
+ assert_raise ( IndexError ) { instance . stages [ 3 ] = 5 }
166
+ ensure
167
+ Fiddle . free instance . to_ptr
168
+ end
144
169
end
145
170
146
171
def test_struct ( )
147
172
s = LIBC ::MyStruct . malloc ( )
148
- s . num = [ 0 , 1 , 2 , 3 , 4 ]
149
- s . c = ?a. ord
150
- s . buff = "012345\377 "
151
- assert_equal ( [ 0 , 1 , 2 , 3 , 4 ] , s . num )
152
- assert_equal ( ?a. ord , s . c )
153
- assert_equal ( [ ?0. ord , ?1. ord , ?2. ord , ?3. ord , ?4. ord , ?5. ord , ?\377. ord ] , s . buff )
173
+ begin
174
+ s . num = [ 0 , 1 , 2 , 3 , 4 ]
175
+ s . c = ?a. ord
176
+ s . buff = "012345\377 "
177
+ assert_equal ( [ 0 , 1 , 2 , 3 , 4 ] , s . num )
178
+ assert_equal ( ?a. ord , s . c )
179
+ assert_equal ( [ ?0. ord , ?1. ord , ?2. ord , ?3. ord , ?4. ord , ?5. ord , ?\377. ord ] , s . buff )
180
+ ensure
181
+ Fiddle . free s . to_ptr
182
+ end
154
183
end
155
184
156
185
def test_gettimeofday ( )
157
186
if ( defined? ( LIBC . gettimeofday ) )
158
187
timeval = LIBC ::Timeval . malloc ( )
159
- timezone = LIBC ::Timezone . malloc ( )
160
- LIBC . gettimeofday ( timeval , timezone )
161
- cur = Time . now ( )
162
- assert ( cur . to_i - 2 <= timeval . tv_sec && timeval . tv_sec <= cur . to_i )
188
+ begin
189
+ timezone = LIBC ::Timezone . malloc ( )
190
+ begin
191
+ LIBC . gettimeofday ( timeval , timezone )
192
+ ensure
193
+ Fiddle . free timezone . to_ptr
194
+ end
195
+ cur = Time . now ( )
196
+ assert ( cur . to_i - 2 <= timeval . tv_sec && timeval . tv_sec <= cur . to_i )
197
+ ensure
198
+ Fiddle . free timeval . to_ptr
199
+ end
163
200
end
164
201
end
165
202
0 commit comments