PageRenderTime 207ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/rubicon/test_string.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1393 lines | 1147 code | 203 blank | 43 comment | 32 complexity | 2a9c071be621050500a408f525ea3200 MD5 | raw file
  1. require 'test/unit'
  2. #
  3. # The tests in this file are written in terms of "stringlike" objects
  4. # instead of using the class "String" directly. The purpose of this is
  5. # to make it possible to test other stringlike classes too (a simple
  6. # example would be to test a subclass of "String", i.e. how the
  7. # String-class behaves when sub-classed).
  8. #
  9. # This flexibility has not been used yet, but will soon ...
  10. #
  11. # It is implemented by having the member variable @cls initialized to
  12. # the stringlike class we want to test. The S() utility method (used
  13. # "everywhere" in this file) converts String objects to stringlike
  14. # objects of the class @cls.
  15. #
  16. # This file will probably need to splitted again when another class
  17. # is tested too. Something like "TestString.rb", "TestStringSubclass.rb"
  18. # and "StringBase.rb".
  19. #
  20. class TestString < Test::Unit::TestCase
  21. IS19 = RUBY_VERSION =~ /1\.9/
  22. def initialize(*args)
  23. @cls = String
  24. begin
  25. S("Foo")[/./, 1]
  26. @aref_re_nth = true
  27. rescue
  28. @aref_re_nth = false
  29. end
  30. begin
  31. S("Foo")[/Bar/] = S("")
  32. @aref_re_silent = true
  33. rescue
  34. @aref_re_silent = false
  35. end
  36. begin
  37. S("Foo").slice!(4)
  38. @aref_slicebang_silent = true
  39. rescue
  40. @aref_slicebang_silent = false
  41. end
  42. super
  43. end
  44. # The "S" method converts String objects into objects of the
  45. # stringlike class given in the variable @cls. The parameter to
  46. # "S" may be either a single String, or an array of Strings.
  47. #
  48. def S(str_or_array)
  49. if str_or_array.instance_of?(Array)
  50. str_or_array.map {|str| @cls.new(str) }
  51. else
  52. @cls.new(str_or_array)
  53. end
  54. end
  55. def test_AREF # '[]'
  56. assert_equal(IS19 ? "A" : 65, S("AooBar")[0])
  57. assert_equal(IS19 ? "B" : 66, S("FooBaB")[-1])
  58. assert_equal(nil, S("FooBar")[6])
  59. assert_equal(nil, S("FooBar")[-7])
  60. assert_equal(S("Foo"), S("FooBar")[0,3])
  61. assert_equal(S("Bar"), S("FooBar")[-3,3])
  62. assert_equal(S(""), S("FooBar")[6,2])
  63. assert_equal(nil, S("FooBar")[-7,10])
  64. assert_equal(S("Foo"), S("FooBar")[0..2])
  65. assert_equal(S("Foo"), S("FooBar")[0...3])
  66. assert_equal(S("Bar"), S("FooBar")[-3..-1])
  67. assert_equal("", S("FooBar")[6..2])
  68. assert_equal(nil, S("FooBar")[-10..-7])
  69. assert_equal(S("Foo"), S("FooBar")[/^F../])
  70. assert_equal(S("Bar"), S("FooBar")[/..r$/])
  71. assert_equal(nil, S("FooBar")[/xyzzy/])
  72. assert_equal(nil, S("FooBar")[/plugh/])
  73. assert_equal(S("Foo"), S("FooBar")[S("Foo")])
  74. assert_equal(S("Bar"), S("FooBar")[S("Bar")])
  75. assert_equal(nil, S("FooBar")[S("xyzzy")])
  76. assert_equal(nil, S("FooBar")[S("plugh")])
  77. if @aref_re_nth
  78. assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1])
  79. assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2])
  80. assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3])
  81. assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
  82. assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
  83. assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
  84. end
  85. end
  86. def test_ASET # '[]='
  87. s = S("FooBar")
  88. s[0] = S('A')
  89. assert_equal(S("AooBar"), s)
  90. s[-1]= S('B')
  91. assert_equal(S("AooBaB"), s)
  92. assert_raise(IndexError) { s[-7] = S("xyz") }
  93. assert_equal(S("AooBaB"), s)
  94. s[0] = S("ABC")
  95. assert_equal(S("ABCooBaB"), s)
  96. s = S("FooBar")
  97. s[0,3] = S("A")
  98. assert_equal(S("ABar"),s)
  99. s[0] = S("Foo")
  100. assert_equal(S("FooBar"), s)
  101. s[-3,3] = S("Foo")
  102. assert_equal(S("FooFoo"), s)
  103. assert_raise(IndexError) { s[7,3] = S("Bar") }
  104. assert_raise(IndexError) { s[-7,3] = S("Bar") }
  105. s = S("FooBar")
  106. s[0..2] = S("A")
  107. assert_equal(S("ABar"), s)
  108. s[1..3] = S("Foo")
  109. assert_equal(S("AFoo"), s)
  110. s[-4..-4] = S("Foo")
  111. assert_equal(S("FooFoo"), s)
  112. assert_raise(RangeError) { s[7..10] = S("Bar") }
  113. assert_raise(RangeError) { s[-7..-10] = S("Bar") }
  114. s = S("FooBar")
  115. s[/^F../]= S("Bar")
  116. assert_equal(S("BarBar"), s)
  117. s[/..r$/] = S("Foo")
  118. assert_equal(S("BarFoo"), s)
  119. if @aref_re_silent
  120. s[/xyzzy/] = S("None")
  121. assert_equal(S("BarFoo"), s)
  122. else
  123. assert_raise(IndexError) { s[/xyzzy/] = S("None") }
  124. end
  125. if @aref_re_nth
  126. s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo")
  127. assert_equal(S("FooFoo"), s)
  128. s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar")
  129. assert_equal(S("FooBar"), s)
  130. assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" }
  131. s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo")
  132. assert_equal(S("FooFoo"), s)
  133. s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar")
  134. assert_equal(S("BarFoo"), s)
  135. assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" }
  136. end
  137. s = S("FooBar")
  138. s[S("Foo")] = S("Bar")
  139. assert_equal(S("BarBar"), s)
  140. s = S("a string")
  141. s[0..s.size] = S("another string")
  142. assert_equal(S("another string"), s)
  143. end
  144. def test_CMP # '<=>'
  145. assert_equal(1, S("abcdef") <=> S("abcde"))
  146. assert_equal(0, S("abcdef") <=> S("abcdef"))
  147. assert_equal(-1, S("abcde") <=> S("abcdef"))
  148. assert_equal(-1, S("ABCDEF") <=> S("abcdef"))
  149. end
  150. def test_EQUAL # '=='
  151. assert_equal(false, S("foo") == :foo)
  152. assert(S("abcdef") == S("abcdef"))
  153. assert(S("CAT") != S('cat'))
  154. assert(S("CaT") != S('cAt'))
  155. end
  156. def test_LSHIFT # '<<'
  157. assert_equal(S("world!"), S("world") << 33)
  158. assert_equal(S("world!"), S("world") << S('!'))
  159. end
  160. # Helper class to test String#=~.
  161. #
  162. class MatchDefiner
  163. def initialize(result)
  164. @result = result
  165. end
  166. def =~(other)
  167. [other, @result]
  168. end
  169. end
  170. def test_MATCH # '=~'
  171. # "str =~ regexp" same as "regexp =~ str"
  172. assert_equal(10, S("FeeFieFoo-Fum") =~ /Fum$/)
  173. assert_equal(nil, S("FeeFieFoo-Fum") =~ /FUM$/)
  174. # "str =~ obj" calls "obj =~ str"
  175. assert_equal(["aaa", 123], "aaa" =~ MatchDefiner.new(123))
  176. assert_equal(["bbb", :foo], "bbb" =~ MatchDefiner.new(:foo))
  177. assert_equal(["ccc", nil], "ccc" =~ MatchDefiner.new(nil))
  178. # default Object#=~ method.
  179. assert_equal(IS19 ? nil : false, "a string" =~ Object.new)
  180. end
  181. def test_MOD # '%'
  182. assert_equal(S("00123"), S("%05d") % 123)
  183. assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1])
  184. x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") %
  185. [S("hi"),
  186. 123,
  187. S("never seen"),
  188. 456,
  189. 0,
  190. ?A,
  191. 3.0999,
  192. 11,
  193. 171,
  194. 171,
  195. 11,
  196. 171,
  197. 171]
  198. if IS19
  199. assert_equal(S(' hi 123 %foo 456 0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
  200. else
  201. assert_equal(S(' hi 123 %foo 456 0x0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
  202. end
  203. end
  204. def test_MUL # '*'
  205. assert_equal(S("XXX"), S("X") * 3)
  206. assert_equal(S("HOHO"), S("HO") * 2)
  207. end
  208. def test_PLUS # '+'
  209. assert_equal(S("Yodel"), S("Yo") + S("del"))
  210. end
  211. def casetest(a, b, rev=false)
  212. case a
  213. when b
  214. assert(!rev)
  215. else
  216. assert(rev)
  217. end
  218. end
  219. def test_VERY_EQUAL # '==='
  220. assert_equal(false, S("foo") === :foo)
  221. casetest(S("abcdef"), S("abcdef"))
  222. casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to
  223. casetest(S("CaT"), S('cAt'), true) # find these in the case.
  224. end
  225. def test_capitalize
  226. assert_equal(S("Hello"), S("hello").capitalize)
  227. assert_equal(S("Hello"), S("hELLO").capitalize)
  228. assert_equal(S("123abc"), S("123ABC").capitalize)
  229. end
  230. def test_capitalize!
  231. a = S("hello"); a.capitalize!
  232. assert_equal(S("Hello"), a)
  233. a = S("hELLO"); a.capitalize!
  234. assert_equal(S("Hello"), a)
  235. a = S("123ABC"); a.capitalize!
  236. assert_equal(S("123abc"), a)
  237. assert_equal(nil, S("123abc").capitalize!)
  238. assert_equal(S("123abc"), S("123ABC").capitalize!)
  239. assert_equal(S("Abc"), S("ABC").capitalize!)
  240. assert_equal(S("Abc"), S("abc").capitalize!)
  241. assert_equal(nil, S("Abc").capitalize!)
  242. a = S("hello")
  243. b = a.dup
  244. assert_equal(S("Hello"), a.capitalize!)
  245. assert_equal(S("hello"), b)
  246. end
  247. def test_casecmp
  248. # 0
  249. assert_equal(0, S("123abc").casecmp(S("123ABC")))
  250. assert_equal(0, S("123AbC").casecmp(S("123aBc")))
  251. assert_equal(0, S("123ABC").casecmp(S("123ABC")))
  252. # 1
  253. assert_equal(1, S("1X3ABC").casecmp(S("123ABC")))
  254. assert_equal(1, S("123AXC").casecmp(S("123ABC")))
  255. assert_equal(1, S("123ABX").casecmp(S("123ABC")))
  256. assert_equal(1, S("123ABCX").casecmp(S("123ABC")))
  257. # -1
  258. assert_equal(-1, S("1#3ABC").casecmp(S("123ABC")))
  259. assert_equal(-1, S("123A#C").casecmp(S("123ABC")))
  260. assert_equal(-1, S("123AB#").casecmp(S("123ABC")))
  261. assert_equal(-1, S("123AB").casecmp(S("123ABC")))
  262. end
  263. def test_center
  264. assert_equal(S("hello"), S("hello").center(4))
  265. assert_equal(S(" hello "), S("hello").center(11))
  266. # with explicit padding
  267. assert_equal(S("1111hi11111"), S("hi").center(11, "1"))
  268. assert_equal(S("1212hi12121"), S("hi").center(11, "12"))
  269. assert_equal(S("1234hi12341"), S("hi").center(11, "1234"))
  270. # zero width padding
  271. assert_raise(ArgumentError) { S("hi").center(11, "") }
  272. end
  273. def test_chomp
  274. assert_equal(S("hello"), S("hello").chomp("\n"))
  275. assert_equal(S("hello"), S("hello\n").chomp("\n"))
  276. $/ = "\n"
  277. assert_equal(S("hello"), S("hello").chomp)
  278. assert_equal(S("hello"), S("hello\n").chomp)
  279. $/ = "!"
  280. assert_equal(S("hello"), S("hello").chomp)
  281. assert_equal(S("hello"), S("hello!").chomp)
  282. $/ = "\n"
  283. end
  284. def test_chomp!
  285. a = S("hello")
  286. a.chomp!(S("\n"))
  287. assert_equal(S("hello"), a)
  288. assert_equal(nil, a.chomp!(S("\n")))
  289. a = S("hello\n")
  290. a.chomp!(S("\n"))
  291. assert_equal(S("hello"), a)
  292. $/ = "\n"
  293. a = S("hello")
  294. a.chomp!
  295. assert_equal(S("hello"), a)
  296. a = S("hello\n")
  297. a.chomp!
  298. assert_equal(S("hello"), a)
  299. $/ = "!"
  300. a = S("hello")
  301. a.chomp!
  302. assert_equal(S("hello"), a)
  303. a="hello!"
  304. a.chomp!
  305. assert_equal(S("hello"), a)
  306. $/ = "\n"
  307. a = S("hello\n")
  308. b = a.dup
  309. assert_equal(S("hello"), a.chomp!)
  310. assert_equal(S("hello\n"), b)
  311. end
  312. def test_chop
  313. assert_equal(S("hell"), S("hello").chop)
  314. assert_equal(S("hello"), S("hello\r\n").chop)
  315. assert_equal(S("hello\n"), S("hello\n\r").chop)
  316. assert_equal(S(""), S("\r\n").chop)
  317. assert_equal(S(""), S("").chop)
  318. end
  319. def test_chop!
  320. a = S("hello").chop!
  321. assert_equal(S("hell"), a)
  322. a = S("hello\r\n").chop!
  323. assert_equal(S("hello"), a)
  324. a = S("hello\n\r").chop!
  325. assert_equal(S("hello\n"), a)
  326. a = S("\r\n").chop!
  327. assert_equal(S(""), a)
  328. a = S("").chop!
  329. assert_nil(a)
  330. a = S("hello\n")
  331. b = a.dup
  332. assert_equal(S("hello"), a.chop!)
  333. assert_equal(S("hello\n"), b)
  334. end
  335. def test_clone
  336. for taint in [ false, true ]
  337. for frozen in [ false, true ]
  338. a = S("Cool")
  339. a.taint if taint
  340. a.freeze if frozen
  341. b = a.clone
  342. assert_equal(a, b)
  343. assert(a.__id__ != b.__id__)
  344. assert_equal(a.frozen?, b.frozen?)
  345. assert_equal(a.tainted?, b.tainted?)
  346. end
  347. end
  348. end
  349. def test_concat
  350. assert_equal(S("world!"), S("world").concat(33))
  351. assert_equal(S("world!"), S("world").concat(S('!')))
  352. end
  353. def test_count
  354. a = S("hello world")
  355. assert_equal(5, a.count(S("lo")))
  356. assert_equal(2, a.count(S("lo"), S("o")))
  357. assert_equal(4, a.count(S("hello"), S("^l")))
  358. assert_equal(4, a.count(S("ej-m")))
  359. end
  360. def test_crypt
  361. assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa")))
  362. assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab")))
  363. # "salt" should be at least 2 characters
  364. assert_raise(ArgumentError) { S("mypassword").crypt("a")}
  365. end
  366. def test_delete
  367. assert_equal(S("heo"), S("hello").delete(S("l"), S("lo")))
  368. assert_equal(S("he"), S("hello").delete(S("lo")))
  369. assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e")))
  370. assert_equal(S("ho"), S("hello").delete(S("ej-m")))
  371. end
  372. def test_delete!
  373. a = S("hello")
  374. a.delete!(S("l"), S("lo"))
  375. assert_equal(S("heo"), a)
  376. a = S("hello")
  377. a.delete!(S("lo"))
  378. assert_equal(S("he"), a)
  379. a = S("hello")
  380. a.delete!(S("aeiou"), S("^e"))
  381. assert_equal(S("hell"), a)
  382. a = S("hello")
  383. a.delete!(S("ej-m"))
  384. assert_equal(S("ho"), a)
  385. a = S("hello")
  386. assert_nil(a.delete!(S("z")))
  387. a = S("hello")
  388. b = a.dup
  389. a.delete!(S("lo"))
  390. assert_equal(S("he"), a)
  391. assert_equal(S("hello"), b)
  392. end
  393. def test_downcase
  394. assert_equal(S("hello"), S("helLO").downcase)
  395. assert_equal(S("hello"), S("hello").downcase)
  396. assert_equal(S("hello"), S("HELLO").downcase)
  397. assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase)
  398. end
  399. def test_downcase!
  400. a = S("helLO")
  401. b = a.dup
  402. assert_equal(S("hello"), a.downcase!)
  403. assert_equal(S("hello"), a)
  404. assert_equal(S("helLO"), b)
  405. a=S("hello")
  406. assert_nil(a.downcase!)
  407. assert_equal(S("hello"), a)
  408. end
  409. def test_dump
  410. a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10
  411. assert_equal(S(IS19 ? '"Test\\x01\\x02\\x03\\t\\r\\n"' : '"Test\\001\\002\\003\\t\\r\\n"'), a.dump)
  412. end
  413. def test_dup
  414. for taint in [ false, true ]
  415. for frozen in [ false, true ]
  416. a = S("hello")
  417. a.taint if taint
  418. a.freeze if frozen
  419. b = a.dup
  420. assert_equal(a, b)
  421. assert(a.__id__ != b.__id__)
  422. assert(!b.frozen?)
  423. assert_equal(a.tainted?, b.tainted?)
  424. end
  425. end
  426. end
  427. unless IS19
  428. def test_each
  429. $/ = "\n"
  430. res=[]
  431. S("hello\nworld").each {|x| res << x}
  432. assert_equal(S("hello\n"), res[0])
  433. assert_equal(S("world"), res[1])
  434. res=[]
  435. S("hello\n\n\nworld").each(S('')) {|x| res << x}
  436. assert_equal(S("hello\n\n\n"), res[0])
  437. assert_equal(S("world"), res[1])
  438. $/ = "!"
  439. res=[]
  440. S("hello!world").each {|x| res << x}
  441. assert_equal(S("hello!"), res[0])
  442. assert_equal(S("world"), res[1])
  443. $/ = "\n"
  444. end
  445. end
  446. def test_each_byte
  447. res = []
  448. S("ABC").each_byte {|x| res << x }
  449. assert_equal(65, res[0])
  450. assert_equal(66, res[1])
  451. assert_equal(67, res[2])
  452. end
  453. def test_each_line
  454. $/ = "\n"
  455. res=[]
  456. S("hello\nworld").each_line {|x| res << x}
  457. assert_equal(S("hello\n"), res[0])
  458. assert_equal(S("world"), res[1])
  459. res=[]
  460. S("hello\n\n\nworld").each_line(S('')) {|x| res << x}
  461. assert_equal(S("hello\n\n\n"), res[0])
  462. assert_equal(S("world"), res[1])
  463. $/ = "!"
  464. res=[]
  465. S("hello!world").each_line {|x| res << x}
  466. assert_equal(S("hello!"), res[0])
  467. assert_equal(S("world"), res[1])
  468. $/ = "\n"
  469. end
  470. def test_empty?
  471. assert(S("").empty?)
  472. assert(!S("not").empty?)
  473. end
  474. def test_eql?
  475. a = S("hello")
  476. assert(a.eql?(S("hello")))
  477. assert(a.eql?(a))
  478. end
  479. def test_gsub
  480. assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*')))
  481. assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>')))
  482. assert_equal(S(IS19 ? "h e l l o " : "104 101 108 108 111 "),
  483. S("hello").gsub(/./) { |s| s[0].to_s + S(' ')})
  484. assert_equal(S("HELL-o"),
  485. S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 })
  486. a = S("hello")
  487. a.taint
  488. assert(a.gsub(/./, S('X')).tainted?)
  489. end
  490. def test_gsub!
  491. a = S("hello")
  492. b = a.dup
  493. a.gsub!(/[aeiou]/, S('*'))
  494. assert_equal(S("h*ll*"), a)
  495. assert_equal(S("hello"), b)
  496. a = S("hello")
  497. a.gsub!(/([aeiou])/, S('<\1>'))
  498. assert_equal(S("h<e>ll<o>"), a)
  499. a = S("hello")
  500. a.gsub!(/./) { |s| s[0].to_s + S(' ')}
  501. assert_equal(S(IS19 ? "h e l l o " : "104 101 108 108 111 "), a)
  502. a = S("hello")
  503. a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
  504. assert_equal(S("HELL-o"), a)
  505. r = S('X')
  506. r.taint
  507. a.gsub!(/./, r)
  508. assert(a.tainted?)
  509. a = S("hello")
  510. assert_nil(a.sub!(S('X'), S('Y')))
  511. end
  512. def test_hash
  513. assert_equal(S("hello").hash, S("hello").hash)
  514. assert(S("hello").hash != S("helLO").hash)
  515. end
  516. def test_hex
  517. assert_equal(255, S("0xff").hex)
  518. assert_equal(-255, S("-0xff").hex)
  519. assert_equal(255, S("ff").hex)
  520. assert_equal(-255, S("-ff").hex)
  521. assert_equal(0, S("-ralph").hex)
  522. assert_equal(-15, S("-fred").hex)
  523. assert_equal(15, S("fred").hex)
  524. end
  525. def test_include?
  526. assert( S("foobar").include?(?f))
  527. assert( S("foobar").include?(S("foo")))
  528. assert(!S("foobar").include?(S("baz")))
  529. assert(!S("foobar").include?(?z))
  530. end
  531. def test_index
  532. assert_equal(0, S("hello").index(?h))
  533. assert_equal(1, S("hello").index(S("ell")))
  534. assert_equal(2, S("hello").index(/ll./))
  535. assert_equal(3, S("hello").index(?l, 3))
  536. assert_equal(3, S("hello").index(S("l"), 3))
  537. assert_equal(3, S("hello").index(/l./, 3))
  538. assert_nil(S("hello").index(?z, 3))
  539. assert_nil(S("hello").index(S("z"), 3))
  540. assert_nil(S("hello").index(/z./, 3))
  541. assert_nil(S("hello").index(?z))
  542. assert_nil(S("hello").index(S("z")))
  543. assert_nil(S("hello").index(/z./))
  544. end
  545. def test_insert
  546. assert_equal(S("BCAD"), S("AD").insert(0, S("BC")))
  547. assert_equal(S("ABCD"), S("AD").insert(1, S("BC")))
  548. assert_equal(S("ADBC"), S("AD").insert(2, S("BC")))
  549. assert_raise(IndexError) { S("AD").insert(3, S("BC")) }
  550. assert_equal(S("ADBC"), S("AD").insert(-1, S("BC")))
  551. assert_equal(S("ABCD"), S("AD").insert(-2, S("BC")))
  552. assert_equal(S("BCAD"), S("AD").insert(-3, S("BC")))
  553. assert_raise(IndexError) { S("AD").insert(-4, S("BC")) }
  554. end
  555. def generic_test_intern(method)
  556. assert_equal(:koala, S("koala").send(method))
  557. assert(:koala != S("Koala").send(method))
  558. # Verify that different types of strings are allowed,
  559. # and do it without using the new literal syntax
  560. # for symbols (i.e. :"with spaces").
  561. #
  562. for str in ["identifier", "with spaces", "9with_digits", "9and spaces"]
  563. sym = S(str).send(method)
  564. assert_instance_of(Symbol, sym)
  565. assert_equal(str, sym.to_s)
  566. end
  567. # error cases
  568. unless IS19
  569. assert_raise(ArgumentError) { S("").send(method) }
  570. assert_raise(ArgumentError) { S("with\0null\0inside").send(method) }
  571. end
  572. end
  573. def test_intern
  574. generic_test_intern(:intern)
  575. end
  576. def test_length
  577. assert_equal(0, S("").length)
  578. assert_equal(4, S("1234").length)
  579. assert_equal(6, S("1234\r\n").length)
  580. assert_equal(7, S("\0011234\r\n").length)
  581. end
  582. def test_ljust
  583. assert_equal(S("hello"), S("hello").ljust(4))
  584. assert_equal(S("hello "), S("hello").ljust(11))
  585. # with explicit padding
  586. assert_equal(S("hi111111111"), S("hi").ljust(11, "1"))
  587. assert_equal(S("hi121212121"), S("hi").ljust(11, "12"))
  588. assert_equal(S("hi123412341"), S("hi").ljust(11, "1234"))
  589. # zero width padding
  590. assert_raise(ArgumentError) { S("hello").ljust(11, "") }
  591. end
  592. def test_lstrip
  593. a = S(" hello")
  594. assert_equal(S("hello"), a.lstrip)
  595. assert_equal(S(" hello"), a)
  596. assert_equal(S("hello "), S(" hello ").lstrip)
  597. assert_equal(S("hello"), S("hello").lstrip)
  598. end
  599. def test_lstrip!
  600. a = S(" abc")
  601. b = a.dup
  602. assert_equal(S("abc"), a.lstrip!)
  603. assert_equal(S("abc"), a)
  604. assert_equal(S(" abc"), b)
  605. end
  606. def test_match
  607. a = S("cruel world")
  608. m = a.match(/\w+/)
  609. assert_kind_of(MatchData, m)
  610. assert_equal(S("cruel"), m.to_s)
  611. m = a.match('\w+')
  612. assert_kind_of(MatchData, m)
  613. assert_equal(S("cruel"), m.to_s)
  614. end
  615. def test_next
  616. assert_equal(S("abd"), S("abc").next)
  617. assert_equal(S("z"), S("y").next)
  618. assert_equal(S("aaa"), S("zz").next)
  619. assert_equal(S("124"), S("123").next)
  620. assert_equal(S("1000"), S("999").next)
  621. assert_equal(S("2000aaa"), S("1999zzz").next)
  622. assert_equal(S("AAAAA000"), S("ZZZZ999").next)
  623. assert_equal(S("*+"), S("**").next)
  624. end
  625. def test_next!
  626. a = S("abc")
  627. b = a.dup
  628. assert_equal(S("abd"), a.next!)
  629. assert_equal(S("abd"), a)
  630. assert_equal(S("abc"), b)
  631. a = S("y")
  632. assert_equal(S("z"), a.next!)
  633. assert_equal(S("z"), a)
  634. a = S("zz")
  635. assert_equal(S("aaa"), a.next!)
  636. assert_equal(S("aaa"), a)
  637. a = S("123")
  638. assert_equal(S("124"), a.next!)
  639. assert_equal(S("124"), a)
  640. a = S("999")
  641. assert_equal(S("1000"), a.next!)
  642. assert_equal(S("1000"), a)
  643. a = S("1999zzz")
  644. assert_equal(S("2000aaa"), a.next!)
  645. assert_equal(S("2000aaa"), a)
  646. a = S("ZZZZ999")
  647. assert_equal(S("AAAAA000"), a.next!)
  648. assert_equal(S("AAAAA000"), a)
  649. a = S("**")
  650. assert_equal(S("*+"), a.next!)
  651. assert_equal(S("*+"), a)
  652. end
  653. def test_oct
  654. assert_equal(255, S("0377").oct)
  655. assert_equal(255, S("377").oct)
  656. assert_equal(-255, S("-0377").oct)
  657. assert_equal(-255, S("-377").oct)
  658. assert_equal(0, S("OO").oct)
  659. assert_equal(24, S("030OO").oct)
  660. end
  661. def test_replace
  662. a = S("foo")
  663. assert_equal(S("f"), a.replace(S("f")))
  664. a = S("foo")
  665. assert_equal(S("foobar"), a.replace(S("foobar")))
  666. a = S("foo")
  667. a.taint
  668. b = a.replace(S("xyz"))
  669. assert_equal(S("xyz"), b)
  670. assert(b.tainted?)
  671. end
  672. def test_reverse
  673. assert_equal(S("beta"), S("ateb").reverse)
  674. assert_equal(S("madamImadam"), S("madamImadam").reverse)
  675. a=S("beta")
  676. assert_equal(S("ateb"), a.reverse)
  677. assert_equal(S("beta"), a)
  678. end
  679. def test_reverse!
  680. a = S("beta")
  681. b = a.dup
  682. assert_equal(S("ateb"), a.reverse!)
  683. assert_equal(S("ateb"), a)
  684. assert_equal(S("beta"), b)
  685. assert_equal(S("madamImadam"), S("madamImadam").reverse!)
  686. a = S("madamImadam")
  687. assert_equal(S("madamImadam"), a.reverse!) # ??
  688. assert_equal(S("madamImadam"), a)
  689. end
  690. def test_rindex
  691. assert_equal(3, S("hello").rindex(?l))
  692. assert_equal(6, S("ell, hello").rindex(S("ell")))
  693. assert_equal(7, S("ell, hello").rindex(/ll./))
  694. assert_equal(3, S("hello,lo").rindex(?l, 3))
  695. assert_equal(3, S("hello,lo").rindex(S("l"), 3))
  696. assert_equal(3, S("hello,lo").rindex(/l./, 3))
  697. assert_nil(S("hello").rindex(?z, 3))
  698. assert_nil(S("hello").rindex(S("z"), 3))
  699. assert_nil(S("hello").rindex(/z./, 3))
  700. assert_nil(S("hello").rindex(?z))
  701. assert_nil(S("hello").rindex(S("z")))
  702. assert_nil(S("hello").rindex(/z./))
  703. end
  704. def test_rjust
  705. assert_equal(S("hello"), S("hello").rjust(4))
  706. assert_equal(S(" hello"), S("hello").rjust(11))
  707. # with explicit padding
  708. assert_equal(S("111111111hi"), S("hi").rjust(11, "1"))
  709. assert_equal(S("121212121hi"), S("hi").rjust(11, "12"))
  710. assert_equal(S("123412341hi"), S("hi").rjust(11, "1234"))
  711. # zero width padding
  712. assert_raise(ArgumentError) { S("hi").rjust(11, "") }
  713. end
  714. def test_rstrip
  715. a = S("hello ")
  716. assert_equal(S("hello"), a.rstrip)
  717. assert_equal(S("hello "), a)
  718. assert_equal(S(" hello"), S(" hello ").rstrip)
  719. assert_equal(S("hello"), S("hello").rstrip)
  720. end
  721. def test_rstrip!
  722. a = S("abc ")
  723. b = a.dup
  724. assert_equal(S("abc"), a.rstrip!)
  725. assert_equal(S("abc"), a)
  726. assert_equal(S("abc "), b)
  727. end
  728. def test_scan
  729. a = S("cruel world")
  730. assert_equal([S("cruel"), S("world")],a.scan(/\w+/))
  731. assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../))
  732. assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/))
  733. res = []
  734. a.scan(/\w+/) { |w| res << w }
  735. assert_equal([S("cruel"), S("world") ],res)
  736. res = []
  737. a.scan(/.../) { |w| res << w }
  738. assert_equal([S("cru"), S("el "), S("wor")],res)
  739. res = []
  740. a.scan(/(...)/) { |w| res << w }
  741. assert_equal([[S("cru")], [S("el ")], [S("wor")]],res)
  742. # with a pattern that match empty string
  743. assert_equal(S(["", "", "1", "", "", "22", "", "", "333", ""]),
  744. S("aa1bb22cc333").scan(/\d*/))
  745. end
  746. def test_size
  747. assert_equal(0, S("").size)
  748. assert_equal(4, S("1234").size)
  749. assert_equal(6, S("1234\r\n").size)
  750. assert_equal(7, S("\0011234\r\n").size)
  751. end
  752. def test_slice
  753. assert_equal(IS19 ? "A" : 65, S("AooBar").slice(0))
  754. assert_equal(IS19 ? "B" : 66, S("FooBaB").slice(-1))
  755. assert_nil(S("FooBar").slice(6))
  756. assert_nil(S("FooBar").slice(-7))
  757. assert_equal(S("Foo"), S("FooBar").slice(0,3))
  758. assert_equal(S(S("Bar")), S("FooBar").slice(-3,3))
  759. assert_nil(S("FooBar").slice(7,2)) # Maybe should be six?
  760. assert_nil(S("FooBar").slice(-7,10))
  761. assert_equal(S("Foo"), S("FooBar").slice(0..2))
  762. assert_equal(S("Bar"), S("FooBar").slice(-3..-1))
  763. assert_equal("", S("FooBar").slice(6..2))
  764. assert_nil(S("FooBar").slice(-10..-7))
  765. assert_equal(S("Foo"), S("FooBar").slice(/^F../))
  766. assert_equal(S("Bar"), S("FooBar").slice(/..r$/))
  767. assert_nil(S("FooBar").slice(/xyzzy/))
  768. assert_nil(S("FooBar").slice(/plugh/))
  769. assert_equal(S("Foo"), S("FooBar").slice(S("Foo")))
  770. assert_equal(S("Bar"), S("FooBar").slice(S("Bar")))
  771. assert_nil(S("FooBar").slice(S("xyzzy")))
  772. assert_nil(S("FooBar").slice(S("plugh")))
  773. end
  774. def test_slice!
  775. a = S("AooBar")
  776. b = a.dup
  777. assert_equal(IS19 ? "A" : 65, a.slice!(0))
  778. assert_equal(S("ooBar"), a)
  779. assert_equal(S("AooBar"), b)
  780. a = S("FooBar")
  781. assert_equal(?r,a.slice!(-1))
  782. assert_equal(S("FooBa"), a)
  783. a = S("FooBar")
  784. if @aref_slicebang_silent
  785. assert_nil( a.slice!(6) )
  786. else
  787. assert_raise(:IndexError) { a.slice!(6) }
  788. end
  789. assert_equal(S("FooBar"), a)
  790. if @aref_slicebang_silent
  791. assert_nil( a.slice!(-7) )
  792. else
  793. assert_raise(:IndexError) { a.slice!(-7) }
  794. end
  795. assert_equal(S("FooBar"), a)
  796. a = S("FooBar")
  797. assert_equal(S("Foo"), a.slice!(0,3))
  798. assert_equal(S("Bar"), a)
  799. a = S("FooBar")
  800. assert_equal(S("Bar"), a.slice!(-3,3))
  801. assert_equal(S("Foo"), a)
  802. a=S("FooBar")
  803. if @aref_slicebang_silent
  804. assert_nil(a.slice!(7,2)) # Maybe should be six?
  805. else
  806. assert_raise(:IndexError) {a.slice!(7,2)} # Maybe should be six?
  807. end
  808. assert_equal(S("FooBar"), a)
  809. if @aref_slicebang_silent
  810. assert_nil(a.slice!(-7,10))
  811. else
  812. assert_raise(:IndexError) {a.slice!(-7,10)}
  813. end
  814. assert_equal(S("FooBar"), a)
  815. a=S("FooBar")
  816. assert_equal(S("Foo"), a.slice!(0..2))
  817. assert_equal(S("Bar"), a)
  818. a=S("FooBar")
  819. assert_equal(S("Bar"), a.slice!(-3..-1))
  820. assert_equal(S("Foo"), a)
  821. a=S("FooBar")
  822. if @aref_slicebang_silent
  823. assert_equal("", a.slice!(6..2))
  824. else
  825. assert_raise(:RangeError) {a.slice!(6..2)}
  826. end
  827. assert_equal(S("FooBar"), a)
  828. if @aref_slicebang_silent
  829. assert_nil(a.slice!(-10..-7))
  830. else
  831. assert_raise(:RangeError) {a.slice!(-10..-7)}
  832. end
  833. assert_equal(S("FooBar"), a)
  834. a=S("FooBar")
  835. assert_equal(S("Foo"), a.slice!(/^F../))
  836. assert_equal(S("Bar"), a)
  837. a=S("FooBar")
  838. assert_equal(S("Bar"), a.slice!(/..r$/))
  839. assert_equal(S("Foo"), a)
  840. a=S("FooBar")
  841. if @aref_slicebang_silent
  842. assert_nil(a.slice!(/xyzzy/))
  843. else
  844. assert_raise(:IndexError) {a.slice!(/xyzzy/)}
  845. end
  846. assert_equal(S("FooBar"), a)
  847. if @aref_slicebang_silent
  848. assert_nil(a.slice!(/plugh/))
  849. else
  850. assert_raise(:IndexError) {a.slice!(/plugh/)}
  851. end
  852. assert_equal(S("FooBar"), a)
  853. a=S("FooBar")
  854. assert_equal(S("Foo"), a.slice!(S("Foo")))
  855. assert_equal(S("Bar"), a)
  856. a=S("FooBar")
  857. assert_equal(S("Bar"), a.slice!(S("Bar")))
  858. assert_equal(S("Foo"), a)
  859. end
  860. class MyString < String
  861. end
  862. def test_split
  863. assert_nil($;)
  864. assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split)
  865. assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" ")))
  866. assert_equal([S(" a "), S(" b "), S(" c ")], S(" a | b | c ").split(S("|")))
  867. assert_equal([S("a"), S("b"), S("c")], S("aXXbXXcXX").split(/X./))
  868. assert_equal([S("a"), S("b"), S("c")], S("abc").split(//))
  869. assert_equal([S("a|b|c")], S("a|b|c").split(S('|'), 1))
  870. assert_equal([S("a"), S("b|c")], S("a|b|c").split(S('|'), 2))
  871. assert_equal([S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3))
  872. assert_equal([S("a"), S("b"), S("c"), S("")], S("a|b|c|").split(S('|'), -1))
  873. assert_equal([S("a"), S("b"), S("c"), S(""), S("")], S("a|b|c||").split(S('|'), -1))
  874. assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|')))
  875. assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1))
  876. assert_equal(MyString, MyString.new("a|b").split(S('|'))[0].class)
  877. end
  878. def test_squeeze
  879. assert_equal(S("abc"), S("aaabbbbccc").squeeze)
  880. assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" ")))
  881. assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z")))
  882. end
  883. def test_squeeze!
  884. a = S("aaabbbbccc")
  885. b = a.dup
  886. assert_equal(S("abc"), a.squeeze!)
  887. assert_equal(S("abc"), a)
  888. assert_equal(S("aaabbbbccc"), b)
  889. a = S("aa bb cc")
  890. assert_equal(S("aa bb cc"), a.squeeze!(S(" ")))
  891. assert_equal(S("aa bb cc"), a)
  892. a = S("BxxxTyyyWzzzzz")
  893. assert_equal(S("BxTyWz"), a.squeeze!(S("a-z")))
  894. assert_equal(S("BxTyWz"), a)
  895. a=S("The quick brown fox")
  896. assert_nil(a.squeeze!)
  897. end
  898. def test_strip
  899. assert_equal(S("x"), S(" x ").strip)
  900. assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
  901. end
  902. def test_strip!
  903. a = S(" x ")
  904. b = a.dup
  905. assert_equal(S("x") ,a.strip!)
  906. assert_equal(S("x") ,a)
  907. assert_equal(S(" x "), b)
  908. a = S(" \n\r\t x \t\r\n\n ")
  909. assert_equal(S("x"), a.strip!)
  910. assert_equal(S("x"), a)
  911. a = S("x")
  912. assert_nil(a.strip!)
  913. assert_equal(S("x") ,a)
  914. end
  915. def test_sub
  916. assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*')))
  917. assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>')))
  918. assert_equal(S(IS19 ? "h ello" : "104 ello"), S("hello").sub(/./) {
  919. |s| s[0].to_s + S(' ')})
  920. assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) {
  921. |s| $1.upcase + S('-') + $2
  922. })
  923. assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\'))
  924. assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\'))
  925. assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1'))
  926. assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1'))
  927. assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1'))
  928. assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1'))
  929. assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1'))
  930. assert_equal(S("a--ababababababababab"),
  931. S("abababababababababab").sub(/(b)/, '-\9-'))
  932. assert_equal(S("1-b-0"),
  933. S("1b2b3b4b5b6b7b8b9b0").
  934. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-'))
  935. assert_equal(S("1-b-0"),
  936. S("1b2b3b4b5b6b7b8b9b0").
  937. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-'))
  938. assert_equal(S("1-\\9-0"),
  939. S("1b2b3b4b5b6b7b8b9b0").
  940. sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-'))
  941. assert_equal(S("k"),
  942. S("1a2b3c4d5e6f7g8h9iAjBk").
  943. sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+'))
  944. assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\'))
  945. assert_equal(S("ababa"), S("ababa").sub(/b/, '\&'))
  946. assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&'))
  947. assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&'))
  948. assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&'))
  949. assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&'))
  950. a = S("hello")
  951. a.taint
  952. assert(a.sub(/./, S('X')).tainted?)
  953. end
  954. def test_sub!
  955. a = S("hello")
  956. b = a.dup
  957. a.sub!(/[aeiou]/, S('*'))
  958. assert_equal(S("h*llo"), a)
  959. assert_equal(S("hello"), b)
  960. a = S("hello")
  961. a.sub!(/([aeiou])/, S('<\1>'))
  962. assert_equal(S("h<e>llo"), a)
  963. a = S("hello")
  964. a.sub!(/./) { |s| s[0].to_s + S(' ')}
  965. assert_equal(S(IS19 ? "h ello" : "104 ello"), a)
  966. a = S("hello")
  967. a.sub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
  968. assert_equal(S("HELL-o"), a)
  969. a=S("hello")
  970. assert_nil(a.sub!(/X/, S('Y')))
  971. r = S('X')
  972. r.taint
  973. a.sub!(/./, r)
  974. assert(a.tainted?)
  975. end
  976. def test_succ
  977. assert_equal(S("abd"), S("abc").succ)
  978. assert_equal(S("z"), S("y").succ)
  979. assert_equal(S("aaa"), S("zz").succ)
  980. assert_equal(S("124"), S("123").succ)
  981. assert_equal(S("1000"), S("999").succ)
  982. assert_equal(S("2000aaa"), S("1999zzz").succ)
  983. assert_equal(S("AAAAA000"), S("ZZZZ999").succ)
  984. assert_equal(S("*+"), S("**").succ)
  985. end
  986. def test_succ!
  987. a = S("abc")
  988. b = a.dup
  989. assert_equal(S("abd"), a.succ!)
  990. assert_equal(S("abd"), a)
  991. assert_equal(S("abc"), b)
  992. a = S("y")
  993. assert_equal(S("z"), a.succ!)
  994. assert_equal(S("z"), a)
  995. a = S("zz")
  996. assert_equal(S("aaa"), a.succ!)
  997. assert_equal(S("aaa"), a)
  998. a = S("123")
  999. assert_equal(S("124"), a.succ!)
  1000. assert_equal(S("124"), a)
  1001. a = S("999")
  1002. assert_equal(S("1000"), a.succ!)
  1003. assert_equal(S("1000"), a)
  1004. a = S("1999zzz")
  1005. assert_equal(S("2000aaa"), a.succ!)
  1006. assert_equal(S("2000aaa"), a)
  1007. a = S("ZZZZ999")
  1008. assert_equal(S("AAAAA000"), a.succ!)
  1009. assert_equal(S("AAAAA000"), a)
  1010. a = S("**")
  1011. assert_equal(S("*+"), a.succ!)
  1012. assert_equal(S("*+"), a)
  1013. end
  1014. def test_sum
  1015. n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001")
  1016. assert_equal(15, n.sum)
  1017. assert_equal(15, n.sum(32))
  1018. n += S("\001")
  1019. assert_equal(16, n.sum(17))
  1020. assert_equal(16, n.sum(32))
  1021. if IS19
  1022. n.setbyte(0, 2)
  1023. else
  1024. n[0] = 2
  1025. end
  1026. assert(15 != n.sum)
  1027. # basic test of all reasonable "bit sizes"
  1028. str = S("\xFF" * 257)
  1029. 2.upto(32) do |bits|
  1030. assert_equal(65535 % (2**bits), str.sum(bits))
  1031. end
  1032. # with 16 bits the result is modulo 65536
  1033. assert_equal( 255, S("\xFF").sum)
  1034. assert_equal( 510, S("\xFF\xFF").sum)
  1035. assert_equal(65535, S("\xFF" * 257).sum)
  1036. assert_equal( 254, S("\xFF" * 258).sum)
  1037. # with 32 bits the result is modulo 2**32
  1038. assert_equal( 255, S("\xFF").sum(32))
  1039. assert_equal( 510, S("\xFF\xFF").sum(32))
  1040. assert_equal(65535, S("\xFF" * 257).sum(32))
  1041. assert_equal(65790, S("\xFF" * 258).sum(32))
  1042. # the following case takes 16MB and takes a long time to compute,
  1043. # so it is commented out.
  1044. #assert_equal( 254, S("\xFF" * (257 * 65537 + 1)).sum(32))
  1045. end
  1046. def test_swapcase
  1047. assert_equal(S("hi&LOW"), S("HI&low").swapcase)
  1048. end
  1049. def test_swapcase!
  1050. a = S("hi&LOW")
  1051. b = a.dup
  1052. assert_equal(S("HI&low"), a.swapcase!)
  1053. assert_equal(S("HI&low"), a)
  1054. assert_equal(S("hi&LOW"), b)
  1055. a = S("$^#^%$#!!")
  1056. assert_nil(a.swapcase!)
  1057. assert_equal(S("$^#^%$#!!"), a)
  1058. end
  1059. def test_to_f
  1060. assert_equal(344.3, S("344.3").to_f)
  1061. assert_equal(5.9742e24, S("5.9742e24").to_f)
  1062. assert_equal(98.6, S("98.6 degrees").to_f)
  1063. assert_equal(0.0, S("degrees 100.0").to_f)
  1064. end
  1065. def test_to_i
  1066. assert_equal(1480, S("1480ft/sec").to_i)
  1067. assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i)
  1068. end
  1069. def test_to_s
  1070. a = S("me")
  1071. assert_equal("me", a.to_s)
  1072. assert_equal(a.__id__, a.to_s.__id__) if @cls == String
  1073. end
  1074. def test_to_str
  1075. a = S("me")
  1076. assert_equal("me", a.to_s)
  1077. assert_equal(a.__id__, a.to_s.__id__) if @cls == String
  1078. end
  1079. def test_to_sym
  1080. generic_test_intern(:to_sym)
  1081. end
  1082. def test_tr
  1083. assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip")))
  1084. assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*")))
  1085. assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z")))
  1086. end
  1087. def test_tr!
  1088. a = S("hello")
  1089. b = a.dup
  1090. assert_equal(S("hippo"), a.tr!(S("el"), S("ip")))
  1091. assert_equal(S("hippo"), a)
  1092. assert_equal(S("hello"),b)
  1093. a = S("hello")
  1094. assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*")))
  1095. assert_equal(S("*e**o"), a)
  1096. a = S("IBM")
  1097. assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z")))
  1098. assert_equal(S("HAL"), a)
  1099. a = S("ibm")
  1100. assert_nil(a.tr!(S("B-Z"), S("A-Z")))
  1101. assert_equal(S("ibm"), a)
  1102. end
  1103. def test_tr_s
  1104. assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
  1105. assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
  1106. end
  1107. def test_tr_s!
  1108. a = S("hello")
  1109. b = a.dup
  1110. assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp")))
  1111. assert_equal(S("hypo"), a)
  1112. assert_equal(S("hello"), b)
  1113. a = S("hello")
  1114. assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
  1115. assert_equal(S("h*o"), a)
  1116. end
  1117. def test_upcase
  1118. assert_equal(S("HELLO"), S("hello").upcase)
  1119. assert_equal(S("HELLO"), S("hello").upcase)
  1120. assert_equal(S("HELLO"), S("HELLO").upcase)
  1121. assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase)
  1122. end
  1123. def test_upcase!
  1124. a = S("hello")
  1125. b = a.dup
  1126. assert_equal(S("HELLO"), a.upcase!)
  1127. assert_equal(S("HELLO"), a)
  1128. assert_equal(S("hello"), b)
  1129. a = S("HELLO")
  1130. assert_nil(a.upcase!)
  1131. assert_equal(S("HELLO"), a)
  1132. end
  1133. def test_upto
  1134. a = S("aa")
  1135. start = S("aa")
  1136. count = 0
  1137. assert_equal(S("aa"), a.upto(S("zz")) {|s|
  1138. assert_equal(start, s)
  1139. start.succ!
  1140. count += 1
  1141. })
  1142. assert_equal(676, count)
  1143. end
  1144. def test_s_new
  1145. assert_equal("RUBY", S("RUBY"))
  1146. end
  1147. end