| 1 |
# coding: ascii-8bit |
| 2 |
# frozen_string_literal: false |
| 3 |
require 'test/unit' |
| 4 |
require "-test-/string" |
| 5 |
require "rbconfig/sizeof" |
| 6 |
|
| 7 |
class Test_StringCoderange < Test::Unit::TestCase |
| 8 |
def setup |
| 9 |
@sizeof_voidp = RbConfig::SIZEOF["void*"] |
| 10 |
@a8 = Encoding::ASCII_8BIT |
| 11 |
@a7 = Encoding::US_ASCII |
| 12 |
@u8 = Encoding::UTF_8 |
| 13 |
end |
| 14 |
|
| 15 |
def test_ascii8bit |
| 16 |
enc = @a8 |
| 17 |
str = "a" |
| 18 |
str.force_encoding(enc) |
| 19 |
assert_equal :"7bit", Bug::String.new(str).coderange_scan |
| 20 |
|
| 21 |
str = "a\xBE".force_encoding(enc) |
| 22 |
assert_equal :valid, Bug::String.new(str).coderange_scan |
| 23 |
end |
| 24 |
|
| 25 |
def test_usascii |
| 26 |
enc = @a7 |
| 27 |
str = "a" |
| 28 |
str.force_encoding(enc) |
| 29 |
assert_equal :"7bit", Bug::String.new(str).coderange_scan |
| 30 |
|
| 31 |
str = "a" * (@sizeof_voidp * 2) |
| 32 |
str << "\xBE" |
| 33 |
str.force_encoding(enc) |
| 34 |
assert_equal :broken, Bug::String.new(str).coderange_scan |
| 35 |
end |
| 36 |
|
| 37 |
def test_utf8 |
| 38 |
enc = @u8 |
| 39 |
str = "a" |
| 40 |
str.force_encoding(enc) |
| 41 |
assert_equal :"7bit", Bug::String.new(str).coderange_scan |
| 42 |
|
| 43 |
str = "a" * (@sizeof_voidp * 3) |
| 44 |
str << "aa\xC2\x80" |
| 45 |
str.force_encoding(enc) |
| 46 |
assert_equal :valid, Bug::String.new(str).coderange_scan |
| 47 |
|
| 48 |
str = "a" * (@sizeof_voidp * 2) |
| 49 |
str << "\xC2\x80" |
| 50 |
str << "a" * (@sizeof_voidp * 2) |
| 51 |
str.force_encoding(enc) |
| 52 |
assert_equal :valid, Bug::String.new(str).coderange_scan |
| 53 |
|
| 54 |
str = "a" * (@sizeof_voidp * 2) |
| 55 |
str << "\xC1\x80" |
| 56 |
str << "a" * (@sizeof_voidp * 2) |
| 57 |
str.force_encoding(enc) |
| 58 |
assert_equal :broken, Bug::String.new(str).coderange_scan |
| 59 |
end |
| 60 |
end |