PageRenderTime 43ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_string.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1830 lines | 1590 code | 238 blank | 2 comment | 36 complexity | de9afed95d2f26e44511839d7c91138d MD5 | raw file
  1. require 'test/unit'
  2. require_relative 'envutil'
  3. # use of $= is deprecated after 1.7.1
  4. def pre_1_7_1
  5. end
  6. class TestString < Test::Unit::TestCase
  7. def initialize(*args)
  8. @cls = String
  9. @aref_re_nth = true
  10. @aref_re_silent = false
  11. @aref_slicebang_silent = true
  12. super
  13. end
  14. def S(str)
  15. @cls.new(str)
  16. end
  17. def test_s_new
  18. assert_equal("RUBY", S("RUBY"))
  19. end
  20. def test_AREF # '[]'
  21. assert_equal("A", S("AooBar")[0])
  22. assert_equal("B", S("FooBaB")[-1])
  23. assert_equal(nil, S("FooBar")[6])
  24. assert_equal(nil, S("FooBar")[-7])
  25. assert_equal(S("Foo"), S("FooBar")[0,3])
  26. assert_equal(S("Bar"), S("FooBar")[-3,3])
  27. assert_equal(S(""), S("FooBar")[6,2])
  28. assert_equal(nil, S("FooBar")[-7,10])
  29. assert_equal(S("Foo"), S("FooBar")[0..2])
  30. assert_equal(S("Foo"), S("FooBar")[0...3])
  31. assert_equal(S("Bar"), S("FooBar")[-3..-1])
  32. assert_equal(S(""), S("FooBar")[6..2])
  33. assert_equal(nil, S("FooBar")[-10..-7])
  34. assert_equal(S("Foo"), S("FooBar")[/^F../])
  35. assert_equal(S("Bar"), S("FooBar")[/..r$/])
  36. assert_equal(nil, S("FooBar")[/xyzzy/])
  37. assert_equal(nil, S("FooBar")[/plugh/])
  38. assert_equal(S("Foo"), S("FooBar")[S("Foo")])
  39. assert_equal(S("Bar"), S("FooBar")[S("Bar")])
  40. assert_equal(nil, S("FooBar")[S("xyzzy")])
  41. assert_equal(nil, S("FooBar")[S("plugh")])
  42. if @aref_re_nth
  43. assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1])
  44. assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2])
  45. assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3])
  46. assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
  47. assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
  48. assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
  49. end
  50. o = Object.new
  51. def o.to_int; 2; end
  52. assert_equal("o", "foo"[o])
  53. assert_raise(ArgumentError) { "foo"[] }
  54. end
  55. def test_ASET # '[]='
  56. s = S("FooBar")
  57. s[0] = S('A')
  58. assert_equal(S("AooBar"), s)
  59. s[-1]= S('B')
  60. assert_equal(S("AooBaB"), s)
  61. assert_raise(IndexError) { s[-7] = S("xyz") }
  62. assert_equal(S("AooBaB"), s)
  63. s[0] = S("ABC")
  64. assert_equal(S("ABCooBaB"), s)
  65. s = S("FooBar")
  66. s[0,3] = S("A")
  67. assert_equal(S("ABar"),s)
  68. s[0] = S("Foo")
  69. assert_equal(S("FooBar"), s)
  70. s[-3,3] = S("Foo")
  71. assert_equal(S("FooFoo"), s)
  72. assert_raise(IndexError) { s[7,3] = S("Bar") }
  73. assert_raise(IndexError) { s[-7,3] = S("Bar") }
  74. s = S("FooBar")
  75. s[0..2] = S("A")
  76. assert_equal(S("ABar"), s)
  77. s[1..3] = S("Foo")
  78. assert_equal(S("AFoo"), s)
  79. s[-4..-4] = S("Foo")
  80. assert_equal(S("FooFoo"), s)
  81. assert_raise(RangeError) { s[7..10] = S("Bar") }
  82. assert_raise(RangeError) { s[-7..-10] = S("Bar") }
  83. s = S("FooBar")
  84. s[/^F../]= S("Bar")
  85. assert_equal(S("BarBar"), s)
  86. s[/..r$/] = S("Foo")
  87. assert_equal(S("BarFoo"), s)
  88. if @aref_re_silent
  89. s[/xyzzy/] = S("None")
  90. assert_equal(S("BarFoo"), s)
  91. else
  92. assert_raise(IndexError) { s[/xyzzy/] = S("None") }
  93. end
  94. if @aref_re_nth
  95. s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo")
  96. assert_equal(S("FooFoo"), s)
  97. s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar")
  98. assert_equal(S("FooBar"), s)
  99. assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" }
  100. s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo")
  101. assert_equal(S("FooFoo"), s)
  102. s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar")
  103. assert_equal(S("BarFoo"), s)
  104. assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" }
  105. end
  106. s = S("FooBar")
  107. s[S("Foo")] = S("Bar")
  108. assert_equal(S("BarBar"), s)
  109. pre_1_7_1 do
  110. s = S("FooBar")
  111. s[S("Foo")] = S("xyz")
  112. assert_equal(S("xyzBar"), s)
  113. $= = true
  114. s = S("FooBar")
  115. s[S("FOO")] = S("Bar")
  116. assert_equal(S("BarBar"), s)
  117. s[S("FOO")] = S("xyz")
  118. assert_equal(S("BarBar"), s)
  119. $= = false
  120. end
  121. s = S("a string")
  122. s[0..s.size] = S("another string")
  123. assert_equal(S("another string"), s)
  124. o = Object.new
  125. def o.to_int; 2; end
  126. s = "foo"
  127. s[o] = "bar"
  128. assert_equal("fobar", s)
  129. assert_raise(ArgumentError) { "foo"[1, 2, 3] = "" }
  130. end
  131. def test_CMP # '<=>'
  132. assert_equal(1, S("abcdef") <=> S("abcde"))
  133. assert_equal(0, S("abcdef") <=> S("abcdef"))
  134. assert_equal(-1, S("abcde") <=> S("abcdef"))
  135. assert_equal(-1, S("ABCDEF") <=> S("abcdef"))
  136. pre_1_7_1 do
  137. $= = true
  138. assert_equal(0, S("ABCDEF") <=> S("abcdef"))
  139. $= = false
  140. end
  141. assert_nil("foo" <=> Object.new)
  142. o = Object.new
  143. def o.to_str; "bar"; end
  144. assert_nil("foo" <=> o)
  145. def o.<=>(x); nil; end
  146. assert_nil("foo" <=> o)
  147. class << o;remove_method :<=>;end
  148. def o.<=>(x); 1; end
  149. assert_equal(-1, "foo" <=> o)
  150. class << o;remove_method :<=>;end
  151. def o.<=>(x); 2**100; end
  152. assert_equal(-(2**100), "foo" <=> o)
  153. end
  154. def test_EQUAL # '=='
  155. assert_equal(false, S("foo") == :foo)
  156. assert(S("abcdef") == S("abcdef"))
  157. pre_1_7_1 do
  158. $= = true
  159. assert(S("CAT") == S('cat'))
  160. assert(S("CaT") == S('cAt'))
  161. $= = false
  162. end
  163. assert(S("CAT") != S('cat'))
  164. assert(S("CaT") != S('cAt'))
  165. o = Object.new
  166. def o.to_str; end
  167. def o.==(x); false; end
  168. assert_equal(false, "foo" == o)
  169. class << o;remove_method :==;end
  170. def o.==(x); true; end
  171. assert_equal(true, "foo" == o)
  172. end
  173. def test_LSHIFT # '<<'
  174. assert_equal(S("world!"), S("world") << 33)
  175. assert_equal(S("world!"), S("world") << S('!'))
  176. s = "a"
  177. 10.times {|i|
  178. s << s
  179. assert_equal("a" * (2 << i), s)
  180. }
  181. s = ["foo"].pack("p")
  182. l = s.size
  183. s << "bar"
  184. assert_equal(l + 3, s.size)
  185. bug = '[ruby-core:27583]'
  186. assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -3}
  187. assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -2}
  188. assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -1}
  189. assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << 0x81308130}
  190. assert_nothing_raised {S("a".force_encoding(Encoding::GB18030)) << 0x81308130}
  191. end
  192. def test_MATCH # '=~'
  193. assert_equal(10, S("FeeFieFoo-Fum") =~ /Fum$/)
  194. assert_equal(nil, S("FeeFieFoo-Fum") =~ /FUM$/)
  195. pre_1_7_1 do
  196. $= = true
  197. assert_equal(10, S("FeeFieFoo-Fum") =~ /FUM$/)
  198. $= = false
  199. end
  200. o = Object.new
  201. def o.=~(x); x + "bar"; end
  202. assert_equal("foobar", S("foo") =~ o)
  203. assert_raise(TypeError) { S("foo") =~ "foo" }
  204. end
  205. def test_MOD # '%'
  206. assert_equal(S("00123"), S("%05d") % 123)
  207. assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1])
  208. x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") %
  209. [S("hi"),
  210. 123,
  211. S("never seen"),
  212. 456,
  213. 0,
  214. ?A,
  215. 3.0999,
  216. 11,
  217. 171,
  218. 171,
  219. 11,
  220. 171,
  221. 171]
  222. assert_equal(S(' hi 123 %foo 456 0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
  223. end
  224. def test_MUL # '*'
  225. assert_equal(S("XXX"), S("X") * 3)
  226. assert_equal(S("HOHO"), S("HO") * 2)
  227. end
  228. def test_PLUS # '+'
  229. assert_equal(S("Yodel"), S("Yo") + S("del"))
  230. end
  231. def casetest(a, b, rev=false)
  232. case a
  233. when b
  234. assert(!rev)
  235. else
  236. assert(rev)
  237. end
  238. end
  239. def test_VERY_EQUAL # '==='
  240. # assert_equal(true, S("foo") === :foo)
  241. casetest(S("abcdef"), S("abcdef"))
  242. pre_1_7_1 do
  243. $= = true
  244. casetest(S("CAT"), S('cat'))
  245. casetest(S("CaT"), S('cAt'))
  246. $= = false
  247. end
  248. casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to
  249. casetest(S("CaT"), S('cAt'), true) # find these in the case.
  250. end
  251. def test_capitalize
  252. assert_equal(S("Hello"), S("hello").capitalize)
  253. assert_equal(S("Hello"), S("hELLO").capitalize)
  254. assert_equal(S("123abc"), S("123ABC").capitalize)
  255. end
  256. def test_capitalize!
  257. a = S("hello"); a.capitalize!
  258. assert_equal(S("Hello"), a)
  259. a = S("hELLO"); a.capitalize!
  260. assert_equal(S("Hello"), a)
  261. a = S("123ABC"); a.capitalize!
  262. assert_equal(S("123abc"), a)
  263. assert_equal(nil, S("123abc").capitalize!)
  264. assert_equal(S("123abc"), S("123ABC").capitalize!)
  265. assert_equal(S("Abc"), S("ABC").capitalize!)
  266. assert_equal(S("Abc"), S("abc").capitalize!)
  267. assert_equal(nil, S("Abc").capitalize!)
  268. a = S("hello")
  269. b = a.dup
  270. assert_equal(S("Hello"), a.capitalize!)
  271. assert_equal(S("hello"), b)
  272. end
  273. Bug2463 = '[ruby-dev:39856]'
  274. def test_center
  275. assert_equal(S("hello"), S("hello").center(4))
  276. assert_equal(S(" hello "), S("hello").center(11))
  277. assert_equal(S("ababaababa"), S("").center(10, "ab"), Bug2463)
  278. assert_equal(S("ababaababab"), S("").center(11, "ab"), Bug2463)
  279. end
  280. def test_chomp
  281. assert_equal(S("hello"), S("hello").chomp("\n"))
  282. assert_equal(S("hello"), S("hello\n").chomp("\n"))
  283. save = $/
  284. $/ = "\n"
  285. assert_equal(S("hello"), S("hello").chomp)
  286. assert_equal(S("hello"), S("hello\n").chomp)
  287. $/ = "!"
  288. assert_equal(S("hello"), S("hello").chomp)
  289. assert_equal(S("hello"), S("hello!").chomp)
  290. $/ = save
  291. assert_equal(S("a").hash, S("a\u0101").chomp(S("\u0101")).hash, '[ruby-core:22414]')
  292. end
  293. def test_chomp!
  294. a = S("hello")
  295. a.chomp!(S("\n"))
  296. assert_equal(S("hello"), a)
  297. assert_equal(nil, a.chomp!(S("\n")))
  298. a = S("hello\n")
  299. a.chomp!(S("\n"))
  300. assert_equal(S("hello"), a)
  301. save = $/
  302. $/ = "\n"
  303. a = S("hello")
  304. a.chomp!
  305. assert_equal(S("hello"), a)
  306. a = S("hello\n")
  307. a.chomp!
  308. assert_equal(S("hello"), a)
  309. $/ = "!"
  310. a = S("hello")
  311. a.chomp!
  312. assert_equal(S("hello"), a)
  313. a="hello!"
  314. a.chomp!
  315. assert_equal(S("hello"), a)
  316. $/ = save
  317. a = S("hello\n")
  318. b = a.dup
  319. assert_equal(S("hello"), a.chomp!)
  320. assert_equal(S("hello\n"), b)
  321. s = "foo\r\n"
  322. s.chomp!
  323. assert_equal("foo", s)
  324. s = "foo\r"
  325. s.chomp!
  326. assert_equal("foo", s)
  327. s = "foo\r\n"
  328. s.chomp!("")
  329. assert_equal("foo", s)
  330. s = "foo\r"
  331. s.chomp!("")
  332. assert_equal("foo\r", s)
  333. assert_equal(S("a").hash, S("a\u0101").chomp!(S("\u0101")).hash, '[ruby-core:22414]')
  334. end
  335. def test_chop
  336. assert_equal(S("hell"), S("hello").chop)
  337. assert_equal(S("hello"), S("hello\r\n").chop)
  338. assert_equal(S("hello\n"), S("hello\n\r").chop)
  339. assert_equal(S(""), S("\r\n").chop)
  340. assert_equal(S(""), S("").chop)
  341. assert_equal(S("a").hash, S("a\u00d8").chop.hash)
  342. end
  343. def test_chop!
  344. a = S("hello").chop!
  345. assert_equal(S("hell"), a)
  346. a = S("hello\r\n").chop!
  347. assert_equal(S("hello"), a)
  348. a = S("hello\n\r").chop!
  349. assert_equal(S("hello\n"), a)
  350. a = S("\r\n").chop!
  351. assert_equal(S(""), a)
  352. a = S("").chop!
  353. assert_nil(a)
  354. a = S("a\u00d8")
  355. a.chop!
  356. assert_equal(S("a").hash, a.hash)
  357. a = S("hello\n")
  358. b = a.dup
  359. assert_equal(S("hello"), a.chop!)
  360. assert_equal(S("hello\n"), b)
  361. end
  362. def test_clone
  363. for taint in [ false, true ]
  364. for untrust in [ false, true ]
  365. for frozen in [ false, true ]
  366. a = S("Cool")
  367. a.taint if taint
  368. a.untrust if untrust
  369. a.freeze if frozen
  370. b = a.clone
  371. assert_equal(a, b)
  372. assert(a.__id__ != b.__id__)
  373. assert_equal(a.frozen?, b.frozen?)
  374. assert_equal(a.untrusted?, b.untrusted?)
  375. assert_equal(a.tainted?, b.tainted?)
  376. end
  377. end
  378. end
  379. null = File.exist?("/dev/null") ? "/dev/null" : "NUL" # maybe DOSISH
  380. assert_equal("", File.read(null).clone, '[ruby-dev:32819] reported by Kazuhiro NISHIYAMA')
  381. end
  382. def test_concat
  383. assert_equal(S("world!"), S("world").concat(33))
  384. assert_equal(S("world!"), S("world").concat(S('!')))
  385. end
  386. def test_count
  387. a = S("hello world")
  388. assert_equal(5, a.count(S("lo")))
  389. assert_equal(2, a.count(S("lo"), S("o")))
  390. assert_equal(4, a.count(S("hello"), S("^l")))
  391. assert_equal(4, a.count(S("ej-m")))
  392. assert_equal(0, S("y").count(S("a\\-z")))
  393. assert_equal(5, "abc\u{3042 3044 3046}".count("^a"))
  394. assert_equal(5, "abc\u{3042 3044 3046}".count("^\u3042"))
  395. assert_equal(2, "abc\u{3042 3044 3046}".count("a-z", "^a"))
  396. assert_raise(ArgumentError) { "foo".count }
  397. end
  398. def test_crypt
  399. assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa")))
  400. assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab")))
  401. end
  402. def test_delete
  403. assert_equal(S("heo"), S("hello").delete(S("l"), S("lo")))
  404. assert_equal(S("he"), S("hello").delete(S("lo")))
  405. assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e")))
  406. assert_equal(S("ho"), S("hello").delete(S("ej-m")))
  407. assert_equal("a".hash, "a\u0101".delete("\u0101").hash, '[ruby-talk:329267]')
  408. assert_equal(true, "a\u0101".delete("\u0101").ascii_only?)
  409. assert_equal(true, "a\u3041".delete("\u3041").ascii_only?)
  410. assert_equal(false, "a\u3041\u3042".tr("\u3041", "a").ascii_only?)
  411. assert_equal("a", "abc\u{3042 3044 3046}".delete("^a"))
  412. assert_equal("bc\u{3042 3044 3046}", "abc\u{3042 3044 3046}".delete("a"))
  413. assert_equal("\u3042", "abc\u{3042 3044 3046}".delete("^\u3042"))
  414. end
  415. def test_delete!
  416. a = S("hello")
  417. a.delete!(S("l"), S("lo"))
  418. assert_equal(S("heo"), a)
  419. a = S("hello")
  420. a.delete!(S("lo"))
  421. assert_equal(S("he"), a)
  422. a = S("hello")
  423. a.delete!(S("aeiou"), S("^e"))
  424. assert_equal(S("hell"), a)
  425. a = S("hello")
  426. a.delete!(S("ej-m"))
  427. assert_equal(S("ho"), a)
  428. a = S("hello")
  429. assert_nil(a.delete!(S("z")))
  430. a = S("hello")
  431. b = a.dup
  432. a.delete!(S("lo"))
  433. assert_equal(S("he"), a)
  434. assert_equal(S("hello"), b)
  435. a = S("hello")
  436. a.delete!(S("^el"))
  437. assert_equal(S("ell"), a)
  438. assert_raise(ArgumentError) { S("foo").delete! }
  439. end
  440. def test_downcase
  441. assert_equal(S("hello"), S("helLO").downcase)
  442. assert_equal(S("hello"), S("hello").downcase)
  443. assert_equal(S("hello"), S("HELLO").downcase)
  444. assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase)
  445. end
  446. def test_downcase!
  447. a = S("helLO")
  448. b = a.dup
  449. assert_equal(S("hello"), a.downcase!)
  450. assert_equal(S("hello"), a)
  451. assert_equal(S("helLO"), b)
  452. a=S("hello")
  453. assert_nil(a.downcase!)
  454. assert_equal(S("hello"), a)
  455. end
  456. def test_dump
  457. a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10
  458. assert_equal(S('"Test\\x01\\x02\\x03\\t\\r\\n"'), a.dump)
  459. end
  460. def test_dup
  461. for taint in [ false, true ]
  462. for untrust in [ false, true ]
  463. for frozen in [ false, true ]
  464. a = S("hello")
  465. a.taint if taint
  466. a.untrust if untrust
  467. a.freeze if frozen
  468. b = a.dup
  469. assert_equal(a, b)
  470. assert(a.__id__ != b.__id__)
  471. assert(!b.frozen?)
  472. assert_equal(a.tainted?, b.tainted?)
  473. assert_equal(a.untrusted?, b.untrusted?)
  474. end
  475. end
  476. end
  477. end
  478. def test_each
  479. save = $/
  480. $/ = "\n"
  481. res=[]
  482. S("hello\nworld").lines.each {|x| res << x}
  483. assert_equal(S("hello\n"), res[0])
  484. assert_equal(S("world"), res[1])
  485. res=[]
  486. S("hello\n\n\nworld").lines(S('')).each {|x| res << x}
  487. assert_equal(S("hello\n\n\n"), res[0])
  488. assert_equal(S("world"), res[1])
  489. $/ = "!"
  490. res=[]
  491. S("hello!world").lines.each {|x| res << x}
  492. assert_equal(S("hello!"), res[0])
  493. assert_equal(S("world"), res[1])
  494. $/ = save
  495. end
  496. def test_each_byte
  497. res = []
  498. S("ABC").each_byte {|x| res << x }
  499. assert_equal(65, res[0])
  500. assert_equal(66, res[1])
  501. assert_equal(67, res[2])
  502. end
  503. def test_each_line
  504. save = $/
  505. $/ = "\n"
  506. res=[]
  507. S("hello\nworld").lines.each {|x| res << x}
  508. assert_equal(S("hello\n"), res[0])
  509. assert_equal(S("world"), res[1])
  510. res=[]
  511. S("hello\n\n\nworld").lines(S('')).each {|x| res << x}
  512. assert_equal(S("hello\n\n\n"), res[0])
  513. assert_equal(S("world"), res[1])
  514. $/ = "!"
  515. res=[]
  516. S("hello!world").lines.each {|x| res << x}
  517. assert_equal(S("hello!"), res[0])
  518. assert_equal(S("world"), res[1])
  519. $/ = save
  520. s = nil
  521. "foo\nbar".each_line(nil) {|s2| s = s2 }
  522. assert_equal("foo\nbar", s)
  523. end
  524. def test_empty?
  525. assert(S("").empty?)
  526. assert(!S("not").empty?)
  527. end
  528. def test_eql?
  529. a = S("hello")
  530. assert(a.eql?(S("hello")))
  531. assert(a.eql?(a))
  532. end
  533. def test_gsub
  534. assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*')))
  535. assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>')))
  536. assert_equal(S("h e l l o "),
  537. S("hello").gsub(/./) { |s| s[0].to_s + S(' ')})
  538. assert_equal(S("HELL-o"),
  539. S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 })
  540. a = S("hello")
  541. a.taint
  542. a.untrust
  543. assert(a.gsub(/./, S('X')).tainted?)
  544. assert(a.gsub(/./, S('X')).untrusted?)
  545. assert_equal("z", "abc".gsub(/./, "a" => "z"), "moved from btest/knownbug")
  546. assert_raise(ArgumentError) { "foo".gsub }
  547. end
  548. def test_gsub_encoding
  549. a = S("hello world")
  550. a.force_encoding Encoding::UTF_8
  551. b = S("hi")
  552. b.force_encoding Encoding::US_ASCII
  553. assert_equal Encoding::UTF_8, a.gsub(/hello/, b).encoding
  554. c = S("everybody")
  555. c.force_encoding Encoding::US_ASCII
  556. assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding
  557. end
  558. def test_gsub!
  559. a = S("hello")
  560. b = a.dup
  561. a.gsub!(/[aeiou]/, S('*'))
  562. assert_equal(S("h*ll*"), a)
  563. assert_equal(S("hello"), b)
  564. a = S("hello")
  565. a.gsub!(/([aeiou])/, S('<\1>'))
  566. assert_equal(S("h<e>ll<o>"), a)
  567. a = S("hello")
  568. a.gsub!(/./) { |s| s[0].to_s + S(' ')}
  569. assert_equal(S("h e l l o "), a)
  570. a = S("hello")
  571. a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
  572. assert_equal(S("HELL-o"), a)
  573. r = S('X')
  574. r.taint
  575. r.untrust
  576. a.gsub!(/./, r)
  577. assert(a.tainted?)
  578. assert(a.untrusted?)
  579. a = S("hello")
  580. assert_nil(a.sub!(S('X'), S('Y')))
  581. end
  582. def test_sub_hash
  583. assert_equal('azc', 'abc'.sub(/b/, "b" => "z"))
  584. assert_equal('ac', 'abc'.sub(/b/, {}))
  585. assert_equal('a1c', 'abc'.sub(/b/, "b" => 1))
  586. assert_equal('aBc', 'abc'.sub(/b/, Hash.new {|h, k| k.upcase }))
  587. assert_equal('a[\&]c', 'abc'.sub(/b/, "b" => '[\&]'))
  588. assert_equal('aBcabc', 'abcabc'.sub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
  589. assert_equal('aBcdef', 'abcdef'.sub(/de|b/, "b" => "B", "de" => "DE"))
  590. end
  591. def test_gsub_hash
  592. assert_equal('azc', 'abc'.gsub(/b/, "b" => "z"))
  593. assert_equal('ac', 'abc'.gsub(/b/, {}))
  594. assert_equal('a1c', 'abc'.gsub(/b/, "b" => 1))
  595. assert_equal('aBc', 'abc'.gsub(/b/, Hash.new {|h, k| k.upcase }))
  596. assert_equal('a[\&]c', 'abc'.gsub(/b/, "b" => '[\&]'))
  597. assert_equal('aBcaBc', 'abcabc'.gsub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
  598. assert_equal('aBcDEf', 'abcdef'.gsub(/de|b/, "b" => "B", "de" => "DE"))
  599. end
  600. def test_hash
  601. assert_equal(S("hello").hash, S("hello").hash)
  602. assert(S("hello").hash != S("helLO").hash)
  603. bug4104 = '[ruby-core:33500]'
  604. assert_not_equal(S("a").hash, S("a\0").hash, bug4104)
  605. end
  606. def test_hash_random
  607. str = 'abc'
  608. a = [str.hash.to_s]
  609. 3.times {
  610. assert_in_out_err(["-e", "print #{str.dump}.hash"], "") do |r, e|
  611. a += r
  612. assert_equal([], e)
  613. end
  614. }
  615. assert_not_equal([str.hash.to_s], a.uniq)
  616. end
  617. def test_hex
  618. assert_equal(255, S("0xff").hex)
  619. assert_equal(-255, S("-0xff").hex)
  620. assert_equal(255, S("ff").hex)
  621. assert_equal(-255, S("-ff").hex)
  622. assert_equal(0, S("-ralph").hex)
  623. assert_equal(-15, S("-fred").hex)
  624. assert_equal(15, S("fred").hex)
  625. end
  626. def test_include?
  627. assert( S("foobar").include?(?f))
  628. assert( S("foobar").include?(S("foo")))
  629. assert(!S("foobar").include?(S("baz")))
  630. assert(!S("foobar").include?(?z))
  631. end
  632. def test_index
  633. assert_equal(0, S("hello").index(?h))
  634. assert_equal(1, S("hello").index(S("ell")))
  635. assert_equal(2, S("hello").index(/ll./))
  636. assert_equal(3, S("hello").index(?l, 3))
  637. assert_equal(3, S("hello").index(S("l"), 3))
  638. assert_equal(3, S("hello").index(/l./, 3))
  639. assert_nil(S("hello").index(?z, 3))
  640. assert_nil(S("hello").index(S("z"), 3))
  641. assert_nil(S("hello").index(/z./, 3))
  642. assert_nil(S("hello").index(?z))
  643. assert_nil(S("hello").index(S("z")))
  644. assert_nil(S("hello").index(/z./))
  645. o = Object.new
  646. def o.to_str; "bar"; end
  647. assert_equal(3, "foobarbarbaz".index(o))
  648. assert_raise(TypeError) { "foo".index(Object.new) }
  649. assert_nil("foo".index(//, -100))
  650. assert_nil($~)
  651. end
  652. def test_intern
  653. assert_equal(:koala, S("koala").intern)
  654. assert(:koala != S("Koala").intern)
  655. end
  656. def test_length
  657. assert_equal(0, S("").length)
  658. assert_equal(4, S("1234").length)
  659. assert_equal(6, S("1234\r\n").length)
  660. assert_equal(7, S("\0011234\r\n").length)
  661. end
  662. def test_ljust
  663. assert_equal(S("hello"), S("hello").ljust(4))
  664. assert_equal(S("hello "), S("hello").ljust(11))
  665. assert_equal(S("ababababab"), S("").ljust(10, "ab"), Bug2463)
  666. assert_equal(S("abababababa"), S("").ljust(11, "ab"), Bug2463)
  667. end
  668. def test_next
  669. assert_equal(S("abd"), S("abc").next)
  670. assert_equal(S("z"), S("y").next)
  671. assert_equal(S("aaa"), S("zz").next)
  672. assert_equal(S("124"), S("123").next)
  673. assert_equal(S("1000"), S("999").next)
  674. assert_equal(S("2000aaa"), S("1999zzz").next)
  675. assert_equal(S("AAAAA000"), S("ZZZZ999").next)
  676. assert_equal(S("*+"), S("**").next)
  677. end
  678. def test_next!
  679. a = S("abc")
  680. b = a.dup
  681. assert_equal(S("abd"), a.next!)
  682. assert_equal(S("abd"), a)
  683. assert_equal(S("abc"), b)
  684. a = S("y")
  685. assert_equal(S("z"), a.next!)
  686. assert_equal(S("z"), a)
  687. a = S("zz")
  688. assert_equal(S("aaa"), a.next!)
  689. assert_equal(S("aaa"), a)
  690. a = S("123")
  691. assert_equal(S("124"), a.next!)
  692. assert_equal(S("124"), a)
  693. a = S("999")
  694. assert_equal(S("1000"), a.next!)
  695. assert_equal(S("1000"), a)
  696. a = S("1999zzz")
  697. assert_equal(S("2000aaa"), a.next!)
  698. assert_equal(S("2000aaa"), a)
  699. a = S("ZZZZ999")
  700. assert_equal(S("AAAAA000"), a.next!)
  701. assert_equal(S("AAAAA000"), a)
  702. a = S("**")
  703. assert_equal(S("*+"), a.next!)
  704. assert_equal(S("*+"), a)
  705. end
  706. def test_oct
  707. assert_equal(255, S("0377").oct)
  708. assert_equal(255, S("377").oct)
  709. assert_equal(-255, S("-0377").oct)
  710. assert_equal(-255, S("-377").oct)
  711. assert_equal(0, S("OO").oct)
  712. assert_equal(24, S("030OO").oct)
  713. end
  714. def test_replace
  715. a = S("foo")
  716. assert_equal(S("f"), a.replace(S("f")))
  717. a = S("foo")
  718. assert_equal(S("foobar"), a.replace(S("foobar")))
  719. a = S("foo")
  720. a.taint
  721. a.untrust
  722. b = a.replace(S("xyz"))
  723. assert_equal(S("xyz"), b)
  724. assert(b.tainted?)
  725. assert(b.untrusted?)
  726. s = "foo" * 100
  727. s2 = ("bar" * 100).dup
  728. s.replace(s2)
  729. assert_equal(s2, s)
  730. s2 = ["foo"].pack("p")
  731. s.replace(s2)
  732. assert_equal(s2, s)
  733. fs = "".freeze
  734. assert_raise(RuntimeError) { fs.replace("a") }
  735. assert_raise(RuntimeError) { fs.replace(fs) }
  736. assert_raise(ArgumentError) { fs.replace() }
  737. assert_raise(RuntimeError) { fs.replace(42) }
  738. end
  739. def test_reverse
  740. assert_equal(S("beta"), S("ateb").reverse)
  741. assert_equal(S("madamImadam"), S("madamImadam").reverse)
  742. a=S("beta")
  743. assert_equal(S("ateb"), a.reverse)
  744. assert_equal(S("beta"), a)
  745. end
  746. def test_reverse!
  747. a = S("beta")
  748. b = a.dup
  749. assert_equal(S("ateb"), a.reverse!)
  750. assert_equal(S("ateb"), a)
  751. assert_equal(S("beta"), b)
  752. assert_equal(S("madamImadam"), S("madamImadam").reverse!)
  753. a = S("madamImadam")
  754. assert_equal(S("madamImadam"), a.reverse!) # ??
  755. assert_equal(S("madamImadam"), a)
  756. end
  757. def test_rindex
  758. assert_equal(3, S("hello").rindex(?l))
  759. assert_equal(6, S("ell, hello").rindex(S("ell")))
  760. assert_equal(7, S("ell, hello").rindex(/ll./))
  761. assert_equal(3, S("hello,lo").rindex(?l, 3))
  762. assert_equal(3, S("hello,lo").rindex(S("l"), 3))
  763. assert_equal(3, S("hello,lo").rindex(/l./, 3))
  764. assert_nil(S("hello").rindex(?z, 3))
  765. assert_nil(S("hello").rindex(S("z"), 3))
  766. assert_nil(S("hello").rindex(/z./, 3))
  767. assert_nil(S("hello").rindex(?z))
  768. assert_nil(S("hello").rindex(S("z")))
  769. assert_nil(S("hello").rindex(/z./))
  770. o = Object.new
  771. def o.to_str; "bar"; end
  772. assert_equal(6, "foobarbarbaz".rindex(o))
  773. assert_raise(TypeError) { "foo".rindex(Object.new) }
  774. assert_nil("foo".rindex(//, -100))
  775. assert_nil($~)
  776. end
  777. def test_rjust
  778. assert_equal(S("hello"), S("hello").rjust(4))
  779. assert_equal(S(" hello"), S("hello").rjust(11))
  780. assert_equal(S("ababababab"), S("").rjust(10, "ab"), Bug2463)
  781. assert_equal(S("abababababa"), S("").rjust(11, "ab"), Bug2463)
  782. end
  783. def test_scan
  784. a = S("cruel world")
  785. assert_equal([S("cruel"), S("world")],a.scan(/\w+/))
  786. assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../))
  787. assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/))
  788. res = []
  789. a.scan(/\w+/) { |w| res << w }
  790. assert_equal([S("cruel"), S("world") ],res)
  791. res = []
  792. a.scan(/.../) { |w| res << w }
  793. assert_equal([S("cru"), S("el "), S("wor")],res)
  794. res = []
  795. a.scan(/(...)/) { |w| res << w }
  796. assert_equal([[S("cru")], [S("el ")], [S("wor")]],res)
  797. a = S("hello")
  798. a.taint
  799. a.untrust
  800. res = []
  801. a.scan(/./) { |w| res << w }
  802. assert(res[0].tainted?, '[ruby-core:33338] #4087')
  803. assert(res[0].untrusted?, '[ruby-core:33338] #4087')
  804. end
  805. def test_size
  806. assert_equal(0, S("").size)
  807. assert_equal(4, S("1234").size)
  808. assert_equal(6, S("1234\r\n").size)
  809. assert_equal(7, S("\0011234\r\n").size)
  810. end
  811. def test_slice
  812. assert_equal(?A, S("AooBar").slice(0))
  813. assert_equal(?B, S("FooBaB").slice(-1))
  814. assert_nil(S("FooBar").slice(6))
  815. assert_nil(S("FooBar").slice(-7))
  816. assert_equal(S("Foo"), S("FooBar").slice(0,3))
  817. assert_equal(S(S("Bar")), S("FooBar").slice(-3,3))
  818. assert_nil(S("FooBar").slice(7,2)) # Maybe should be six?
  819. assert_nil(S("FooBar").slice(-7,10))
  820. assert_equal(S("Foo"), S("FooBar").slice(0..2))
  821. assert_equal(S("Bar"), S("FooBar").slice(-3..-1))
  822. assert_equal(S(""), S("FooBar").slice(6..2))
  823. assert_nil(S("FooBar").slice(-10..-7))
  824. assert_equal(S("Foo"), S("FooBar").slice(/^F../))
  825. assert_equal(S("Bar"), S("FooBar").slice(/..r$/))
  826. assert_nil(S("FooBar").slice(/xyzzy/))
  827. assert_nil(S("FooBar").slice(/plugh/))
  828. assert_equal(S("Foo"), S("FooBar").slice(S("Foo")))
  829. assert_equal(S("Bar"), S("FooBar").slice(S("Bar")))
  830. assert_nil(S("FooBar").slice(S("xyzzy")))
  831. assert_nil(S("FooBar").slice(S("plugh")))
  832. end
  833. def test_slice!
  834. a = S("AooBar")
  835. b = a.dup
  836. assert_equal(?A, a.slice!(0))
  837. assert_equal(S("ooBar"), a)
  838. assert_equal(S("AooBar"), b)
  839. a = S("FooBar")
  840. assert_equal(?r,a.slice!(-1))
  841. assert_equal(S("FooBa"), a)
  842. a = S("FooBar")
  843. if @aref_slicebang_silent
  844. assert_nil( a.slice!(6) )
  845. else
  846. assert_raise(IndexError) { a.slice!(6) }
  847. end
  848. assert_equal(S("FooBar"), a)
  849. if @aref_slicebang_silent
  850. assert_nil( a.slice!(-7) )
  851. else
  852. assert_raise(IndexError) { a.slice!(-7) }
  853. end
  854. assert_equal(S("FooBar"), a)
  855. a = S("FooBar")
  856. assert_equal(S("Foo"), a.slice!(0,3))
  857. assert_equal(S("Bar"), a)
  858. a = S("FooBar")
  859. assert_equal(S("Bar"), a.slice!(-3,3))
  860. assert_equal(S("Foo"), a)
  861. a=S("FooBar")
  862. if @aref_slicebang_silent
  863. assert_nil(a.slice!(7,2)) # Maybe should be six?
  864. else
  865. assert_raise(IndexError) {a.slice!(7,2)} # Maybe should be six?
  866. end
  867. assert_equal(S("FooBar"), a)
  868. if @aref_slicebang_silent
  869. assert_nil(a.slice!(-7,10))
  870. else
  871. assert_raise(IndexError) {a.slice!(-7,10)}
  872. end
  873. assert_equal(S("FooBar"), a)
  874. a=S("FooBar")
  875. assert_equal(S("Foo"), a.slice!(0..2))
  876. assert_equal(S("Bar"), a)
  877. a=S("FooBar")
  878. assert_equal(S("Bar"), a.slice!(-3..-1))
  879. assert_equal(S("Foo"), a)
  880. a=S("FooBar")
  881. if @aref_slicebang_silent
  882. assert_equal(S(""), a.slice!(6..2))
  883. else
  884. assert_raise(RangeError) {a.slice!(6..2)}
  885. end
  886. assert_equal(S("FooBar"), a)
  887. if @aref_slicebang_silent
  888. assert_nil(a.slice!(-10..-7))
  889. else
  890. assert_raise(RangeError) {a.slice!(-10..-7)}
  891. end
  892. assert_equal(S("FooBar"), a)
  893. a=S("FooBar")
  894. assert_equal(S("Foo"), a.slice!(/^F../))
  895. assert_equal(S("Bar"), a)
  896. a=S("FooBar")
  897. assert_equal(S("Bar"), a.slice!(/..r$/))
  898. assert_equal(S("Foo"), a)
  899. a=S("FooBar")
  900. if @aref_slicebang_silent
  901. assert_nil(a.slice!(/xyzzy/))
  902. else
  903. assert_raise(IndexError) {a.slice!(/xyzzy/)}
  904. end
  905. assert_equal(S("FooBar"), a)
  906. if @aref_slicebang_silent
  907. assert_nil(a.slice!(/plugh/))
  908. else
  909. assert_raise(IndexError) {a.slice!(/plugh/)}
  910. end
  911. assert_equal(S("FooBar"), a)
  912. a=S("FooBar")
  913. assert_equal(S("Foo"), a.slice!(S("Foo")))
  914. assert_equal(S("Bar"), a)
  915. a=S("FooBar")
  916. assert_equal(S("Bar"), a.slice!(S("Bar")))
  917. assert_equal(S("Foo"), a)
  918. pre_1_7_1 do
  919. a=S("FooBar")
  920. assert_nil(a.slice!(S("xyzzy")))
  921. assert_equal(S("FooBar"), a)
  922. assert_nil(a.slice!(S("plugh")))
  923. assert_equal(S("FooBar"), a)
  924. end
  925. assert_raise(ArgumentError) { "foo".slice! }
  926. end
  927. def test_split
  928. assert_nil($;)
  929. assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split)
  930. assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" ")))
  931. assert_equal([S(" a "), S(" b "), S(" c ")], S(" a | b | c ").split(S("|")))
  932. assert_equal([S("a"), S("b"), S("c")], S("aXXbXXcXX").split(/X./))
  933. assert_equal([S("a"), S("b"), S("c")], S("abc").split(//))
  934. assert_equal([S("a|b|c")], S("a|b|c").split(S('|'), 1))
  935. assert_equal([S("a"), S("b|c")], S("a|b|c").split(S('|'), 2))
  936. assert_equal([S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3))
  937. assert_equal([S("a"), S("b"), S("c"), S("")], S("a|b|c|").split(S('|'), -1))
  938. assert_equal([S("a"), S("b"), S("c"), S(""), S("")], S("a|b|c||").split(S('|'), -1))
  939. assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|')))
  940. assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1))
  941. assert_equal([], "".split(//, 1))
  942. assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug")
  943. bug6206 = '[ruby-dev:45441]'
  944. Encoding.list.each do |enc|
  945. next unless enc.ascii_compatible?
  946. s = S("a:".force_encoding(enc))
  947. assert_equal([enc]*2, s.split(":", 2).map(&:encoding), bug6206)
  948. end
  949. end
  950. def test_squeeze
  951. assert_equal(S("abc"), S("aaabbbbccc").squeeze)
  952. assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" ")))
  953. assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z")))
  954. end
  955. def test_squeeze!
  956. a = S("aaabbbbccc")
  957. b = a.dup
  958. assert_equal(S("abc"), a.squeeze!)
  959. assert_equal(S("abc"), a)
  960. assert_equal(S("aaabbbbccc"), b)
  961. a = S("aa bb cc")
  962. assert_equal(S("aa bb cc"), a.squeeze!(S(" ")))
  963. assert_equal(S("aa bb cc"), a)
  964. a = S("BxxxTyyyWzzzzz")
  965. assert_equal(S("BxTyWz"), a.squeeze!(S("a-z")))
  966. assert_equal(S("BxTyWz"), a)
  967. a=S("The quick brown fox")
  968. assert_nil(a.squeeze!)
  969. end
  970. def test_strip
  971. assert_equal(S("x"), S(" x ").strip)
  972. assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
  973. assert_equal("0b0 ".force_encoding("UTF-16BE"),
  974. "\x00 0b0 ".force_encoding("UTF-16BE").strip)
  975. assert_equal("0\x000b0 ".force_encoding("UTF-16BE"),
  976. "0\x000b0 ".force_encoding("UTF-16BE").strip)
  977. end
  978. def test_strip!
  979. a = S(" x ")
  980. b = a.dup
  981. assert_equal(S("x") ,a.strip!)
  982. assert_equal(S("x") ,a)
  983. assert_equal(S(" x "), b)
  984. a = S(" \n\r\t x \t\r\n\n ")
  985. assert_equal(S("x"), a.strip!)
  986. assert_equal(S("x"), a)
  987. a = S("x")
  988. assert_nil(a.strip!)
  989. assert_equal(S("x") ,a)
  990. end
  991. def test_sub
  992. assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*')))
  993. assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>')))
  994. assert_equal(S("h ello"), S("hello").sub(/./) {
  995. |s| s[0].to_s + S(' ')})
  996. assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) {
  997. |s| $1.upcase + S('-') + $2
  998. })
  999. assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\'))
  1000. assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\'))
  1001. assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1'))
  1002. assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1'))
  1003. assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1'))
  1004. assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1'))
  1005. assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1'))
  1006. assert_equal(S("a--ababababababababab"),
  1007. S("abababababababababab").sub(/(b)/, '-\9-'))
  1008. assert_equal(S("1-b-0"),
  1009. S("1b2b3b4b5b6b7b8b9b0").
  1010. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-'))
  1011. assert_equal(S("1-b-0"),
  1012. S("1b2b3b4b5b6b7b8b9b0").
  1013. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-'))
  1014. assert_equal(S("1-\\9-0"),
  1015. S("1b2b3b4b5b6b7b8b9b0").
  1016. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-'))
  1017. assert_equal(S("k"),
  1018. S("1a2b3c4d5e6f7g8h9iAjBk").
  1019. sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+'))
  1020. assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\'))
  1021. assert_equal(S("ababa"), S("ababa").sub(/b/, '\&'))
  1022. assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&'))
  1023. assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&'))
  1024. assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&'))
  1025. assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&'))
  1026. a = S("hello")
  1027. a.taint
  1028. a.untrust
  1029. x = a.sub(/./, S('X'))
  1030. assert(x.tainted?)
  1031. assert(x.untrusted?)
  1032. o = Object.new
  1033. def o.to_str; "bar"; end
  1034. assert_equal("fooBARbaz", "foobarbaz".sub(o, "BAR"))
  1035. assert_raise(TypeError) { "foo".sub(Object.new, "") }
  1036. assert_raise(ArgumentError) { "foo".sub }
  1037. assert_raise(IndexError) { "foo"[/(?:(o$)|(x))/, 2] = 'bar' }
  1038. o = Object.new
  1039. def o.to_s; self; end
  1040. assert_match(/^foo#<Object:0x.*>baz$/, "foobarbaz".sub("bar") { o })
  1041. end
  1042. def test_sub!
  1043. a = S("hello")
  1044. b = a.dup
  1045. a.sub!(/[aeiou]/, S('*'))
  1046. assert_equal(S("h*llo"), a)
  1047. assert_equal(S("hello"), b)
  1048. a = S("hello")
  1049. a.sub!(/([aeiou])/, S('<\1>'))
  1050. assert_equal(S("h<e>llo"), a)
  1051. a = S("hello")
  1052. a.sub!(/./) { |s| s[0].to_s + S(' ')}
  1053. assert_equal(S("h ello"), a)
  1054. a = S("hello")
  1055. a.sub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
  1056. assert_equal(S("HELL-o"), a)
  1057. a=S("hello")
  1058. assert_nil(a.sub!(/X/, S('Y')))
  1059. r = S('X')
  1060. r.taint
  1061. r.untrust
  1062. a.sub!(/./, r)
  1063. assert(a.tainted?)
  1064. assert(a.untrusted?)
  1065. end
  1066. def test_succ
  1067. assert_equal(S("abd"), S("abc").succ)
  1068. assert_equal(S("z"), S("y").succ)
  1069. assert_equal(S("aaa"), S("zz").succ)
  1070. assert_equal(S("124"), S("123").succ)
  1071. assert_equal(S("1000"), S("999").succ)
  1072. assert_equal(S("2.000"), S("1.999").succ)
  1073. assert_equal(S("No.10"), S("No.9").succ)
  1074. assert_equal(S("2000aaa"), S("1999zzz").succ)
  1075. assert_equal(S("AAAAA000"), S("ZZZZ999").succ)
  1076. assert_equal(S("*+"), S("**").succ)
  1077. assert_equal("abce", "abcd".succ)
  1078. assert_equal("THX1139", "THX1138".succ)
  1079. assert_equal("<<koalb>>", "<<koala>>".succ)
  1080. assert_equal("2000aaa", "1999zzz".succ)
  1081. assert_equal("AAAA0000", "ZZZ9999".succ)
  1082. assert_equal("**+", "***".succ)
  1083. end
  1084. def test_succ!
  1085. a = S("abc")
  1086. b = a.dup
  1087. assert_equal(S("abd"), a.succ!)
  1088. assert_equal(S("abd"), a)
  1089. assert_equal(S("abc"), b)
  1090. a = S("y")
  1091. assert_equal(S("z"), a.succ!)
  1092. assert_equal(S("z"), a)
  1093. a = S("zz")
  1094. assert_equal(S("aaa"), a.succ!)
  1095. assert_equal(S("aaa"), a)
  1096. a = S("123")
  1097. assert_equal(S("124"), a.succ!)
  1098. assert_equal(S("124"), a)
  1099. a = S("999")
  1100. assert_equal(S("1000"), a.succ!)
  1101. assert_equal(S("1000"), a)
  1102. a = S("1999zzz")
  1103. assert_equal(S("2000aaa"), a.succ!)
  1104. assert_equal(S("2000aaa"), a)
  1105. a = S("ZZZZ999")
  1106. assert_equal(S("AAAAA000"), a.succ!)
  1107. assert_equal(S("AAAAA000"), a)
  1108. a = S("**")
  1109. assert_equal(S("*+"), a.succ!)
  1110. assert_equal(S("*+"), a)
  1111. a = S("No.9")
  1112. assert_equal(S("No.10"), a.succ!)
  1113. assert_equal(S("No.10"), a)
  1114. assert_equal("aaaaaaaaaaaa", "zzzzzzzzzzz".succ!)
  1115. assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzz".succ!)
  1116. end
  1117. def test_sum
  1118. n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001")
  1119. assert_equal(15, n.sum)
  1120. n += S("\001")
  1121. assert_equal(16, n.sum(17))
  1122. n[0] = 2.chr
  1123. assert(15 != n.sum)
  1124. end
  1125. def check_sum(str, bits=16)
  1126. sum = 0
  1127. str.each_byte {|c| sum += c}
  1128. sum = sum & ((1 << bits) - 1) if bits != 0
  1129. assert_equal(sum, str.sum(bits))
  1130. end
  1131. def test_sum_2
  1132. assert_equal(0, "".sum)
  1133. assert_equal(294, "abc".sum)
  1134. check_sum("abc")
  1135. check_sum("\x80")
  1136. 0.upto(70) {|bits|
  1137. check_sum("xyz", bits)
  1138. }
  1139. end
  1140. def test_sum_long
  1141. s8421505 = "\xff" * 8421505
  1142. assert_equal(127, s8421505.sum(31))
  1143. assert_equal(2147483775, s8421505.sum(0))
  1144. s16843010 = ("\xff" * 16843010)
  1145. assert_equal(254, s16843010.sum(32))
  1146. assert_equal(4294967550, s16843010.sum(0))
  1147. end
  1148. def test_swapcase
  1149. assert_equal(S("hi&LOW"), S("HI&low").swapcase)
  1150. end
  1151. def test_swapcase!
  1152. a = S("hi&LOW")
  1153. b = a.dup
  1154. assert_equal(S("HI&low"), a.swapcase!)
  1155. assert_equal(S("HI&low"), a)
  1156. assert_equal(S("hi&LOW"), b)
  1157. a = S("$^#^%$#!!")
  1158. assert_nil(a.swapcase!)
  1159. assert_equal(S("$^#^%$#!!"), a)
  1160. end
  1161. def test_to_f
  1162. assert_equal(344.3, S("344.3").to_f)
  1163. assert_equal(5.9742e24, S("5.9742e24").to_f)
  1164. assert_equal(98.6, S("98.6 degrees").to_f)
  1165. assert_equal(0.0, S("degrees 100.0").to_f)
  1166. assert_equal([ 0.0].pack('G'), [S(" 0.0").to_f].pack('G'))
  1167. assert_equal([-0.0].pack('G'), [S("-0.0").to_f].pack('G'))
  1168. end
  1169. def test_to_i
  1170. assert_equal(1480, S("1480ft/sec").to_i)
  1171. assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i)
  1172. assert_equal(0, " 0".to_i)
  1173. assert_equal(0, "+0".to_i)
  1174. assert_equal(0, "-0".to_i)
  1175. assert_equal(0, "--0".to_i)
  1176. assert_equal(16, "0x10".to_i(0))
  1177. assert_equal(16, "0X10".to_i(0))
  1178. assert_equal(2, "0b10".to_i(0))
  1179. assert_equal(2, "0B10".to_i(0))
  1180. assert_equal(8, "0o10".to_i(0))
  1181. assert_equal(8, "0O10".to_i(0))
  1182. assert_equal(10, "0d10".to_i(0))
  1183. assert_equal(10, "0D10".to_i(0))
  1184. assert_equal(8, "010".to_i(0))
  1185. assert_raise(ArgumentError) { "010".to_i(-10) }
  1186. 2.upto(36) {|radix|
  1187. assert_equal(radix, "10".to_i(radix))
  1188. assert_equal(radix**2, "100".to_i(radix))
  1189. }
  1190. assert_raise(ArgumentError) { "0".to_i(1) }
  1191. assert_raise(ArgumentError) { "0".to_i(37) }
  1192. assert_equal(0, "z".to_i(10))
  1193. assert_equal(12, "1_2".to_i(10))
  1194. assert_equal(0x40000000, "1073741824".to_i(10))
  1195. assert_equal(0x4000000000000000, "4611686018427387904".to_i(10))
  1196. assert_equal(1, "1__2".to_i(10))
  1197. assert_equal(1, "1_z".to_i(10))
  1198. end
  1199. def test_to_s
  1200. a = S("me")
  1201. assert_equal("me", a.to_s)
  1202. assert_equal(a.__id__, a.to_s.__id__) if @cls == String
  1203. end
  1204. def test_to_str
  1205. a = S("me")
  1206. assert_equal("me", a.to_s)
  1207. assert_equal(a.__id__, a.to_s.__id__) if @cls == String
  1208. o = Object.new
  1209. def o.to_str
  1210. "at"
  1211. end
  1212. assert_equal("meat", a.concat(o))
  1213. o = Object.new
  1214. def o.to_str
  1215. foo_bar()
  1216. end
  1217. assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message)
  1218. end
  1219. def test_tr
  1220. assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip")))
  1221. assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*")))
  1222. assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z")))
  1223. a = "abc".force_encoding(Encoding::US_ASCII)
  1224. assert_equal(Encoding::US_ASCII, a.tr(S("z"), S("\u0101")).encoding, '[ruby-core:22326]')
  1225. assert_equal("a".hash, "a".tr("a", "\u0101").tr("\u0101", "a").hash, '[ruby-core:22328]')
  1226. assert_equal(true, "\u0101".tr("\u0101", "a").ascii_only?)
  1227. assert_equal(true, "\u3041".tr("\u3041", "a").ascii_only?)
  1228. assert_equal(false, "\u3041\u3042".tr("\u3041", "a").ascii_only?)
  1229. end
  1230. def test_tr!
  1231. a = S("hello")
  1232. b = a.dup
  1233. assert_equal(S("hippo"), a.tr!(S("el"), S("ip")))
  1234. assert_equal(S("hippo"), a)
  1235. assert_equal(S("hello"),b)
  1236. a = S("hello")
  1237. assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*")))
  1238. assert_equal(S("*e**o"), a)
  1239. a = S("IBM")
  1240. assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z")))
  1241. assert_equal(S("HAL"), a)
  1242. a = S("ibm")
  1243. assert_nil(a.tr!(S("B-Z"), S("A-Z")))
  1244. assert_equal(S("ibm"), a)
  1245. a = "abc".force_encoding(Encoding::US_ASCII)
  1246. assert_nil(a.tr!(S("z"), S("\u0101")), '[ruby-core:22326]')
  1247. assert_equal(Encoding::US_ASCII, a.encoding, '[ruby-core:22326]')
  1248. end
  1249. def test_tr_s
  1250. assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
  1251. assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
  1252. assert_equal("a".hash, "\u0101\u0101".tr_s("\u0101", "a").hash)
  1253. assert_equal(true, "\u3041\u3041".tr("\u3041", "a").ascii_only?)
  1254. end
  1255. def test_tr_s!
  1256. a = S("hello")
  1257. b = a.dup
  1258. assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp")))
  1259. assert_equal(S("hypo"), a)
  1260. assert_equal(S("hello"), b)
  1261. a = S("hello")
  1262. assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
  1263. assert_equal(S("h*o"), a)
  1264. end
  1265. def test_unpack
  1266. a = [S("cat"), S("wom"), S("x"), S("yy")]
  1267. assert_equal(a, S("catwomx yy ").unpack(S("A3A3A3A3")))
  1268. assert_equal([S("cat")], S("cat \000\000").unpack(S("A*")))
  1269. assert_equal([S("cwx"), S("wx"), S("x"), S("yy")],
  1270. S("cwx yy ").unpack(S("A3@1A3@2A3A3")))
  1271. assert_equal([S("cat"), S("wom"), S("x\000\000"), S("yy\000")],
  1272. S("catwomx\000\000yy\000").unpack(S("a3a3a3a3")))
  1273. assert_equal([S("cat \000\000")], S("cat \000\000").unpack(S("a*")))
  1274. assert_equal([S("ca")], S("catdog").unpack(S("a2")))
  1275. assert_equal([S("cat\000\000")],
  1276. S("cat\000\000\000\000\000dog").unpack(S("a5")))
  1277. assert_equal([S("01100001")], S("\x61").unpack(S("B8")))
  1278. assert_equal([S("01100001")], S("\x61").unpack(S("B*")))
  1279. assert_equal([S("0110000100110111")], S("\x61\x37").unpack(S("B16")))
  1280. assert_equal([S("01100001"), S("00110111")], S("\x61\x37").unpack(S("B8B8")))
  1281. assert_equal([S("0110")], S("\x60").unpack(S("B4")))
  1282. assert_equal([S("01")], S("\x40").unpack(S("B2")))
  1283. assert_equal([S("01100001")], S("\x86").unpack(S("b8")))
  1284. assert_equal([S("01100001")], S("\x86").unpack(S("b*")))
  1285. assert_equal([S("0110000100110111")], S("\x86\xec").unpack(S("b16")))
  1286. assert_equal([S("01100001"), S("00110111")], S("\x86\xec").unpack(S("b8b8")))
  1287. assert_equal([S("0110")], S("\x06").unpack(S("b4")))
  1288. assert_equal([S("01")], S("\x02").unpack(S("b2")))
  1289. assert_equal([ 65, 66, 67 ], S("ABC").unpack(S("C3")))
  1290. assert_equal([ 255, 66, 67 ], S("\377BC").unpack("C*"))
  1291. assert_equal([ 65, 66, 67 ], S("ABC").unpack("c3"))
  1292. assert_equal([ -1, 66, 67 ], S("\377BC").unpack("c*"))
  1293. assert_equal([S("4142"), S("0a"), S("1")], S("AB\n\x10").unpack(S("H4H2H1")))
  1294. assert_equal([S("1424"), S("a0"), S("2")], S("AB\n\x02").unpack(S("h4h2h1")))
  1295. assert_equal([S("abc\002defcat\001"), S(""), S("")],
  1296. S("abc=02def=\ncat=\n=01=\n").unpack(S("M9M3M4")))
  1297. assert_equal([S("hello\n")], S("aGVsbG8K\n").unpack(S("m")))
  1298. assert_equal([S("hello\nhello\n")], S(",:&5L;&\\*:&5L;&\\*\n").unpack(S("u")))
  1299. assert_equal([0xa9, 0x42, 0x2260], S("\xc2\xa9B\xe2\x89\xa0").unpack(S("U*")))
  1300. =begin
  1301. skipping "Not tested:
  1302. D,d & double-precision float, native format\\
  1303. E & double-precision float, little-endian byte order\\
  1304. e & single-precision float, little-endian byte order\\
  1305. F,f & single-precision float, native format\\
  1306. G & double-precision float, network (big-endian) byte order\\
  1307. g & single-precision float, network (big-endian) byte order\\
  1308. I & unsigned integer\\
  1309. i & integer\\
  1310. L & unsigned long\\
  1311. l & long\\
  1312. m & string encoded in base64 (uuencoded)\\
  1313. N & long, network (big-endian) byte order\\
  1314. n & short, network (big-endian) byte-order\\
  1315. P & pointer to a structure (fixed-length string)\\
  1316. p & pointer to a null-terminated string\\
  1317. S & unsigned short\\
  1318. s & short\\
  1319. V & long, little-endian byte order\\
  1320. v & short, little-endian byte order\\
  1321. X & back up a byte\\
  1322. x & null byte\\
  1323. Z & ASCII string (null padded, count is width)\\
  1324. "
  1325. =end
  1326. end
  1327. def test_upcase
  1328. assert_equal(S("HELLO"), S("hello").upcase)
  1329. assert_equal(S("HELLO"), S("hello").upcase)
  1330. assert_equal(S("HELLO"), S("HELLO").upcase)
  1331. assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase)
  1332. end
  1333. def test_upcase!
  1334. a = S("hello")
  1335. b = a.dup
  1336. assert_equal(S("HELLO"), a.upcase!)
  1337. assert_equal(S("HELLO"), a)
  1338. assert_equal(S("hello"), b)
  1339. a = S("HELLO")
  1340. assert_nil(a.upcase!)
  1341. assert_equal(S("HELLO"), a)
  1342. end
  1343. def test_upto
  1344. a = S("aa")
  1345. start = S("aa")
  1346. count = 0
  1347. assert_equal(S("aa"), a.upto(S("zz")) {|s|
  1348. assert_equal(start, s)
  1349. start.succ!
  1350. count += 1
  1351. })
  1352. assert_equal(676, count)
  1353. end
  1354. def test_upto_numeric
  1355. a = S("00")
  1356. start = S("00")
  1357. count = 0
  1358. assert_equal(S("00"), a.upto(S("23")) {|s|
  1359. assert_equal(start, s, "[ruby-dev:39361]")
  1360. assert_equal(Encoding::US_ASCII, s.encoding)
  1361. start.succ!
  1362. count += 1
  1363. })
  1364. assert_equal(24, count, "[ruby-dev:39361]")
  1365. end
  1366. def test_upto_nonalnum
  1367. first = S("\u3041")
  1368. last = S("\u3093")
  1369. count = 0
  1370. assert_equal(first, first.upto(last) {|s|
  1371. count += 1
  1372. s.replace(last)
  1373. })
  1374. assert_equal(83, count, "[ruby-dev:39626]")
  1375. end
  1376. def test_mod_check
  1377. assert_raise(RuntimeError) {
  1378. s = ""
  1379. s.sub!(/\A/) { s.replace "z" * 2000; "zzz" }
  1380. }
  1381. end
  1382. def test_frozen_check
  1383. assert_raise(RuntimeError) {
  1384. s = ""
  1385. s.sub!(/\A/) { s.freeze; "zzz" }
  1386. }
  1387. end
  1388. class S2 < String
  1389. end
  1390. def test_str_new4
  1391. s = (0..54).to_a.join # length = 100
  1392. s2 = S2.new(s[10,90])
  1393. s3 = s2[10,80]
  1394. assert_equal((10..54).to_a.to_a.join, s2)
  1395. assert_equal((15..54).to_a.to_a.join, s3)
  1396. end
  1397. def test_rb_str_new4
  1398. s = "a" * 100
  1399. s2 = s[10,90]
  1400. assert_equal("a" * 90, s2)
  1401. s3 = s2[10,80]
  1402. assert_equal("a" * 80, s3)
  1403. end
  1404. class StringLike
  1405. def initialize(str)
  1406. @str = str
  1407. end
  1408. def to_str
  1409. @str
  1410. end
  1411. end
  1412. def test_rb_str_to_str
  1413. assert_equal("ab", "a" + StringLike.new("b"))
  1414. end
  1415. def test_rb_str_shared_replace
  1416. s = "a" * 100
  1417. s.succ!
  1418. assert_equal("a" * 99 + "b", s)
  1419. s = ""
  1420. s.succ!
  1421. assert_equal("", s)
  1422. end
  1423. def test_times
  1424. assert_raise(ArgumentError) { "a" * (-1) }
  1425. end
  1426. def test_splice!
  1427. l = S("1234\n234\n34\n4\n")
  1428. assert_equal(S("1234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
  1429. assert_equal(S("234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
  1430. assert_equal(S("34\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
  1431. assert_equal(S("4\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
  1432. assert_nil(l.slice!(/\A.*\n/), "[ruby-dev:31665]")
  1433. end
  1434. def test_end_with?
  1435. assert("abc".end_with?("c"))
  1436. end
  1437. def test_times2
  1438. s1 = ''
  1439. 100.times {|n|
  1440. s2 = "a" * n
  1441. assert_equal(s1, s2)
  1442. s1 << 'a'
  1443. }
  1444. assert_raise(ArgumentError) { "foo" * (-1) }
  1445. end
  1446. def test_respond_to
  1447. o = Object.new
  1448. def o.respond_to?(arg) [:to_str].include?(arg) ? nil : super end
  1449. def o.to_str() "" end
  1450. def o.==(other) "" == other end
  1451. assert_equal(false, "" == o)
  1452. end
  1453. def test_match_method
  1454. assert_equal("bar", "foobarbaz".match(/bar/).to_s)
  1455. o = /foo/
  1456. def o.match(x, y, z); x + y + z; end
  1457. assert_equal("foobarbaz", "foo".match(o, "bar", "baz"))
  1458. x = nil
  1459. "foo".match(o, "bar", "baz") {|y| x = y }
  1460. assert_equal("foobarbaz", x)
  1461. assert_raise(ArgumentError) { "foo".match }
  1462. end
  1463. def test_clear
  1464. s = "foo" * 100
  1465. s.clear
  1466. assert_equal("", s)
  1467. end
  1468. def test_to_s_2
  1469. c = Class.new(String)
  1470. s = c.new
  1471. s.replace("foo")
  1472. assert_equal("foo", s.to_s)
  1473. assert_instance_of(String, s.to_s)
  1474. end
  1475. def test_partition
  1476. assert_equal(%w(he l lo), "hello".partition(/l/))
  1477. assert_equal(%w(he l lo), "hello".partition("l"))
  1478. assert_raise(TypeError) { "hello".partition(1) }
  1479. def (hyphen = Object.new).to_str; "-"; end
  1480. assert_equal(%w(foo - bar), "foo-bar".partition(hyphen), '[ruby-core:23540]')
  1481. bug6206 = '[ruby-dev:45441]'
  1482. Encoding.list.each do |enc|
  1483. next unless enc.ascii_compatible?
  1484. s = S("a:".force_encoding(enc))
  1485. assert_equal([enc]*3, s.partition("|").map(&:encoding), bug6206)
  1486. end
  1487. end
  1488. def test_rpartition
  1489. assert_equal(%w(hel l o), "hello".rpartition(/l/))
  1490. assert_equal(%w(hel l o), "hello".rpartition("l"))
  1491. assert_raise(TypeError) { "hello".rpartition(1) }
  1492. def (hyphen = Object.new).to_str; "-"; end
  1493. assert_equal(%w(foo - bar), "foo-bar".rpartition(hyphen), '[ruby-core:23540]')
  1494. bug6206 = '[ruby-dev:45441]'
  1495. Encoding.list.each do |enc|
  1496. next unless e