1require 'test/unit' 2require_relative 'envutil' 3 4# use of $= is deprecated after 1.7.1 5def pre_1_7_1 6end 7 8class TestString < Test::Unit::TestCase 9 10 def initialize(*args) 11 @cls = String 12 @aref_re_nth = true 13 @aref_re_silent = false 14 @aref_slicebang_silent = true 15 super 16 end 17 18 def S(str) 19 @cls.new(str) 20 end 21 22 def test_s_new 23 assert_equal("RUBY", S("RUBY")) 24 end 25 26 def test_AREF # '[]' 27 assert_equal("A", S("AooBar")[0]) 28 assert_equal("B", S("FooBaB")[-1]) 29 assert_equal(nil, S("FooBar")[6]) 30 assert_equal(nil, S("FooBar")[-7]) 31 32 assert_equal(S("Foo"), S("FooBar")[0,3]) 33 assert_equal(S("Bar"), S("FooBar")[-3,3]) 34 assert_equal(S(""), S("FooBar")[6,2]) 35 assert_equal(nil, S("FooBar")[-7,10]) 36 37 assert_equal(S("Foo"), S("FooBar")[0..2]) 38 assert_equal(S("Foo"), S("FooBar")[0...3]) 39 assert_equal(S("Bar"), S("FooBar")[-3..-1]) 40 assert_equal(S(""), S("FooBar")[6..2]) 41 assert_equal(nil, S("FooBar")[-10..-7]) 42 43 assert_equal(S("Foo"), S("FooBar")[/^F../]) 44 assert_equal(S("Bar"), S("FooBar")[/..r$/]) 45 assert_equal(nil, S("FooBar")[/xyzzy/]) 46 assert_equal(nil, S("FooBar")[/plugh/]) 47 48 assert_equal(S("Foo"), S("FooBar")[S("Foo")]) 49 assert_equal(S("Bar"), S("FooBar")[S("Bar")]) 50 assert_equal(nil, S("FooBar")[S("xyzzy")]) 51 assert_equal(nil, S("FooBar")[S("plugh")]) 52 53 if @aref_re_nth 54 assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1]) 55 assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2]) 56 assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3]) 57 assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1]) 58 assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2]) 59 assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3]) 60 end 61 62 o = Object.new 63 def o.to_int; 2; end 64 assert_equal("o", "foo"[o]) 65 66 assert_raise(ArgumentError) { "foo"[] } 67 end 68 69 def test_ASET # '[]=' 70 s = S("FooBar") 71 s[0] = S('A') 72 assert_equal(S("AooBar"), s) 73 74 s[-1]= S('B') 75 assert_equal(S("AooBaB"), s) 76 assert_raise(IndexError) { s[-7] = S("xyz") } 77 assert_equal(S("AooBaB"), s) 78 s[0] = S("ABC") 79 assert_equal(S("ABCooBaB"), s) 80 81 s = S("FooBar") 82 s[0,3] = S("A") 83 assert_equal(S("ABar"),s) 84 s[0] = S("Foo") 85 assert_equal(S("FooBar"), s) 86 s[-3,3] = S("Foo") 87 assert_equal(S("FooFoo"), s) 88 assert_raise(IndexError) { s[7,3] = S("Bar") } 89 assert_raise(IndexError) { s[-7,3] = S("Bar") } 90 91 s = S("FooBar") 92 s[0..2] = S("A") 93 assert_equal(S("ABar"), s) 94 s[1..3] = S("Foo") 95 assert_equal(S("AFoo"), s) 96 s[-4..-4] = S("Foo") 97 assert_equal(S("FooFoo"), s) 98 assert_raise(RangeError) { s[7..10] = S("Bar") } 99 assert_raise(RangeError) { s[-7..-10] = S("Bar") } 100 101 s = S("FooBar") 102 s[/^F../]= S("Bar") 103 assert_equal(S("BarBar"), s) 104 s[/..r$/] = S("Foo") 105 assert_equal(S("BarFoo"), s) 106 if @aref_re_silent 107 s[/xyzzy/] = S("None") 108 assert_equal(S("BarFoo"), s) 109 else 110 assert_raise(IndexError) { s[/xyzzy/] = S("None") } 111 end 112 if @aref_re_nth 113 s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo") 114 assert_equal(S("FooFoo"), s) 115 s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar") 116 assert_equal(S("FooBar"), s) 117 assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" } 118 s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo") 119 assert_equal(S("FooFoo"), s) 120 s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar") 121 assert_equal(S("BarFoo"), s) 122 assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" } 123 end 124 125 s = S("FooBar") 126 s[S("Foo")] = S("Bar") 127 assert_equal(S("BarBar"), s) 128 129 pre_1_7_1 do 130 s = S("FooBar") 131 s[S("Foo")] = S("xyz") 132 assert_equal(S("xyzBar"), s) 133 134 $= = true 135 s = S("FooBar") 136 s[S("FOO")] = S("Bar") 137 assert_equal(S("BarBar"), s) 138 s[S("FOO")] = S("xyz") 139 assert_equal(S("BarBar"), s) 140 $= = false 141 end 142 143 s = S("a string") 144 s[0..s.size] = S("another string") 145 assert_equal(S("another string"), s) 146 147 o = Object.new 148 def o.to_int; 2; end 149 s = "foo" 150 s[o] = "bar" 151 assert_equal("fobar", s) 152 153 assert_raise(ArgumentError) { "foo"[1, 2, 3] = "" } 154 end 155 156 def test_CMP # '<=>' 157 assert_equal(1, S("abcdef") <=> S("abcde")) 158 assert_equal(0, S("abcdef") <=> S("abcdef")) 159 assert_equal(-1, S("abcde") <=> S("abcdef")) 160 161 assert_equal(-1, S("ABCDEF") <=> S("abcdef")) 162 163 pre_1_7_1 do 164 $= = true 165 assert_equal(0, S("ABCDEF") <=> S("abcdef")) 166 $= = false 167 end 168 169 assert_nil("foo" <=> Object.new) 170 171 o = Object.new 172 def o.to_str; "bar"; end 173 assert_equal(1, "foo" <=> o) 174 175 class << o;remove_method :to_str;end 176 def o.<=>(x); nil; end 177 assert_nil("foo" <=> o) 178 179 class << o;remove_method :<=>;end 180 def o.<=>(x); 1; end 181 assert_equal(-1, "foo" <=> o) 182 183 class << o;remove_method :<=>;end 184 def o.<=>(x); 2**100; end 185 assert_equal(-1, "foo" <=> o) 186 end 187 188 def test_EQUAL # '==' 189 assert_equal(false, S("foo") == :foo) 190 assert(S("abcdef") == S("abcdef")) 191 192 pre_1_7_1 do 193 $= = true 194 assert(S("CAT") == S('cat')) 195 assert(S("CaT") == S('cAt')) 196 $= = false 197 end 198 199 assert(S("CAT") != S('cat')) 200 assert(S("CaT") != S('cAt')) 201 202 o = Object.new 203 def o.to_str; end 204 def o.==(x); false; end 205 assert_equal(false, "foo" == o) 206 class << o;remove_method :==;end 207 def o.==(x); true; end 208 assert_equal(true, "foo" == o) 209 end 210 211 def test_LSHIFT # '<<' 212 assert_equal(S("world!"), S("world") << 33) 213 assert_equal(S("world!"), S("world") << S("!")) 214 215 s = "a" 216 10.times {|i| 217 s << s 218 assert_equal("a" * (2 << i), s) 219 } 220 221 s = ["foo"].pack("p") 222 l = s.size 223 s << "bar" 224 assert_equal(l + 3, s.size) 225 226 bug = '[ruby-core:27583]' 227 assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -3} 228 assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -2} 229 assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -1} 230 assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << 0x81308130} 231 assert_nothing_raised {S("a".force_encoding(Encoding::GB18030)) << 0x81308130} 232 end 233 234 def test_MATCH # '=~' 235 assert_equal(10, S("FeeFieFoo-Fum") =~ /Fum$/) 236 assert_equal(nil, S("FeeFieFoo-Fum") =~ /FUM$/) 237 238 pre_1_7_1 do 239 $= = true 240 assert_equal(10, S("FeeFieFoo-Fum") =~ /FUM$/) 241 $= = false 242 end 243 244 o = Object.new 245 def o.=~(x); x + "bar"; end 246 assert_equal("foobar", S("foo") =~ o) 247 248 assert_raise(TypeError) { S("foo") =~ "foo" } 249 end 250 251 def test_MOD # '%' 252 assert_equal(S("00123"), S("%05d") % 123) 253 assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1]) 254 x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") % 255 [S("hi"), 256 123, 257 S("never seen"), 258 456, 259 0, 260 ?A, 261 3.0999, 262 11, 263 171, 264 171, 265 11, 266 171, 267 171] 268 269 assert_equal(S(' hi 123 %foo 456 0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x) 270 end 271 272 def test_MUL # '*' 273 assert_equal(S("XXX"), S("X") * 3) 274 assert_equal(S("HOHO"), S("HO") * 2) 275 end 276 277 def test_PLUS # '+' 278 assert_equal(S("Yodel"), S("Yo") + S("del")) 279 end 280 281 def casetest(a, b, rev=false) 282 case a 283 when b 284 assert(!rev) 285 else 286 assert(rev) 287 end 288 end 289 290 def test_VERY_EQUAL # '===' 291 # assert_equal(true, S("foo") === :foo) 292 casetest(S("abcdef"), S("abcdef")) 293 294 pre_1_7_1 do 295 $= = true 296 casetest(S("CAT"), S('cat')) 297 casetest(S("CaT"), S('cAt')) 298 $= = false 299 end 300 301 casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to 302 casetest(S("CaT"), S('cAt'), true) # find these in the case. 303 end 304 305 def test_capitalize 306 assert_equal(S("Hello"), S("hello").capitalize) 307 assert_equal(S("Hello"), S("hELLO").capitalize) 308 assert_equal(S("123abc"), S("123ABC").capitalize) 309 end 310 311 def test_capitalize! 312 a = S("hello"); a.capitalize! 313 assert_equal(S("Hello"), a) 314 315 a = S("hELLO"); a.capitalize! 316 assert_equal(S("Hello"), a) 317 318 a = S("123ABC"); a.capitalize! 319 assert_equal(S("123abc"), a) 320 321 assert_equal(nil, S("123abc").capitalize!) 322 assert_equal(S("123abc"), S("123ABC").capitalize!) 323 assert_equal(S("Abc"), S("ABC").capitalize!) 324 assert_equal(S("Abc"), S("abc").capitalize!) 325 assert_equal(nil, S("Abc").capitalize!) 326 327 a = S("hello") 328 b = a.dup 329 assert_equal(S("Hello"), a.capitalize!) 330 assert_equal(S("hello"), b) 331 332 end 333 334 Bug2463 = '[ruby-dev:39856]' 335 def test_center 336 assert_equal(S("hello"), S("hello").center(4)) 337 assert_equal(S(" hello "), S("hello").center(11)) 338 assert_equal(S("ababaababa"), S("").center(10, "ab"), Bug2463) 339 assert_equal(S("ababaababab"), S("").center(11, "ab"), Bug2463) 340 end 341 342 def test_chomp 343 assert_equal(S("hello"), S("hello").chomp("\n")) 344 assert_equal(S("hello"), S("hello\n").chomp("\n")) 345 save = $/ 346 347 $/ = "\n" 348 349 assert_equal(S("hello"), S("hello").chomp) 350 assert_equal(S("hello"), S("hello\n").chomp) 351 352 $/ = "!" 353 assert_equal(S("hello"), S("hello").chomp) 354 assert_equal(S("hello"), S("hello!").chomp) 355 $/ = save 356 357 assert_equal(S("a").hash, S("a\u0101").chomp(S("\u0101")).hash, '[ruby-core:22414]') 358 end 359 360 def test_chomp! 361 a = S("hello") 362 a.chomp!(S("\n")) 363 364 assert_equal(S("hello"), a) 365 assert_equal(nil, a.chomp!(S("\n"))) 366 367 a = S("hello\n") 368 a.chomp!(S("\n")) 369 assert_equal(S("hello"), a) 370 save = $/ 371 372 $/ = "\n" 373 a = S("hello") 374 a.chomp! 375 assert_equal(S("hello"), a) 376 377 a = S("hello\n") 378 a.chomp! 379 assert_equal(S("hello"), a) 380 381 $/ = "!" 382 a = S("hello") 383 a.chomp! 384 assert_equal(S("hello"), a) 385 386 a="hello!" 387 a.chomp! 388 assert_equal(S("hello"), a) 389 390 $/ = save 391 392 a = S("hello\n") 393 b = a.dup 394 assert_equal(S("hello"), a.chomp!) 395 assert_equal(S("hello\n"), b) 396 397 s = "foo\r\n" 398 s.chomp! 399 assert_equal("foo", s) 400 401 s = "foo\r" 402 s.chomp! 403 assert_equal("foo", s) 404 405 s = "foo\r\n" 406 s.chomp!("") 407 assert_equal("foo", s) 408 409 s = "foo\r" 410 s.chomp!("") 411 assert_equal("foo\r", s) 412 413 assert_equal(S("a").hash, S("a\u0101").chomp!(S("\u0101")).hash, '[ruby-core:22414]') 414 end 415 416 def test_chop 417 assert_equal(S("hell"), S("hello").chop) 418 assert_equal(S("hello"), S("hello\r\n").chop) 419 assert_equal(S("hello\n"), S("hello\n\r").chop) 420 assert_equal(S(""), S("\r\n").chop) 421 assert_equal(S(""), S("").chop) 422 assert_equal(S("a").hash, S("a\u00d8").chop.hash) 423 end 424 425 def test_chop! 426 a = S("hello").chop! 427 assert_equal(S("hell"), a) 428 429 a = S("hello\r\n").chop! 430 assert_equal(S("hello"), a) 431 432 a = S("hello\n\r").chop! 433 assert_equal(S("hello\n"), a) 434 435 a = S("\r\n").chop! 436 assert_equal(S(""), a) 437 438 a = S("").chop! 439 assert_nil(a) 440 441 a = S("a\u00d8") 442 a.chop! 443 assert_equal(S("a").hash, a.hash) 444 445 a = S("hello\n") 446 b = a.dup 447 assert_equal(S("hello"), a.chop!) 448 assert_equal(S("hello\n"), b) 449 end 450 451 def test_clone 452 for taint in [ false, true ] 453 for untrust in [ false, true ] 454 for frozen in [ false, true ] 455 a = S("Cool") 456 a.taint if taint 457 a.untrust if untrust 458 a.freeze if frozen 459 b = a.clone 460 461 assert_equal(a, b) 462 assert(a.__id__ != b.__id__) 463 assert_equal(a.frozen?, b.frozen?) 464 assert_equal(a.untrusted?, b.untrusted?) 465 assert_equal(a.tainted?, b.tainted?) 466 end 467 end 468 end 469 470 null = File.exist?("/dev/null") ? "/dev/null" : "NUL" # maybe DOSISH 471 assert_equal("", File.read(null).clone, '[ruby-dev:32819] reported by Kazuhiro NISHIYAMA') 472 end 473 474 def test_concat 475 assert_equal(S("world!"), S("world").concat(33)) 476 assert_equal(S("world!"), S("world").concat(S('!'))) 477 478 bug7090 = '[ruby-core:47751]' 479 result = S("").force_encoding(Encoding::UTF_16LE) 480 result << 0x0300 481 expected = S("\u0300".encode(Encoding::UTF_16LE)) 482 assert_equal(expected, result, bug7090) 483 end 484 485 def test_count 486 a = S("hello world") 487 assert_equal(5, a.count(S("lo"))) 488 assert_equal(2, a.count(S("lo"), S("o"))) 489 assert_equal(4, a.count(S("hello"), S("^l"))) 490 assert_equal(4, a.count(S("ej-m"))) 491 assert_equal(0, S("y").count(S("a\\-z"))) 492 assert_equal(5, "abc\u{3042 3044 3046}".count("^a")) 493 assert_equal(1, "abc\u{3042 3044 3046}".count("\u3042")) 494 assert_equal(5, "abc\u{3042 3044 3046}".count("^\u3042")) 495 assert_equal(2, "abc\u{3042 3044 3046}".count("a-z", "^a")) 496 assert_equal(0, "abc\u{3042 3044 3046}".count("a", "\u3042")) 497 assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "a")) 498 assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "\u3044")) 499 assert_equal(4, "abc\u{3042 3044 3046}".count("^a", "^\u3044")) 500 assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3044", "^a")) 501 assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3042", "^\u3044")) 502 503 assert_raise(ArgumentError) { "foo".count } 504 end 505 506 def test_crypt 507 assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa"))) 508 assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab"))) 509 end 510 511 def test_delete 512 assert_equal(S("heo"), S("hello").delete(S("l"), S("lo"))) 513 assert_equal(S("he"), S("hello").delete(S("lo"))) 514 assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e"))) 515 assert_equal(S("ho"), S("hello").delete(S("ej-m"))) 516 517 assert_equal("a".hash, "a\u0101".delete("\u0101").hash, '[ruby-talk:329267]') 518 assert_equal(true, "a\u0101".delete("\u0101").ascii_only?) 519 assert_equal(true, "a\u3041".delete("\u3041").ascii_only?) 520 assert_equal(false, "a\u3041\u3042".tr("\u3041", "a").ascii_only?) 521 522 assert_equal("a", "abc\u{3042 3044 3046}".delete("^a")) 523 assert_equal("bc\u{3042 3044 3046}", "abc\u{3042 3044 3046}".delete("a")) 524 assert_equal("\u3042", "abc\u{3042 3044 3046}".delete("^\u3042")) 525 526 bug6160 = '[ruby-dev:45374]' 527 assert_equal("", '\\'.delete('\\'), bug6160) 528 end 529 530 def test_delete! 531 a = S("hello") 532 a.delete!(S("l"), S("lo")) 533 assert_equal(S("heo"), a) 534 535 a = S("hello") 536 a.delete!(S("lo")) 537 assert_equal(S("he"), a) 538 539 a = S("hello") 540 a.delete!(S("aeiou"), S("^e")) 541 assert_equal(S("hell"), a) 542 543 a = S("hello") 544 a.delete!(S("ej-m")) 545 assert_equal(S("ho"), a) 546 547 a = S("hello") 548 assert_nil(a.delete!(S("z"))) 549 550 a = S("hello") 551 b = a.dup 552 a.delete!(S("lo")) 553 assert_equal(S("he"), a) 554 assert_equal(S("hello"), b) 555 556 a = S("hello") 557 a.delete!(S("^el")) 558 assert_equal(S("ell"), a) 559 560 assert_raise(ArgumentError) { S("foo").delete! } 561 end 562 563 564 def test_downcase 565 assert_equal(S("hello"), S("helLO").downcase) 566 assert_equal(S("hello"), S("hello").downcase) 567 assert_equal(S("hello"), S("HELLO").downcase) 568 assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase) 569 end 570 571 def test_downcase! 572 a = S("helLO") 573 b = a.dup 574 assert_equal(S("hello"), a.downcase!) 575 assert_equal(S("hello"), a) 576 assert_equal(S("helLO"), b) 577 578 a=S("hello") 579 assert_nil(a.downcase!) 580 assert_equal(S("hello"), a) 581 end 582 583 def test_dump 584 a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10 585 assert_equal(S('"Test\\x01\\x02\\x03\\t\\r\\n"'), a.dump) 586 end 587 588 def test_dup 589 for taint in [ false, true ] 590 for untrust in [ false, true ] 591 for frozen in [ false, true ] 592 a = S("hello") 593 a.taint if taint 594 a.untrust if untrust 595 a.freeze if frozen 596 b = a.dup 597 598 assert_equal(a, b) 599 assert(a.__id__ != b.__id__) 600 assert(!b.frozen?) 601 assert_equal(a.tainted?, b.tainted?) 602 assert_equal(a.untrusted?, b.untrusted?) 603 end 604 end 605 end 606 end 607 608 def test_each 609 save = $/ 610 $/ = "\n" 611 res=[] 612 S("hello\nworld").lines.each {|x| res << x} 613 assert_equal(S("hello\n"), res[0]) 614 assert_equal(S("world"), res[1]) 615 616 res=[] 617 S("hello\n\n\nworld").lines(S('')).each {|x| res << x} 618 assert_equal(S("hello\n\n\n"), res[0]) 619 assert_equal(S("world"), res[1]) 620 621 $/ = "!" 622 res=[] 623 S("hello!world").lines.each {|x| res << x} 624 assert_equal(S("hello!"), res[0]) 625 assert_equal(S("world"), res[1]) 626 $/ = save 627 end 628 629 def test_each_byte 630 s = S("ABC") 631 632 res = [] 633 assert_equal s.object_id, s.each_byte {|x| res << x }.object_id 634 assert_equal(65, res[0]) 635 assert_equal(66, res[1]) 636 assert_equal(67, res[2]) 637 638 assert_equal 65, s.each_byte.next 639 end 640 641 def test_bytes 642 s = S("ABC") 643 assert_equal [65, 66, 67], s.bytes 644 645 if RUBY_VERSION >= "2.1.0" 646 assert_warn(/block not used/) { 647 assert_equal [65, 66, 67], s.bytes {} 648 } 649 else 650 assert_warning(/deprecated/) { 651 res = [] 652 assert_equal s.object_id, s.bytes {|x| res << x }.object_id 653 assert_equal(65, res[0]) 654 assert_equal(66, res[1]) 655 assert_equal(67, res[2]) 656 } 657 end 658 end 659 660 def test_each_codepoint 661 # Single byte optimization 662 assert_equal 65, S("ABC").each_codepoint.next 663 664 s = S("\u3042\u3044\u3046") 665 666 res = [] 667 assert_equal s.object_id, s.each_codepoint {|x| res << x }.object_id 668 assert_equal(0x3042, res[0]) 669 assert_equal(0x3044, res[1]) 670 assert_equal(0x3046, res[2]) 671 672 assert_equal 0x3042, s.each_codepoint.next 673 end 674 675 def test_codepoints 676 # Single byte optimization 677 assert_equal [65, 66, 67], S("ABC").codepoints 678 679 s = S("\u3042\u3044\u3046") 680 assert_equal [0x3042, 0x3044, 0x3046], s.codepoints 681 682 if RUBY_VERSION >= "2.1.0" 683 assert_warn(/block not used/) { 684 assert_equal [0x3042, 0x3044, 0x3046], s.codepoints {} 685 } 686 else 687 assert_warning(/deprecated/) { 688 res = [] 689 assert_equal s.object_id, s.codepoints {|x| res << x }.object_id 690 assert_equal(0x3042, res[0]) 691 assert_equal(0x3044, res[1]) 692 assert_equal(0x3046, res[2]) 693 } 694 end 695 end 696 697 def test_each_char 698 s = S("ABC") 699 700 res = [] 701 assert_equal s.object_id, s.each_char {|x| res << x }.object_id 702 assert_equal("A", res[0]) 703 assert_equal("B", res[1]) 704 assert_equal("C", res[2]) 705 706 assert_equal "A", S("ABC").each_char.next 707 end 708 709 def test_chars 710 s = S("ABC") 711 assert_equal ["A", "B", "C"], s.chars 712 713 if RUBY_VERSION >= "2.1.0" 714 assert_warn(/block not used/) { 715 assert_equal ["A", "B", "C"], s.chars {} 716 } 717 else 718 assert_warning(/deprecated/) { 719 res = [] 720 assert_equal s.object_id, s.chars {|x| res << x }.object_id 721 assert_equal("A", res[0]) 722 assert_equal("B", res[1]) 723 assert_equal("C", res[2]) 724 } 725 end 726 end 727 728 def test_each_line 729 save = $/ 730 $/ = "\n" 731 res=[] 732 S("hello\nworld").each_line {|x| res << x} 733 assert_equal(S("hello\n"), res[0]) 734 assert_equal(S("world"), res[1]) 735 736 res=[] 737 S("hello\n\n\nworld").each_line(S('')) {|x| res << x} 738 assert_equal(S("hello\n\n\n"), res[0]) 739 assert_equal(S("world"), res[1]) 740 741 $/ = "!" 742 743 res=[] 744 S("hello!world").each_line {|x| res << x} 745 assert_equal(S("hello!"), res[0]) 746 assert_equal(S("world"), res[1]) 747 748 $/ = "ab" 749 750 res=[] 751 S("a").lines.each {|x| res << x} 752 assert_equal(1, res.size) 753 assert_equal(S("a"), res[0]) 754 755 $/ = save 756 757 s = nil 758 "foo\nbar".each_line(nil) {|s2| s = s2 } 759 assert_equal("foo\nbar", s) 760 761 assert_equal "hello\n", S("hello\nworld").each_line.next 762 assert_equal "hello\nworld", S("hello\nworld").each_line(nil).next 763 764 bug7646 = "[ruby-dev:46827]" 765 assert_nothing_raised(bug7646) do 766 "\n\u0100".each_line("\n") {} 767 end 768 end 769 770 def test_lines 771 s = S("hello\nworld") 772 assert_equal ["hello\n", "world"], s.lines 773 assert_equal ["hello\nworld"], s.lines(nil) 774 775 if RUBY_VERSION >= "2.1.0" 776 assert_warn(/block not used/) { 777 assert_equal ["hello\n", "world"], s.lines {} 778 } 779 else 780 assert_warning(/deprecated/) { 781 res = [] 782 assert_equal s.object_id, s.lines {|x| res << x }.object_id 783 assert_equal(S("hello\n"), res[0]) 784 assert_equal(S("world"), res[1]) 785 } 786 end 787 end 788 789 def test_empty? 790 assert(S("").empty?) 791 assert(!S("not").empty?) 792 end 793 794 def test_end_with? 795 assert_send([S("hello"), :end_with?, S("llo")]) 796 assert_not_send([S("hello"), :end_with?, S("ll")]) 797 assert_send([S("hello"), :end_with?, S("el"), S("lo")]) 798 799 bug5536 = '[ruby-core:40623]' 800 assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string} 801 end 802 803 def test_eql? 804 a = S("hello") 805 assert(a.eql?(S("hello"))) 806 assert(a.eql?(a)) 807 end 808 809 def test_gsub 810 assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*'))) 811 assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>'))) 812 assert_equal(S("h e l l o "), 813 S("hello").gsub(/./) { |s| s[0].to_s + S(' ')}) 814 assert_equal(S("HELL-o"), 815 S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }) 816 817 a = S("hello") 818 a.taint 819 a.untrust 820 assert(a.gsub(/./, S('X')).tainted?) 821 assert(a.gsub(/./, S('X')).untrusted?) 822 823 assert_equal("z", "abc".gsub(/./, "a" => "z"), "moved from btest/knownbug") 824 825 assert_raise(ArgumentError) { "foo".gsub } 826 end 827 828 def test_gsub_encoding 829 a = S("hello world") 830 a.force_encoding Encoding::UTF_8 831 832 b = S("hi") 833 b.force_encoding Encoding::US_ASCII 834 835 assert_equal Encoding::UTF_8, a.gsub(/hello/, b).encoding 836 837 c = S("everybody") 838 c.force_encoding Encoding::US_ASCII 839 840 assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding 841 end 842 843 def test_gsub! 844 a = S("hello") 845 b = a.dup 846 a.gsub!(/[aeiou]/, S('*')) 847 assert_equal(S("h*ll*"), a) 848 assert_equal(S("hello"), b) 849 850 a = S("hello") 851 a.gsub!(/([aeiou])/, S('<\1>')) 852 assert_equal(S("h<e>ll<o>"), a) 853 854 a = S("hello") 855 a.gsub!(/./) { |s| s[0].to_s + S(' ')} 856 assert_equal(S("h e l l o "), a) 857 858 a = S("hello") 859 a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 } 860 assert_equal(S("HELL-o"), a) 861 862 r = S('X') 863 r.taint 864 r.untrust 865 a.gsub!(/./, r) 866 assert(a.tainted?) 867 assert(a.untrusted?) 868 869 a = S("hello") 870 assert_nil(a.sub!(S('X'), S('Y'))) 871 end 872 873 def test_sub_hash 874 assert_equal('azc', 'abc'.sub(/b/, "b" => "z")) 875 assert_equal('ac', 'abc'.sub(/b/, {})) 876 assert_equal('a1c', 'abc'.sub(/b/, "b" => 1)) 877 assert_equal('aBc', 'abc'.sub(/b/, Hash.new {|h, k| k.upcase })) 878 assert_equal('a[\&]c', 'abc'.sub(/b/, "b" => '[\&]')) 879 assert_equal('aBcabc', 'abcabc'.sub(/b/, Hash.new {|h, k| h[k] = k.upcase })) 880 assert_equal('aBcdef', 'abcdef'.sub(/de|b/, "b" => "B", "de" => "DE")) 881 end 882 883 def test_gsub_hash 884 assert_equal('azc', 'abc'.gsub(/b/, "b" => "z")) 885 assert_equal('ac', 'abc'.gsub(/b/, {})) 886 assert_equal('a1c', 'abc'.gsub(/b/, "b" => 1)) 887 assert_equal('aBc', 'abc'.gsub(/b/, Hash.new {|h, k| k.upcase })) 888 assert_equal('a[\&]c', 'abc'.gsub(/b/, "b" => '[\&]')) 889 assert_equal('aBcaBc', 'abcabc'.gsub(/b/, Hash.new {|h, k| h[k] = k.upcase })) 890 assert_equal('aBcDEf', 'abcdef'.gsub(/de|b/, "b" => "B", "de" => "DE")) 891 end 892 893 def test_hash 894 assert_equal(S("hello").hash, S("hello").hash) 895 assert(S("hello").hash != S("helLO").hash) 896 bug4104 = '[ruby-core:33500]' 897 assert_not_equal(S("a").hash, S("a\0").hash, bug4104) 898 end 899 900 def test_hash_random 901 str = 'abc' 902 a = [str.hash.to_s] 903 3.times { 904 assert_in_out_err(["-e", "print #{str.dump}.hash"], "") do |r, e| 905 a += r 906 assert_equal([], e) 907 end 908 } 909 assert_not_equal([str.hash.to_s], a.uniq) 910 end 911 912 def test_hex 913 assert_equal(255, S("0xff").hex) 914 assert_equal(-255, S("-0xff").hex) 915 assert_equal(255, S("ff").hex) 916 assert_equal(-255, S("-ff").hex) 917 assert_equal(0, S("-ralph").hex) 918 assert_equal(-15, S("-fred").hex) 919 assert_equal(15, S("fred").hex) 920 end 921 922 def test_include? 923 assert( S("foobar").include?(?f)) 924 assert( S("foobar").include?(S("foo"))) 925 assert(!S("foobar").include?(S("baz"))) 926 assert(!S("foobar").include?(?z)) 927 end 928 929 def test_index 930 assert_equal(0, S("hello").index(?h)) 931 assert_equal(1, S("hello").index(S("ell"))) 932 assert_equal(2, S("hello").index(/ll./)) 933 934 assert_equal(3, S("hello").index(?l, 3)) 935 assert_equal(3, S("hello").index(S("l"), 3)) 936 assert_equal(3, S("hello").index(/l./, 3)) 937 938 assert_nil(S("hello").index(?z, 3)) 939 assert_nil(S("hello").index(S("z"), 3)) 940 assert_nil(S("hello").index(/z./, 3)) 941 942 assert_nil(S("hello").index(?z)) 943 assert_nil(S("hello").index(S("z"))) 944 assert_nil(S("hello").index(/z./)) 945 946 assert_equal(0, S("").index(S(""))) 947 assert_equal(0, S("").index(//)) 948 assert_nil(S("").index(S("hello"))) 949 assert_nil(S("").index(/hello/)) 950 assert_equal(0, S("hello").index(S(""))) 951 assert_equal(0, S("hello").index(//)) 952 953 s = S("long") * 1000 << "x" 954 assert_nil(s.index(S("y"))) 955 assert_equal(4 * 1000, s.index(S("x"))) 956 s << "yx" 957 assert_equal(4 * 1000, s.index(S("x"))) 958 assert_equal(4 * 1000, s.index(S("xyx"))) 959 960 o = Object.new 961 def o.to_str; "bar"; end 962 assert_equal(3, "foobarbarbaz".index(o)) 963 assert_raise(TypeError) { "foo".index(Object.new) } 964 965 assert_nil("foo".index(//, -100)) 966 assert_nil($~) 967 end 968 969 def test_intern 970 assert_equal(:koala, S("koala").intern) 971 assert(:koala != S("Koala").intern) 972 end 973 974 def test_length 975 assert_equal(0, S("").length) 976 assert_equal(4, S("1234").length) 977 assert_equal(6, S("1234\r\n").length) 978 assert_equal(7, S("\0011234\r\n").length) 979 end 980 981 def test_ljust 982 assert_equal(S("hello"), S("hello").ljust(4)) 983 assert_equal(S("hello "), S("hello").ljust(11)) 984 assert_equal(S("ababababab"), S("").ljust(10, "ab"), Bug2463) 985 assert_equal(S("abababababa"), S("").ljust(11, "ab"), Bug2463) 986 end 987 988 def test_next 989 assert_equal(S("abd"), S("abc").next) 990 assert_equal(S("z"), S("y").next) 991 assert_equal(S("aaa"), S("zz").next) 992 993 assert_equal(S("124"), S("123").next) 994 assert_equal(S("1000"), S("999").next) 995 996 assert_equal(S("2000aaa"), S("1999zzz").next) 997 assert_equal(S("AAAAA000"), S("ZZZZ999").next) 998 999 assert_equal(S("*+"), S("**").next) 1000 end 1001 1002 def test_next! 1003 a = S("abc") 1004 b = a.dup 1005 assert_equal(S("abd"), a.next!) 1006 assert_equal(S("abd"), a) 1007 assert_equal(S("abc"), b) 1008 1009 a = S("y") 1010 assert_equal(S("z"), a.next!) 1011 assert_equal(S("z"), a) 1012 1013 a = S("zz") 1014 assert_equal(S("aaa"), a.next!) 1015 assert_equal(S("aaa"), a) 1016 1017 a = S("123") 1018 assert_equal(S("124"), a.next!) 1019 assert_equal(S("124"), a) 1020 1021 a = S("999") 1022 assert_equal(S("1000"), a.next!) 1023 assert_equal(S("1000"), a) 1024 1025 a = S("1999zzz") 1026 assert_equal(S("2000aaa"), a.next!) 1027 assert_equal(S("2000aaa"), a) 1028 1029 a = S("ZZZZ999") 1030 assert_equal(S("AAAAA000"), a.next!) 1031 assert_equal(S("AAAAA000"), a) 1032 1033 a = S("**") 1034 assert_equal(S("*+"), a.next!) 1035 assert_equal(S("*+"), a) 1036 end 1037 1038 def test_oct 1039 assert_equal(255, S("0377").oct) 1040 assert_equal(255, S("377").oct) 1041 assert_equal(-255, S("-0377").oct) 1042 assert_equal(-255, S("-377").oct) 1043 assert_equal(0, S("OO").oct) 1044 assert_equal(24, S("030OO").oct) 1045 end 1046 1047 def test_replace 1048 a = S("foo") 1049 assert_equal(S("f"), a.replace(S("f"))) 1050 1051 a = S("foo") 1052 assert_equal(S("foobar"), a.replace(S("foobar"))) 1053 1054 a = S("foo") 1055 a.taint 1056 a.untrust 1057 b = a.replace(S("xyz")) 1058 assert_equal(S("xyz"), b) 1059 assert(b.tainted?) 1060 assert(b.untrusted?) 1061 1062 s = "foo" * 100 1063 s2 = ("bar" * 100).dup 1064 s.replace(s2) 1065 assert_equal(s2, s) 1066 1067 s2 = ["foo"].pack("p") 1068 s.replace(s2) 1069 assert_equal(s2, s) 1070 1071 fs = "".freeze 1072 assert_raise(RuntimeError) { fs.replace("a") } 1073 assert_raise(RuntimeError) { fs.replace(fs) } 1074 assert_raise(ArgumentError) { fs.replace() } 1075 assert_raise(RuntimeError) { fs.replace(42) } 1076 end 1077 1078 def test_reverse 1079 assert_equal(S("beta"), S("ateb").reverse) 1080 assert_equal(S("madamImadam"), S("madamImadam").reverse) 1081 1082 a=S("beta") 1083 assert_equal(S("ateb"), a.reverse) 1084 assert_equal(S("beta"), a) 1085 end 1086 1087 def test_reverse! 1088 a = S("beta") 1089 b = a.dup 1090 assert_equal(S("ateb"), a.reverse!) 1091 assert_equal(S("ateb"), a) 1092 assert_equal(S("beta"), b) 1093 1094 assert_equal(S("madamImadam"), S("madamImadam").reverse!) 1095 1096 a = S("madamImadam") 1097 assert_equal(S("madamImadam"), a.reverse!) # ?? 1098 assert_equal(S("madamImadam"), a) 1099 end 1100 1101 def test_rindex 1102 assert_equal(3, S("hello").rindex(?l)) 1103 assert_equal(6, S("ell, hello").rindex(S("ell"))) 1104 assert_equal(7, S("ell, hello").rindex(/ll./)) 1105 1106 assert_equal(3, S("hello,lo").rindex(?l, 3)) 1107 assert_equal(3, S("hello,lo").rindex(S("l"), 3)) 1108 assert_equal(3, S("hello,lo").rindex(/l./, 3)) 1109 1110 assert_nil(S("hello").rindex(?z, 3)) 1111 assert_nil(S("hello").rindex(S("z"), 3)) 1112 assert_nil(S("hello").rindex(/z./, 3)) 1113 1114 assert_nil(S("hello").rindex(?z)) 1115 assert_nil(S("hello").rindex(S("z"))) 1116 assert_nil(S("hello").rindex(/z./)) 1117 1118 o = Object.new 1119 def o.to_str; "bar"; end 1120 assert_equal(6, "foobarbarbaz".rindex(o)) 1121 assert_raise(TypeError) { "foo".rindex(Object.new) } 1122 1123 assert_nil("foo".rindex(//, -100)) 1124 assert_nil($~) 1125 end 1126 1127 def test_rjust 1128 assert_equal(S("hello"), S("hello").rjust(4)) 1129 assert_equal(S(" hello"), S("hello").rjust(11)) 1130 assert_equal(S("ababababab"), S("").rjust(10, "ab"), Bug2463) 1131 assert_equal(S("abababababa"), S("").rjust(11, "ab"), Bug2463) 1132 end 1133 1134 def test_scan 1135 a = S("cruel world") 1136 assert_equal([S("cruel"), S("world")],a.scan(/\w+/)) 1137 assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../)) 1138 assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/)) 1139 1140 res = [] 1141 a.scan(/\w+/) { |w| res << w } 1142 assert_equal([S("cruel"), S("world") ],res) 1143 1144 res = [] 1145 a.scan(/.../) { |w| res << w } 1146 assert_equal([S("cru"), S("el "), S("wor")],res) 1147 1148 res = [] 1149 a.scan(/(...)/) { |w| res << w } 1150 assert_equal([[S("cru")], [S("el ")], [S("wor")]],res) 1151 1152 a = S("hello") 1153 a.taint 1154 a.untrust 1155 res = [] 1156 a.scan(/./) { |w| res << w } 1157 assert(res[0].tainted?, '[ruby-core:33338] #4087') 1158 assert(res[0].untrusted?, '[ruby-core:33338] #4087') 1159 end 1160 1161 def test_size 1162 assert_equal(0, S("").size) 1163 assert_equal(4, S("1234").size) 1164 assert_equal(6, S("1234\r\n").size) 1165 assert_equal(7, S("\0011234\r\n").size) 1166 end 1167 1168 def test_slice 1169 assert_equal(?A, S("AooBar").slice(0)) 1170 assert_equal(?B, S("FooBaB").slice(-1)) 1171 assert_nil(S("FooBar").slice(6)) 1172 assert_nil(S("FooBar").slice(-7)) 1173 1174 assert_equal(S("Foo"), S("FooBar").slice(0,3)) 1175 assert_equal(S(S("Bar")), S("FooBar").slice(-3,3)) 1176 assert_nil(S("FooBar").slice(7,2)) # Maybe should be six? 1177 assert_nil(S("FooBar").slice(-7,10)) 1178 1179 assert_equal(S("Foo"), S("FooBar").slice(0..2)) 1180 assert_equal(S("Bar"), S("FooBar").slice(-3..-1)) 1181 assert_equal(S(""), S("FooBar").slice(6..2)) 1182 assert_nil(S("FooBar").slice(-10..-7)) 1183 1184 assert_equal(S("Foo"), S("FooBar").slice(/^F../)) 1185 assert_equal(S("Bar"), S("FooBar").slice(/..r$/)) 1186 assert_nil(S("FooBar").slice(/xyzzy/)) 1187 assert_nil(S("FooBar").slice(/plugh/)) 1188 1189 assert_equal(S("Foo"), S("FooBar").slice(S("Foo"))) 1190 assert_equal(S("Bar"), S("FooBar").slice(S("Bar"))) 1191 assert_nil(S("FooBar").slice(S("xyzzy"))) 1192 assert_nil(S("FooBar").slice(S("plugh"))) 1193 end 1194 1195 def test_slice! 1196 a = S("AooBar") 1197 b = a.dup 1198 assert_equal(?A, a.slice!(0)) 1199 assert_equal(S("ooBar"), a) 1200 assert_equal(S("AooBar"), b) 1201 1202 a = S("FooBar") 1203 assert_equal(?r,a.slice!(-1)) 1204 assert_equal(S("FooBa"), a) 1205 1206 a = S("FooBar") 1207 if @aref_slicebang_silent 1208 assert_nil( a.slice!(6) ) 1209 else 1210 assert_raise(IndexError) { a.slice!(6) } 1211 end 1212 assert_equal(S("FooBar"), a) 1213 1214 if @aref_slicebang_silent 1215 assert_nil( a.slice!(-7) ) 1216 else 1217 assert_raise(IndexError) { a.slice!(-7) } 1218 end 1219 assert_equal(S("FooBar"), a) 1220 1221 a = S("FooBar") 1222 assert_equal(S("Foo"), a.slice!(0,3)) 1223 assert_equal(S("Bar"), a) 1224 1225 a = S("FooBar") 1226 assert_equal(S("Bar"), a.slice!(-3,3)) 1227 assert_equal(S("Foo"), a) 1228 1229 a=S("FooBar") 1230 if @aref_slicebang_silent 1231 assert_nil(a.slice!(7,2)) # Maybe should be six? 1232 else 1233 assert_raise(IndexError) {a.slice!(7,2)} # Maybe should be six? 1234 end 1235 assert_equal(S("FooBar"), a) 1236 if @aref_slicebang_silent 1237 assert_nil(a.slice!(-7,10)) 1238 else 1239 assert_raise(IndexError) {a.slice!(-7,10)} 1240 end 1241 assert_equal(S("FooBar"), a) 1242 1243 a=S("FooBar") 1244 assert_equal(S("Foo"), a.slice!(0..2)) 1245 assert_equal(S("Bar"), a) 1246 1247 a=S("FooBar") 1248 assert_equal(S("Bar"), a.slice!(-3..-1)) 1249 assert_equal(S("Foo"), a) 1250 1251 a=S("FooBar") 1252 if @aref_slicebang_silent 1253 assert_equal(S(""), a.slice!(6..2)) 1254 else 1255 assert_raise(RangeError) {a.slice!(6..2)} 1256 end 1257 assert_equal(S("FooBar"), a) 1258 if @aref_slicebang_silent 1259 assert_nil(a.slice!(-10..-7)) 1260 else 1261 assert_raise(RangeError) {a.slice!(-10..-7)} 1262 end 1263 assert_equal(S("FooBar"), a) 1264 1265 a=S("FooBar") 1266 assert_equal(S("Foo"), a.slice!(/^F../)) 1267 assert_equal(S("Bar"), a) 1268 1269 a=S("FooBar") 1270 assert_equal(S("Bar"), a.slice!(/..r$/)) 1271 assert_equal(S("Foo"), a) 1272 1273 a=S("FooBar") 1274 if @aref_slicebang_silent 1275 assert_nil(a.slice!(/xyzzy/)) 1276 else 1277 assert_raise(IndexError) {a.slice!(/xyzzy/)} 1278 end 1279 assert_equal(S("FooBar"), a) 1280 if @aref_slicebang_silent 1281 assert_nil(a.slice!(/plugh/)) 1282 else 1283 assert_raise(IndexError) {a.slice!(/plugh/)} 1284 end 1285 assert_equal(S("FooBar"), a) 1286 1287 a=S("FooBar") 1288 assert_equal(S("Foo"), a.slice!(S("Foo"))) 1289 assert_equal(S("Bar"), a) 1290 1291 a=S("FooBar") 1292 assert_equal(S("Bar"), a.slice!(S("Bar"))) 1293 assert_equal(S("Foo"), a) 1294 1295 pre_1_7_1 do 1296 a=S("FooBar") 1297 assert_nil(a.slice!(S("xyzzy"))) 1298 assert_equal(S("FooBar"), a) 1299 assert_nil(a.slice!(S("plugh"))) 1300 assert_equal(S("FooBar"), a) 1301 end 1302 1303 assert_raise(ArgumentError) { "foo".slice! } 1304 end 1305 1306 def test_split 1307 assert_nil($;) 1308 assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split) 1309 assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" "))) 1310 1311 assert_equal([S(" a "), S(" b "), S(" c ")], S(" a | b | c ").split(S("|"))) 1312 1313 assert_equal([S("a"), S("b"), S("c")], S("aXXbXXcXX").split(/X./)) 1314 1315 assert_equal([S("a"), S("b"), S("c")], S("abc").split(//)) 1316 1317 assert_equal([S("a|b|c")], S("a|b|c").split(S('|'), 1)) 1318 1319 assert_equal([S("a"), S("b|c")], S("a|b|c").split(S('|'), 2)) 1320 assert_equal([S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3)) 1321 1322 assert_equal([S("a"), S("b"), S("c"), S("")], S("a|b|c|").split(S('|'), -1)) 1323 assert_equal([S("a"), S("b"), S("c"), S(""), S("")], S("a|b|c||").split(S('|'), -1)) 1324 1325 assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|'))) 1326 assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1)) 1327 1328 assert_equal([], "".split(//, 1)) 1329 1330 assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug") 1331 1332 bug6206 = '[ruby-dev:45441]' 1333 Encoding.list.each do |enc| 1334 next unless enc.ascii_compatible? 1335 s = S("a:".force_encoding(enc)) 1336 assert_equal([enc]*2, s.split(":", 2).map(&:encoding), bug6206) 1337 end 1338 end 1339 1340 def test_squeeze 1341 assert_equal(S("abc"), S("aaabbbbccc").squeeze) 1342 assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" "))) 1343 assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z"))) 1344 end 1345 1346 def test_squeeze! 1347 a = S("aaabbbbccc") 1348 b = a.dup 1349 assert_equal(S("abc"), a.squeeze!) 1350 assert_equal(S("abc"), a) 1351 assert_equal(S("aaabbbbccc"), b) 1352 1353 a = S("aa bb cc") 1354 assert_equal(S("aa bb cc"), a.squeeze!(S(" "))) 1355 assert_equal(S("aa bb cc"), a) 1356 1357 a = S("BxxxTyyyWzzzzz") 1358 assert_equal(S("BxTyWz"), a.squeeze!(S("a-z"))) 1359 assert_equal(S("BxTyWz"), a) 1360 1361 a=S("The quick brown fox") 1362 assert_nil(a.squeeze!) 1363 end 1364 1365 def test_start_with? 1366 assert_send([S("hello"), :start_with?, S("hel")]) 1367 assert_not_send([S("hello"), :start_with?, S("el")]) 1368 assert_send([S("hello"), :start_with?, S("el"), S("he")]) 1369 1370 bug5536 = '[ruby-core:40623]' 1371 assert_raise(TypeError, bug5536) {S("str").start_with? :not_convertible_to_string} 1372 end 1373 1374 def test_strip 1375 assert_equal(S("x"), S(" x ").strip) 1376 assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip) 1377 1378 assert_equal("0b0 ".force_encoding("UTF-16BE"), 1379 "\x00 0b0 ".force_encoding("UTF-16BE").strip) 1380 assert_equal("0\x000b0 ".force_encoding("UTF-16BE"), 1381 "0\x000b0 ".force_encoding("UTF-16BE").strip) 1382 end 1383 1384 def test_strip! 1385 a = S(" x ") 1386 b = a.dup 1387 assert_equal(S("x") ,a.strip!) 1388 assert_equal(S("x") ,a) 1389 assert_equal(S(" x "), b) 1390 1391 a = S(" \n\r\t x \t\r\n\n ") 1392 assert_equal(S("x"), a.strip!) 1393 assert_equal(S("x"), a) 1394 1395 a = S("x") 1396 assert_nil(a.strip!) 1397 assert_equal(S("x") ,a) 1398 end 1399 1400 def test_sub 1401 assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*'))) 1402 assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>'))) 1403 assert_equal(S("h ello"), S("hello").sub(/./) { 1404 |s| s[0].to_s + S(' ')}) 1405 assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) { 1406 |s| $1.upcase + S('-') + $2 1407 }) 1408 1409 assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\')) 1410 assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\')) 1411 assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1')) 1412 assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1')) 1413 assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1')) 1414 assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1')) 1415 assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1')) 1416 1417 assert_equal(S("a--ababababababababab"), 1418 S("abababababababababab").sub(/(b)/, '-\9-')) 1419 assert_equal(S("1-b-0"), 1420 S("1b2b3b4b5b6b7b8b9b0"). 1421 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-')) 1422 assert_equal(S("1-b-0"), 1423 S("1b2b3b4b5b6b7b8b9b0"). 1424 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-')) 1425 assert_equal(S("1-\\9-0"), 1426 S("1b2b3b4b5b6b7b8b9b0"). 1427 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-')) 1428 assert_equal(S("k"), 1429 S("1a2b3c4d5e6f7g8h9iAjBk"). 1430 sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+')) 1431 1432 assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\')) 1433 assert_equal(S("ababa"), S("ababa").sub(/b/, '\&')) 1434 assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&')) 1435 assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&')) 1436 assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&')) 1437 assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&')) 1438 1439 a = S("hello") 1440 a.taint 1441 a.untrust 1442 x = a.sub(/./, S('X')) 1443 assert(x.tainted?) 1444 assert(x.untrusted?) 1445 1446 o = Object.new 1447 def o.to_str; "bar"; end 1448 assert_equal("fooBARbaz", "foobarbaz".sub(o, "BAR")) 1449 1450 assert_raise(TypeError) { "foo".sub(Object.new, "") } 1451 1452 assert_raise(ArgumentError) { "foo".sub } 1453 1454 assert_raise(IndexError) { "foo"[/(?:(o$)|(x))/, 2] = 'bar' } 1455 1456 o = Object.new 1457 def o.to_s; self; end 1458 assert_match(/^foo#<Object:0x.*>baz$/, "foobarbaz".sub("bar") { o }) 1459 end 1460 1461 def test_sub! 1462 a = S("hello") 1463 b = a.dup 1464 a.sub!(/[aeiou]/, S('*')) 1465 assert_equal(S("h*llo"), a) 1466 assert_equal(S("hello"), b) 1467 1468 a = S("hello") 1469 a.sub!(/([aeiou])/, S('<\1>')) 1470 assert_equal(S("h<e>llo"), a) 1471 1472 a = S("hello") 1473 a.sub!(/./) { |s| s[0].to_s + S(' ')} 1474 assert_equal(S("h ello"), a) 1475 1476 a = S("hello") 1477 a.sub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 } 1478 assert_equal(S("HELL-o"), a) 1479 1480 a=S("hello") 1481 assert_nil(a.sub!(/X/, S('Y'))) 1482 1483 r = S('X') 1484 r.taint 1485 r.untrust 1486 a.sub!(/./, r) 1487 assert(a.tainted?) 1488 assert(a.untrusted?) 1489 end 1490 1491 def test_succ 1492 assert_equal(S("abd"), S("abc").succ) 1493 assert_equal(S("z"), S("y").succ) 1494 assert_equal(S("aaa"), S("zz").succ) 1495 1496 assert_equal(S("124"), S("123").succ) 1497 assert_equal(S("1000"), S("999").succ) 1498 assert_equal(S("2.000"), S("1.999").succ) 1499 1500 assert_equal(S("No.10"), S("No.9").succ) 1501 assert_equal(S("2000aaa"), S("1999zzz").succ) 1502 assert_equal(S("AAAAA000"), S("ZZZZ999").succ) 1503 assert_equal(S("*+"), S("**").succ) 1504 1505 assert_equal("abce", "abcd".succ) 1506 assert_equal("THX1139", "THX1138".succ) 1507 assert_equal("<\<koalb>>", "<\<koala>>".succ) 1508 assert_equal("2000aaa", "1999zzz".succ) 1509 assert_equal("AAAA0000", "ZZZ9999".succ) 1510 assert_equal("**+", "***".succ) 1511 end 1512 1513 def test_succ! 1514 a = S("abc") 1515 b = a.dup 1516 assert_equal(S("abd"), a.succ!) 1517 assert_equal(S("abd"), a) 1518 assert_equal(S("abc"), b) 1519 1520 a = S("y") 1521 assert_equal(S("z"), a.succ!) 1522 assert_equal(S("z"), a) 1523 1524 a = S("zz") 1525 assert_equal(S("aaa"), a.succ!) 1526 assert_equal(S("aaa"), a) 1527 1528 a = S("123") 1529 assert_equal(S("124"), a.succ!) 1530 assert_equal(S("124"), a) 1531 1532 a = S("999") 1533 assert_equal(S("1000"), a.succ!) 1534 assert_equal(S("1000"), a) 1535 1536 a = S("1999zzz") 1537 assert_equal(S("2000aaa"), a.succ!) 1538 assert_equal(S("2000aaa"), a) 1539 1540 a = S("ZZZZ999") 1541 assert_equal(S("AAAAA000"), a.succ!) 1542 assert_equal(S("AAAAA000"), a) 1543 1544 a = S("**") 1545 assert_equal(S("*+"), a.succ!) 1546 assert_equal(S("*+"), a) 1547 1548 a = S("No.9") 1549 assert_equal(S("No.10"), a.succ!) 1550 assert_equal(S("No.10"), a) 1551 1552 assert_equal("aaaaaaaaaaaa", "zzzzzzzzzzz".succ!) 1553 assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzz".succ!) 1554 end 1555 1556 def test_sum 1557 n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001") 1558 assert_equal(15, n.sum) 1559 n += S("\001") 1560 assert_equal(16, n.sum(17)) 1561 n[0] = 2.chr 1562 assert(15 != n.sum) 1563 end 1564 1565 def check_sum(str, bits=16) 1566 sum = 0 1567 str.each_byte {|c| sum += c} 1568 sum = sum & ((1 << bits) - 1) if bits != 0 1569 assert_equal(sum, str.sum(bits)) 1570 end 1571 1572 def test_sum_2 1573 assert_equal(0, "".sum) 1574 assert_equal(294, "abc".sum) 1575 check_sum("abc") 1576 check_sum("\x80") 1577 0.upto(70) {|bits| 1578 check_sum("xyz", bits) 1579 } 1580 end 1581 1582 def test_sum_long 1583 s8421505 = "\xff" * 8421505 1584 assert_equal(127, s8421505.sum(31)) 1585 assert_equal(2147483775, s8421505.sum(0)) 1586 s16843010 = ("\xff" * 16843010) 1587 assert_equal(254, s16843010.sum(32)) 1588 assert_equal(4294967550, s16843010.sum(0)) 1589 end 1590 1591 def test_swapcase 1592 assert_equal(S("hi&LOW"), S("HI&low").swapcase) 1593 end 1594 1595 def test_swapcase! 1596 a = S("hi&LOW") 1597 b = a.dup 1598 assert_equal(S("HI&low"), a.swapcase!) 1599 assert_equal(S("HI&low"), a) 1600 assert_equal(S("hi&LOW"), b) 1601 1602 a = S("$^#^%$#!!") 1603 assert_nil(a.swapcase!) 1604 assert_equal(S("$^#^%$#!!"), a) 1605 end 1606 1607 def test_to_f 1608 assert_equal(344.3, S("344.3").to_f) 1609 assert_equal(5.9742e24, S("5.9742e24").to_f) 1610 assert_equal(98.6, S("98.6 degrees").to_f) 1611 assert_equal(0.0, S("degrees 100.0").to_f) 1612 assert_equal([ 0.0].pack('G'), [S(" 0.0").to_f].pack('G')) 1613 assert_equal([-0.0].pack('G'), [S("-0.0").to_f].pack('G')) 1614 end 1615 1616 def test_to_i 1617 assert_equal(1480, S("1480ft/sec").to_i) 1618 assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i) 1619 assert_equal(0, " 0".to_i) 1620 assert_equal(0, "+0".to_i) 1621 assert_equal(0, "-0".to_i) 1622 assert_equal(0, "--0".to_i) 1623 assert_equal(16, "0x10".to_i(0)) 1624 assert_equal(16, "0X10".to_i(0)) 1625 assert_equal(2, "0b10".to_i(0)) 1626 assert_equal(2, "0B10".to_i(0)) 1627 assert_equal(8, "0o10".to_i(0)) 1628 assert_equal(8, "0O10".to_i(0)) 1629 assert_equal(10, "0d10".to_i(0)) 1630 assert_equal(10, "0D10".to_i(0)) 1631 assert_equal(8, "010".to_i(0)) 1632 assert_raise(ArgumentError) { "010".to_i(-10) } 1633 2.upto(36) {|radix| 1634 assert_equal(radix, "10".to_i(radix)) 1635 assert_equal(radix**2, "100".to_i(radix)) 1636 } 1637 assert_raise(ArgumentError) { "0".to_i(1) } 1638 assert_raise(ArgumentError) { "0".to_i(37) } 1639 assert_equal(0, "z".to_i(10)) 1640 assert_equal(12, "1_2".to_i(10)) 1641 assert_equal(0x40000000, "1073741824".to_i(10)) 1642 assert_equal(0x4000000000000000, "4611686018427387904".to_i(10)) 1643 assert_equal(1, "1__2".to_i(10)) 1644 assert_equal(1, "1_z".to_i(10)) 1645 1646 bug6192 = '[ruby-core:43566]' 1647 assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16be").to_i} 1648 assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16le").to_i} 1649 assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32be").to_i} 1650 assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32le").to_i} 1651 assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("iso-2022-jp").to_i} 1652 end 1653 1654 def test_to_s 1655 a = S("me") 1656 assert_equal("me", a.to_s) 1657 assert_equal(a.__id__, a.to_s.__id__) if @cls == String 1658 end 1659 1660 def test_to_str 1661 a = S("me") 1662 assert_equal("me", a.to_s) 1663 assert_equal(a.__id__, a.to_s.__id__) if @cls == String 1664 1665 o = Object.new 1666 def o.to_str 1667 "at" 1668 end 1669 assert_equal("meat", a.concat(o)) 1670 1671 o = Object.new 1672 def o.to_str 1673 foo_bar() 1674 end 1675 assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message) 1676 end 1677 1678 def test_tr 1679 assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip"))) 1680 assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*"))) 1681 assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z"))) 1682 1683 a = "abc".force_encoding(Encoding::US_ASCII) 1684 assert_equal(Encoding::US_ASCII, a.tr(S("z"), S("\u0101")).encoding, '[ruby-core:22326]') 1685 1686 assert_equal("a".hash, "a".tr("a", "\u0101").tr("\u0101", "a").hash, '[ruby-core:22328]') 1687 assert_equal(true, "\u0101".tr("\u0101", "a").ascii_only?) 1688 assert_equal(true, "\u3041".tr("\u3041", "a").ascii_only?) 1689 assert_equal(false, "\u3041\u3042".tr("\u3041", "a").ascii_only?) 1690 1691 bug6156 = '[ruby-core:43335]' 1692 str, range, star = %w[b a-z *].map{|s|s.encode("utf-16le")} 1693 assert_equal(star, str.tr(range, star), bug6156) 1694 end 1695 1696 def test_tr! 1697 a = S("hello") 1698 b = a.dup 1699 assert_equal(S("hippo"), a.tr!(S("el"), S("ip"))) 1700 assert_equal(S("hippo"), a) 1701 assert_equal(S("hello"),b) 1702 1703 a = S("hello") 1704 assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*"))) 1705 assert_equal(S("*e**o"), a) 1706 1707 a = S("IBM") 1708 assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z"))) 1709 assert_equal(S("HAL"), a) 1710 1711 a = S("ibm") 1712 assert_nil(a.tr!(S("B-Z"), S("A-Z"))) 1713 assert_equal(S("ibm"), a) 1714 1715 a = "abc".force_encoding(Encoding::US_ASCII) 1716 assert_nil(a.tr!(S("z"), S("\u0101")), '[ruby-core:22326]') 1717 assert_equal(Encoding::US_ASCII, a.encoding, '[ruby-core:22326]') 1718 end 1719 1720 def test_tr_s 1721 assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp"))) 1722 assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*"))) 1723 assert_equal("a".hash, "\u0101\u0101".tr_s("\u0101", "a").hash) 1724 assert_equal(true, "\u3041\u3041".tr("\u3041", "a").ascii_only?) 1725 end 1726 1727 def test_tr_s! 1728 a = S("hello") 1729 b = a.dup 1730 assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp"))) 1731 assert_equal(S("hypo"), a) 1732 assert_equal(S("hello"), b) 1733 1734 a = S("hello") 1735 assert_equal(S("h*o"), a.tr_s!(S("el"), S("*"))) 1736 assert_equal(S("h*o"), a) 1737 end 1738 1739 def test_unpack 1740 a = [S("cat"), S("wom"), S("x"), S("yy")] 1741 assert_equal(a, S("catwomx yy ").unpack(S("A3A3A3A3"))) 1742 1743 assert_equal([S("cat")], S("cat \000\000").unpack(S("A*"))) 1744 assert_equal([S("cwx"), S("wx"), S("x"), S("yy")], 1745 S("cwx yy ").unpack(S("A3@1A3@2A3A3"))) 1746 assert_equal([S("cat"), S("wom"), S("x\000\000"), S("yy\000")], 1747 S("catwomx\000\000yy\000").unpack(S("a3a3a3a3"))) 1748 assert_equal([S("cat \000\000")], S("cat \000\000").unpack(S("a*"))) 1749 assert_equal([S("ca")], S("catdog").unpack(S("a2"))) 1750 1751 assert_equal([S("cat\000\000")], 1752 S("cat\000\000\000\000\000dog").unpack(S("a5"))) 1753 1754 assert_equal([S("01100001")], S("\x61").unpack(S("B8"))) 1755 assert_equal([S("01100001")], S("\x61").unpack(S("B*"))) 1756 assert_equal([S("0110000100110111")], S("\x61\x37").unpack(S("B16"))) 1757 assert_equal([S("01100001"), S("00110111")], S("\x61\x37").unpack(S("B8B8"))) 1758 assert_equal([S("0110")], S("\x60").unpack(S("B4"))) 1759 1760 assert_equal([S("01")], S("\x40").unpack(S("B2"))) 1761 1762 assert_equal([S("01100001")], S("\x86").unpack(S("b8"))) 1763 assert_equal([S("01100001")], S("\x86").unpack(S("b*"))) 1764 1765 assert_equal([S("0110000100110111")], S("\x86\xec").unpack(S("b16"))) 1766 assert_equal([S("01100001"), S("00110111")], S("\x86\xec").unpack(S("b8b8"))) 1767 1768 assert_equal([S("0110")], S("\x06").unpack(S("b4"))) 1769 assert_equal([S("01")], S("\x02").unpack(S("b2"))) 1770 1771 assert_equal([ 65, 66, 67 ], S("ABC").unpack(S("C3"))) 1772 assert_equal([ 255, 66, 67 ], S("\377BC").unpack("C*")) 1773 assert_equal([ 65, 66, 67 ], S("ABC").unpack("c3")) 1774 assert_equal([ -1, 66, 67 ], S("\377BC").unpack("c*")) 1775 1776 1777 assert_equal([S("4142"), S("0a"), S("1")], S("AB\n\x10").unpack(S("H4H2H1"))) 1778 assert_equal([S("1424"), S("a0"), S("2")], S("AB\n\x02").unpack(S("h4h2h1"))) 1779 1780 assert_equal([S("abc\002defcat\001"), S(""), S("")], 1781 S("abc=02def=\ncat=\n=01=\n").unpack(S("M9M3M4"))) 1782 1783 assert_equal([S("hello\n")], S("aGVsbG8K\n").unpack(S("m"))) 1784 1785 assert_equal([S("hello\nhello\n")], S(",:&5L;&\\*:&5L;&\\*\n").unpack(S("u"))) 1786 1787 assert_equal([0xa9, 0x42, 0x2260], S("\xc2\xa9B\xe2\x89\xa0").unpack(S("U*"))) 1788 1789=begin 1790 skipping "Not tested: 1791 D,d & double-precision float, native format\\ 1792 E & double-precision float, little-endian byte order\\ 1793 e & single-precision float, little-endian byte order\\ 1794 F,f & single-precision float, native format\\ 1795 G & double-precision float, network (big-endian) byte order\\ 1796 g & single-precision float, network (big-endian) byte order\\ 1797 I & unsigned integer\\ 1798 i & integer\\ 1799 L & unsigned long\\ 1800 l & long\\ 1801 1802 m & string encoded in base64 (uuencoded)\\ 1803 N & long, network (big-endian) byte order\\ 1804 n & short, network (big-endian) byte-order\\ 1805 P & pointer to a structure (fixed-length string)\\ 1806 p & pointer to a null-terminated string\\ 1807 S & unsigned short\\ 1808 s & short\\ 1809 V & long, little-endian byte order\\ 1810 v & short, little-endian byte order\\ 1811 X & back up a byte\\ 1812 x & null byte\\ 1813 Z & ASCII string (null padded, count is width)\\ 1814" 1815=end 1816 end 1817 1818 def test_upcase 1819 assert_equal(S("HELLO"), S("hello").upcase) 1820 assert_equal(S("HELLO"), S("hello").upcase) 1821 assert_equal(S("HELLO"), S("HELLO").upcase) 1822 assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase) 1823 end 1824 1825 def test_upcase! 1826 a = S("hello") 1827 b = a.dup 1828 assert_equal(S("HELLO"), a.upcase!) 1829 assert_equal(S("HELLO"), a) 1830 assert_equal(S("hello"), b) 1831 1832 a = S("HELLO") 1833 assert_nil(a.upcase!) 1834 assert_equal(S("HELLO"), a) 1835 end 1836 1837 def test_upto 1838 a = S("aa") 1839 start = S("aa") 1840 count = 0 1841 assert_equal(S("aa"), a.upto(S("zz")) {|s| 1842 assert_equal(start, s) 1843 start.succ! 1844 count += 1 1845 }) 1846 assert_equal(676, count) 1847 end 1848 1849 def test_upto_numeric 1850 a = S("00") 1851 start = S("00") 1852 count = 0 1853 assert_equal(S("00"), a.upto(S("23")) {|s| 1854 assert_equal(start, s, "[ruby-dev:39361]") 1855 assert_equal(Encoding::US_ASCII, s.encoding) 1856 start.succ! 1857 count += 1 1858 }) 1859 assert_equal(24, count, "[ruby-dev:39361]") 1860 end 1861 1862 def test_upto_nonalnum 1863 first = S("\u3041") 1864 last = S("\u3093") 1865 count = 0 1866 assert_equal(first, first.upto(last) {|s| 1867 count += 1 1868 s.replace(last) 1869 }) 1870 assert_equal(83, count, "[ruby-dev:39626]") 1871 end 1872 1873 def test_mod_check 1874 assert_raise(RuntimeError) { 1875 s = "" 1876 s.sub!(/\A/) { s.replace "z" * 2000; "zzz" } 1877 } 1878 end 1879 1880 def test_frozen_check 1881 assert_raise(RuntimeError) { 1882 s = "" 1883 s.sub!(/\A/) { s.freeze; "zzz" } 1884 } 1885 end 1886 1887 class S2 < String 1888 end 1889 def test_str_new4 1890 s = (0..54).to_a.join # length = 100 1891 s2 = S2.new(s[10,90]) 1892 s3 = s2[10,80] 1893 assert_equal((10..54).to_a.to_a.join, s2) 1894 assert_equal((15..54).to_a.to_a.join, s3) 1895 end 1896 1897 def test_rb_str_new4 1898 s = "a" * 100 1899 s2 = s[10,90] 1900 assert_equal("a" * 90, s2) 1901 s3 = s2[10,80] 1902 assert_equal("a" * 80, s3) 1903 end 1904 1905 class StringLike 1906 def initialize(str) 1907 @str = str 1908 end 1909 1910 def to_str 1911 @str 1912 end 1913 end 1914 1915 def test_rb_str_to_str 1916 assert_equal("ab", "a" + StringLike.new("b")) 1917 end 1918 1919 def test_rb_str_shared_replace 1920 s = "a" * 100 1921 s.succ! 1922 assert_equal("a" * 99 + "b", s) 1923 s = "" 1924 s.succ! 1925 assert_equal("", s) 1926 end 1927 1928 def test_times 1929 assert_raise(ArgumentError) { "a" * (-1) } 1930 end 1931 1932 def test_splice! 1933 l = S("1234\n234\n34\n4\n") 1934 assert_equal(S("1234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]") 1935 assert_equal(S("234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]") 1936 assert_equal(S("34\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]") 1937 assert_equal(S("4\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]") 1938 assert_nil(l.slice!(/\A.*\n/), "[ruby-dev:31665]") 1939 end 1940 1941 def test_times2 1942 s1 = '' 1943 100.times {|n| 1944 s2 = "a" * n 1945 assert_equal(s1, s2) 1946 s1 << 'a' 1947 } 1948 1949 assert_raise(ArgumentError) { "foo" * (-1) } 1950 end 1951 1952 def test_respond_to 1953 o = Object.new 1954 def o.respond_to?(arg) [:to_str].include?(arg) ? nil : super end 1955 def o.to_str() "" end 1956 def o.==(other) "" == other end 1957 assert_equal(false, "" == o) 1958 end 1959 1960 def test_match_method 1961 assert_equal("bar", "foobarbaz".match(/bar/).to_s) 1962 1963 o = Regexp.new('foo') 1964 def o.match(x, y, z); x + y + z; end 1965 assert_equal("foobarbaz", "foo".match(o, "bar", "baz")) 1966 x = nil 1967 "foo".match(o, "bar", "baz") {|y| x = y } 1968 assert_equal("foobarbaz", x) 1969 1970 assert_raise(ArgumentError) { "foo".match } 1971 end 1972 1973 def test_clear 1974 s = "foo" * 100 1975 s.clear 1976 assert_equal("", s) 1977 end 1978 1979 def test_to_s_2 1980 c = Class.new(String) 1981 s = c.new 1982 s.replace("foo") 1983 assert_equal("foo", s.to_s) 1984 assert_instance_of(String, s.to_s) 1985 end 1986 1987 def test_inspect_nul 1988 bug8290 = '[ruby-core:54458]' 1989 s = "\0" + "12" 1990 assert_equal '"\u000012"', s.inspect, bug8290 1991 s = "\0".b + "12" 1992 assert_equal '"\x0012"', s.inspect, bug8290 1993 end 1994 1995 def test_partition 1996 assert_equal(%w(he l lo), "hello".partition(/l/)) 1997 assert_equal(%w(he l lo), "hello".partition("l")) 1998 assert_raise(TypeError) { "hello".partition(1) } 1999 def (hyphen = Object.new).to_str; "-"; end 2000 assert_equal(%w(foo - bar), "foo-bar".partition(hyphen), '[ruby-core:23540]') 2001 2002 bug6206 = '[ruby-dev:45441]' 2003 Encoding.list.each do |enc| 2004 next unless enc.ascii_compatible? 2005 s = S("a:".force_encoding(enc)) 2006 assert_equal([enc]*3, s.partition("|").map(&:encoding), bug6206) 2007 end 2008 end 2009 2010 def test_rpartition 2011 assert_equal(%w(hel l o), "hello".rpartition(/l/)) 2012 assert_equal(%w(hel l o), "hello".rpartition("l")) 2013 assert_raise(TypeError) { "hello".rpartition(1) } 2014 def (hyphen = Object.new).to_str; "-"; end 2015 assert_equal(%w(foo - bar), "foo-bar".rpartition(hyphen), '[ruby-core:23540]') 2016 2017 bug6206 = '[ruby-dev:45441]' 2018 Encoding.list.each do |enc| 2019 next unless enc.ascii_compatible? 2020 s = S("a:".force_encoding(enc)) 2021 assert_equal([enc]*3, s.rpartition("|").map(&:encoding), bug6206) 2022 end 2023 end 2024 2025 def test_setter 2026 assert_raise(TypeError) { $/ = 1 } 2027 end 2028 2029 def test_to_id 2030 c = Class.new 2031 c.class_eval do 2032 def initialize 2033 @foo = :foo 2034 end 2035 end 2036 2037 assert_raise(TypeError) do 2038 c.class_eval { attr 1 } 2039 end 2040 2041 o = Object.new 2042 def o.to_str; :foo; end 2043 assert_raise(TypeError) do 2044 c.class_eval { attr 1 } 2045 end 2046 2047 class << o;remove_method :to_str;end 2048 def o.to_str; "foo"; end 2049 assert_nothing_raised do 2050 c.class_eval { attr o } 2051 end 2052 assert_equal(:foo, c.new.foo) 2053 end 2054 2055 def test_gsub_enumerator 2056 assert_normal_exit %q{"abc".gsub(/./).next}, "[ruby-dev:34828]" 2057 end 2058 2059 def test_clear_nonasciicompat 2060 assert_equal("", "\u3042".encode("ISO-2022-JP").clear) 2061 end 2062 2063 def test_try_convert 2064 assert_equal(nil, String.try_convert(1)) 2065 assert_equal("foo", String.try_convert("foo")) 2066 end 2067 2068 def test_substr_negative_begin 2069 assert_equal("\u3042", ("\u3042" * 100)[-1]) 2070 end 2071 2072=begin 2073 def test_compare_different_encoding_string 2074 s1 = "\xff".force_encoding("UTF-8") 2075 s2 = "\xff".force_encoding("ISO-2022-JP") 2076 assert_equal([-1, 1], [s1 <=> s2, s2 <=> s1].sort) 2077 end 2078=end 2079 2080 def test_casecmp 2081 assert_equal(1, "\u3042B".casecmp("\u3042a")) 2082 end 2083 2084 def test_upcase2 2085 assert_equal("\u3042AB", "\u3042aB".upcase) 2086 end 2087 2088 def test_downcase2 2089 assert_equal("\u3042ab", "\u3042aB".downcase) 2090 end 2091 2092 def test_rstrip 2093 assert_equal("\u3042", "\u3042 ".rstrip) 2094 assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip } 2095 end 2096 2097=begin 2098 def test_symbol_table_overflow 2099 assert_in_out_err([], <<-INPUT, [], /symbol table overflow \(symbol [a-z]{8}\) \(RuntimeError\)/) 2100 ("aaaaaaaa".."zzzzzzzz").each {|s| s.to_sym } 2101 INPUT 2102 end 2103=end 2104 2105 def test_shared_force_encoding 2106 s = "\u{3066}\u{3059}\u{3068}".gsub(//, '') 2107 h = {} 2108 h[s] = nil 2109 k = h.keys[0] 2110 assert_equal(s, k, '[ruby-dev:39068]') 2111 assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]') 2112 s.dup.force_encoding(Encoding::ASCII_8BIT).gsub(//, '') 2113 k = h.keys[0] 2114 assert_equal(s, k, '[ruby-dev:39068]') 2115 assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]') 2116 end 2117 2118 def test_ascii_incomat_inspect 2119 bug4081 = '[ruby-core:33283]' 2120 [Encoding::UTF_16LE, Encoding::UTF_16BE, 2121 Encoding::UTF_32LE, Encoding::UTF_32BE].each do |e| 2122 assert_equal('"abc"', "abc".encode(e).inspect) 2123 assert_equal('"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).inspect) 2124 assert_equal('"ab\\"c"', "ab\"c".encode(e).inspect, bug4081) 2125 end 2126 begin 2127 verbose, $VERBOSE = $VERBOSE, nil 2128 ext = Encoding.default_external 2129 Encoding.default_external = "us-ascii" 2130 $VERBOSE = verbose 2131 i = "abc\"\\".force_encoding("utf-8").inspect 2132 ensure 2133 $VERBOSE = nil 2134 Encoding.default_external = ext 2135 $VERBOSE = verbose 2136 end 2137 assert_equal('"abc\\"\\\\"', i, bug4081) 2138 end 2139 2140 def test_dummy_inspect 2141 assert_equal('"\e\x24\x42\x22\x4C\x22\x68\e\x28\x42"', 2142 "\u{ffe2}\u{2235}".encode("cp50220").inspect) 2143 end 2144 2145 def test_prepend 2146 assert_equal(S("hello world!"), "world!".prepend("hello ")) 2147 2148 foo = Object.new 2149 def foo.to_str 2150 "b" 2151 end 2152 assert_equal(S("ba"), "a".prepend(foo)) 2153 2154 a = S("world") 2155 b = S("hello ") 2156 a.prepend(b) 2157 assert_equal(S("hello world"), a) 2158 assert_equal(S("hello "), b) 2159 end 2160 2161 def u(str) 2162 str.force_encoding(Encoding::UTF_8) 2163 end 2164 2165 def test_byteslice 2166 assert_equal("h", "hello".byteslice(0)) 2167 assert_equal(nil, "hello".byteslice(5)) 2168 assert_equal("o", "hello".byteslice(-1)) 2169 assert_equal(nil, "hello".byteslice(-6)) 2170 2171 assert_equal("", "hello".byteslice(0, 0)) 2172 assert_equal("hello", "hello".byteslice(0, 6)) 2173 assert_equal("hello", "hello".byteslice(0, 6)) 2174 assert_equal("", "hello".byteslice(5, 1)) 2175 assert_equal("o", "hello".byteslice(-1, 6)) 2176 assert_equal(nil, "hello".byteslice(-6, 1)) 2177 assert_equal(nil, "hello".byteslice(0, -1)) 2178 2179 assert_equal("h", "hello".byteslice(0..0)) 2180 assert_equal("", "hello".byteslice(5..0)) 2181 assert_equal("o", "hello".byteslice(4..5)) 2182 assert_equal(nil, "hello".byteslice(6..0)) 2183 assert_equal("", "hello".byteslice(-1..0)) 2184 assert_equal("llo", "hello".byteslice(-3..5)) 2185 2186 assert_equal(u("\x81"), "\u3042".byteslice(1)) 2187 assert_equal(u("\x81\x82"), "\u3042".byteslice(1, 2)) 2188 assert_equal(u("\x81\x82"), "\u3042".byteslice(1..2)) 2189 2190 assert_equal(u("\x82")+("\u3042"*9), ("\u3042"*10).byteslice(2, 28)) 2191 2192 bug7954 = '[ruby-dev:47108]' 2193 assert_equal(false, "\u3042".byteslice(0, 2).valid_encoding?) 2194 assert_equal(false, ("\u3042"*10).byteslice(0, 20).valid_encoding?) 2195 end 2196end 2197 2198class TestString2 < TestString 2199 def initialize(*args) 2200 super 2201 @cls = S2 2202 end 2203end 2204