Debian Codegraph @

test/-ext-/string/test_set_len.rb

raw
1 # frozen_string_literal: false
2 require 'test/unit'
3 require "-test-/string"
4
5 class Test_StrSetLen < Test::Unit::TestCase
6 def setup
7 # Make string long enough so that it is not embedded
8 @range_end = ("0".ord + GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]).chr
9 @s0 = [*"0"..@range_end].join("").freeze
10 @s1 = Bug::String.new(@s0)
11 end
12
13 def teardown
14 orig = [*"0"..@range_end].join("")
15 assert_equal(orig, @s0)
16 end
17
18 def test_non_shared
19 @s1.modify!
20 assert_equal("012", @s1.set_len(3))
21 end
22
23 def test_shared
24 assert_raise(RuntimeError) {
25 @s1.set_len(3)
26 }
27 end
28
29 def test_capacity_equals_to_new_size
30 bug12757 = "[ruby-core:77257] [Bug #12757]"
31 # fill to ensure capacity does not decrease with force_encoding
32 str = Bug::String.new("\x00" * 128, capacity: 128)
33 str.force_encoding("UTF-32BE")
34 assert_equal 128, Bug::String.capacity(str)
35 assert_equal 127, str.set_len(127).bytesize, bug12757
36 end
37
38 def test_coderange_after_append
39 u = -"\u3042"
40 str = Bug::String.new(encoding: Encoding::UTF_8)
41 bsize = u.bytesize
42 str.append(u)
43 assert_equal 0, str.bytesize
44 str.set_len(bsize)
45 assert_equal bsize, str.bytesize
46 assert_predicate str, :valid_encoding?
47 assert_not_predicate str, :ascii_only?
48 assert_equal u, str
49 end
50
51 def test_coderange_after_trunc
52 u = -"\u3042"
53 bsize = u.bytesize
54 str = Bug::String.new(u)
55 str.set_len(bsize - 1)
56 assert_equal bsize - 1, str.bytesize
57 assert_not_predicate str, :valid_encoding?
58 assert_not_predicate str, :ascii_only?
59 str.append(u.byteslice(-1))
60 str.set_len(bsize)
61 assert_equal bsize, str.bytesize
62 assert_predicate str, :valid_encoding?
63 assert_not_predicate str, :ascii_only?
64 assert_equal u, str
65 end
66 end