public
Description: ruby lang (www.ruby-lang.org) svn mirror
Homepage: http://svn.ruby-lang.org/repos/ruby/
Clone URL: git://github.com/juretta/ruby.git
* test/ruby/test_string.rb: add tests to achieve over 90% test
  coverage of string.c.

* test/ruby/test_m17n.rb: ditto.

* test/ruby/test_symbol.rb: ditto.

* test/ruby/test_pack.rb: ditto.


git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@16427 
b2dd03c8-39d4-4d8f-98ff-823fe69b080e
mame (author)
Thu May 15 07:37:18 -0700 2008
commit  abc1fbf6b22454366d957f427ef1e2ea32eda7e8
tree    7d2d9d454ecb67d5acd5b36f7a344891af96777b
parent  73c51efe974f7c0b9490680824f1f948ac797388
...
 
 
 
 
 
 
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -1,3 +1,14 @@
0
+Thu May 15 23:36:09 2008 Yusuke Endoh <mame@tsg.ne.jp>
0
+
0
+ * test/ruby/test_string.rb: add tests to achieve over 90% test
0
+ coverage of string.c.
0
+
0
+ * test/ruby/test_m17n.rb: ditto.
0
+
0
+ * test/ruby/test_symbol.rb: ditto.
0
+
0
+ * test/ruby/test_pack.rb: ditto.
0
+
0
 Thu May 15 23:01:06 2008 Yusuke Endoh <mame@tsg.ne.jp>
0
 
0
   * string.c (tr_find): String#delete returned wrong result when multiple
...
877
878
879
 
 
 
880
881
882
...
968
969
970
 
 
 
 
 
 
971
972
973
...
1201
1202
1203
1204
1205
1206
1207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1208
...
877
878
879
880
881
882
883
884
885
...
971
972
973
974
975
976
977
978
979
980
981
982
...
1210
1211
1212
 
 
 
 
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
0
@@ -877,6 +877,9 @@ class TestM17N < Test::Unit::TestCase
0
     }
0
 
0
     assert_equal(e("\xA1\xA1"), a("a").tr(a("a"), e("\xA1\xA1")))
0
+
0
+ assert_equal("X\u3042\u3044X", "A\u3042\u3044\u3046".tr("^\u3042\u3044", "X"))
0
+ assert_equal("\u3042\u3046" * 100, ("\u3042\u3044" * 100).tr("\u3044", "\u3046"))
0
   end
0
 
0
   def test_tr_s
0
@@ -968,6 +971,12 @@ class TestM17N < Test::Unit::TestCase
0
     assert_equal(u("\xf0jihgfedcba"), u("abcdefghij\xf0").reverse)
0
   end
0
 
0
+ def test_reverse_bang
0
+ s = u("abcdefghij\xf0")
0
+ s.reverse!
0
+ assert_equal(u("\xf0jihgfedcba"), s)
0
+ end
0
+
0
   def test_plus
0
     assert_raise(ArgumentError){u("\xe3\x81\x82") + a("\xa1")}
0
   end
0
@@ -1201,8 +1210,27 @@ class TestM17N < Test::Unit::TestCase
0
     assert_equal(true, (s.dup << s).valid_encoding?)
0
     assert_equal(true, "".center(2, s).valid_encoding?)
0
 
0
- s = "\xa1\xa1\x8f".force_encoding("euc-jp")
0
- assert_equal(false, s.valid_encoding?)
0
- assert_equal(true, s.reverse.valid_encoding?)
0
- end
0
+ s = "\xa1\xa1\x8f".force_encoding("euc-jp")
0
+ assert_equal(false, s.valid_encoding?)
0
+ assert_equal(true, s.reverse.valid_encoding?)
0
+ end
0
+
0
+ def test_getbyte
0
+ assert_equal(0x82, u("\xE3\x81\x82\xE3\x81\x84").getbyte(2))
0
+ assert_equal(0x82, u("\xE3\x81\x82\xE3\x81\x84").getbyte(-4))
0
+ assert_nil(u("\xE3\x81\x82\xE3\x81\x84").getbyte(100))
0
+ end
0
+
0
+ def test_setbyte
0
+ s = u("\xE3\x81\x82\xE3\x81\x84")
0
+ s.setbyte(2, 0x84)
0
+ assert_equal(u("\xE3\x81\x84\xE3\x81\x84"), s)
0
+
0
+ s = u("\xE3\x81\x82\xE3\x81\x84")
0
+ assert_raise(IndexError) { s.setbyte(100, 0) }
0
+
0
+ s = u("\xE3\x81\x82\xE3\x81\x84")
0
+ s.setbyte(-4, 0x84)
0
+ assert_equal(u("\xE3\x81\x84\xE3\x81\x84"), s)
0
+ end
0
 end
...
63
64
65
 
66
67
68
...
70
71
72
 
73
74
75
...
429
430
431
 
 
 
 
 
 
 
 
 
 
432
...
63
64
65
66
67
68
69
...
71
72
73
74
75
76
77
...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
0
@@ -63,6 +63,7 @@ class TestPack < Test::Unit::TestCase
0
     assert_equal a, a.pack("P").unpack("P*")
0
     assert_equal "a", a.pack("P").unpack("P")[0]
0
     assert_equal a, a.pack("P").freeze.unpack("P*")
0
+ assert_raise(ArgumentError) { (a.pack("P") + "").unpack("P*") }
0
   end
0
 
0
   def test_pack_p
0
@@ -70,6 +71,7 @@ class TestPack < Test::Unit::TestCase
0
     assert_equal a, a.pack("p").unpack("p*")
0
     assert_equal a[0], a.pack("p").unpack("p")[0]
0
     assert_equal a, a.pack("p").freeze.unpack("p*")
0
+ assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
0
   end
0
 
0
   def test_format_string_modified
0
@@ -429,4 +431,14 @@ class TestPack < Test::Unit::TestCase
0
     assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff])
0
     assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000])
0
   end
0
+
0
+ def test_modify_under_safe4
0
+ s = "foo"
0
+ assert_raise(SecurityError) do
0
+ Thread.new do
0
+ $SAFE = 4
0
+ s.clear
0
+ end.join
0
+ end
0
+ end
0
 end
...
57
58
59
 
 
 
 
 
 
60
61
62
...
136
137
138
 
 
 
 
 
 
 
 
139
140
141
...
150
151
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
154
155
...
165
166
167
 
 
 
 
 
 
 
168
169
170
...
176
177
178
 
 
 
 
 
179
180
181
...
187
188
189
 
 
 
 
 
 
190
191
192
...
329
330
331
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334
335
...
393
394
395
 
 
396
397
398
...
432
433
434
 
 
 
 
 
 
435
436
437
...
525
526
527
 
 
 
 
528
529
530
...
549
550
551
 
 
552
553
554
...
637
638
639
 
 
 
 
 
 
 
 
640
641
642
...
727
728
729
 
 
 
 
 
 
 
 
 
730
731
732
...
768
769
770
 
 
 
 
 
 
 
 
771
772
773
...
935
936
937
 
 
938
939
940
...
958
959
960
 
 
961
962
963
...
1048
1049
1050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1051
1052
1053
...
1402
1403
1404
 
 
1405
1406
1407
...
1411
1412
1413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1414
...
57
58
59
60
61
62
63
64
65
66
67
68
...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
...
194
195
196
197
198
199
200
201
202
203
204
205
206
...
212
213
214
215
216
217
218
219
220
221
222
...
228
229
230
231
232
233
234
235
236
237
238
239
...
376
377
378
 
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
455
456
457
458
459
460
461
462
...
496
497
498
499
500
501
502
503
504
505
506
507
...
595
596
597
598
599
600
601
602
603
604
...
623
624
625
626
627
628
629
630
...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
...
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
...
861
862
863
864
865
866
867
868
869
870
871
872
873
874
...
1036
1037
1038
1039
1040
1041
1042
1043
...
1061
1062
1063
1064
1065
1066
1067
1068
...
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
...
1521
1522
1523
1524
1525
1526
1527
1528
...
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
0
@@ -57,6 +57,12 @@ class TestString < Test::Unit::TestCase
0
       assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
0
       assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
0
     end
0
+
0
+ o = Object.new
0
+ def o.to_int; 2; end
0
+ assert_equal("o", "foo"[o])
0
+
0
+ assert_raise(ArgumentError) { "foo"[] }
0
   end
0
 
0
   def test_ASET # '[]='
0
@@ -136,6 +142,14 @@ class TestString < Test::Unit::TestCase
0
     s = S("a string")
0
     s[0..s.size] = S("another string")
0
     assert_equal(S("another string"), s)
0
+
0
+ o = Object.new
0
+ def o.to_int; 2; end
0
+ s = "foo"
0
+ s[o] = "bar"
0
+ assert_equal("fobar", s)
0
+
0
+ assert_raise(ArgumentError) { "foo"[1, 2, 3] = "" }
0
   end
0
 
0
   def test_CMP # '<=>'
0
@@ -150,6 +164,21 @@ class TestString < Test::Unit::TestCase
0
       assert_equal(0, S("ABCDEF") <=> S("abcdef"))
0
       $= = false
0
     end
0
+
0
+ assert_nil("foo" <=> Object.new)
0
+
0
+ o = Object.new
0
+ def o.to_str; "bar"; end
0
+ assert_nil("foo" <=> o)
0
+
0
+ def o.<=>(x); nil; end
0
+ assert_nil("foo" <=> o)
0
+
0
+ def o.<=>(x); 1; end
0
+ assert_equal(-1, "foo" <=> o)
0
+
0
+ def o.<=>(x); 2**100; end
0
+ assert_equal(-(2**100), "foo" <=> o)
0
   end
0
 
0
   def test_EQUAL # '=='
0
@@ -165,6 +194,13 @@ class TestString < Test::Unit::TestCase
0
 
0
     assert(S("CAT") != S('cat'))
0
     assert(S("CaT") != S('cAt'))
0
+
0
+ o = Object.new
0
+ def o.to_str; end
0
+ def o.==(x); false; end
0
+ assert_equal(false, "foo" == o)
0
+ def o.==(x); true; end
0
+ assert_equal(true, "foo" == o)
0
   end
0
 
0
   def test_LSHIFT # '<<'
0
@@ -176,6 +212,11 @@ class TestString < Test::Unit::TestCase
0
       s << s
0
       assert_equal("a" * (2 << i), s)
0
     }
0
+
0
+ s = ["foo"].pack("p")
0
+ l = s.size
0
+ s << "bar"
0
+ assert_equal(l + 3, s.size)
0
   end
0
 
0
   def test_MATCH # '=~'
0
@@ -187,6 +228,12 @@ class TestString < Test::Unit::TestCase
0
       assert_equal(10, S("FeeFieFoo-Fum") =~ /FUM$/)
0
       $= = false
0
     end
0
+
0
+ o = Object.new
0
+ def o.=~(x); x + "bar"; end
0
+ assert_equal("foobar", S("foo") =~ o)
0
+
0
+ assert_raise(TypeError) { S("foo") =~ "foo" }
0
   end
0
 
0
   def test_MOD # '%'
0
@@ -329,7 +376,22 @@ class TestString < Test::Unit::TestCase
0
     b = a.dup
0
     assert_equal(S("hello"), a.chomp!)
0
     assert_equal(S("hello\n"), b)
0
-
0
+
0
+ s = "foo\r\n"
0
+ s.chomp!
0
+ assert_equal("foo", s)
0
+
0
+ s = "foo\r"
0
+ s.chomp!
0
+ assert_equal("foo", s)
0
+
0
+ s = "foo\r\n"
0
+ s.chomp!("")
0
+ assert_equal("foo", s)
0
+
0
+ s = "foo\r"
0
+ s.chomp!("")
0
+ assert_equal("foo\r", s)
0
   end
0
 
0
   def test_chop
0
@@ -393,6 +455,8 @@ class TestString < Test::Unit::TestCase
0
     assert_equal(4, a.count(S("hello"), S("^l")))
0
     assert_equal(4, a.count(S("ej-m")))
0
     assert_equal(0, S("y").count(S("a\\-z")))
0
+
0
+ assert_raise(ArgumentError) { "foo".count }
0
   end
0
 
0
   def test_crypt
0
@@ -432,6 +496,12 @@ class TestString < Test::Unit::TestCase
0
     a.delete!(S("lo"))
0
     assert_equal(S("he"), a)
0
     assert_equal(S("hello"), b)
0
+
0
+ a = S("hello")
0
+ a.delete!(S("^el"))
0
+ assert_equal(S("ell"), a)
0
+
0
+ assert_raise(ArgumentError) { S("foo").delete! }
0
   end
0
 
0
 
0
@@ -525,6 +595,10 @@ class TestString < Test::Unit::TestCase
0
     assert_equal(S("world"), res[1])
0
     
0
     $/ = save
0
+
0
+ s = nil
0
+ "foo\nbar".each_line(nil) {|s2| s = s2 }
0
+ assert_equal("foo\nbar", s)
0
   end
0
 
0
   def test_empty?
0
@@ -549,6 +623,8 @@ class TestString < Test::Unit::TestCase
0
     a = S("hello")
0
     a.taint
0
     assert(a.gsub(/./, S('X')).tainted?)
0
+
0
+ assert_raise(ArgumentError) { "foo".gsub }
0
   end
0
 
0
   def test_gsub!
0
@@ -637,6 +713,14 @@ class TestString < Test::Unit::TestCase
0
     assert_nil(S("hello").index(?z))
0
     assert_nil(S("hello").index(S("z")))
0
     assert_nil(S("hello").index(/z./))
0
+
0
+ o = Object.new
0
+ def o.to_str; "bar"; end
0
+ assert_equal(3, "foobarbarbaz".index(o))
0
+ assert_raise(TypeError) { "foo".index(Object.new) }
0
+
0
+ assert_nil("foo".index(//, -100))
0
+ assert_nil($~)
0
   end
0
 
0
   def test_intern
0
@@ -727,6 +811,15 @@ class TestString < Test::Unit::TestCase
0
     b = a.replace(S("xyz"))
0
     assert_equal(S("xyz"), b)
0
     assert(b.tainted?)
0
+
0
+ s = "foo" * 100
0
+ s2 = ("bar" * 100).dup
0
+ s.replace(s2)
0
+ assert_equal(s2, s)
0
+
0
+ s2 = ["foo"].pack("p")
0
+ s.replace(s2)
0
+ assert_equal(s2, s)
0
   end
0
 
0
   def test_reverse
0
@@ -768,6 +861,14 @@ class TestString < Test::Unit::TestCase
0
     assert_nil(S("hello").rindex(?z))
0
     assert_nil(S("hello").rindex(S("z")))
0
     assert_nil(S("hello").rindex(/z./))
0
+
0
+ o = Object.new
0
+ def o.to_str; "bar"; end
0
+ assert_equal(6, "foobarbarbaz".rindex(o))
0
+ assert_raise(TypeError) { "foo".rindex(Object.new) }
0
+
0
+ assert_nil("foo".rindex(//, -100))
0
+ assert_nil($~)
0
   end
0
 
0
   def test_rjust
0
@@ -935,6 +1036,8 @@ class TestString < Test::Unit::TestCase
0
       assert_nil(a.slice!(S("plugh")))
0
       assert_equal(S("FooBar"), a)
0
     end
0
+
0
+ assert_raise(ArgumentError) { "foo".slice! }
0
   end
0
 
0
   def test_split
0
@@ -958,6 +1061,8 @@ class TestString < Test::Unit::TestCase
0
 
0
     assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|')))
0
     assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1))
0
+
0
+ assert_equal([], "".split(//, 1))
0
   end
0
 
0
   def test_squeeze
0
@@ -1048,6 +1153,20 @@ class TestString < Test::Unit::TestCase
0
     a = S("hello")
0
     a.taint
0
     assert(a.sub(/./, S('X')).tainted?)
0
+
0
+ o = Object.new
0
+ def o.to_str; "bar"; end
0
+ assert_equal("fooBARbaz", "foobarbaz".sub(o, "BAR"))
0
+
0
+ assert_raise(TypeError) { "foo".sub(Object.new, "") }
0
+
0
+ assert_raise(ArgumentError) { "foo".sub }
0
+
0
+ assert_raise(IndexError) { "foo"[/(?:(o$)|(x))/, 2] = 'bar' }
0
+
0
+ o = Object.new
0
+ def o.to_s; self; end
0
+ assert_match(/^foo#<Object:0x.*>baz$/, "foobarbaz".sub("bar") { o })
0
   end
0
 
0
   def test_sub!
0
@@ -1402,6 +1521,8 @@ class TestString < Test::Unit::TestCase
0
       assert_equal(s1, s2)
0
       s1 << 'a'
0
     }
0
+
0
+ assert_raise(ArgumentError) { "foo" * (-1) }
0
   end
0
 
0
   def test_respond_to
0
@@ -1411,4 +1532,72 @@ class TestString < Test::Unit::TestCase
0
     def o.==(other) "" == other end
0
     assert_equal(false, "" == o)
0
   end
0
+
0
+ def test_match_method
0
+ assert_equal("bar", "foobarbaz".match(/bar/).to_s)
0
+
0
+ o = /foo/
0
+ def o.match(x, y, z); x + y + z; end
0
+ assert_equal("foobarbaz", "foo".match(o, "bar", "baz"))
0
+ x = nil
0
+ "foo".match(o, "bar", "baz") {|y| x = y }
0
+ assert_equal("foobarbaz", x)
0
+
0
+ assert_raise(ArgumentError) { "foo".match }
0
+ end
0
+
0
+ def test_clear
0
+ s = "foo" * 100
0
+ s.clear
0
+ assert_equal("", s)
0
+ end
0
+
0
+ def test_to_s_2
0
+ c = Class.new(String)
0
+ s = c.new
0
+ s.replace("foo")
0
+ assert_equal("foo", s.to_s)
0
+ assert_instance_of(String, s.to_s)
0
+ end
0
+
0
+ def test_partition
0
+ assert_equal(%w(he l lo), "hello".partition(/l/))
0
+ assert_equal(%w(he l lo), "hello".partition("l"))
0
+ assert_raise(TypeError) { "hello".partition(1) }
0
+ end
0
+
0
+ def test_rpartition
0
+ assert_equal(%w(hel l o), "hello".rpartition(/l/))
0
+ assert_equal(%w(hel l o), "hello".rpartition("l"))
0
+ assert_raise(TypeError) { "hello".rpartition(1) }
0
+ end
0
+
0
+ def test_setter
0
+ assert_raise(TypeError) { $/ = 1 }
0
+ end
0
+
0
+ def test_to_id
0
+ c = Class.new
0
+ c.class_eval do
0
+ def initialize
0
+ @foo = :foo
0
+ end
0
+ end
0
+
0
+ assert_raise(TypeError) do
0
+ c.class_eval { attr 1 }
0
+ end
0
+
0
+ o = Object.new
0
+ def o.to_str; :foo; end
0
+ assert_raise(TypeError) do
0
+ c.class_eval { attr 1 }
0
+ end
0
+
0
+ def o.to_str; "foo"; end
0
+ assert_nothing_raised do
0
+ c.class_eval { attr o }
0
+ end
0
+ assert_equal(:foo, c.new.foo)
0
+ end
0
 end
...
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
0
@@ -89,4 +89,47 @@ class TestSymbol < Test::Unit::TestCase
0
       assert_equal ary_ids, ary.collect(&:object_id)
0
     end
0
   end
0
+
0
+ def test_call
0
+ o = Object.new
0
+ def o.foo(x, y); x + y; end
0
+
0
+ assert_equal(3, :foo.to_proc.call(o, 1, 2))
0
+ assert_raise(ArgumentError) { :foo.to_proc.call }
0
+ end
0
+
0
+ def test_succ
0
+ assert_equal(:fop, :foo.succ)
0
+ end
0
+
0
+ def test_cmp
0
+ assert_equal(0, :FoO <=> :FoO)
0
+ assert_equal(-1, :FoO <=> :fOO)
0
+ assert_equal(1, :fOO <=> :FoO)
0
+ assert_nil(:foo <=> "foo")
0
+ end
0
+
0
+ def test_casecmp
0
+ assert_equal(0, :FoO.casecmp(:fOO))
0
+ assert_equal(1, :FoO.casecmp(:BaR))
0
+ assert_equal(-1, :baR.casecmp(:FoO))
0
+ assert_nil(:foo.casecmp("foo"))
0
+ end
0
+
0
+ def test_length
0
+ assert_equal(3, :FoO.length)
0
+ assert_equal(3, :FoO.size)
0
+ end
0
+
0
+ def test_empty
0
+ assert_equal(false, :FoO.empty?)
0
+ assert_equal(true, :"".empty?)
0
+ end
0
+
0
+ def test_case
0
+ assert_equal(:FOO, :FoO.upcase)
0
+ assert_equal(:foo, :FoO.downcase)
0
+ assert_equal(:Foo, :foo.capitalize)
0
+ assert_equal(:fOo, :FoO.swapcase)
0
+ end
0
 end

Comments

    No one has commented yet.