fix integer to_s and put, with tests

This commit is contained in:
Torsten Ruger 2015-11-10 19:09:17 +02:00
parent 760a4beb86
commit c92814440f
3 changed files with 88 additions and 21 deletions

View File

@ -1,20 +1,25 @@
class Integer < Value
int digit()
if_plus( self - 10)
int as_char()
if_plus( self - 9)
return 32
else
Word numbers = "0123456789"
return numbers.at(self)
int index = self + 1
return numbers.char_at(index)
end
end
Word as_string(Word str)
if_minus( self - 10 )
int num = digit()
str = str.add_digit( num )
int num = as_char()
str = str.push_char( num )
else
int rest = self / 10
str = rest.as_string( str )
rest = rest * 10
rest = self - rest
str = rest.as_string(str)
end
return str
end
@ -23,4 +28,10 @@ class Integer < Value
Word start = " "
return as_string( start )
end
int puti()
Word str = self.to_s()
str.putstring()
return self
end
end

View File

@ -0,0 +1,72 @@
require_relative 'helper'
class TestPutiRT < MiniTest::Test
include RuntimeTests
def test_as_char1
@string_input = <<HERE
return 5.as_char()
HERE
check_return 53
end
def test_as_char2
@string_input = <<HERE
return 10.as_char()
HERE
check_return 32
end
def test_tos_one_digit
@string_input = <<HERE
Word five = 5.to_s()
five.putstring()
HERE
@stdout = " 5"
check
end
def test_tos_zero
@string_input = <<HERE
Word five = 0.to_s()
five.putstring()
HERE
@stdout = " 0"
check
end
def test_tos_two_digit
@string_input = <<HERE
Word five = 15.to_s()
five.putstring()
HERE
@stdout = " 15"
check
end
def test_tos_three_digit
@string_input = <<HERE
Word five = 150.to_s()
five.putstring()
HERE
@stdout = " 150"
check
end
def test_puti_four_digit
@string_input = "return 1234.puti()"
@stdout = " 1234"
check_return 1234
end
def test_puti_seven_digit
@string_input = "return 1234567.puti()"
@stdout = " 1234567"
check_return 1234567
end
def test_puti_eigth_digit
@string_input = "return 123445678.puti()"
@stdout = " 123445678"
check_return 123445678
end
end

View File

@ -1,16 +0,0 @@
require_relative 'helper'
class TestPutiRT < MiniTest::Test
include RuntimeTests
def test_puti
@string_input = <<HERE
Word five = 4.to_s()
five.putstring()
HERE
@stdout = "5"
check
end
end