Commit d45ecc32 authored by Daniel Bovensiepen's avatar Daniel Bovensiepen

Complete ISO test cases for Hash, Range, String and Symbol

parent d0a7b083
......@@ -3,3 +3,4 @@ Original Authors "mruby developers" are:
FUKUOKA CSK CORPORATION
Kyushu Institute of Technology
Network Applied Communication Laboratory, Inc.
Daniel Bovensiepen
##
# Hash ISO Test
assert('Hash', '15.2.13') do
Hash.class == Class
end
assert('Hash#==', '15.2.13.4.1') do
({ 'abc' => 'abc' } == { 'abc' => 'abc' }) and
not ({ 'abc' => 'abc' } == { 'cba' => 'cba' })
end
assert('Hash#[]', '15.2.13.4.2') do
a = { 'abc' => 'abc' }
a['abc'] == 'abc'
end
assert('Hash#[]=', '15.2.13.4.3') do
a = Hash.new
a['abc'] = 'abc'
a['abc'] == 'abc'
end
assert('Hash#clear', '15.2.13.4.4') do
a = { 'abc' => 'abc' }
a.clear
a == { }
end
assert('Hash#default', '15.2.13.4.5') do
a = Hash.new
b = Hash.new('abc')
c = Hash.new {|s,k| s[k] = k}
a.default == nil and b.default == 'abc' and
c.default == nil and c.default('abc') == 'abc'
end
assert('Hash#default=', '15.2.13.4.6') do
a = { 'abc' => 'abc' }
a.default = 'cba'
a['abc'] == 'abc' and a['notexist'] == 'cba'
end
assert('Hash#default_proc', '15.2.13.4.7') do
a = Hash.new
b = Hash.new {|s,k| s[k] = k}
a.default_proc == nil and b.default_proc.class == Proc
end
assert('Hash#delete', '15.2.13.4.8') do
a = { 'abc' => 'abc' }
b = { 'abc' => 'abc' }
b_tmp_1 = false
b_tmp_2 = false
a.delete('abc')
b.delete('abc') do |k|
b_tmp_1 = true
end
b.delete('abc') do |k|
b_tmp_2 = true
end
a.delete('cba') == nil and not a.has_key?('abc') and
not b_tmp_1 and b_tmp_2
end
assert('Hash#each', '15.2.13.4.9') do
a = { 'abc_key' => 'abc_value' }
key = nil
value = nil
a.each do |k,v|
key = k
value = v
end
key == 'abc_key' and value == 'abc_value'
end
assert('Hash#each_key', '15.2.13.4.10') do
a = { 'abc_key' => 'abc_value' }
key = nil
a.each_key do |k|
key = k
end
key == 'abc_key'
end
assert('Hash#each_value', '15.2.13.4.11') do
a = { 'abc_key' => 'abc_value' }
value = nil
a.each_value do |v|
value = v
end
value == 'abc_value'
end
assert('Hash#empty?', '15.2.13.4.12') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
not a.empty? and b.empty?
end
assert('Hash#has_key?', '15.2.13.4.13') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.has_key?('abc_key') and not b.has_key?('cba')
end
assert('Hash#has_value?', '15.2.13.4.14') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.has_value?('abc_value') and not b.has_value?('cba')
end
assert('Hash#include?', '15.2.13.4.15') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.include?('abc_key') and not b.include?('cba')
end
assert('Hash#initialize copy', '15.2.13.4.17') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new.initialize_copy(a)
b == { 'abc_key' => 'abc_value' }
end
assert('Hash#key?', '15.2.13.4.18') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.key?('abc_key') and not b.key?('cba')
end
assert('Hash#keys', '15.2.13.4.19') do
a = { 'abc_key' => 'abc_value' }
a.keys == ['abc_key']
end
assert('Hash#length', '15.2.13.4.20') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.length == 1 and b.length == 0
end
assert('Hash#member?', '15.2.13.4.21') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.member?('abc_key') and not b.member?('cba')
end
assert('Hash#merge', '15.2.13.4.22') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' }
result_1 = a.merge b
result_2 = a.merge(b) do |key, original, new|
original
end
result_1 == {'abc_key' => 'abc_value', 'cba_key' => 'XXX',
'xyz_key' => 'xyz_value' } and
result_2 == {'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
'xyz_key' => 'xyz_value' }
end
assert('Hash#replace', '15.2.13.4.23') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new.replace(a)
b == { 'abc_key' => 'abc_value' }
end
assert('Hash#shift', '15.2.13.4.24') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
# TODO Broken ATM (Hash instead of Array
b = a.shift
a == { 'abc_key' => 'abc_value' }
end
assert('Hash#size', '15.2.13.4.25') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.size == 1 and b.size == 0
end
assert('Hash#store', '15.2.13.4.26') do
a = Hash.new
a.store('abc', 'abc')
a['abc'] == 'abc'
end
assert('Hash#value?', '15.2.13.4.27') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
a.value?('abc_value') and not b.value?('cba')
end
assert('Hash#values', '15.2.13.4.28') do
a = { 'abc_key' => 'abc_value' }
a.values == ['abc_value']
end
##
# Range ISO Test
assert('Range', '15.2.14') do
Range.class == Class
end
assert('Range#==', '15.2.14.4.1') do
(1..10) == (1..10) and not (1..10) == (1..100)
end
assert('Range#===', '15.2.14.4.2') do
a = (1..10)
a === 5 and not a === 20
end
assert('Range#begin', '15.2.14.4.3') do
(1..10).begin == 1
end
assert('Range#each', '15.2.14.4.4') do
a = (1..3)
b = 0
a.each {|i| b += i}
b == 6
end
assert('Range#end', '15.2.14.4.5') do
(1..10).end == 10
end
assert('Range#exclude_end?', '15.2.14.4.6') do
(1...10).exclude_end? and not (1..10).exclude_end?
end
assert('Range#first', '15.2.14.4.7') do
(1..10).first == 1
end
assert('Range#include', '15.2.14.4.8') do
a = (1..10)
a.include?(5) and not a.include?(20)
end
# TODO SEGFAULT ATM
#assert('Range#initialize', '15.2.14.4.9') do
# a = Range.new(1, 10, true)
# b = Range.new(1, 10, false)
#
# a == (1..10) and a.exclude_end? and b == (1..10) and
# not b.exclude_end?
#end
assert('Range#last', '15.2.14.4.10') do
(1..10).last == 10
end
assert('Range#member?', '15.2.14.4.11') do
a = (1..10)
a.member?(5) and not a.member?(20)
end
##
# String ISO Test
assert('String', '15.2.10') do
String.class == Class
end
assert('String#*', '15.2.10.5.1') do
'a' * 5 == 'aaaaa'
end
assert('String#+', '15.2.10.5.2') do
'a' + 'b' == 'ab'
end
assert('String#<=>', '15.2.10.5.3') do
a = '' <=> ''
b = '' <=> 'not empty'
c = 'not empty' <=> ''
d = 'abc' <=> 'cba'
e = 'cba' <=> 'abc'
a == 0 and b == -1 and c == 1 and
d == -1 and e == 1
end
assert('String#==', '15.2.10.5.4') do
'abc' == 'abc' and not 'abc' == 'cba'
end
# TODO: SEGFAULT ATM assert('String#=~', '15.2.10.5.5')
assert('String#[]', '15.2.10.5.6') do
# length of args is 1
a = 'abc'[0]
b = 'abc'[-1]
c = 'abc'[10]
d = 'abc'[-10]
# length of args is 2
a1 = 'abc'[0, -1]
b1 = 'abc'[10, 0]
c1 = 'abc'[-10, 0]
d1 = 'abc'[0, 0]
e1 = 'abc'[1, 2]
# args is RegExp
# TODO SEGFAULT ATM
# args is String
a3 = 'abc'['bc']
b3 = 'abc'['XX']
a == 'a' and b == 'c' and c == nil and d == nil and
a1 == nil and b1 == nil and c1 == nil and d1 == '' and
e1 == 'bc' and
a3 == 'bc' and b3 == nil
end
assert('String#capitalize', '15.2.10.5.7') do
a = 'abc'
a.capitalize
a == 'abc' and 'abc'.capitalize == 'Abc'
end
assert('String#capitalize!', '15.2.10.5.8') do
a = 'abc'
a.capitalize!
a == 'Abc'
end
assert('String#chomp', '15.2.10.5.9') do
a = 'abc'.chomp
b = ''.chomp
c = "abc\n".chomp
d = "abc\n\n".chomp
e = "abc\t".chomp("\t")
f = "abc\n"
f.chomp
a == 'abc' and b == '' and c == 'abc' and
d == "abc\n" and e == 'abc' and f == "abc\n"
end
assert('String#chomp', '15.2.10.5.10') do
a = 'abc'
b = ''
c = "abc\n"
d = "abc\n\n"
e = "abc\t"
a.chomp!
b.chomp!
c.chomp!
d.chomp!
e.chomp!("\t")
a == 'abc' and b == '' and c == 'abc' and
d == "abc\n" and e == 'abc'
end
assert('String#chop', '15.2.10.5.11') do
a = ''.chop
b = 'abc'.chop
c = 'abc'
c.chop
a == '' and b == 'ab' and c == 'abc'
end
assert('String#chop!', '15.2.10.5.12') do
a = ''
b = 'abc'
a.chop!
b.chop!
a == '' and b == 'ab'
end
assert('String#downcase', '15.2.10.5.13') do
a = 'ABC'.downcase
b = 'ABC'
b.downcase
a == 'abc' and b == 'ABC'
end
assert('String#downcase!', '15.2.10.5.14') do
a = 'ABC'
a.downcase!
a == 'abc'
end
assert('String#each_line', '15.2.10.5.15') do
a = "first line\nsecond line\nthird line"
list = ["first line\n", "second line\n", "third line"]
n_list = []
a.each_line do |line|
n_list << line
end
list == n_list
end
assert('String#empty?', '15.2.10.5.16') do
a = ''
b = 'not empty'
a.empty? and not b.empty?
end
assert('String#eql?', '15.2.10.5.17') do
'abc'.eql?('abc') and not 'abc'.eql?('cba')
end
# TODO ATM broken assert('String#gsub', '15.2.10.5.18') do
# TODO ATM broken assert('String#gsub!', '15.2.10.5.19') do
assert('String#hash', '15.2.10.5.20') do
a = 'abc'
a.hash == 'abc'.hash
end
assert('String#include?', '15.2.10.5.21') do
'abc'.include?(97) and not 'abc'.include?(100) and
'abc'.include?('a') and not 'abc'.include?('d')
end
assert('String#index', '15.2.10.5.22') do
'abc'.index('a') == 0 and 'abc'.index('d') == nil and
'abcabc'.index('a', 1) == 3
end
assert('String#initialize', '15.2.10.5.23') do
a = ''
a.initialize('abc')
a == 'abc'
end
assert('String#initialize_copy', '15.2.10.5.24') do
a = ''
a.initialize_copy('abc')
a == 'abc'
end
assert('String#intern', '15.2.10.5.25') do
'abc'.intern == :abc
end
assert('String#length', '15.2.10.5.26') do
'abc'.length == 3
end
# TODO Broken ATM assert('String#match', '15.2.10.5.27') do
assert('String#replace', '15.2.10.5.28') do
a = ''
a.replace('abc')
a == 'abc'
end
assert('String#reverse', '15.2.10.5.29') do
a = 'abc'
a.reverse
a == 'abc' and 'abc'.reverse == 'cba'
end
assert('String#reverse!', '15.2.10.5.30') do
a = 'abc'
a.reverse!
a == 'cba' and 'abc'.reverse! == 'cba'
end
assert('String#rindex', '15.2.10.5.31') do
'abc'.rindex('a') == 0 and 'abc'.rindex('d') == nil and
'abcabc'.rindex('a', 1) == 0 and 'abcabc'.rindex('a', 4) == 3
end
# TODO Broken ATM assert('String#scan', '15.2.10.5.32') do
assert('String#size', '15.2.10.5.33') do
'abc'.size == 3
end
assert('String#slice', '15.2.10.5.34') do
# length of args is 1
a = 'abc'.slice(0)
b = 'abc'.slice(-1)
c = 'abc'.slice(10)
d = 'abc'.slice(-10)
# length of args is 2
a1 = 'abc'.slice(0, -1)
b1 = 'abc'.slice(10, 0)
c1 = 'abc'.slice(-10, 0)
d1 = 'abc'.slice(0, 0)
e1 = 'abc'.slice(1, 2)
# args is RegExp
# TODO SEGFAULT ATM
# args is String
a3 = 'abc'.slice('bc')
b3 = 'abc'.slice('XX')
a == 'a' and b == 'c' and c == nil and d == nil and
a1 == nil and b1 == nil and c1 == nil and d1 == '' and
e1 == 'bc' and
a3 == 'bc' and b3 == nil
end
# TODO Broken ATM
assert('String#split', '15.2.10.5.35') do
# without RegExp behavior is actually unspecified
a = 'abc abc abc'.split
a == ['abc', 'abc', 'abc']
end
# TODO ATM broken assert('String#sub', '15.2.10.5.36') do
# TODO ATM broken assert('String#sub!', '15.2.10.5.37') do
assert('String#to_i', '15.2.10.5.38') do
a = ''.to_i
b = '123456789'.to_i
c = 'a'.to_i(16)
d = '100'.to_i(2)
a == 0 and b == 123456789 and c == 10 and d == 4
end
assert('String#to_f', '15.2.10.5.39') do
a = ''.to_f
b = '123456789'.to_f
c = '12345.6789'.to_f
check_float(a, 0.0) and check_float(b, 123456789.0) and
check_float(c, 12345.6789)
end
assert('String#to_s', '15.2.10.5.40') do
'abc'.to_s == 'abc'
end
assert('String#to_sym', '15.2.10.5.41') do
'abc'.to_sym == :abc
end
assert('String#upcase', '15.2.10.5.42') do
a = 'abc'.upcase
b = 'abc'
b.upcase
a == 'ABC' and b == 'abc'
end
assert('String#upcase!', '15.2.10.5.43') do
a = 'abc'
a.upcase!
a == 'ABC'
end
##
# Symbol ISO Test
assert('Symbol', '15.2.11') do
Symbol.class == Class
end
assert('Symbol#===', '15.2.11.3.1') do
:abc === :abc and not :abc === :cba
end
assert('Symbol#id2name', '15.2.11.3.2') do
:abc.id2name == 'abc'
end
assert('Symbol#to_s', '15.2.11.3.3') do
:abc.to_s == 'abc'
end
assert('Symbol#to_sym', '15.2.11.3.4') do
:abc.to_sym == :abc
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment