@@ -1373,4 +1373,106 @@ def inspect
13731373 @s . rb_str_to_interned_str ( "hello" ) . should . equal? ( -"hello" )
13741374 end
13751375 end
1376+
1377+ describe "rb_interned_str" do
1378+ it "returns a frozen string" do
1379+ str = "hello"
1380+ result = @s . rb_interned_str ( str , str . bytesize )
1381+ result . should . is_a? ( String )
1382+ result . should . frozen?
1383+ result . encoding . should == Encoding ::US_ASCII
1384+ end
1385+
1386+ it "returns the same frozen string" do
1387+ str = "hello"
1388+ result1 = @s . rb_interned_str ( str , str . bytesize )
1389+ result2 = @s . rb_interned_str ( str , str . bytesize )
1390+ result1 . should . equal? ( result2 )
1391+ end
1392+
1393+ it "supports strings with embedded null bytes" do
1394+ str = "foo\x00 bar\x00 baz" . b
1395+ result = @s . rb_interned_str ( str , str . bytesize )
1396+ result . should == str
1397+ end
1398+
1399+ it "support binary strings that are invalid in ASCII encoding" do
1400+ str = "foo\x81 bar\x82 baz" . b
1401+ result = @s . rb_interned_str ( str , str . bytesize )
1402+ result . encoding . should == Encoding ::US_ASCII
1403+ result . should == str . dup . force_encoding ( Encoding ::US_ASCII )
1404+ result . should_not . valid_encoding?
1405+ end
1406+
1407+ it "returns the same frozen strings for different encodings" do
1408+ str1 = "hello" . dup . force_encoding ( Encoding ::US_ASCII )
1409+ str2 = "hello" . dup . force_encoding ( Encoding ::UTF_8 )
1410+ result1 = @s . rb_interned_str ( str1 , str1 . bytesize )
1411+ result2 = @s . rb_interned_str ( str2 , str2 . bytesize )
1412+ result1 . should . equal? ( result2 )
1413+ end
1414+
1415+ it 'returns the same string when using non-ascii characters' do
1416+ str = 'こんにちは'
1417+ result1 = @s . rb_interned_str ( str , str . bytesize )
1418+ result2 = @s . rb_interned_str ( str , str . bytesize )
1419+ result1 . should . equal? ( result2 )
1420+ end
1421+
1422+ it "returns the same string as String#-@" do
1423+ str = "hello" . dup . force_encoding ( Encoding ::US_ASCII )
1424+ @s . rb_interned_str ( str , str . bytesize ) . should . equal? ( -str )
1425+ end
1426+ end
1427+
1428+ describe "rb_interned_str_cstr" do
1429+ it "returns a frozen string" do
1430+ str = "hello"
1431+ result = @s . rb_interned_str_cstr ( str )
1432+ result . should . is_a? ( String )
1433+ result . should . frozen?
1434+ result . encoding . should == Encoding ::US_ASCII
1435+ end
1436+
1437+ it "returns the same frozen string" do
1438+ str = "hello"
1439+ result1 = @s . rb_interned_str_cstr ( str )
1440+ result2 = @s . rb_interned_str_cstr ( str )
1441+ result1 . should . equal? ( result2 )
1442+ end
1443+
1444+ it "does not support strings with embedded null bytes" do
1445+ str = "foo\x00 bar\x00 baz" . b
1446+ result = @s . rb_interned_str_cstr ( str )
1447+ result . should == "foo"
1448+ end
1449+
1450+ it "support binary strings that are invalid in ASCII encoding" do
1451+ str = "foo\x81 bar\x82 baz" . b
1452+ result = @s . rb_interned_str_cstr ( str )
1453+ result . encoding . should == Encoding ::US_ASCII
1454+ result . should == str . dup . force_encoding ( Encoding ::US_ASCII )
1455+ result . should_not . valid_encoding?
1456+ end
1457+
1458+ it "returns the same frozen strings for different encodings" do
1459+ str1 = "hello" . dup . force_encoding ( Encoding ::US_ASCII )
1460+ str2 = "hello" . dup . force_encoding ( Encoding ::UTF_8 )
1461+ result1 = @s . rb_interned_str_cstr ( str1 )
1462+ result2 = @s . rb_interned_str_cstr ( str2 )
1463+ result1 . should . equal? ( result2 )
1464+ end
1465+
1466+ it 'returns the same string when using non-ascii characters' do
1467+ str = 'こんにちは'
1468+ result1 = @s . rb_interned_str_cstr ( str )
1469+ result2 = @s . rb_interned_str_cstr ( str )
1470+ result1 . should . equal? ( result2 )
1471+ end
1472+
1473+ it "returns the same string as String#-@" do
1474+ str = "hello" . dup . force_encoding ( Encoding ::US_ASCII )
1475+ @s . rb_interned_str_cstr ( str ) . should . equal? ( -str )
1476+ end
1477+ end
13761478end
0 commit comments