Menu

[r2570]: / trunk / src / tests / tc_objcptr.rb  Maximize  Restore  History

Download this file

293 lines (259 with data), 11.1 kB

  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
#
# $Id$
#
# Copyright (c) 2001-2003 FUJIMOTO Hisakuni
#
require 'test/unit'
require 'osx/cocoa'
system 'make -s' || raise(RuntimeError, "'make' failed")
require './objc_test.bundle'
OSX.load_bridge_support_file 'ObjcPtrTest.bridgesupport'
class TC_ObjcPtr < Test::Unit::TestCase
include OSX
def test_s_new
length = 123456
cptr = ObjcPtr.new( length )
assert_kind_of( ObjcPtr, cptr )
assert_equal( length, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_s_new_with_type
assert_nothing_raised { ObjcPtr.new(:char) }
assert_nothing_raised { ObjcPtr.new(:uchar) }
assert_nothing_raised { ObjcPtr.new(:short) }
assert_nothing_raised { ObjcPtr.new(:ushort) }
assert_nothing_raised { ObjcPtr.new(:int) }
assert_nothing_raised { ObjcPtr.new(:uint) }
assert_nothing_raised { ObjcPtr.new(:long) }
assert_nothing_raised { ObjcPtr.new(:ulong) }
assert_nothing_raised { ObjcPtr.new(:longlong) }
assert_nothing_raised { ObjcPtr.new(:ulonglong) }
assert_nothing_raised { ObjcPtr.new(:float) }
assert_nothing_raised { ObjcPtr.new(:double) }
assert_raises(RuntimeError) { ObjcPtr.new(:unknown_type) }
assert_equal( 1, ObjcPtr.new(:char).allocated_size )
assert_equal( 1, ObjcPtr.new(:uchar).allocated_size )
assert_equal( 2, ObjcPtr.new(:short).allocated_size )
assert_equal( 2, ObjcPtr.new(:ushort).allocated_size )
# assert_equal( 4, ObjcPtr.new(:int).allocated_size )
# assert_equal( 4, ObjcPtr.new(:uint).allocated_size )
if OSX::RUBYCOCOA_BUILD_LP64
assert_equal( 8, ObjcPtr.new(:long).allocated_size )
assert_equal( 8, ObjcPtr.new(:ulong).allocated_size )
else
assert_equal( 4, ObjcPtr.new(:long).allocated_size )
assert_equal( 4, ObjcPtr.new(:ulong).allocated_size )
end
assert_equal( 8, ObjcPtr.new(:longlong).allocated_size )
assert_equal( 8, ObjcPtr.new(:ulonglong).allocated_size )
assert_equal( 4, ObjcPtr.new(:float).allocated_size )
assert_equal( 8, ObjcPtr.new(:double).allocated_size )
assert_equal( "c", ObjcPtr.new(:char).encoding )
assert_equal( "C", ObjcPtr.new(:uchar).encoding )
assert_equal( "s", ObjcPtr.new(:short).encoding )
assert_equal( "S", ObjcPtr.new(:ushort).encoding )
assert_equal( "i", ObjcPtr.new(:int).encoding )
assert_equal( "I", ObjcPtr.new(:uint).encoding )
assert_equal( "l", ObjcPtr.new(:long).encoding )
assert_equal( "L", ObjcPtr.new(:ulong).encoding )
assert_equal( "q", ObjcPtr.new(:longlong).encoding )
assert_equal( "Q", ObjcPtr.new(:ulonglong).encoding )
assert_equal( "f", ObjcPtr.new(:float).encoding )
assert_equal( "d", ObjcPtr.new(:double).encoding )
end
def test_s_new_with_type_and_count
assert_nothing_raised { ObjcPtr.new(:char, 17) }
assert_nothing_raised { ObjcPtr.new(:uchar, 17) }
assert_nothing_raised { ObjcPtr.new(:short, 17) }
assert_nothing_raised { ObjcPtr.new(:ushort, 17) }
assert_nothing_raised { ObjcPtr.new(:int, 17) }
assert_nothing_raised { ObjcPtr.new(:uint, 17) }
assert_nothing_raised { ObjcPtr.new(:long, 17) }
assert_nothing_raised { ObjcPtr.new(:ulong, 17) }
assert_nothing_raised { ObjcPtr.new(:longlong, 17) }
assert_nothing_raised { ObjcPtr.new(:ulonglong, 17) }
assert_nothing_raised { ObjcPtr.new(:float, 17) }
assert_nothing_raised { ObjcPtr.new(:double, 17) }
assert_raises(RuntimeError) { ObjcPtr.new(:unknown_type, 17) }
assert_equal( 1 * 17, ObjcPtr.new(:char, 17).allocated_size )
assert_equal( 1 * 17, ObjcPtr.new(:uchar, 17).allocated_size )
assert_equal( 2 * 17, ObjcPtr.new(:short, 17).allocated_size )
assert_equal( 2 * 17, ObjcPtr.new(:ushort, 17).allocated_size )
# assert_equal( 4 * 17, ObjcPtr.new(:int).allocated_size )
# assert_equal( 4 * 17, ObjcPtr.new(:uint).allocated_size )
if OSX::RUBYCOCOA_BUILD_LP64
assert_equal( 8 * 17, ObjcPtr.new(:long, 17).allocated_size )
assert_equal( 8 * 17, ObjcPtr.new(:ulong, 17).allocated_size )
else
assert_equal( 4 * 17, ObjcPtr.new(:long, 17).allocated_size )
assert_equal( 4 * 17, ObjcPtr.new(:ulong, 17).allocated_size )
end
assert_equal( 8 * 17, ObjcPtr.new(:longlong, 17).allocated_size )
assert_equal( 8 * 17, ObjcPtr.new(:ulonglong, 17).allocated_size )
assert_equal( 4 * 17, ObjcPtr.new(:float, 17).allocated_size )
assert_equal( 8 * 17, ObjcPtr.new(:double, 17).allocated_size )
assert_equal( "c", ObjcPtr.new(:char, 17).encoding )
assert_equal( "C", ObjcPtr.new(:uchar, 17).encoding )
assert_equal( "s", ObjcPtr.new(:short, 17).encoding )
assert_equal( "S", ObjcPtr.new(:ushort, 17).encoding )
assert_equal( "i", ObjcPtr.new(:int, 17).encoding )
assert_equal( "I", ObjcPtr.new(:uint, 17).encoding )
assert_equal( "l", ObjcPtr.new(:long, 17).encoding )
assert_equal( "L", ObjcPtr.new(:ulong, 17).encoding )
assert_equal( "q", ObjcPtr.new(:longlong, 17).encoding )
assert_equal( "Q", ObjcPtr.new(:ulonglong, 17).encoding )
assert_equal( "f", ObjcPtr.new(:float, 17).encoding )
assert_equal( "d", ObjcPtr.new(:double, 17).encoding )
end
def test_s_allocate_as_int8
cptr = ObjcPtr.allocate_as_int8
assert_kind_of( ObjcPtr, cptr )
assert_equal( 1, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_s_allocate_as_int16
cptr = ObjcPtr.allocate_as_int16
assert_kind_of( ObjcPtr, cptr )
assert_equal( 2, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_s_allocate_as_int32
cptr = ObjcPtr.allocate_as_int32
assert_kind_of( ObjcPtr, cptr )
assert_equal( 4, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_s_allocate_as_bool
cptr = ObjcPtr.allocate_as_bool
assert_kind_of( ObjcPtr, cptr )
assert_equal( 1, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_s_allocate_as_int
cptr = ObjcPtr.allocate_as_int
assert_kind_of( ObjcPtr, cptr )
assert_equal( 4, cptr.allocated_size )
assert( ! cptr.tainted? )
end
def test_cptr_as_returned_value_of_method_call
cptr = NSData.dataWithContentsOfFile('/etc/passwd').bytes
assert_kind_of( ObjcPtr, cptr )
assert_equal( 0, cptr.allocated_size )
assert( cptr.tainted? )
end
def test_cptr_as_param_of_method_call
src = 'hello world'
data = NSData.dataWithRubyString( src )
cptr = ObjcPtr.new( src.size )
data.getBytes_length( cptr )
assert_equal( src.size, cptr.allocated_size )
end
def test_bytestr_at
src = 'hello world'
cptr = NSData.dataWithRubyString(src).bytes
bstr = cptr.bytestr_at(3,4)
if RUBY_VERSION >= '2.0'
assert_equal( Encoding.find("RUBYCOCOA_UNKNOWN"), bstr.encoding )
bstr.force_encoding("ASCII-8BIT")
end
assert_equal( src[3,4], bstr )
assert( bstr.tainted? )
end
def test_bytestr
src = 'hello world'
cptr = NSData.dataWithRubyString(src).bytes
bstr = cptr.bytestr(src.size)
if RUBY_VERSION >= '2.0'
assert_equal( Encoding.find("RUBYCOCOA_UNKNOWN"), bstr.encoding )
bstr.force_encoding("ASCII-8BIT")
end
assert_equal( src, bstr )
assert( bstr.tainted? )
end
def test_ocptr_ary_like
components = [0.1, 0.5, 0.9, 0]
color = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components)
# FIXME: color becomes NSObject(__NSCFType) via toll-free on 10.6
#assert_kind_of(CGColorRef, color)
components2 = CGColorGetComponents(color)
assert((components2[0] >= 0.09 and components2[0] <= 0.11))
assert((components2[1] >= 0.49 and components2[1] <= 0.51))
assert((components2[2] >= 0.89 and components2[2] <= 0.91))
end
def test_ocptr_aref
nearly_equal = proc do |v0,v1|
x = (v0 - v1).to_f
(-0.01 <= x && x <= 0.01) ? true : false
end
components = [0.1, 0.5, 0.9, 0]
color = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components)
# FIXME: color becomes NSObject(__NSCFType) via toll-free on 10.6
#assert_kind_of(CGColorRef, color)
components2 = CGColorGetComponents(color)
ary = nil
assert_nothing_raised { ary = components2[0, 4] }
assert_equal( true, nearly_equal.call( ary[0], 0.1 ))
assert_equal( true, nearly_equal.call( ary[1], 0.5 ))
assert_equal( true, nearly_equal.call( ary[2], 0.9 ))
assert_equal( true, nearly_equal.call( ary[3], 0.0 ))
end
def test_ocptr_int_assign
obj = ObjcPtr.new(:int)
obj.assign(123)
assert_equal(123, obj.int)
number = NSNumber.numberWithInt(42)
obj.assign(number)
assert_equal(42, obj.int)
assert_raises(ArgumentError) { obj.assign('foo') }
end
def test_ocptr_ary_assign
str = 'foobar'
obj = ObjcPtr.new(:char, str.length)
# Note: ruby-1.8 String#bytes does not returns an array.
str.length.times { |i| obj[i] = str.bytes.to_a[i] }
bstr = obj.bytestr
if RUBY_VERSION >= '2.0'
bstr.force_encoding("ASCII-8BIT")
end
assert_equal('foobar', bstr)
assert_raises(ArgumentError) { obj[0] = 'blah' }
end
def test_ocptr_as_id
obj = ObjcPtrTest.new.returnVoidPtrForArrayOfString
ary = obj.cast_as('@')
assert_kind_of(OSX::NSArray, ary)
assert_kind_of(OSX::NSString, ary.first)
obj = ObjcPtrTest.new.returnVoidPtrForKCFBooleanTrue
assert_equal(true, obj.cast_as('@').boolValue)
obj = ObjcPtrTest.new.returnVoidPtrForKCFBooleanFalse
assert_equal(false, obj.cast_as('@').boolValue)
obj = ObjcPtrTest.new.returnVoidPtrForInt
assert_equal(-2147483648, obj.cast_as('^i'))
obj = ObjcPtrTest.new.returnVoidPtrForUInt
assert_equal(4294967295, obj.cast_as('^I'))
obj = ObjcPtrTest.new.returnVoidPtrForCStr
str = obj.cast_as('*')
if RUBY_VERSION >= '2.0'
str.force_encoding("ASCII-8BIT")
end
assert_equal('foobar', str)
end
# rb_define_method (_kObjcPtr, "int8_at", rb_objcptr_int8_at, 1);
# rb_define_method (_kObjcPtr, "uint8_at", rb_objcptr_uint8_at, 1);
# rb_define_method (_kObjcPtr, "int16_at", rb_objcptr_int16_at, 1);
# rb_define_method (_kObjcPtr, "uint16_at", rb_objcptr_uint16_at, 1);
# rb_define_method (_kObjcPtr, "int32_at", rb_objcptr_int32_at, 1);
# rb_define_method (_kObjcPtr, "uint32_at", rb_objcptr_uint32_at, 1);
# rb_define_alias (_kObjcPtr, "int_at", "int32_at");
# rb_define_alias (_kObjcPtr, "uint_at", "uint32_at");
# rb_define_alias (_kObjcPtr, "bool_at", "uint8_at");
# rb_define_method (_kObjcPtr, "int8", rb_objcptr_int8, 0);
# rb_define_method (_kObjcPtr, "uint8", rb_objcptr_uint8, 0);
# rb_define_method (_kObjcPtr, "int16", rb_objcptr_int16, 0);
# rb_define_method (_kObjcPtr, "uint16", rb_objcptr_uint16, 0);
# rb_define_method (_kObjcPtr, "int32", rb_objcptr_int32, 0);
# rb_define_method (_kObjcPtr, "uint32", rb_objcptr_uint32, 0);
# rb_define_alias (_kObjcPtr, "int", "int32");
# rb_define_alias (_kObjcPtr, "uint", "uint32");
# rb_define_alias (_kObjcPtr, "bool", "uint8");
end