1# 2# $Id: tc_objcptr.rb 2262 2009-09-22 06:12:31Z kimuraw $ 3# 4# Copyright (c) 2001-2003 FUJIMOTO Hisakuni 5# 6 7require 'test/unit' 8require 'osx/cocoa' 9 10system 'make -s' || raise(RuntimeError, "'make' failed") 11require 'objc_test.bundle' 12OSX.load_bridge_support_file 'ObjcPtrTest.bridgesupport' 13 14class TC_ObjcPtr < Test::Unit::TestCase 15 include OSX 16 17 def test_s_new 18 length = 123456 19 cptr = ObjcPtr.new( length ) 20 assert_kind_of( ObjcPtr, cptr ) 21 assert_equal( length, cptr.allocated_size ) 22 assert( ! cptr.tainted? ) 23 end 24 25 def test_s_new_with_type 26 assert_nothing_raised { ObjcPtr.new(:char) } 27 assert_nothing_raised { ObjcPtr.new(:uchar) } 28 assert_nothing_raised { ObjcPtr.new(:short) } 29 assert_nothing_raised { ObjcPtr.new(:ushort) } 30 assert_nothing_raised { ObjcPtr.new(:int) } 31 assert_nothing_raised { ObjcPtr.new(:uint) } 32 assert_nothing_raised { ObjcPtr.new(:long) } 33 assert_nothing_raised { ObjcPtr.new(:ulong) } 34 assert_nothing_raised { ObjcPtr.new(:longlong) } 35 assert_nothing_raised { ObjcPtr.new(:ulonglong) } 36 assert_nothing_raised { ObjcPtr.new(:float) } 37 assert_nothing_raised { ObjcPtr.new(:double) } 38 assert_raises(RuntimeError) { ObjcPtr.new(:unknown_type) } 39 40 assert_equal( 1, ObjcPtr.new(:char).allocated_size ) 41 assert_equal( 1, ObjcPtr.new(:uchar).allocated_size ) 42 assert_equal( 2, ObjcPtr.new(:short).allocated_size ) 43 assert_equal( 2, ObjcPtr.new(:ushort).allocated_size ) 44 # assert_equal( 4, ObjcPtr.new(:int).allocated_size ) 45 # assert_equal( 4, ObjcPtr.new(:uint).allocated_size ) 46 if OSX::RUBYCOCOA_BUILD_LP64 47 assert_equal( 8, ObjcPtr.new(:long).allocated_size ) 48 assert_equal( 8, ObjcPtr.new(:ulong).allocated_size ) 49 else 50 assert_equal( 4, ObjcPtr.new(:long).allocated_size ) 51 assert_equal( 4, ObjcPtr.new(:ulong).allocated_size ) 52 end 53 assert_equal( 8, ObjcPtr.new(:longlong).allocated_size ) 54 assert_equal( 8, ObjcPtr.new(:ulonglong).allocated_size ) 55 assert_equal( 4, ObjcPtr.new(:float).allocated_size ) 56 assert_equal( 8, ObjcPtr.new(:double).allocated_size ) 57 58 assert_equal( "c", ObjcPtr.new(:char).encoding ) 59 assert_equal( "C", ObjcPtr.new(:uchar).encoding ) 60 assert_equal( "s", ObjcPtr.new(:short).encoding ) 61 assert_equal( "S", ObjcPtr.new(:ushort).encoding ) 62 assert_equal( "i", ObjcPtr.new(:int).encoding ) 63 assert_equal( "I", ObjcPtr.new(:uint).encoding ) 64 assert_equal( "l", ObjcPtr.new(:long).encoding ) 65 assert_equal( "L", ObjcPtr.new(:ulong).encoding ) 66 assert_equal( "q", ObjcPtr.new(:longlong).encoding ) 67 assert_equal( "Q", ObjcPtr.new(:ulonglong).encoding ) 68 assert_equal( "f", ObjcPtr.new(:float).encoding ) 69 assert_equal( "d", ObjcPtr.new(:double).encoding ) 70 end 71 72 def test_s_new_with_type_and_count 73 assert_nothing_raised { ObjcPtr.new(:char, 17) } 74 assert_nothing_raised { ObjcPtr.new(:uchar, 17) } 75 assert_nothing_raised { ObjcPtr.new(:short, 17) } 76 assert_nothing_raised { ObjcPtr.new(:ushort, 17) } 77 assert_nothing_raised { ObjcPtr.new(:int, 17) } 78 assert_nothing_raised { ObjcPtr.new(:uint, 17) } 79 assert_nothing_raised { ObjcPtr.new(:long, 17) } 80 assert_nothing_raised { ObjcPtr.new(:ulong, 17) } 81 assert_nothing_raised { ObjcPtr.new(:longlong, 17) } 82 assert_nothing_raised { ObjcPtr.new(:ulonglong, 17) } 83 assert_nothing_raised { ObjcPtr.new(:float, 17) } 84 assert_nothing_raised { ObjcPtr.new(:double, 17) } 85 assert_raises(RuntimeError) { ObjcPtr.new(:unknown_type, 17) } 86 87 assert_equal( 1 * 17, ObjcPtr.new(:char, 17).allocated_size ) 88 assert_equal( 1 * 17, ObjcPtr.new(:uchar, 17).allocated_size ) 89 assert_equal( 2 * 17, ObjcPtr.new(:short, 17).allocated_size ) 90 assert_equal( 2 * 17, ObjcPtr.new(:ushort, 17).allocated_size ) 91 # assert_equal( 4 * 17, ObjcPtr.new(:int).allocated_size ) 92 # assert_equal( 4 * 17, ObjcPtr.new(:uint).allocated_size ) 93 if OSX::RUBYCOCOA_BUILD_LP64 94 assert_equal( 8 * 17, ObjcPtr.new(:long, 17).allocated_size ) 95 assert_equal( 8 * 17, ObjcPtr.new(:ulong, 17).allocated_size ) 96 else 97 assert_equal( 4 * 17, ObjcPtr.new(:long, 17).allocated_size ) 98 assert_equal( 4 * 17, ObjcPtr.new(:ulong, 17).allocated_size ) 99 end 100 assert_equal( 8 * 17, ObjcPtr.new(:longlong, 17).allocated_size ) 101 assert_equal( 8 * 17, ObjcPtr.new(:ulonglong, 17).allocated_size ) 102 assert_equal( 4 * 17, ObjcPtr.new(:float, 17).allocated_size ) 103 assert_equal( 8 * 17, ObjcPtr.new(:double, 17).allocated_size ) 104 105 assert_equal( "c", ObjcPtr.new(:char, 17).encoding ) 106 assert_equal( "C", ObjcPtr.new(:uchar, 17).encoding ) 107 assert_equal( "s", ObjcPtr.new(:short, 17).encoding ) 108 assert_equal( "S", ObjcPtr.new(:ushort, 17).encoding ) 109 assert_equal( "i", ObjcPtr.new(:int, 17).encoding ) 110 assert_equal( "I", ObjcPtr.new(:uint, 17).encoding ) 111 assert_equal( "l", ObjcPtr.new(:long, 17).encoding ) 112 assert_equal( "L", ObjcPtr.new(:ulong, 17).encoding ) 113 assert_equal( "q", ObjcPtr.new(:longlong, 17).encoding ) 114 assert_equal( "Q", ObjcPtr.new(:ulonglong, 17).encoding ) 115 assert_equal( "f", ObjcPtr.new(:float, 17).encoding ) 116 assert_equal( "d", ObjcPtr.new(:double, 17).encoding ) 117 end 118 119 def test_s_allocate_as_int8 120 cptr = ObjcPtr.allocate_as_int8 121 assert_kind_of( ObjcPtr, cptr ) 122 assert_equal( 1, cptr.allocated_size ) 123 assert( ! cptr.tainted? ) 124 end 125 126 def test_s_allocate_as_int16 127 cptr = ObjcPtr.allocate_as_int16 128 assert_kind_of( ObjcPtr, cptr ) 129 assert_equal( 2, cptr.allocated_size ) 130 assert( ! cptr.tainted? ) 131 end 132 133 def test_s_allocate_as_int32 134 cptr = ObjcPtr.allocate_as_int32 135 assert_kind_of( ObjcPtr, cptr ) 136 assert_equal( 4, cptr.allocated_size ) 137 assert( ! cptr.tainted? ) 138 end 139 140 def test_s_allocate_as_bool 141 cptr = ObjcPtr.allocate_as_bool 142 assert_kind_of( ObjcPtr, cptr ) 143 assert_equal( 1, cptr.allocated_size ) 144 assert( ! cptr.tainted? ) 145 end 146 147 def test_s_allocate_as_int 148 cptr = ObjcPtr.allocate_as_int 149 assert_kind_of( ObjcPtr, cptr ) 150 assert_equal( 4, cptr.allocated_size ) 151 assert( ! cptr.tainted? ) 152 end 153 154 def test_cptr_as_returned_value_of_method_call 155 cptr = NSData.dataWithContentsOfFile('/etc/passwd').bytes 156 assert_kind_of( ObjcPtr, cptr ) 157 assert_equal( 0, cptr.allocated_size ) 158 assert( cptr.tainted? ) 159 end 160 161 def test_cptr_as_param_of_method_call 162 src = 'hello world' 163 data = NSData.dataWithRubyString( src ) 164 cptr = ObjcPtr.new( src.size ) 165 data.getBytes_length( cptr ) 166 assert_equal( src.size, cptr.allocated_size ) 167 end 168 169 def test_bytestr_at 170 src = 'hello world' 171 cptr = NSData.dataWithRubyString(src).bytes 172 bstr = cptr.bytestr_at(3,4) 173 assert_equal( src[3,4], bstr ) 174 assert( bstr.tainted? ) 175 end 176 177 def test_bytestr 178 src = 'hello world' 179 cptr = NSData.dataWithRubyString(src).bytes 180 bstr = cptr.bytestr(src.size) 181 assert_equal( src, bstr ) 182 assert( bstr.tainted? ) 183 end 184 185 def test_ocptr_ary_like 186 components = [0.1, 0.5, 0.9, 0] 187 color = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components) 188 # FIXME: color becomes NSObject(__NSCFType) via toll-free on 10.6 189 #assert_kind_of(CGColorRef, color) 190 components2 = CGColorGetComponents(color) 191 assert((components2[0] >= 0.09 and components2[0] <= 0.11)) 192 assert((components2[1] >= 0.49 and components2[1] <= 0.51)) 193 assert((components2[2] >= 0.89 and components2[2] <= 0.91)) 194 end 195 196 def test_ocptr_aref 197 nearly_equal = proc do |v0,v1| 198 x = (v0 - v1).to_f 199 (-0.01 <= x && x <= 0.01) ? true : false 200 end 201 202 components = [0.1, 0.5, 0.9, 0] 203 color = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components) 204 # FIXME: color becomes NSObject(__NSCFType) via toll-free on 10.6 205 #assert_kind_of(CGColorRef, color) 206 components2 = CGColorGetComponents(color) 207 ary = nil 208 assert_nothing_raised { ary = components2[0, 4] } 209 assert_equal( true, nearly_equal.call( ary[0], 0.1 )) 210 assert_equal( true, nearly_equal.call( ary[1], 0.5 )) 211 assert_equal( true, nearly_equal.call( ary[2], 0.9 )) 212 assert_equal( true, nearly_equal.call( ary[3], 0.0 )) 213 end 214 215 def test_ocptr_int_assign 216 obj = ObjcPtr.new(:int) 217 obj.assign(123) 218 assert_equal(123, obj.int) 219 number = NSNumber.numberWithInt(42) 220 obj.assign(number) 221 assert_equal(42, obj.int) 222 assert_raises(ArgumentError) { obj.assign('foo') } 223 end 224 225 def test_ocptr_ary_assign 226 str = 'foobar' 227 obj = ObjcPtr.new(:char, str.length) 228 str.length.times { |i| obj[i] = str[i] } 229 assert_equal('foobar', obj.bytestr) 230 assert_raises(ArgumentError) { obj[0] = 'blah' } 231 end 232 233 def test_ocptr_as_id 234 obj = ObjcPtrTest.new.returnVoidPtrForArrayOfString 235 ary = obj.cast_as('@') 236 assert_kind_of(OSX::NSArray, ary) 237 assert_kind_of(OSX::NSString, ary.first) 238 239 obj = ObjcPtrTest.new.returnVoidPtrForKCFBooleanTrue 240 assert_equal(true, obj.cast_as('@').boolValue) 241 242 obj = ObjcPtrTest.new.returnVoidPtrForKCFBooleanFalse 243 assert_equal(false, obj.cast_as('@').boolValue) 244 245 obj = ObjcPtrTest.new.returnVoidPtrForInt 246 assert_equal(-2147483648, obj.cast_as('^i')) 247 248 obj = ObjcPtrTest.new.returnVoidPtrForUInt 249 assert_equal(4294967295, obj.cast_as('^I')) 250 251 obj = ObjcPtrTest.new.returnVoidPtrForCStr 252 assert_equal('foobar', obj.cast_as('*')) 253 end 254 255# rb_define_method (_kObjcPtr, "int8_at", rb_objcptr_int8_at, 1); 256# rb_define_method (_kObjcPtr, "uint8_at", rb_objcptr_uint8_at, 1); 257# rb_define_method (_kObjcPtr, "int16_at", rb_objcptr_int16_at, 1); 258# rb_define_method (_kObjcPtr, "uint16_at", rb_objcptr_uint16_at, 1); 259# rb_define_method (_kObjcPtr, "int32_at", rb_objcptr_int32_at, 1); 260# rb_define_method (_kObjcPtr, "uint32_at", rb_objcptr_uint32_at, 1); 261# rb_define_alias (_kObjcPtr, "int_at", "int32_at"); 262# rb_define_alias (_kObjcPtr, "uint_at", "uint32_at"); 263# rb_define_alias (_kObjcPtr, "bool_at", "uint8_at"); 264 265# rb_define_method (_kObjcPtr, "int8", rb_objcptr_int8, 0); 266# rb_define_method (_kObjcPtr, "uint8", rb_objcptr_uint8, 0); 267# rb_define_method (_kObjcPtr, "int16", rb_objcptr_int16, 0); 268# rb_define_method (_kObjcPtr, "uint16", rb_objcptr_uint16, 0); 269# rb_define_method (_kObjcPtr, "int32", rb_objcptr_int32, 0); 270# rb_define_method (_kObjcPtr, "uint32", rb_objcptr_uint32, 0); 271# rb_define_alias (_kObjcPtr, "int", "int32"); 272# rb_define_alias (_kObjcPtr, "uint", "uint32"); 273# rb_define_alias (_kObjcPtr, "bool", "uint8"); 274 275end 276