PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1623 lines | 1503 code | 107 blank | 13 comment | 309 complexity | 716a6d81d5a53612d6e18ed9b9a79903 MD5 | raw file
  1. require 'test/unit'
  2. require 'stringio'
  3. require_relative 'allpairs'
  4. class TestM17NComb < Test::Unit::TestCase
  5. def assert_encoding(encname, actual, message=nil)
  6. assert_equal(Encoding.find(encname), actual, message)
  7. end
  8. module AESU
  9. def a(str) str.dup.force_encoding("ASCII-8BIT") end
  10. def e(str) str.dup.force_encoding("EUC-JP") end
  11. def s(str) str.dup.force_encoding("Shift_JIS") end
  12. def u(str) str.dup.force_encoding("UTF-8") end
  13. end
  14. include AESU
  15. extend AESU
  16. def assert_strenc(bytes, enc, actual, message=nil)
  17. assert_instance_of(String, actual, message)
  18. enc = Encoding.find(enc) if String === enc
  19. assert_equal(enc, actual.encoding, message)
  20. assert_equal(a(bytes), a(actual), message)
  21. end
  22. def assert_warning(pat, mesg=nil)
  23. begin
  24. org_stderr = $stderr
  25. $stderr = StringIO.new(warn = '')
  26. yield
  27. ensure
  28. $stderr = org_stderr
  29. end
  30. assert_match(pat, warn, mesg)
  31. end
  32. def assert_regexp_generic_encoding(r)
  33. assert(!r.fixed_encoding?)
  34. %w[ASCII-8BIT EUC-JP Shift_JIS UTF-8].each {|ename|
  35. # "\xc2\xa1" is a valid sequence for ASCII-8BIT, EUC-JP, Shift_JIS and UTF-8.
  36. assert_nothing_raised { r =~ "\xc2\xa1".force_encoding(ename) }
  37. }
  38. end
  39. def assert_regexp_fixed_encoding(r)
  40. assert(r.fixed_encoding?)
  41. %w[ASCII-8BIT EUC-JP Shift_JIS UTF-8].each {|ename|
  42. enc = Encoding.find(ename)
  43. if enc == r.encoding
  44. assert_nothing_raised { r =~ "\xc2\xa1".force_encoding(enc) }
  45. else
  46. assert_raise(ArgumentError) { r =~ "\xc2\xa1".force_encoding(enc) }
  47. end
  48. }
  49. end
  50. def assert_regexp_generic_ascii(r)
  51. assert_encoding("ASCII-8BIT", r.encoding)
  52. assert_regexp_generic_encoding(r)
  53. end
  54. def assert_regexp_fixed_ascii8bit(r)
  55. assert_encoding("ASCII-8BIT", r.encoding)
  56. assert_regexp_fixed_encoding(r)
  57. end
  58. def assert_regexp_fixed_eucjp(r)
  59. assert_encoding("EUC-JP", r.encoding)
  60. assert_regexp_fixed_encoding(r)
  61. end
  62. def assert_regexp_fixed_sjis(r)
  63. assert_encoding("Shift_JIS", r.encoding)
  64. assert_regexp_fixed_encoding(r)
  65. end
  66. def assert_regexp_fixed_utf8(r)
  67. assert_encoding("UTF-8", r.encoding)
  68. assert_regexp_fixed_encoding(r)
  69. end
  70. STRINGS = [
  71. a(""), e(""), s(""), u(""),
  72. a("a"), e("a"), s("a"), u("a"),
  73. a("."), e("."), s("."), u("."),
  74. # single character
  75. a("\x80"), a("\xff"),
  76. e("\xa1\xa1"), e("\xfe\xfe"),
  77. e("\x8e\xa1"), e("\x8e\xfe"),
  78. e("\x8f\xa1\xa1"), e("\x8f\xfe\xfe"),
  79. s("\x81\x40"), s("\xfc\xfc"),
  80. s("\xa1"), s("\xdf"),
  81. u("\xc2\x80"), u("\xf4\x8f\xbf\xbf"),
  82. # same byte sequence
  83. a("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1"),
  84. s("\x81A"), # mutibyte character which contains "A"
  85. s("\x81a"), # mutibyte character which contains "a"
  86. # invalid
  87. e("\xa1"), e("\x80"),
  88. s("\x81"), s("\x80"),
  89. u("\xc2"), u("\x80"),
  90. # for transitivity test
  91. u("\xe0\xa0\xa1"), e("\xe0\xa0\xa1"), s("\xe0\xa0\xa1"), # [ruby-dev:32693]
  92. e("\xa1\xa1"), a("\xa1\xa1"), s("\xa1\xa1"), # [ruby-dev:36484]
  93. #"aa".force_encoding("utf-16be"),
  94. #"aaaa".force_encoding("utf-32be"),
  95. #"aaa".force_encoding("utf-32be"),
  96. ]
  97. def combination(*args, &b)
  98. AllPairs.each(*args, &b)
  99. #AllPairs.exhaustive_each(*args, &b)
  100. end
  101. def encdump(str)
  102. d = str.dump
  103. if /\.force_encoding\("[A-Za-z0-9.:_+-]*"\)\z/ =~ d
  104. d
  105. else
  106. "#{d}.force_encoding(#{str.encoding.name.dump})"
  107. end
  108. end
  109. def encdumpargs(args)
  110. r = '('
  111. args.each_with_index {|a, i|
  112. r << ',' if 0 < i
  113. if String === a
  114. r << encdump(a)
  115. else
  116. r << a.inspect
  117. end
  118. }
  119. r << ')'
  120. r
  121. end
  122. def enccall(recv, meth, *args, &block)
  123. desc = ''
  124. if String === recv
  125. desc << encdump(recv)
  126. else
  127. desc << recv.inspect
  128. end
  129. desc << '.' << meth.to_s
  130. if !args.empty?
  131. desc << '('
  132. args.each_with_index {|a, i|
  133. desc << ',' if 0 < i
  134. if String === a
  135. desc << encdump(a)
  136. else
  137. desc << a.inspect
  138. end
  139. }
  140. desc << ')'
  141. end
  142. if block
  143. desc << ' {}'
  144. end
  145. result = nil
  146. assert_nothing_raised(desc) {
  147. result = recv.send(meth, *args, &block)
  148. }
  149. result
  150. end
  151. def assert_str_enc_propagation(t, s1, s2)
  152. if !s1.ascii_only?
  153. assert_equal(s1.encoding, t.encoding)
  154. elsif !s2.ascii_only?
  155. assert_equal(s2.encoding, t.encoding)
  156. else
  157. assert([s1.encoding, s2.encoding].include?(t.encoding))
  158. end
  159. end
  160. def assert_same_result(expected_proc, actual_proc)
  161. e = nil
  162. begin
  163. t = expected_proc.call
  164. rescue
  165. e = $!
  166. end
  167. if e
  168. assert_raise(e.class) { actual_proc.call }
  169. else
  170. assert_equal(t, actual_proc.call)
  171. end
  172. end
  173. def each_slice_call
  174. combination(STRINGS, -2..2) {|s, nth|
  175. yield s, nth
  176. }
  177. combination(STRINGS, -2..2, 0..2) {|s, nth, len|
  178. yield s, nth, len
  179. }
  180. combination(STRINGS, STRINGS) {|s, substr|
  181. yield s, substr
  182. }
  183. combination(STRINGS, -2..2, 0..2) {|s, first, last|
  184. yield s, first..last
  185. yield s, first...last
  186. }
  187. combination(STRINGS, STRINGS) {|s1, s2|
  188. if !s2.valid_encoding?
  189. next
  190. end
  191. yield s1, Regexp.new(Regexp.escape(s2))
  192. }
  193. combination(STRINGS, STRINGS, 0..2) {|s1, s2, nth|
  194. if !s2.valid_encoding?
  195. next
  196. end
  197. yield s1, Regexp.new(Regexp.escape(s2)), nth
  198. }
  199. end
  200. ASCII_INCOMPATIBLE_ENCODINGS = %w[
  201. UTF-16BE
  202. UTF-16LE
  203. UTF-32BE
  204. UTF-32LE
  205. ]
  206. def str_enc_compatible?(*strs)
  207. encs = []
  208. ascii_incompatible_encodings = {}
  209. has_ascii_compatible = false
  210. strs.each {|s|
  211. encs << s.encoding if !s.ascii_only?
  212. if /\A#{Regexp.union ASCII_INCOMPATIBLE_ENCODINGS}\z/o =~ s.encoding.name
  213. ascii_incompatible_encodings[s.encoding] = true
  214. else
  215. has_ascii_compatible = true
  216. end
  217. }
  218. if ascii_incompatible_encodings.empty?
  219. encs.uniq!
  220. encs.length <= 1
  221. else
  222. !has_ascii_compatible && ascii_incompatible_encodings.size == 1
  223. end
  224. end
  225. # tests start
  226. def test_str_new
  227. STRINGS.each {|s|
  228. t = String.new(s)
  229. assert_strenc(a(s), s.encoding, t)
  230. }
  231. end
  232. def test_str_plus
  233. combination(STRINGS, STRINGS) {|s1, s2|
  234. if s1.encoding != s2.encoding && !s1.ascii_only? && !s2.ascii_only?
  235. assert_raise(Encoding::CompatibilityError) { s1 + s2 }
  236. else
  237. t = enccall(s1, :+, s2)
  238. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  239. assert_equal(a(s1) + a(s2), a(t))
  240. assert_str_enc_propagation(t, s1, s2)
  241. end
  242. }
  243. end
  244. def test_str_times
  245. STRINGS.each {|s|
  246. [0,1,2].each {|n|
  247. t = s * n
  248. assert(t.valid_encoding?) if s.valid_encoding?
  249. assert_strenc(a(s) * n, s.encoding, t)
  250. }
  251. }
  252. end
  253. def test_sprintf_s
  254. STRINGS.each {|s|
  255. assert_strenc(a(s), s.encoding, "%s".force_encoding(s.encoding) % s)
  256. if !s.empty? # xxx
  257. t = enccall(a("%s"), :%, s)
  258. assert_strenc(a(s), (a('')+s).encoding, t)
  259. end
  260. }
  261. end
  262. def test_str_eq_reflexive
  263. STRINGS.each {|s|
  264. assert(s == s, "#{encdump s} == #{encdump s}")
  265. }
  266. end
  267. def test_str_eq_symmetric
  268. combination(STRINGS, STRINGS) {|s1, s2|
  269. if s1 == s2
  270. assert(s2 == s1, "#{encdump s2} == #{encdump s1}")
  271. else
  272. assert(!(s2 == s1), "!(#{encdump s2} == #{encdump s1})")
  273. end
  274. }
  275. end
  276. def test_str_eq_transitive
  277. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  278. if s1 == s2 && s2 == s3
  279. assert(s1 == s3, "transitive: #{encdump s1} == #{encdump s2} == #{encdump s3}")
  280. end
  281. }
  282. end
  283. def test_str_eq
  284. combination(STRINGS, STRINGS) {|s1, s2|
  285. desc_eq = "#{encdump s1} == #{encdump s2}"
  286. if a(s1) == a(s2) and
  287. (s1.ascii_only? && s2.ascii_only? or
  288. s1.encoding == s2.encoding) then
  289. assert(s1 == s2, desc_eq)
  290. assert(!(s1 != s2))
  291. assert_equal(0, s1 <=> s2)
  292. assert(s1.eql?(s2), desc_eq)
  293. else
  294. assert(!(s1 == s2), "!(#{desc_eq})")
  295. assert(s1 != s2)
  296. assert_not_equal(0, s1 <=> s2)
  297. assert(!s1.eql?(s2))
  298. end
  299. }
  300. end
  301. def test_str_concat
  302. combination(STRINGS, STRINGS) {|s1, s2|
  303. s = s1.dup
  304. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  305. s << s2
  306. assert(s.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  307. assert_equal(a(s), a(s1) + a(s2))
  308. assert_str_enc_propagation(s, s1, s2)
  309. else
  310. assert_raise(Encoding::CompatibilityError) { s << s2 }
  311. end
  312. }
  313. end
  314. def test_str_aref
  315. STRINGS.each {|s|
  316. t = ''.force_encoding(s.encoding)
  317. 0.upto(s.length-1) {|i|
  318. u = s[i]
  319. assert(u.valid_encoding?) if s.valid_encoding?
  320. t << u
  321. }
  322. assert_equal(t, s)
  323. }
  324. end
  325. def test_str_aref_len
  326. STRINGS.each {|s|
  327. t = ''.force_encoding(s.encoding)
  328. 0.upto(s.length-1) {|i|
  329. u = s[i,1]
  330. assert(u.valid_encoding?) if s.valid_encoding?
  331. t << u
  332. }
  333. assert_equal(t, s)
  334. }
  335. STRINGS.each {|s|
  336. t = ''.force_encoding(s.encoding)
  337. 0.step(s.length-1, 2) {|i|
  338. u = s[i,2]
  339. assert(u.valid_encoding?) if s.valid_encoding?
  340. t << u
  341. }
  342. assert_equal(t, s)
  343. }
  344. end
  345. def test_str_aref_substr
  346. combination(STRINGS, STRINGS) {|s1, s2|
  347. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  348. t = enccall(s1, :[], s2)
  349. if t != nil
  350. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  351. assert_equal(s2, t)
  352. assert_match(/#{Regexp.escape(a(s2))}/, a(s1))
  353. if s1.valid_encoding?
  354. assert_match(/#{Regexp.escape(s2)}/, s1)
  355. end
  356. end
  357. else
  358. assert_raise(Encoding::CompatibilityError) { s1[s2] }
  359. end
  360. }
  361. end
  362. def test_str_aref_range2
  363. combination(STRINGS, -2..2, -2..2) {|s, first, last|
  364. desc = "#{encdump s}[#{first}..#{last}]"
  365. t = s[first..last]
  366. if first < 0
  367. first += s.length
  368. if first < 0
  369. assert_nil(t, desc)
  370. next
  371. end
  372. end
  373. if s.length < first
  374. assert_nil(t, desc)
  375. next
  376. end
  377. assert(t.valid_encoding?) if s.valid_encoding?
  378. if last < 0
  379. last += s.length
  380. end
  381. t2 = ''
  382. first.upto(last) {|i|
  383. c = s[i]
  384. t2 << c if c
  385. }
  386. assert_equal(t2, t, desc)
  387. }
  388. end
  389. def test_str_aref_range3
  390. combination(STRINGS, -2..2, -2..2) {|s, first, last|
  391. desc = "#{encdump s}[#{first}..#{last}]"
  392. t = s[first...last]
  393. if first < 0
  394. first += s.length
  395. if first < 0
  396. assert_nil(t, desc)
  397. next
  398. end
  399. end
  400. if s.length < first
  401. assert_nil(t, desc)
  402. next
  403. end
  404. if last < 0
  405. last += s.length
  406. end
  407. assert(t.valid_encoding?) if s.valid_encoding?
  408. t2 = ''
  409. first.upto(last-1) {|i|
  410. c = s[i]
  411. t2 << c if c
  412. }
  413. assert_equal(t2, t, desc)
  414. }
  415. end
  416. def test_str_assign
  417. combination(STRINGS, STRINGS) {|s1, s2|
  418. (-2).upto(2) {|i|
  419. t = s1.dup
  420. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  421. if i < -s1.length || s1.length < i
  422. assert_raise(IndexError) { t[i] = s2 }
  423. else
  424. t[i] = s2
  425. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  426. assert(a(t).index(a(s2)))
  427. if s1.valid_encoding? && s2.valid_encoding?
  428. if i == s1.length && s2.empty?
  429. assert_nil(t[i])
  430. elsif i < 0
  431. assert_equal(s2, t[i-s2.length+1,s2.length],
  432. "t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i-s2.length+1},#{s2.length}]")
  433. else
  434. assert_equal(s2, t[i,s2.length],
  435. "t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
  436. end
  437. end
  438. end
  439. else
  440. assert_raise(Encoding::CompatibilityError) { t[i] = s2 }
  441. end
  442. }
  443. }
  444. end
  445. def test_str_assign_len
  446. combination(STRINGS, -2..2, 0..2, STRINGS) {|s1, i, len, s2|
  447. t = s1.dup
  448. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  449. if i < -s1.length || s1.length < i
  450. assert_raise(IndexError) { t[i,len] = s2 }
  451. else
  452. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  453. t[i,len] = s2
  454. assert(a(t).index(a(s2)))
  455. if s1.valid_encoding? && s2.valid_encoding?
  456. if i == s1.length && s2.empty?
  457. assert_nil(t[i])
  458. elsif i < 0
  459. if -i < len
  460. len = -i
  461. end
  462. assert_equal(s2, t[i-s2.length+len,s2.length],
  463. "t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i-s2.length+len},#{s2.length}]")
  464. else
  465. assert_equal(s2, t[i,s2.length],
  466. "t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
  467. end
  468. end
  469. end
  470. else
  471. assert_raise(Encoding::CompatibilityError) { t[i,len] = s2 }
  472. end
  473. }
  474. end
  475. def test_str_assign_substr
  476. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  477. t = s1.dup
  478. encs = [
  479. !s1.ascii_only? ? s1.encoding : nil,
  480. !s2.ascii_only? ? s2.encoding : nil,
  481. !s3.ascii_only? ? s3.encoding : nil].uniq.compact
  482. if 1 < encs.length
  483. assert_raise(Encoding::CompatibilityError, IndexError) { t[s2] = s3 }
  484. else
  485. if encs.empty?
  486. encs = [
  487. s1.encoding,
  488. s2.encoding,
  489. s3.encoding].uniq.reject {|e| e == Encoding.find("ASCII-8BIT") }
  490. if encs.empty?
  491. encs = [Encoding.find("ASCII-8BIT")]
  492. end
  493. end
  494. if !t[s2]
  495. else
  496. enccall(t, :[]=, s2, s3)
  497. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding? && s3.valid_encoding?
  498. end
  499. end
  500. }
  501. end
  502. def test_str_assign_range2
  503. combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
  504. t = s1.dup
  505. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  506. if first < -s1.length || s1.length < first
  507. assert_raise(RangeError) { t[first..last] = s2 }
  508. else
  509. enccall(t, :[]=, first..last, s2)
  510. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  511. assert(a(t).index(a(s2)))
  512. if s1.valid_encoding? && s2.valid_encoding?
  513. if first < 0
  514. assert_equal(s2, t[s1.length+first, s2.length])
  515. else
  516. assert_equal(s2, t[first, s2.length])
  517. end
  518. end
  519. end
  520. else
  521. assert_raise(Encoding::CompatibilityError, RangeError,
  522. "t=#{encdump(s1)};t[#{first}..#{last}]=#{encdump(s2)}") {
  523. t[first..last] = s2
  524. }
  525. end
  526. }
  527. end
  528. def test_str_assign_range3
  529. combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
  530. t = s1.dup
  531. if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
  532. if first < -s1.length || s1.length < first
  533. assert_raise(RangeError) { t[first...last] = s2 }
  534. else
  535. enccall(t, :[]=, first...last, s2)
  536. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  537. assert(a(t).index(a(s2)))
  538. if s1.valid_encoding? && s2.valid_encoding?
  539. if first < 0
  540. assert_equal(s2, t[s1.length+first, s2.length])
  541. else
  542. assert_equal(s2, t[first, s2.length])
  543. end
  544. end
  545. end
  546. else
  547. assert_raise(Encoding::CompatibilityError, RangeError,
  548. "t=#{encdump(s1)};t[#{first}...#{last}]=#{encdump(s2)}") {
  549. t[first...last] = s2
  550. }
  551. end
  552. }
  553. end
  554. def test_str_cmp
  555. combination(STRINGS, STRINGS) {|s1, s2|
  556. desc = "#{encdump s1} <=> #{encdump s2}"
  557. r = s1 <=> s2
  558. if s1 == s2
  559. assert_equal(0, r, desc)
  560. else
  561. assert_not_equal(0, r, desc)
  562. end
  563. }
  564. end
  565. def test_str_capitalize
  566. STRINGS.each {|s|
  567. begin
  568. t1 = s.capitalize
  569. rescue ArgumentError
  570. assert(!s.valid_encoding?)
  571. next
  572. end
  573. assert(t1.valid_encoding?) if s.valid_encoding?
  574. assert(t1.casecmp(s))
  575. t2 = s.dup
  576. t2.capitalize!
  577. assert_equal(t1, t2)
  578. r = s.downcase
  579. r = enccall(r, :sub, /\A[a-z]/) {|ch| a(ch).upcase }
  580. assert_equal(r, t1)
  581. }
  582. end
  583. def test_str_casecmp
  584. combination(STRINGS, STRINGS) {|s1, s2|
  585. #puts "#{encdump(s1)}.casecmp(#{encdump(s2)})"
  586. next unless s1.valid_encoding? && s2.valid_encoding? && Encoding.compatible?(s1, s2)
  587. r = s1.casecmp(s2)
  588. assert_equal(s1.upcase <=> s2.upcase, r)
  589. }
  590. end
  591. def test_str_center
  592. combination(STRINGS, [0,1,2,3,10]) {|s1, width|
  593. t = s1.center(width)
  594. assert(a(t).index(a(s1)))
  595. }
  596. combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
  597. if s2.empty?
  598. assert_raise(ArgumentError) { s1.center(width, s2) }
  599. next
  600. end
  601. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  602. assert_raise(Encoding::CompatibilityError) { s1.center(width, s2) }
  603. next
  604. end
  605. t = enccall(s1, :center, width, s2)
  606. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  607. assert(a(t).index(a(s1)))
  608. assert_str_enc_propagation(t, s1, s2) if (t != s1)
  609. }
  610. end
  611. def test_str_ljust
  612. combination(STRINGS, [0,1,2,3,10]) {|s1, width|
  613. t = s1.ljust(width)
  614. assert(a(t).index(a(s1)))
  615. }
  616. combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
  617. if s2.empty?
  618. assert_raise(ArgumentError) { s1.ljust(width, s2) }
  619. next
  620. end
  621. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  622. assert_raise(Encoding::CompatibilityError) { s1.ljust(width, s2) }
  623. next
  624. end
  625. t = enccall(s1, :ljust, width, s2)
  626. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  627. assert(a(t).index(a(s1)))
  628. assert_str_enc_propagation(t, s1, s2) if (t != s1)
  629. }
  630. end
  631. def test_str_rjust
  632. combination(STRINGS, [0,1,2,3,10]) {|s1, width|
  633. t = s1.rjust(width)
  634. assert(a(t).index(a(s1)))
  635. }
  636. combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
  637. if s2.empty?
  638. assert_raise(ArgumentError) { s1.rjust(width, s2) }
  639. next
  640. end
  641. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  642. assert_raise(Encoding::CompatibilityError) { s1.rjust(width, s2) }
  643. next
  644. end
  645. t = enccall(s1, :rjust, width, s2)
  646. assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
  647. assert(a(t).index(a(s1)))
  648. assert_str_enc_propagation(t, s1, s2) if (t != s1)
  649. }
  650. end
  651. def test_str_chomp
  652. combination(STRINGS, STRINGS) {|s1, s2|
  653. if !s1.ascii_only? && !s2.ascii_only? && !Encoding.compatible?(s1,s2)
  654. if s1.bytesize > s2.bytesize
  655. assert_raise(Encoding::CompatibilityError) { s1.chomp(s2) }
  656. end
  657. next
  658. end
  659. t = enccall(s1, :chomp, s2)
  660. assert(t.valid_encoding?, "#{encdump(s1)}.chomp(#{encdump(s2)})") if s1.valid_encoding? && s2.valid_encoding?
  661. assert_equal(s1.encoding, t.encoding)
  662. t2 = s1.dup
  663. t2.chomp!(s2)
  664. assert_equal(t, t2)
  665. }
  666. end
  667. def test_str_chop
  668. STRINGS.each {|s|
  669. s = s.dup
  670. desc = "#{encdump s}.chop"
  671. t = nil
  672. assert_nothing_raised(desc) { t = s.chop }
  673. assert(t.valid_encoding?) if s.valid_encoding?
  674. assert(a(s).index(a(t)))
  675. t2 = s.dup
  676. t2.chop!
  677. assert_equal(t, t2)
  678. }
  679. end
  680. def test_str_clear
  681. STRINGS.each {|s|
  682. t = s.dup
  683. t.clear
  684. assert(t.valid_encoding?)
  685. assert(t.empty?)
  686. }
  687. end
  688. def test_str_clone
  689. STRINGS.each {|s|
  690. t = s.clone
  691. assert_equal(s, t)
  692. assert_equal(s.encoding, t.encoding)
  693. assert_equal(a(s), a(t))
  694. }
  695. end
  696. def test_str_dup
  697. STRINGS.each {|s|
  698. t = s.dup
  699. assert_equal(s, t)
  700. assert_equal(s.encoding, t.encoding)
  701. assert_equal(a(s), a(t))
  702. }
  703. end
  704. def test_str_count
  705. combination(STRINGS, STRINGS) {|s1, s2|
  706. if !s1.valid_encoding? || !s2.valid_encoding?
  707. assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.count(s2) }
  708. next
  709. end
  710. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  711. assert_raise(Encoding::CompatibilityError) { s1.count(s2) }
  712. next
  713. end
  714. n = enccall(s1, :count, s2)
  715. n0 = a(s1).count(a(s2))
  716. assert_operator(n, :<=, n0)
  717. }
  718. end
  719. def test_str_crypt
  720. combination(STRINGS, STRINGS) {|str, salt|
  721. if a(salt).length < 2
  722. assert_raise(ArgumentError) { str.crypt(salt) }
  723. next
  724. end
  725. t = str.crypt(salt)
  726. assert_equal(a(str).crypt(a(salt)), t, "#{encdump(str)}.crypt(#{encdump(salt)})")
  727. assert_encoding('ASCII-8BIT', t.encoding)
  728. }
  729. end
  730. def test_str_delete
  731. combination(STRINGS, STRINGS) {|s1, s2|
  732. if s1.empty?
  733. assert_equal(s1, s1.delete(s2))
  734. next
  735. end
  736. if !s1.valid_encoding? || !s2.valid_encoding?
  737. assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.delete(s2) }
  738. next
  739. end
  740. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  741. assert_raise(Encoding::CompatibilityError) { s1.delete(s2) }
  742. next
  743. end
  744. t = enccall(s1, :delete, s2)
  745. assert(t.valid_encoding?)
  746. assert_equal(t.encoding, s1.encoding)
  747. assert_operator(t.length, :<=, s1.length)
  748. t2 = s1.dup
  749. t2.delete!(s2)
  750. assert_equal(t, t2)
  751. }
  752. end
  753. def test_str_downcase
  754. STRINGS.each {|s|
  755. if !s.valid_encoding?
  756. assert_raise(ArgumentError) { s.downcase }
  757. next
  758. end
  759. t = s.downcase
  760. assert(t.valid_encoding?)
  761. assert_equal(t.encoding, s.encoding)
  762. assert(t.casecmp(s))
  763. t2 = s.dup
  764. t2.downcase!
  765. assert_equal(t, t2)
  766. }
  767. end
  768. def test_str_dump
  769. STRINGS.each {|s|
  770. t = s.dump
  771. assert(t.valid_encoding?)
  772. assert(t.ascii_only?)
  773. u = eval(t)
  774. assert_equal(a(s), a(u))
  775. }
  776. end
  777. def test_str_each_line
  778. combination(STRINGS, STRINGS) {|s1, s2|
  779. if !s1.valid_encoding? || !s2.valid_encoding?
  780. assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.each_line(s2) {} }
  781. next
  782. end
  783. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  784. assert_raise(Encoding::CompatibilityError) { s1.each_line(s2) {} }
  785. next
  786. end
  787. lines = []
  788. enccall(s1, :each_line, s2) {|line|
  789. assert(line.valid_encoding?)
  790. assert_equal(s1.encoding, line.encoding)
  791. lines << line
  792. }
  793. next if lines.size == 0
  794. s2 = lines.join('')
  795. assert_equal(s1.encoding, s2.encoding)
  796. assert_equal(s1, s2)
  797. }
  798. end
  799. def test_str_each_byte
  800. STRINGS.each {|s|
  801. bytes = []
  802. s.each_byte {|b|
  803. bytes << b
  804. }
  805. a(s).split(//).each_with_index {|ch, i|
  806. assert_equal(ch.ord, bytes[i])
  807. }
  808. }
  809. end
  810. def test_str_empty?
  811. STRINGS.each {|s|
  812. if s.length == 0
  813. assert(s.empty?)
  814. else
  815. assert(!s.empty?)
  816. end
  817. }
  818. end
  819. def test_str_hex
  820. STRINGS.each {|s|
  821. t = s.hex
  822. t2 = a(s)[/\A[0-9a-fA-Fx]*/].hex
  823. assert_equal(t2, t)
  824. }
  825. end
  826. def test_str_include?
  827. combination(STRINGS, STRINGS) {|s1, s2|
  828. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  829. assert_raise(Encoding::CompatibilityError) { s1.include?(s2) }
  830. assert_raise(Encoding::CompatibilityError) { s1.index(s2) }
  831. assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) }
  832. next
  833. end
  834. t = enccall(s1, :include?, s2)
  835. if t
  836. assert(a(s1).include?(a(s2)))
  837. assert(s1.index(s2))
  838. assert(s1.rindex(s2))
  839. else
  840. assert(!s1.index(s2))
  841. assert(!s1.rindex(s2), "!#{encdump(s1)}.rindex(#{encdump(s2)})")
  842. end
  843. if s2.empty?
  844. assert_equal(true, t)
  845. next
  846. end
  847. if !s1.valid_encoding? || !s2.valid_encoding?
  848. assert_equal(false, t, "#{encdump s1}.include?(#{encdump s2})")
  849. next
  850. end
  851. if t && s1.valid_encoding? && s2.valid_encoding?
  852. assert_match(/#{Regexp.escape(s2)}/, s1)
  853. else
  854. assert_no_match(/#{Regexp.escape(s2)}/, s1)
  855. end
  856. }
  857. end
  858. def test_str_index
  859. combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos|
  860. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  861. assert_raise(Encoding::CompatibilityError) { s1.index(s2) }
  862. next
  863. end
  864. t = enccall(s1, :index, s2, pos)
  865. if s2.empty?
  866. if pos < 0 && pos+s1.length < 0
  867. assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
  868. elsif pos < 0
  869. assert_equal(s1.length+pos, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
  870. elsif s1.length < pos
  871. assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
  872. else
  873. assert_equal(pos, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
  874. end
  875. next
  876. end
  877. if !s1.valid_encoding? || !s2.valid_encoding?
  878. assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
  879. next
  880. end
  881. if t
  882. re = /#{Regexp.escape(s2)}/
  883. assert(re.match(s1, pos))
  884. assert_equal($`.length, t, "#{encdump s1}.index(#{encdump s2}, #{pos})")
  885. else
  886. assert_no_match(/#{Regexp.escape(s2)}/, s1[pos..-1])
  887. end
  888. }
  889. end
  890. def test_str_rindex
  891. combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos|
  892. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  893. assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) }
  894. next
  895. end
  896. t = enccall(s1, :rindex, s2, pos)
  897. if s2.empty?
  898. if pos < 0 && pos+s1.length < 0
  899. assert_equal(nil, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  900. elsif pos < 0
  901. assert_equal(s1.length+pos, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  902. elsif s1.length < pos
  903. assert_equal(s1.length, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  904. else
  905. assert_equal(pos, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  906. end
  907. next
  908. end
  909. if !s1.valid_encoding? || !s2.valid_encoding?
  910. assert_equal(nil, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  911. next
  912. end
  913. if t
  914. #puts "#{encdump s1}.rindex(#{encdump s2}, #{pos}) => #{t}"
  915. assert(a(s1).index(a(s2)))
  916. pos2 = pos
  917. pos2 += s1.length if pos < 0
  918. re = /\A(.{0,#{pos2}})#{Regexp.escape(s2)}/m
  919. m = enccall(re, :match, s1)
  920. assert(m, "#{re.inspect}.match(#{encdump(s1)})")
  921. assert_equal(m[1].length, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
  922. else
  923. re = /#{Regexp.escape(s2)}/
  924. n = re =~ s1
  925. if n
  926. if pos < 0
  927. assert_operator(n, :>, s1.length+pos)
  928. else
  929. assert_operator(n, :>, pos)
  930. end
  931. end
  932. end
  933. }
  934. end
  935. def test_str_insert
  936. combination(STRINGS, 0..2, STRINGS) {|s1, nth, s2|
  937. t1 = s1.dup
  938. t2 = s1.dup
  939. begin
  940. t1[nth, 0] = s2
  941. rescue Encoding::CompatibilityError, IndexError => e1
  942. end
  943. begin
  944. t2.insert(nth, s2)
  945. rescue Encoding::CompatibilityError, IndexError => e2
  946. end
  947. assert_equal(t1, t2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t")
  948. assert_equal(e1.class, e2.class, "begin #{encdump s1}.insert(#{nth},#{encdump s2}); rescue ArgumentError, IndexError => e; e end")
  949. }
  950. combination(STRINGS, -2..-1, STRINGS) {|s1, nth, s2|
  951. next if s1.length + nth < 0
  952. next unless s1.valid_encoding?
  953. next unless s2.valid_encoding?
  954. t1 = s1.dup
  955. begin
  956. t1.insert(nth, s2)
  957. slen = s2.length
  958. assert_equal(t1[nth-slen+1,slen], s2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t")
  959. rescue Encoding::CompatibilityError, IndexError
  960. end
  961. }
  962. end
  963. def test_str_intern
  964. STRINGS.each {|s|
  965. if /\0/ =~ a(s)
  966. assert_raise(ArgumentError) { s.intern }
  967. elsif s.valid_encoding?
  968. sym = s.intern
  969. assert_equal(s, sym.to_s, "#{encdump s}.intern.to_s")
  970. assert_equal(sym, s.to_sym)
  971. else
  972. assert_raise(EncodingError) { s.intern }
  973. end
  974. }
  975. end
  976. def test_str_length
  977. STRINGS.each {|s|
  978. assert_operator(s.length, :<=, s.bytesize)
  979. }
  980. end
  981. def test_str_oct
  982. STRINGS.each {|s|
  983. t = s.oct
  984. t2 = a(s)[/\A[0-9a-fA-FxX]*/].oct
  985. assert_equal(t2, t)
  986. }
  987. end
  988. def test_str_replace
  989. combination(STRINGS, STRINGS) {|s1, s2|
  990. t = s1.dup
  991. t.replace s2
  992. assert_equal(s2, t)
  993. assert_equal(s2.encoding, t.encoding)
  994. }
  995. end
  996. def test_str_reverse
  997. STRINGS.each {|s|
  998. t = s.reverse
  999. assert_equal(s.bytesize, t.bytesize)
  1000. if !s.valid_encoding?
  1001. assert_operator(t.length, :<=, s.length)
  1002. next
  1003. end
  1004. assert_equal(s, t.reverse)
  1005. }
  1006. end
  1007. def test_str_scan
  1008. combination(STRINGS, STRINGS) {|s1, s2|
  1009. if !s2.valid_encoding?
  1010. assert_raise(RegexpError) { s1.scan(s2) }
  1011. next
  1012. end
  1013. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  1014. if s1.valid_encoding?
  1015. assert_raise(Encoding::CompatibilityError) { s1.scan(s2) }
  1016. else
  1017. assert_match(/invalid byte sequence/, assert_raise(ArgumentError) { s1.scan(s2) }.message)
  1018. end
  1019. next
  1020. end
  1021. if !s1.valid_encoding?
  1022. assert_raise(ArgumentError) { s1.scan(s2) }
  1023. next
  1024. end
  1025. r = enccall(s1, :scan, s2)
  1026. r.each {|t|
  1027. assert_equal(s2, t)
  1028. }
  1029. }
  1030. end
  1031. def test_str_slice
  1032. each_slice_call {|obj, *args|
  1033. assert_same_result(lambda { obj[*args] }, lambda { obj.slice(*args) })
  1034. }
  1035. end
  1036. def test_str_slice!
  1037. each_slice_call {|s, *args|
  1038. desc_slice = "#{encdump s}.slice#{encdumpargs args}"
  1039. desc_slice_bang = "#{encdump s}.slice!#{encdumpargs args}"
  1040. t = s.dup
  1041. begin
  1042. r = t.slice!(*args)
  1043. rescue
  1044. e = $!
  1045. end
  1046. if e
  1047. assert_raise(e.class, desc_slice) { s.slice(*args) }
  1048. next
  1049. end
  1050. if !r
  1051. assert_nil(s.slice(*args), desc_slice)
  1052. next
  1053. end
  1054. assert_equal(s.slice(*args), r, desc_slice_bang)
  1055. assert_equal(s.bytesize, r.bytesize + t.bytesize)
  1056. if args.length == 1 && String === args[0]
  1057. assert_equal(args[0].encoding, r.encoding,
  1058. "#{encdump s}.slice!#{encdumpargs args}.encoding")
  1059. else
  1060. assert_equal(s.encoding, r.encoding,
  1061. "#{encdump s}.slice!#{encdumpargs args}.encoding")
  1062. end
  1063. if [s, *args].all? {|o| !(String === o) || o.valid_encoding? }
  1064. assert(r.valid_encoding?)
  1065. assert(t.valid_encoding?)
  1066. assert_equal(s.length, r.length + t.length)
  1067. end
  1068. }
  1069. end
  1070. def test_str_split
  1071. combination(STRINGS, STRINGS) {|s1, s2|
  1072. if !s2.valid_encoding?
  1073. assert_raise(ArgumentError, RegexpError) { s1.split(s2) }
  1074. next
  1075. end
  1076. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  1077. assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.split(s2) }
  1078. next
  1079. end
  1080. if !s1.valid_encoding?
  1081. assert_raise(ArgumentError) { s1.split(s2) }
  1082. next
  1083. end
  1084. t = enccall(s1, :split, s2)
  1085. t.each {|r|
  1086. assert(a(s1).include?(a(r)))
  1087. assert_equal(s1.encoding, r.encoding)
  1088. }
  1089. assert(a(s1).include?(t.map {|u| a(u) }.join(a(s2))))
  1090. if s1.valid_encoding? && s2.valid_encoding?
  1091. t.each {|r|
  1092. assert(r.valid_encoding?)
  1093. }
  1094. end
  1095. }
  1096. end
  1097. def test_str_squeeze
  1098. combination(STRINGS, STRINGS) {|s1, s2|
  1099. if !s1.valid_encoding? || !s2.valid_encoding?
  1100. assert_raise(ArgumentError, Encoding::CompatibilityError, "#{encdump s1}.squeeze(#{encdump s2})") { s1.squeeze(s2) }
  1101. next
  1102. end
  1103. if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
  1104. assert_raise(Encoding::CompatibilityError) { s1.squeeze(s2) }
  1105. next
  1106. end
  1107. t = enccall(s1, :squeeze, s2)
  1108. assert_operator(t.length, :<=, s1.length)
  1109. t2 = s1.dup
  1110. t2.squeeze!(s2)
  1111. assert_equal(t, t2)
  1112. }
  1113. end
  1114. def test_str_strip
  1115. STRINGS.each {|s|
  1116. if !s.valid_encoding?
  1117. assert_raise(ArgumentError, "#{encdump s}.strip") { s.strip }
  1118. next
  1119. end
  1120. t = s.strip
  1121. l = s.lstrip
  1122. r = s.rstrip
  1123. assert_operator(l.length, :<=, s.length)
  1124. assert_operator(r.length, :<=, s.length)
  1125. assert_operator(t.length, :<=, l.length)
  1126. assert_operator(t.length, :<=, r.length)
  1127. t2 = s.dup
  1128. t2.strip!
  1129. assert_equal(t, t2)
  1130. l2 = s.dup
  1131. l2.lstrip!
  1132. assert_equal(l, l2)
  1133. r2 = s.dup
  1134. r2.rstrip!
  1135. assert_equal(r, r2)
  1136. }
  1137. end
  1138. def test_str_sum
  1139. STRINGS.each {|s|
  1140. assert_equal(a(s).sum, s.sum)
  1141. }
  1142. end
  1143. def test_str_swapcase
  1144. STRINGS.each {|s|
  1145. if !s.valid_encoding?
  1146. assert_raise(ArgumentError, "#{encdump s}.swapcase") { s.swapcase }
  1147. next
  1148. end
  1149. t1 = s.swapcase
  1150. assert(t1.valid_encoding?) if s.valid_encoding?
  1151. assert(t1.casecmp(s))
  1152. t2 = s.dup
  1153. t2.swapcase!
  1154. assert_equal(t1, t2)
  1155. t3 = t1.swapcase
  1156. assert_equal(s, t3);
  1157. }
  1158. end
  1159. def test_str_to_f
  1160. STRINGS.each {|s|
  1161. assert_nothing_raised { s.to_f }
  1162. }
  1163. end
  1164. def test_str_to_i
  1165. STRINGS.each {|s|
  1166. assert_nothing_raised { s.to_i }
  1167. 2.upto(36) {|radix|
  1168. assert_nothing_raised { s.to_i(radix) }
  1169. }
  1170. }
  1171. end
  1172. def test_str_to_s
  1173. STRINGS.each {|s|
  1174. assert_same(s, s.to_s)
  1175. assert_same(s, s.to_str)
  1176. }
  1177. end
  1178. def test_tr
  1179. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  1180. desc = "#{encdump s1}.tr(#{encdump s2}, #{encdump s3})"
  1181. if s1.empty?
  1182. assert_equal(s1, s1.tr(s2, s3), desc)
  1183. next
  1184. end
  1185. if !str_enc_compatible?(s1, s2, s3)
  1186. assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) }
  1187. next
  1188. end
  1189. if !s1.valid_encoding?
  1190. assert_raise(ArgumentError, desc) { s1.tr(s2, s3) }
  1191. next
  1192. end
  1193. if s2.empty?
  1194. t = enccall(s1, :tr, s2, s3)
  1195. assert_equal(s1, t, desc)
  1196. next
  1197. end
  1198. if !s2.valid_encoding? || !s3.valid_encoding?
  1199. assert_raise(ArgumentError, desc) { s1.tr(s2, s3) }
  1200. next
  1201. end
  1202. t = enccall(s1, :tr, s2, s3)
  1203. assert_operator(s1.length, :>=, t.length, desc)
  1204. }
  1205. end
  1206. def test_tr_s
  1207. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  1208. desc = "#{encdump s1}.tr_s(#{encdump s2}, #{encdump s3})"
  1209. if s1.empty?
  1210. assert_equal(s1, s1.tr_s(s2, s3), desc)
  1211. next
  1212. end
  1213. if !s1.valid_encoding?
  1214. assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.tr_s(s2, s3) }
  1215. next
  1216. end
  1217. if !str_enc_compatible?(s1, s2, s3)
  1218. assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) }
  1219. next
  1220. end
  1221. if s2.empty?
  1222. t = enccall(s1, :tr_s, s2, s3)
  1223. assert_equal(s1, t, desc)
  1224. next
  1225. end
  1226. if !s2.valid_encoding? || !s3.valid_encoding?
  1227. assert_raise(ArgumentError, desc) { s1.tr_s(s2, s3) }
  1228. next
  1229. end
  1230. t = enccall(s1, :tr_s, s2, s3)
  1231. assert_operator(s1.length, :>=, t.length, desc)
  1232. }
  1233. end
  1234. def test_str_upcase
  1235. STRINGS.each {|s|
  1236. desc = "#{encdump s}.upcase"
  1237. if !s.valid_encoding?
  1238. assert_raise(ArgumentError, desc) { s.upcase }
  1239. next
  1240. end
  1241. t1 = s.upcase
  1242. assert(t1.valid_encoding?)
  1243. assert(t1.casecmp(s))
  1244. t2 = s.dup
  1245. t2.upcase!
  1246. assert_equal(t1, t2)
  1247. }
  1248. end
  1249. def test_str_succ
  1250. STRINGS.each {|s0|
  1251. next if s0.empty?
  1252. s = s0.dup
  1253. n = 300
  1254. h = {}
  1255. n.times {|i|
  1256. if h[s]
  1257. assert(false, "#{encdump s} cycle with succ #{i-h[s]} times")
  1258. end
  1259. h[s] = i
  1260. assert_operator(s.length, :<=, s0.length + Math.log2(i+1) + 1, "#{encdump s0} succ #{i} times => #{encdump s}")
  1261. #puts encdump(s)
  1262. t = s.succ
  1263. if s.valid_encoding?
  1264. assert(t.valid_encoding?, "#{encdump s}.succ.valid_encoding?")
  1265. end
  1266. s = t
  1267. }
  1268. }
  1269. end
  1270. def test_str_hash
  1271. combination(STRINGS, STRINGS) {|s1, s2|
  1272. if s1.eql?(s2)
  1273. assert_equal(s1.hash, s2.hash, "#{encdump s1}.hash == #{encdump s2}.dump")
  1274. end
  1275. }
  1276. end
  1277. def test_marshal
  1278. STRINGS.each {|s|
  1279. m = Marshal.dump(s)
  1280. t = Marshal.load(m)
  1281. assert_equal(s, t)
  1282. }
  1283. end
  1284. def test_str_sub
  1285. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  1286. if !s2.valid_encoding?
  1287. assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) }
  1288. next
  1289. end
  1290. r2 = Regexp.new(Regexp.escape(s2))
  1291. [
  1292. [
  1293. "#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})",
  1294. lambda { s1.sub(r2, s3) },
  1295. false
  1296. ],
  1297. [
  1298. "#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})",
  1299. lambda { s1.sub(r2) { s3 } },
  1300. false
  1301. ],
  1302. [
  1303. "#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})",
  1304. lambda { s1.gsub(r2, s3) },
  1305. true
  1306. ],
  1307. [
  1308. "#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})",
  1309. lambda { s1.gsub(r2) { s3 } },
  1310. true
  1311. ]
  1312. ].each {|desc, doit, g|
  1313. if !s1.valid_encoding?
  1314. assert_raise(ArgumentError, desc) { doit.call }
  1315. next
  1316. end
  1317. if !str_enc_compatible?(s1, s2)
  1318. assert_raise(Encoding::CompatibilityError, desc) { doit.call }
  1319. next
  1320. end
  1321. if !enccall(s1, :include?, s2)
  1322. assert_equal(s1, doit.call)
  1323. next
  1324. end
  1325. if !str_enc_compatible?(g ? s1.gsub(r2, '') : s1.sub(r2, ''), s3)
  1326. assert_raise(Encoding::CompatibilityError, desc) { doit.call }
  1327. next
  1328. end
  1329. t = nil
  1330. assert_nothing_raised(desc) {
  1331. t = doit.call
  1332. }
  1333. if s2 == s3
  1334. assert_equal(s1, t, desc)
  1335. else
  1336. assert_not_equal(s1, t, desc)
  1337. end
  1338. }
  1339. }
  1340. end
  1341. def test_str_sub!
  1342. combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
  1343. if !s2.valid_encoding?
  1344. assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) }
  1345. next
  1346. end
  1347. r2 = Regexp.new(Regexp.escape(s2))
  1348. [
  1349. [
  1350. "t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})",
  1351. lambda { t=s1.dup; [t, t.sub!(r2, s3)] },
  1352. false
  1353. ],
  1354. [
  1355. "t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})",
  1356. lambda { t=s1.dup; [t, t.sub!(r2) { s3 }] },
  1357. false
  1358. ],
  1359. [
  1360. "t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})",
  1361. lambda { t=s1.dup; [t, t.gsub!(r2, s3)] },
  1362. true
  1363. ],
  1364. [
  1365. "t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})",
  1366. lambda { t=s1.dup; [t, t.gsub!(r2) { s3 }] },
  1367. true
  1368. ]
  1369. ].each {|desc, doit, g|
  1370. if !s1.valid_encoding?
  1371. assert_raise(ArgumentError, desc) { doit.call }
  1372. next
  1373. end
  1374. if !str_enc_compatible?(s1, s2)
  1375. assert_raise(Encoding::CompatibilityError, desc) { doit.call }
  1376. next
  1377. end
  1378. if !enccall(s1, :include?, s2)
  1379. assert_equal([s1, nil], doit.call)
  1380. next
  1381. end
  1382. if !str_enc_compatible?(g ? s1.gsub(r2, '') : s1.sub(r2, ''), s3)
  1383. assert_raise(Encoding::CompatibilityError, desc) { doit.call }
  1384. next
  1385. end
  1386. t = ret = nil
  1387. assert_nothing_raised(desc) {
  1388. t, ret = doit.call
  1389. }
  1390. assert(ret)
  1391. if s2 == s3
  1392. assert_equal(s1, t, desc)
  1393. else
  1394. assert_not_equal(s1, t, desc)
  1395. end
  1396. }
  1397. }
  1398. end
  1399. def test_str_bytes
  1400. STRINGS.each {|s1|
  1401. ary = []
  1402. s1.bytes.each {|b|
  1403. ary << b
  1404. }
  1405. assert_equal(s1.unpack("C*"), ary)
  1406. }
  1407. end
  1408. def test_str_bytesize
  1409. STRINGS.each {|s1|
  1410. assert_equal(s1.unpack("C*").length, s1.bytesize)
  1411. }
  1412. end
  1413. def test_str_chars
  1414. STRINGS.each {|s1|
  1415. ary = []
  1416. s1.chars.each {|c|
  1417. ary << c
  1418. }
  1419. expected = []
  1420. s1.length.times {|i|
  1421. expected << s1[i]
  1422. }
  1423. assert_equal(expected, ary)
  1424. }
  1425. end
  1426. def test_str_chr
  1427. STRINGS.each {|s1|
  1428. if s1.empty?
  1429. assert_equal("", s1.chr)
  1430. next
  1431. end
  1432. assert_equal(s1[0], s1.chr)
  1433. }
  1434. end
  1435. def test_str_end_with?
  1436. combination(STRINGS, STRINGS) {|s1, s2|
  1437. desc = "#{encdump s1}.end_with?(#{encdump s2})"
  1438. if !str_enc_compatible?(s1, s2)
  1439. assert_raise(Encoding::CompatibilityError, desc) { s1.end_with?(s2) }
  1440. next
  1441. end
  1442. if s1.length < s2.length
  1443. assert_equal(false, enccall(s1, :end_with?, s2), desc)
  1444. next
  1445. end
  1446. if s1[s1.length-s2.length, s2.length] == s2
  1447. assert_equal(true, enccall(s1, :end_with?, s2), desc)
  1448. next
  1449. end
  1450. assert_equal(false, enccall(s1, :end_with?, s2), desc)
  1451. }
  1452. end
  1453. def test_str_start_with?
  1454. combination(STRINGS, STRINGS) {|s1, s2|
  1455. desc = "#{encdump s1}.start_with?(#{encdump s2})"
  1456. if !str_enc_compatible?(s1, s2)
  1457. assert_raise(Encoding::CompatibilityError, desc) { s1.start_with?(s2) }
  1458. next
  1459. end
  1460. s1 = s1.dup.force_encoding("ASCII-8BIT")
  1461. s2 = s2.dup.force_encoding("ASCII-8BIT")
  1462. if s1.length < s2.length
  1463. assert_equal(false, enccall(s1, :start_with?, s2), desc)
  1464. next
  1465. end
  1466. if s1[0, s2.length] == s2
  1467. assert_equal(true, enccall(s1, :start_with?, s2), desc)
  1468. next
  1469. end
  1470. assert_equal(false, enccall(s1, :start_with?, s2), desc)
  1471. }
  1472. end
  1473. def test_str_ord
  1474. STRINGS.each {|s1|
  1475. if s1.empty?
  1476. assert_raise(ArgumentError) { s1.ord }
  1477. next
  1478. end
  1479. if !s1.valid_encoding?
  1480. assert_raise(ArgumentError) { s1.ord }
  1481. next
  1482. end
  1483. assert_equal(s1[0].ord, s1.ord)
  1484. }
  1485. end
  1486. def test_str_partition
  1487. combination(STRINGS, STRINGS) {|s1, s2|
  1488. desc = "#{encdump s1}.partition(#{encdump s2})"
  1489. if !str_enc_compatible?(s1, s2)
  1490. assert_raise(Encoding::CompatibilityError, desc) { s1.partition(s2) }
  1491. next
  1492. end
  1493. i = enccall(s1, :index, s2)
  1494. if !i
  1495. assert_equal([s1, "", ""], s1.partition(s2), desc)
  1496. next
  1497. end
  1498. assert_equal([s1[0,i], s2, s1[(i+s2.length)..-1]], s1.partition(s2), desc)
  1499. }
  1500. end
  1501. def test_str_rpartition
  1502. combination(STRINGS, STRINGS) {|s1, s2|
  1503. desc = "#{encdump s1}.rpartition(#{encdump s2})"
  1504. if !str_enc_compatible?(s1, s2)
  1505. assert_raise(Encoding::CompatibilityError, desc) { s1.rpartition(s2) }
  1506. next
  1507. end
  1508. i = enccall(s1, :rindex, s2)
  1509. if !i
  1510. assert_equal(["", "", s1], s1.rpartition(s2), desc)
  1511. next
  1512. end
  1513. assert_equal([s1[0,i], s2, s1[(i+s2.length)..-1]], s1.rpartition(s2), desc)
  1514. }
  1515. end
  1516. end