public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / test / test_structs.rb
100644 100 lines (81 sloc) 3.02 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
#
# test_structs.rb
#
# Tests RubyObjC support for bridged structures.
#
# Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
# For more information about this file, visit http://www.rubyobjc.com.
#
 
require 'test/unit'
require 'test/teststructs'
require 'lib/objc'
 
def assert_arrays_in_delta(a, b, delta, message=nil)
  a.size.times{|i|
    assert_in_delta(a[i], b[i], delta, message)
  }
end
 
class Array
  def scaleBy(scale)
    map{|x| scale*x}
  end
end
 
class ObjC::StructTester
  use "rubyScaleSize:by:", "{_NSSize=ff}@:{_NSSize=ff}f"
  def rubyScaleSize_by_(size, scale)
    #puts "rubyScaleSize(#{size.inspect})"
    result = size.scaleBy(scale)
    #puts "produced #{result.inspect}"
    result
  end
 
  use "rubyScalePoint:by:", "{_NSPoint=ff}@:{_NSPoint=ff}f"
  def rubyScalePoint_by_(point, scale)
    #puts "rubyScalePoint(#{point.inspect})"
    result = point.scaleBy(scale)
    #puts "produced #{point.inspect}"
    result
  end
 
  use "rubyScaleRect:by:", "{_NSRect={_NSPoint=ff}{_NSSize=ff}}@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}f"
  def rubyScaleRect_by_(rect, scale)
    $result = rect.scaleBy(scale)
    #puts "rubyScaleRect(#{rect.inspect}, #{scale}) produced #{result.inspect}"
    $result
  end
 
  use "rubyScaleRange:by:", "{_NSRange=II}@:{_NSRange=II}f"
  def rubyScaleRange_by_(range, scale)
    #puts "rubyScaleRange(#{range.inspect})"
    result = range.scaleBy(scale)
    #puts "produced #{result.inspect}"
    result
  end
end
 
#
# Ruby calls Objective-C, which in turn calls Ruby to scale the specified objects
#
class TestObjCStructs < Test::Unit::TestCase
  def test_structs
    tester = ObjC::StructTester.alloc.init
 
    delta = 0.00001
 
    size = [2,4]; s = 2
    assert_arrays_in_delta(size.scaleBy(s), tester.scaleSize_by_(size, s), delta)
 
    size = [2.0,4.0]; s = 3.1415927**2
    assert_arrays_in_delta(size.scaleBy(s), tester.scaleSize_by_(size, s), delta)
 
    point = [1,2]; s = 4
    assert_arrays_in_delta(point.scaleBy(s), tester.scalePoint_by_(point, s), delta)
 
    point = [2.0,4.0]; s = 3.1415
    assert_arrays_in_delta(point.scaleBy(s), tester.scalePoint_by_(point, s), delta)
    rect = [1,1,1,1]; s = 2
# assert_arrays_in_delta(rect.scaleBy(s), tester.scaleRect_by_(rect, s), delta)
 
    rect = [1,2,3,4]; s = 32.1
# assert_arrays_in_delta(rect.scaleBy(s), tester.scaleRect_by_(rect, s), delta)
    range = [1,2]; s = -10
# assert_arrays_in_delta(range.scaleBy(s), tester.scaleRange_by_(range, s), delta)
 
    scaleSize = ObjC::Function.wrap("scaleSize", "{_NSSize=ff}", ["{_NSSize=ff}", "f"])
 
    size = [3,4]; s = 2
    assert_arrays_in_delta(size.scaleBy(s), scaleSize.call(size, s), delta)
 
    size = [2.0, 4.0]
    assert_arrays_in_delta(size.scaleBy(s), scaleSize.call(size, s), delta)
 
    scaleRect = ObjC::Function.wrap("scaleRect", "{_NSRect={_NSPoint=ff}{_NSSize=ff}}", ["{_NSRect={_NSPoint=ff}{_NSSize=ff}}", "f"])
    rect = [1,2,3,4]; s = 32.1
    assert_arrays_in_delta(rect.scaleBy(s), scaleRect.call(rect, s), delta)
  end
end