PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/ruby/test_string.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 1885 lines | 1652 code | 231 blank | 2 comment | 36 complexity | 7ca978ee6cf242cc938f8aae8a9c437c MD5 | raw file
Possible License(s): GPL-3.0, JSON

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

Large files files are truncated, but you can click here to view the full file