1require 'test/unit' 2 3class TestNum2int < Test::Unit::TestCase 4 module Num2int 5 end 6 require '-test-/num2int/num2int' 7 8 SHRT_MIN = -32768 9 SHRT_MAX = 32767 10 USHRT_MAX = 65535 11 12 INT_MIN = -2147483648 13 INT_MAX = 2147483647 14 UINT_MAX = 4294967295 15 16 case [0].pack('L!').size 17 when 4 18 LONG_MAX = 2147483647 19 LONG_MIN = -2147483648 20 ULONG_MAX = 4294967295 21 when 8 22 LONG_MAX = 9223372036854775807 23 LONG_MIN = -9223372036854775808 24 ULONG_MAX = 18446744073709551615 25 end 26 27 LLONG_MAX = 9223372036854775807 28 LLONG_MIN = -9223372036854775808 29 ULLONG_MAX = 18446744073709551615 30 31 FIXNUM_MAX = LONG_MAX/2 32 FIXNUM_MIN = LONG_MIN/2 33 34 def test_num2short 35 assert_output(SHRT_MIN.to_s) do 36 Num2int.print_num2short(SHRT_MIN) 37 end 38 assert_output(SHRT_MAX.to_s) do 39 Num2int.print_num2short(SHRT_MAX) 40 end 41 assert_raise(RangeError) do 42 Num2int.print_num2short(SHRT_MIN-1) 43 end 44 assert_raise(RangeError) do 45 Num2int.print_num2short(SHRT_MAX+1) 46 end 47 end 48 49 def test_num2ushort 50 assert_output("0") do 51 Num2int.print_num2ushort(0) 52 end 53 assert_output(USHRT_MAX.to_s) do 54 Num2int.print_num2ushort(USHRT_MAX) 55 end 56 assert_output(USHRT_MAX.to_s) do 57 Num2int.print_num2ushort(-1) 58 end 59 assert_output((SHRT_MAX+1).to_s) do 60 Num2int.print_num2ushort(SHRT_MIN) 61 end 62 assert_raise(RangeError) do 63 Num2int.print_num2ushort(SHRT_MIN-1) 64 end 65 assert_raise(RangeError) do 66 Num2int.print_num2ushort(USHRT_MAX+1) 67 end 68 end 69 70 def test_num2int 71 assert_output(INT_MIN.to_s) do 72 Num2int.print_num2int(INT_MIN) 73 end 74 assert_output(INT_MAX.to_s) do 75 Num2int.print_num2int(INT_MAX) 76 end 77 assert_raise(RangeError) do 78 Num2int.print_num2int(INT_MIN-1) 79 end 80 assert_raise(RangeError) do 81 Num2int.print_num2int(INT_MAX+1) 82 end 83 end 84 85 def test_num2uint 86 assert_output("0") do 87 Num2int.print_num2uint(0) 88 end 89 assert_output(UINT_MAX.to_s) do 90 Num2int.print_num2uint(UINT_MAX) 91 end 92 assert_output(UINT_MAX.to_s) do 93 Num2int.print_num2uint(-1) 94 end 95 assert_output((INT_MAX+1).to_s) do 96 Num2int.print_num2uint(INT_MIN) 97 end 98 assert_raise(RangeError) do 99 Num2int.print_num2uint(INT_MIN-1) 100 end 101 assert_raise(RangeError) do 102 Num2int.print_num2uint(UINT_MAX+1) 103 end 104 end 105 106 def test_num2long 107 assert_output(LONG_MIN.to_s) do 108 Num2int.print_num2long(LONG_MIN) 109 end 110 assert_output(LONG_MAX.to_s) do 111 Num2int.print_num2long(LONG_MAX) 112 end 113 assert_raise(RangeError) do 114 Num2int.print_num2long(LONG_MIN-1) 115 end 116 assert_raise(RangeError) do 117 Num2int.print_num2long(LONG_MAX+1) 118 end 119 assert_output(FIXNUM_MIN.to_s) do 120 Num2int.print_num2long(FIXNUM_MIN) 121 end 122 assert_output((FIXNUM_MIN-1).to_s) do 123 Num2int.print_num2long(FIXNUM_MIN-1) 124 end 125 assert_output(FIXNUM_MAX.to_s) do 126 Num2int.print_num2long(FIXNUM_MAX) 127 end 128 assert_output((FIXNUM_MAX+1).to_s) do 129 Num2int.print_num2long(FIXNUM_MAX+1) 130 end 131 end 132 133 def test_num2ulong 134 assert_output("0") do 135 Num2int.print_num2ulong(0) 136 end 137 assert_output(ULONG_MAX.to_s) do 138 Num2int.print_num2ulong(ULONG_MAX) 139 end 140 assert_output(ULONG_MAX.to_s) do 141 Num2int.print_num2ulong(-1) 142 end 143 assert_output((LONG_MAX+1).to_s) do 144 Num2int.print_num2ulong(LONG_MIN) 145 end 146 assert_raise(RangeError) do 147 Num2int.print_num2ulong(LONG_MIN-1) 148 end 149 assert_raise(RangeError) do 150 Num2int.print_num2ulong(ULONG_MAX+1) 151 end 152 assert_output((ULONG_MAX-FIXNUM_MAX).to_s) do 153 Num2int.print_num2ulong(FIXNUM_MIN) 154 end 155 assert_output((ULONG_MAX-FIXNUM_MAX-1).to_s) do 156 Num2int.print_num2ulong(FIXNUM_MIN-1) 157 end 158 assert_output(FIXNUM_MAX.to_s) do 159 Num2int.print_num2ulong(FIXNUM_MAX) 160 end 161 assert_output((FIXNUM_MAX+1).to_s) do 162 Num2int.print_num2ulong(FIXNUM_MAX+1) 163 end 164 end 165 166 def test_num2ll 167 assert_output(LONG_MIN.to_s) do 168 Num2int.print_num2ll(LONG_MIN) 169 end 170 assert_output(LLONG_MAX.to_s) do 171 Num2int.print_num2ll(LLONG_MAX) 172 end 173 assert_raise(RangeError) do 174 Num2int.print_num2ll(LLONG_MIN-1) 175 end 176 assert_raise(RangeError) do 177 Num2int.print_num2ll(LLONG_MAX+1) 178 end 179 assert_output(FIXNUM_MIN.to_s) do 180 Num2int.print_num2ll(FIXNUM_MIN) 181 end 182 assert_output((FIXNUM_MIN-1).to_s) do 183 Num2int.print_num2ll(FIXNUM_MIN-1) 184 end 185 assert_output(FIXNUM_MAX.to_s) do 186 Num2int.print_num2ll(FIXNUM_MAX) 187 end 188 assert_output((FIXNUM_MAX+1).to_s) do 189 Num2int.print_num2ll(FIXNUM_MAX+1) 190 end 191 end if defined?(Num2int.print_num2ll) 192 193 def test_num2ull 194 assert_output("0") do 195 Num2int.print_num2ull(0) 196 end 197 assert_output(ULLONG_MAX.to_s) do 198 Num2int.print_num2ull(ULLONG_MAX) 199 end 200 assert_output(ULLONG_MAX.to_s) do 201 Num2int.print_num2ull(-1) 202 end 203 assert_output((LLONG_MAX+1).to_s) do 204 Num2int.print_num2ull(LLONG_MIN) 205 end 206 assert_raise(RangeError) do 207 Num2int.print_num2ull(LLONG_MIN-1) 208 end 209 assert_raise(RangeError) do 210 Num2int.print_num2ull(ULLONG_MAX+1) 211 end 212 assert_output((ULLONG_MAX-FIXNUM_MAX).to_s) do 213 Num2int.print_num2ull(FIXNUM_MIN) 214 end 215 assert_output((ULLONG_MAX-FIXNUM_MAX-1).to_s) do 216 Num2int.print_num2ull(FIXNUM_MIN-1) 217 end 218 assert_output(FIXNUM_MAX.to_s) do 219 Num2int.print_num2ull(FIXNUM_MAX) 220 end 221 assert_output((FIXNUM_MAX+1).to_s) do 222 Num2int.print_num2ull(FIXNUM_MAX+1) 223 end 224 end if defined?(Num2int.print_num2ull) 225end 226 227 228