[331] | 1 | ##
|
---|
| 2 | # InlineStruct Test
|
---|
| 3 |
|
---|
| 4 | class InlineStructTest
|
---|
| 5 | def extra_method
|
---|
| 6 | :ok
|
---|
| 7 | end
|
---|
| 8 |
|
---|
| 9 | def test_ivar_set
|
---|
| 10 | @var = :ivar
|
---|
| 11 | end
|
---|
| 12 |
|
---|
| 13 | def test_ivar_get
|
---|
| 14 | @vat
|
---|
| 15 | end
|
---|
| 16 | end
|
---|
| 17 |
|
---|
| 18 | assert('InlineStructTest#dup') do
|
---|
| 19 | obj = InlineStructTest.new(1)
|
---|
| 20 | assert_equal obj.to_s, 'fixnum(1)'
|
---|
| 21 | assert_equal obj.dup.to_s, 'fixnum(1)'
|
---|
| 22 | end
|
---|
| 23 |
|
---|
| 24 | assert('InlineStructTest#clone') do
|
---|
| 25 | obj = InlineStructTest.new(1)
|
---|
| 26 | assert_equal obj.to_s, 'fixnum(1)'
|
---|
| 27 | assert_equal obj.clone.to_s, 'fixnum(1)'
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | assert('InlineStruct#object_id') do
|
---|
| 31 | obj1 = InlineStructTest.new(1)
|
---|
| 32 | obj2 = InlineStructTest.new(1)
|
---|
| 33 | assert_not_equal obj1, obj2
|
---|
| 34 | assert_not_equal obj1.object_id, obj2.object_id
|
---|
| 35 | assert_not_equal obj1.object_id, obj1.dup.object_id
|
---|
| 36 | assert_not_equal obj1.object_id, obj1.clone.object_id
|
---|
| 37 | end
|
---|
| 38 |
|
---|
| 39 | assert('InlineStructTest#mutate (dup)') do
|
---|
| 40 | obj1 = InlineStructTest.new("foo")
|
---|
| 41 | assert_equal obj1.to_s, "string(foo)"
|
---|
| 42 | obj2 = obj1.dup
|
---|
| 43 | assert_equal obj2.to_s, "string(foo)"
|
---|
| 44 | obj1.mutate
|
---|
| 45 | assert_equal obj1.to_s, "mutate(foo)"
|
---|
| 46 | assert_equal obj2.to_s, "string(foo)"
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | assert('InlineStructTest#mutate (clone)') do
|
---|
| 50 | obj1 = InlineStructTest.new("foo")
|
---|
| 51 | assert_equal obj1.to_s, "string(foo)"
|
---|
| 52 | obj2 = obj1.clone
|
---|
| 53 | assert_equal obj2.to_s, "string(foo)"
|
---|
| 54 | obj1.mutate
|
---|
| 55 | assert_equal obj1.to_s, "mutate(foo)"
|
---|
| 56 | assert_equal obj2.to_s, "string(foo)"
|
---|
| 57 | end
|
---|
| 58 |
|
---|
| 59 | assert('InlineStructTest#test_receive(string)') do
|
---|
| 60 | assert_equal InlineStructTest.test_receive(InlineStructTest.new('a')), true
|
---|
| 61 | end
|
---|
| 62 |
|
---|
| 63 | assert('InlineStructTest#test_receive(float)') do
|
---|
| 64 | assert_equal InlineStructTest.test_receive(InlineStructTest.new(1.25)), false
|
---|
| 65 | end
|
---|
| 66 |
|
---|
| 67 | assert('InlineStructTest#test_receive(invalid object)') do
|
---|
| 68 | assert_raise(TypeError) do
|
---|
| 69 | InlineStructTest.test_receive([])
|
---|
| 70 | end
|
---|
| 71 | end
|
---|
| 72 |
|
---|
| 73 | assert('InlineStructTest#test_receive(string)') do
|
---|
| 74 | assert_equal InlineStructTest.test_receive_direct(InlineStructTest.new('a')), true
|
---|
| 75 | end
|
---|
| 76 |
|
---|
| 77 | assert('InlineStructTest#test_receive(float)') do
|
---|
| 78 | assert_equal InlineStructTest.test_receive_direct(InlineStructTest.new(1.25)), false
|
---|
| 79 | end
|
---|
| 80 |
|
---|
| 81 | assert('InlineStructTest#test_receive(invalid object)') do
|
---|
| 82 | assert_raise(TypeError) do
|
---|
| 83 | InlineStructTest.test_receive_direct([])
|
---|
| 84 | end
|
---|
| 85 | end
|
---|
| 86 |
|
---|
| 87 | assert('InlineStructTest#extra_method') do
|
---|
| 88 | assert_equal InlineStructTest.new(1).extra_method, :ok
|
---|
| 89 | end
|
---|
| 90 |
|
---|
| 91 | assert('InlineStructTest instance variable') do
|
---|
| 92 | obj = InlineStructTest.new(1)
|
---|
| 93 | assert_raise(ArgumentError) do
|
---|
| 94 | obj.test_ivar_set
|
---|
| 95 | end
|
---|
| 96 | assert_equal obj.test_ivar_get, nil
|
---|
| 97 | end
|
---|
| 98 |
|
---|
| 99 | # 64-bit mode
|
---|
| 100 | if InlineStructTest.length == 24
|
---|
| 101 | assert('InlineStructTest length [64 bit]') do
|
---|
| 102 | assert_equal InlineStructTest.length, 3 * 8
|
---|
| 103 | end
|
---|
| 104 |
|
---|
| 105 | assert('InlineStructTest w/float [64 bit]') do
|
---|
| 106 | obj = InlineStructTest.new(1.25)
|
---|
| 107 | assert_equal obj.to_s, "float(1.250)"
|
---|
| 108 | end
|
---|
| 109 |
|
---|
| 110 | assert('InlineStructTest w/fixnum [64 bit]') do
|
---|
| 111 | obj = InlineStructTest.new(42)
|
---|
| 112 | assert_equal obj.to_s, "fixnum(42)"
|
---|
| 113 | end
|
---|
| 114 |
|
---|
| 115 | assert('InlineStructTest w/string [64 bit]') do
|
---|
| 116 | obj = InlineStructTest.new("hello")
|
---|
| 117 | assert_equal obj.to_s, "string(hello)"
|
---|
| 118 | end
|
---|
| 119 |
|
---|
| 120 | assert('InlineStructTest w/long string [64 bit]') do
|
---|
| 121 | obj = InlineStructTest.new("this won't fit in 3 * 8 bytes available for the structure")
|
---|
| 122 | assert_equal obj.to_s, "string(this won't fit i"
|
---|
| 123 | end
|
---|
| 124 | end
|
---|
| 125 |
|
---|
| 126 | # 32-bit mode
|
---|
| 127 | if InlineStructTest.length == 12
|
---|
| 128 | assert('InlineStructTest length [32 bit]') do
|
---|
| 129 | assert_equal InlineStructTest.length, 3 * 4
|
---|
| 130 | end
|
---|
| 131 |
|
---|
| 132 | assert('InlineStructTest w/float [32 bit]') do
|
---|
| 133 | obj = InlineStructTest.new(1.25)
|
---|
| 134 | assert_equal obj.to_s, "float(1.250"
|
---|
| 135 | end
|
---|
| 136 |
|
---|
| 137 | assert('InlineStructTest w/fixnum [32 bit]') do
|
---|
| 138 | obj = InlineStructTest.new(42)
|
---|
| 139 | assert_equal obj.to_s, "fixnum(42)"
|
---|
| 140 | end
|
---|
| 141 |
|
---|
| 142 | assert('InlineStructTest w/string [32 bit]') do
|
---|
| 143 | obj = InlineStructTest.new("hello")
|
---|
| 144 | assert_equal obj.to_s, "string(hell"
|
---|
| 145 | end
|
---|
| 146 |
|
---|
| 147 | assert('InlineStructTest w/long string [32 bit]') do
|
---|
| 148 | obj = InlineStructTest.new("this won't fit in 3 * 4 bytes available for the structure")
|
---|
| 149 | assert_equal obj.to_s, "string(this"
|
---|
| 150 | end
|
---|
| 151 | end
|
---|