diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/-ext-/string |
Fresh start
Diffstat (limited to 'jni/ruby/test/-ext-/string')
-rw-r--r-- | jni/ruby/test/-ext-/string/test_coderange.rb | 59 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_cstr.rb | 119 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_ellipsize.rb | 46 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_enc_associate.rb | 12 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_enc_str_buf_cat.rb | 15 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_modify_expand.rb | 15 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_nofree.rb | 10 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_normalize.rb | 106 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_qsort.rb | 19 | ||||
-rw-r--r-- | jni/ruby/test/-ext-/string/test_set_len.rb | 25 |
10 files changed, 426 insertions, 0 deletions
diff --git a/jni/ruby/test/-ext-/string/test_coderange.rb b/jni/ruby/test/-ext-/string/test_coderange.rb new file mode 100644 index 0000000..83cebf1 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_coderange.rb @@ -0,0 +1,59 @@ +# coding: ascii-8bit +require 'test/unit' +require "-test-/string/string" +require "rbconfig/sizeof" + +class Test_StringCoderange < Test::Unit::TestCase + def setup + @sizeof_voidp = RbConfig::SIZEOF["void*"] + @a8 = Encoding::ASCII_8BIT + @a7 = Encoding::US_ASCII + @u8 = Encoding::UTF_8 + end + + def test_ascii8bit + enc = @a8 + str = "a" + str.force_encoding(enc) + assert_equal :"7bit", Bug::String.new(str).coderange_scan + + str = "a\xBE".force_encoding(enc) + assert_equal :valid, Bug::String.new(str).coderange_scan + end + + def test_usascii + enc = @a7 + str = "a" + str.force_encoding(enc) + assert_equal :"7bit", Bug::String.new(str).coderange_scan + + str = "a" * (@sizeof_voidp * 2) + str << "\xBE" + str.force_encoding(enc) + assert_equal :broken, Bug::String.new(str).coderange_scan + end + + def test_utf8 + enc = @u8 + str = "a" + str.force_encoding(enc) + assert_equal :"7bit", Bug::String.new(str).coderange_scan + + str = "a" * (@sizeof_voidp * 3) + str << "aa\xC2\x80" + str.force_encoding(enc) + assert_equal :valid, Bug::String.new(str).coderange_scan + + str = "a" * (@sizeof_voidp * 2) + str << "\xC2\x80" + str << "a" * (@sizeof_voidp * 2) + str.force_encoding(enc) + assert_equal :valid, Bug::String.new(str).coderange_scan + + str = "a" * (@sizeof_voidp * 2) + str << "\xC1\x80" + str << "a" * (@sizeof_voidp * 2) + str.force_encoding(enc) + assert_equal :broken, Bug::String.new(str).coderange_scan + end +end diff --git a/jni/ruby/test/-ext-/string/test_cstr.rb b/jni/ruby/test/-ext-/string/test_cstr.rb new file mode 100644 index 0000000..272e090 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_cstr.rb @@ -0,0 +1,119 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StringCStr < Test::Unit::TestCase + Bug4319 = '[ruby-dev:43094]' + + def test_embed + s = Bug::String.new("abcdef") + s.set_len(3) + assert_equal(0, s.cstr_term, Bug4319) + end + + def test_long + s = Bug::String.new("abcdef")*100000 + assert_equal(0, s.cstr_term, Bug4319) + end + + WCHARS = [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE] + + def test_wchar_embed + WCHARS.each do |enc| + s = Bug::String.new("\u{4022}a".encode(enc)) + assert_nothing_raised(ArgumentError) {s.cstr_term} + s.set_len(s.bytesize / 2) + assert_equal(1, s.size) + assert_equal(0, s.cstr_term) + end + end + + def test_wchar_long + str = "\u{4022}abcdef" + n = 100 + len = str.size * n + WCHARS.each do |enc| + s = Bug::String.new(str.encode(enc))*n + assert_nothing_raised(ArgumentError, enc.name) {s.cstr_term} + s.set_len(s.bytesize / 2) + assert_equal(len / 2, s.size, enc.name) + assert_equal(0, s.cstr_term, enc.name) + end + end + + def test_wchar_lstrip! + assert_wchars_term_char(" a") {|s| s.lstrip!} + end + + def test_wchar_rstrip! + assert_wchars_term_char("a ") {|s| s.rstrip!} + end + + def test_wchar_chop! + assert_wchars_term_char("a\n") {|s| s.chop!} + end + + def test_wchar_chomp! + assert_wchars_term_char("a\n") {|s| s.chomp!} + end + + def test_wchar_aset + assert_wchars_term_char("a"*30) {|s| s[29,1] = ""} + end + + def test_wchar_sub! + assert_wchars_term_char("foobar") {|s| s.sub!(/#{"foo".encode(s.encoding)}/, "")} + end + + def test_wchar_delete! + assert_wchars_term_char("foobar") {|s| s.delete!("ao".encode(s.encoding))} + end + + def test_wchar_squeeze! + assert_wchars_term_char("foo!") {|s| s.squeeze!} + end + + def test_wchar_tr! + assert_wchars_term_char("\u{3042}foobar") {|s| + enc = s.encoding + s.tr!("\u{3042}".encode(enc), "c".encode(enc)) + } + end + + def test_wchar_tr_s! + assert_wchars_term_char("\u{3042}foobar") {|s| + enc = s.encoding + s.tr_s!("\u{3042}".encode(enc), "c".encode(enc)) + } + end + + def test_embedded_from_heap + gh821 = "[GH-821]" + embedded_string = "abcdefghi" + string = embedded_string.gsub("efg", "123") + {}[string] = 1 + non_terminated = "#{string}#{nil}" + assert_nil(Bug::String.cstr_term_char(non_terminated), gh821) + + result = {} + WCHARS.map do |enc| + embedded_string = "ab".encode(enc) + string = embedded_string.gsub("b".encode(enc), "1".encode(enc)) + {}[string] = 1 + non_terminated = "#{string}#{nil}" + c = Bug::String.cstr_term_char(non_terminated) + result[enc] = c if c + end + assert_empty(result, gh821) + end + + def assert_wchars_term_char(str) + result = {} + WCHARS.map do |enc| + s = Bug::String.new(str.encode(enc)) + yield s + c = s.cstr_term_char + result[enc] = c if c + end + assert_empty(result) + end +end diff --git a/jni/ruby/test/-ext-/string/test_ellipsize.rb b/jni/ruby/test/-ext-/string/test_ellipsize.rb new file mode 100644 index 0000000..2c14c0c --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_ellipsize.rb @@ -0,0 +1,46 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StringEllipsize < Test::Unit::TestCase + def setup + @foobar = Bug::String.new("foobar") + end + + def assert_equal_with_class(expected, result, *rest) + assert_equal(expected.encoding, result.encoding, *rest) + assert_equal(expected, result, result.encoding.name) + assert_instance_of(Bug::String, result, *rest) + end + + def test_longer + assert_equal_with_class("", @foobar.ellipsize(0)) + assert_equal_with_class(".", @foobar.ellipsize(1)) + assert_equal_with_class("..", @foobar.ellipsize(2)) + assert_equal_with_class("...", @foobar.ellipsize(3)) + assert_equal_with_class("f...", @foobar.ellipsize(4)) + assert_equal_with_class("fo...", @foobar.ellipsize(5)) + end + + def test_shorter + assert_same(@foobar, @foobar.ellipsize(6)) + assert_same(@foobar, @foobar.ellipsize(7)) + end + + def test_negative_length + assert_raise(IndexError) {@foobar.ellipsize(-1)} + end + + def test_nonascii + a = "\u3042" + Encoding.list.each do |enc| + next if enc.dummy? + begin + s = a.encode(enc) + e = "...".encode(enc) + rescue + else + assert_equal_with_class(s*12+e, Bug::String.new(s*20).ellipsize(15)) + end + end + end +end diff --git a/jni/ruby/test/-ext-/string/test_enc_associate.rb b/jni/ruby/test/-ext-/string/test_enc_associate.rb new file mode 100644 index 0000000..edddfb4 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_enc_associate.rb @@ -0,0 +1,12 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StrEncAssociate < Test::Unit::TestCase + def test_frozen + s = Bug::String.new("abc") + s.force_encoding(Encoding::US_ASCII) + s.freeze + assert_raise(RuntimeError) {s.associate_encoding!(Encoding::US_ASCII)} + assert_raise(RuntimeError) {s.associate_encoding!(Encoding::UTF_8)} + end +end diff --git a/jni/ruby/test/-ext-/string/test_enc_str_buf_cat.rb b/jni/ruby/test/-ext-/string/test_enc_str_buf_cat.rb new file mode 100644 index 0000000..9582fe2 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_enc_str_buf_cat.rb @@ -0,0 +1,15 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StringEncStrBufCat < Test::Unit::TestCase + Bug6509 = '[ruby-dev:45688]' + + def test_unknown + a8_str = "a\xBE".force_encoding(Encoding::ASCII_8BIT) + cr_unknown_str = [0x62].pack('C*') + assert_equal(true, a8_str.valid_encoding?, "an assertion for following tests") + assert_equal(:valid, Bug::String.new(a8_str).coderange, "an assertion for following tests") + assert_equal(:unknown, Bug::String.new(cr_unknown_str).coderange, "an assertion for following tests") + assert_equal(:valid, Bug::String.new(a8_str).enc_str_buf_cat(cr_unknown_str).coderange, Bug6509) + end +end diff --git a/jni/ruby/test/-ext-/string/test_modify_expand.rb b/jni/ruby/test/-ext-/string/test_modify_expand.rb new file mode 100644 index 0000000..34b7be7 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_modify_expand.rb @@ -0,0 +1,15 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StringModifyExpand < Test::Unit::TestCase + def test_modify_expand_memory_leak + assert_no_memory_leak(["-r-test-/string/string"], + <<-PRE, <<-CMD, "rb_str_modify_expand()", limit: 2.5) + s=Bug::String.new + PRE + size = $initial_size + 10.times{s.modify_expand!(size)} + s.replace("") + CMD + end +end diff --git a/jni/ruby/test/-ext-/string/test_nofree.rb b/jni/ruby/test/-ext-/string/test_nofree.rb new file mode 100644 index 0000000..234c84d --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_nofree.rb @@ -0,0 +1,10 @@ +require 'test/unit' + +class Test_StringNoFree < Test::Unit::TestCase + def test_no_memory_leak + bug10942 = '[ruby-core:68436] [Bug #10942] no leak on nofree string' + assert_no_memory_leak(%w(-r-test-/string/string), '', + '1000000.times {Bug::String.nofree << "a" * 100}', + bug10942, rss: true, limit: 2.0) + end +end diff --git a/jni/ruby/test/-ext-/string/test_normalize.rb b/jni/ruby/test/-ext-/string/test_normalize.rb new file mode 100644 index 0000000..283ca93 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_normalize.rb @@ -0,0 +1,106 @@ +require 'test/unit' +require "-test-/string/string" +require "tempfile" + +class Test_StringNormalize < Test::Unit::TestCase +=begin + def test_normalize_all + exclude = [ + #0x340, 0x341, 0x343, 0x344 + ] + (0x0080..0xFFFD).each do |n| + next if 0xD800 <= n && n <= 0xDFFF + next if exclude.include? n + code = n.to_s(16) + Tempfile.create("#{code}-#{n.chr(Encoding::UTF_8)}-") do |tempfile| + ary = Dir.glob(File.expand_path("../#{code}-*", tempfile.path)) + assert_equal 1, ary.size + result = ary[0] + rn = result[/\/\h+-(.+?)-/, 1] + #assert_equal tempfile.path, result, "#{rn.dump} is not U+#{n.to_s(16)}" + r2 = Bug::String.new(result ).normalize_ospath + rn2 = r2[/\/\h+-(.+?)-/, 1] + if tempfile.path == result + if tempfile.path == r2 + else + puts "U+#{n.to_s(16)} shouldn't be r2#{rn2.dump}" + end + else + if tempfile.path == r2 + # puts "U+#{n.to_s(16)} shouldn't be r#{rn.dump}" + elsif result == r2 + puts "U+#{n.to_s(16)} shouldn't be #{rn.dump}" + else + puts "U+#{n.to_s(16)} shouldn't be r#{rn.dump} r2#{rn2.dump}" + end + end + end + end + end +=end + + def test_normalize + %[ + \u304C \u304B\u3099 + \u3077 \u3075\u309A + \u308F\u3099 \u308F\u3099 + \u30F4 \u30A6\u3099 + \u30DD \u30DB\u309A + \u30AB\u303A \u30AB\u303A + \u00C1 A\u0301 + B\u030A B\u030A + \u0386 \u0391\u0301 + \u03D3 \u03D2\u0301 + \u0401 \u0415\u0308 + \u2260 =\u0338 + \u{c548} \u{110b}\u{1161}\u{11ab} + ].scan(/(\S+)\s+(\S+)/) do |expected, src| + result = Bug::String.new(src).normalize_ospath + assert_equal expected, result, + "#{expected.dump} is expected but #{src.dump}" + end + rescue NotImplementedError + end + + def test_not_normalize_kc + %[ + \u2460 + \u2162 + \u3349 + \u33A1 + \u337B + \u2116 + \u33CD + \u2121 + \u32A4 + \u3231 + ].split.each do |src| + result = Bug::String.new(src).normalize_ospath + assert_equal src, result, + "#{src.dump} is expected not to be normalized, but #{result.dump}" + end + rescue NotImplementedError + end + + def test_dont_normalize_hfsplus + %[ + \u2190\u0338 + \u219A + \u212B + \uF90A + \uF9F4 + \uF961 \uF9DB + \uF96F \uF3AA + \uF915 \uF95C \uF9BF + \uFA0C + \uFA10 + \uFA19 + \uFA26 + ].split.each do |src| + result = Bug::String.new(src).normalize_ospath + assert_equal src, result, + "#{src.dump} is expected not to be normalized, but #{result.dump}" + end + rescue NotImplementedError + end +end diff --git a/jni/ruby/test/-ext-/string/test_qsort.rb b/jni/ruby/test/-ext-/string/test_qsort.rb new file mode 100644 index 0000000..3a58523 --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_qsort.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StringQSort < Test::Unit::TestCase + def test_qsort + s = Bug::String.new("xxozfxx") + s.qsort! + assert_equal("foxxxxz", s) + end + + def test_qsort_slice + s = Bug::String.new("xxofzx1") + s.qsort!(nil, nil, 3) + assert_equal("fzxxxo1", s) + s = Bug::String.new("xxofzx231") + s.qsort!(nil, nil, 3) + assert_equal("231fzxxxo", s) + end +end diff --git a/jni/ruby/test/-ext-/string/test_set_len.rb b/jni/ruby/test/-ext-/string/test_set_len.rb new file mode 100644 index 0000000..e257cac --- /dev/null +++ b/jni/ruby/test/-ext-/string/test_set_len.rb @@ -0,0 +1,25 @@ +require 'test/unit' +require "-test-/string/string" + +class Test_StrSetLen < Test::Unit::TestCase + def setup + @s0 = [*"a".."z"].join("").freeze + @s1 = Bug::String.new(@s0) + end + + def teardown + orig = [*"a".."z"].join("") + assert_equal(orig, @s0) + end + + def test_non_shared + @s1.modify! + assert_equal("abc", @s1.set_len(3)) + end + + def test_shared + assert_raise(RuntimeError) { + assert_equal("abc", @s1.set_len(3)) + } + end +end |