public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / test / test_memory.rb
100644 52 lines (45 sloc) 1.165 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
#
# test_memory.rb
#
# Tests RubyObjC memory management.
#
# Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
# For more information about this file, visit http://www.rubyobjc.com.
#
 
require 'test/unit'
require 'lib/objc'
require 'test/testmemory'
 
class MyString < ObjC::NSObject
  use "initWithString:", "@@:@"
  def initWithString_(s)
    init
    @string = s.to_s
    self
  end
  def string
    @string
  end
end
 
N = 10
 
class TestMemory < Test::Unit::TestCase
 
  # objects created from Ruby and owned by Objective-C objects must be preserved through GC
  def test_gc_from_ruby
    array = ObjC::NSMutableArray.alloc.init
    100.times {|i|
      array.addObject_(MyString.alloc.initWithString_("thing #{i}"))
    }
    GC.start
    array.each_with_index {|thing, i|
      assert_equal "thing #{i}", thing.string if i > (100-N)
    }
  end
 
  # objects created from Objective-C and owned by Objective-C objects must be preserved through GC
  def test_gc_from_objc
    tester = ObjC::MemoryTester.alloc.init
    tester.createObjects_(N)
    GC.start
    N.times {|i|
      assert_equal "item #{i}", tester.objectAtIndex_(i).string
    }
  end
end