diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/assert.rb | 20 | ||||
| -rw-r--r-- | test/bintest.rb | 6 | ||||
| -rw-r--r-- | test/driver.c | 6 | ||||
| -rw-r--r-- | test/mrbtest.rake | 17 | ||||
| -rw-r--r-- | test/t/enumerable.rb | 32 | ||||
| -rw-r--r-- | test/t/float.rb | 20 | ||||
| -rw-r--r-- | test/t/hash.rb | 20 | ||||
| -rw-r--r-- | test/t/integer.rb | 16 | ||||
| -rw-r--r-- | test/t/string.rb | 28 | ||||
| -rw-r--r-- | test/t/syntax.rb | 5 |
10 files changed, 157 insertions, 13 deletions
diff --git a/test/assert.rb b/test/assert.rb index d2d865649..30d27d9ef 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -78,7 +78,7 @@ end def assert_true(ret, msg = nil, diff = nil) if $mrbtest_assert $mrbtest_assert_idx += 1 - if !ret + unless ret msg = "Expected #{ret.inspect} to be true" unless msg diff = assertion_diff(true, ret) unless diff $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) @@ -174,6 +174,24 @@ def assert_raise(*exp) ret end +def assert_nothing_raised(*exp) + ret = true + if $mrbtest_assert + $mrbtest_assert_idx += 1 + msg = exp.last.class == String ? exp.pop : "" + begin + yield + rescue Exception => e + msg = "#{msg} exception raised." + diff = " Class: <#{e.class}>\n" + + " Message: #{e.message}" + $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) + ret = false + end + end + ret +end + ## # Fails unless +obj+ is a kind of +cls+. def assert_kind_of(cls, obj, msg = nil) diff --git a/test/bintest.rb b/test/bintest.rb index e9dbb285e..0ff3341a0 100644 --- a/test/bintest.rb +++ b/test/bintest.rb @@ -1,8 +1,10 @@ $:.unshift File.dirname(File.dirname(File.expand_path(__FILE__))) require 'test/assert.rb' -Dir['mrbgems/**/bintest/*.rb'].each do |file| - load file +ARGV.each do |gem| + Dir["#{gem}/bintest/*.rb"].each do |file| + load file + end end load 'test/report.rb' diff --git a/test/driver.c b/test/driver.c index 0116f4584..2af1680f4 100644 --- a/test/driver.c +++ b/test/driver.c @@ -61,14 +61,12 @@ eval_test(mrb_state *mrb) static void t_printstr(mrb_state *mrb, mrb_value obj) { - struct RString *str; char *s; int len; if (mrb_string_p(obj)) { - str = mrb_str_ptr(obj); - s = str->ptr; - len = str->len; + s = RSTRING_PTR(obj); + len = RSTRING_LEN(obj); fwrite(s, len, 1, stdout); } } diff --git a/test/mrbtest.rake b/test/mrbtest.rake index 35495889e..1c52eafbd 100644 --- a/test/mrbtest.rake +++ b/test/mrbtest.rake @@ -12,9 +12,11 @@ MRuby.each_target do ass_lib = ass_c.ext(exts.object) mrbtest_lib = libfile("#{current_build_dir}/mrbtest") - file mrbtest_lib => [mlib, ass_lib, gems.map(&:test_objs), gems.map { |g| g.test_rbireps.ext(exts.object) }].flatten do |t| + gem_test_files = gems.select { |g| g.run_test_in_other_mrb_state? }.map { |g| g.test_rbireps.ext(exts.object) } + file mrbtest_lib => [mlib, ass_lib, gems.map(&:test_objs), gem_test_files].flatten do |t| archiver.run t.name, t.prerequisites end + file mrbtest_lib => "#{build_dir}/test/no_mrb_open_test.o" unless build_mrbtest_lib_only? driver_obj = objfile("#{current_build_dir}/driver") @@ -41,15 +43,28 @@ MRuby.each_target do _pp "GEN", "*.rb", "#{clib.relative_path}" FileUtils.mkdir_p File.dirname(clib) open(clib, 'w') do |f| + f.puts %Q[/*] + f.puts %Q[ * This file contains a list of all] + f.puts %Q[ * test functions.] + f.puts %Q[ *] + f.puts %Q[ * IMPORTANT:] + f.puts %Q[ * This file was generated!] + f.puts %Q[ * All manual changes will get lost.] + f.puts %Q[ */] + f.puts %Q[] f.puts IO.read(init) mrbc.run f, mrbs, 'mrbtest_irep' gems.each do |g| + next unless g.run_test_in_other_mrb_state? f.puts %Q[void GENERATED_TMP_mrb_#{g.funcname}_gem_test(mrb_state *mrb);] end + f.puts %Q[void no_mrb_open_mrbgem_test(mrb_state *mrb);] f.puts %Q[void mrbgemtest_init(mrb_state* mrb) {] gems.each do |g| + next unless g.run_test_in_other_mrb_state? f.puts %Q[ GENERATED_TMP_mrb_#{g.funcname}_gem_test(mrb);] end + f.puts %Q[ no_mrb_open_mrbgem_test(mrb);] f.puts %Q[}] end end diff --git a/test/t/enumerable.rb b/test/t/enumerable.rb index ed062823c..844251b06 100644 --- a/test/t/enumerable.rb +++ b/test/t/enumerable.rb @@ -8,11 +8,43 @@ end assert('Enumerable#all?', '15.3.2.2.1') do assert_true([1,2,3].all?) assert_false([1,false,3].all?) + + a = [2,4,6] + all = a.all? do |e| + if e % 2 == 0 + true + end + end + assert_true(all) + + a = [2,4,7] + all = a.all? do |e| + if e % 2 == 0 + true + end + end + assert_false(all) end assert('Enumerable#any?', '15.3.2.2.2') do assert_true([false,true,false].any?) assert_false([false,false,false].any?) + + a = [1,3,6] + any = a.any? do |e| + if e % 2 == 0 + true + end + end + assert_true(any) + + a = [1,3,5] + any = a.any? do |e| + if e % 2 == 0 + true + end + end + assert_false(any) end assert('Enumerable#collect', '15.3.2.2.3') do diff --git a/test/t/float.rb b/test/t/float.rb index 0c67f510a..c817e01da 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -15,6 +15,9 @@ assert('Float#+', '15.2.9.3.1') do assert_float(3.123456789, a) assert_float(4.123456789, b) + + assert_raise(TypeError){ 0.0+nil } + assert_raise(TypeError){ 1.0+nil } end assert('Float#-', '15.2.9.3.2') do @@ -144,6 +147,23 @@ assert('Float#truncate', '15.2.9.3.15') do assert_equal(-3, -3.1.truncate) end +assert('Float#divmod') do + def check_floats exp, act + assert_float exp[0], act[0] + assert_float exp[1], act[1] + end + + # Note: quotients are Float because mruby does not have Bignum. + check_floats [ 0, 0.0], 0.0.divmod(1) + check_floats [ 0, 1.1], 1.1.divmod(3) + check_floats [ 3, 0.2], 3.2.divmod(1) + check_floats [ 2, 6.3], 20.3.divmod(7) + check_floats [-1, 1.6], -3.4.divmod(5) + check_floats [-2, -0.5], 25.5.divmod(-13) + check_floats [ 1, -6.6], -13.6.divmod(-7) + check_floats [ 3, 0.2], 9.8.divmod(3.2) +end + assert('Float#nan?') do assert_true (0.0/0.0).nan? assert_false 0.0.nan? diff --git a/test/t/hash.rb b/test/t/hash.rb index 837fe0216..4f1edfb49 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -12,6 +12,7 @@ end assert('Hash#==', '15.2.13.4.1') do assert_true({ 'abc' => 'abc' } == { 'abc' => 'abc' }) assert_false({ 'abc' => 'abc' } == { 'cba' => 'cba' }) + assert_true({ :equal => 1 } == { :equal => 1.0 }) end assert('Hash#[]', '15.2.13.4.2') do @@ -234,8 +235,15 @@ assert('Hash#shift', '15.2.13.4.24') do a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } b = a.shift - assert_equal({ 'abc_key' => 'abc_value' }, a) - assert_equal [ 'cba_key', 'cba_value' ], b + assert_equal Array, b.class + assert_equal 2, b.size + assert_equal 1, a.size + + b = a.shift + + assert_equal Array, b.class + assert_equal 2, b.size + assert_equal 0, a.size end assert('Hash#size', '15.2.13.4.25') do @@ -269,6 +277,14 @@ end # Not ISO specified +assert('Hash#eql?') do + a = { 'a' => 1, 'b' => 2, 'c' => 3 } + b = { 'a' => 1, 'b' => 2, 'c' => 3 } + c = { 'a' => 1.0, 'b' => 2, 'c' => 3 } + assert_true(a.eql?(b)) + assert_false(a.eql?(c)) +end + assert('Hash#reject') do h = {:one => 1, :two => 2, :three => 3, :four => 4} ret = h.reject do |k,v| diff --git a/test/t/integer.rb b/test/t/integer.rb index 79ee1e790..66dd61c0b 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -15,6 +15,9 @@ assert('Integer#+', '15.2.8.3.1') do assert_equal 2, a assert_equal 2.0, b + + assert_raise(TypeError){ 0+nil } + assert_raise(TypeError){ 1+nil } end assert('Integer#-', '15.2.8.3.2') do @@ -31,6 +34,9 @@ assert('Integer#*', '15.2.8.3.3') do assert_equal 1, a assert_equal 1.0, b + + assert_raise(TypeError){ 0*nil } + assert_raise(TypeError){ 1*nil } end assert('Integer#/', '15.2.8.3.4') do @@ -207,6 +213,16 @@ end # Not ISO specified +assert('Integer#divmod') do + assert_equal [ 0, 0], 0.divmod(1) + assert_equal [ 0, 1], 1.divmod(3) + assert_equal [ 3, 0], 3.divmod(1) + assert_equal [ 2, 6], 20.divmod(7) + assert_equal [-1, 2], -3.divmod(5) + assert_equal [-2, -1], 25.divmod(-13) + assert_equal [ 1, -6], -13.divmod(-7) +end + assert('Integer#step') do a = [] b = [] diff --git a/test/t/string.rb b/test/t/string.rb index 3219f98c3..445cf7439 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -36,6 +36,10 @@ end assert('String#*', '15.2.10.5.5') do assert_equal 'aaaaa', 'a' * 5 + assert_equal '', 'a' * 0 + assert_raise(ArgumentError) do + 'a' * -1 + end end assert('String#[]', '15.2.10.5.6') do @@ -82,6 +86,7 @@ assert('String#[] with Range') do g1 = 'abc'[-2..3] h1 = 'abc'[3..4] i1 = 'abc'[4..5] + j1 = 'abcdefghijklmnopqrstuvwxyz'[1..3] a2 = 'abc'[1...0] b2 = 'abc'[1...1] c2 = 'abc'[1...2] @@ -91,6 +96,7 @@ assert('String#[] with Range') do g2 = 'abc'[-2...3] h2 = 'abc'[3...4] i2 = 'abc'[4...5] + j2 = 'abcdefghijklmnopqrstuvwxyz'[1...3] assert_equal '', a1 assert_equal 'b', b1 @@ -101,6 +107,7 @@ assert('String#[] with Range') do assert_equal 'bc', g1 assert_equal '', h1 assert_nil i2 + assert_equal 'bcd', j1 assert_equal '', a2 assert_equal '', b2 assert_equal 'b', c2 @@ -110,6 +117,7 @@ assert('String#[] with Range') do assert_equal 'bc', g2 assert_equal '', h2 assert_nil i2 + assert_equal 'bc', j2 end assert('String#capitalize', '15.2.10.5.7') do @@ -277,8 +285,10 @@ end assert('String#initialize', '15.2.10.5.23') do a = '' a.initialize('abc') - assert_equal 'abc', a + + a.initialize('abcdefghijklmnopqrstuvwxyz') + assert_equal 'abcdefghijklmnopqrstuvwxyz', a end assert('String#initialize_copy', '15.2.10.5.24') do @@ -303,6 +313,13 @@ assert('String#replace', '15.2.10.5.28') do a.replace('abc') assert_equal 'abc', a + assert_equal 'abc', 'cba'.replace(a) + + b = 'abc' * 10 + c = ('cba' * 10).dup + b.replace(c); + c.replace(b); + assert_equal c, b end assert('String#reverse', '15.2.10.5.29') do @@ -326,6 +343,9 @@ assert('String#rindex', '15.2.10.5.31') do assert_nil 'abc'.rindex('d') assert_equal 0, 'abcabc'.rindex('a', 1) assert_equal 3, 'abcabc'.rindex('a', 4) + + assert_equal 3, 'abcabc'.rindex(97) + assert_equal nil, 'abcabc'.rindex(0) end # 'String#scan', '15.2.10.5.32' will be tested in mrbgems. @@ -445,6 +465,12 @@ assert('String#upcase!', '15.2.10.5.43') do assert_equal 'ABC', a assert_equal nil, 'ABC'.upcase! + + a = 'abcdefghijklmnopqrstuvwxyz' + b = a.dup + a.upcase! + b.upcase! + assert_equal 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', b end # Not ISO specified diff --git a/test/t/syntax.rb b/test/t/syntax.rb index ee300c54d..3569193bc 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -1,9 +1,10 @@ assert('__FILE__') do - assert_equal 'test/t/syntax.rb', __FILE__ + file = __FILE__ + assert_true 'test/t/syntax.rb' == file || 'test\t\syntax.rb' == file end assert('__LINE__') do - assert_equal 6, __LINE__ + assert_equal 7, __LINE__ end assert('super', '11.3.4') do |
