@@ -57,35 +57,56 @@ def test_ensure_call_dlload
5757 def test_struct_memory_access ( )
5858 # check memory operations performed directly on struct
5959 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
6569 end
6670
6771 def test_struct_ptr_array_subscript_multiarg ( )
6872 # check memory operations performed on struct#to_ptr
6973 struct = Fiddle ::Importer . struct ( [ 'int x' ] ) . malloc
70- ptr = struct . to_ptr
74+ begin
75+ ptr = struct . to_ptr
7176
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 ] )
7479
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
7785 end
7886
7987 def test_malloc ( )
8088 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
8399 end
84100
85101 def test_sizeof ( )
86102 assert_equal ( SIZEOF_VOIDP , LIBC . sizeof ( "FILE*" ) )
87103 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
89110 assert_equal ( SIZEOF_LONG_LONG , LIBC . sizeof ( "long long" ) ) if defined? ( SIZEOF_LONG_LONG )
90111 end
91112
@@ -131,35 +152,51 @@ def test_value()
131152
132153 def test_struct_array_assignment ( )
133154 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
144169 end
145170
146171 def test_struct ( )
147172 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
154183 end
155184
156185 def test_gettimeofday ( )
157186 if ( defined? ( LIBC . gettimeofday ) )
158187 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
163200 end
164201 end
165202
0 commit comments