-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_convert.rb
57 lines (47 loc) · 1.94 KB
/
test_convert.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require_relative "convert"
require "test/unit"
class TestConvert < Test::Unit::TestCase
def setup
# pass
end
def teardown
# pass
end
def test_invalid
assert_throws(ArgumentError) {Convert.string_to_integer('kmkywap')}
assert_throws(ArgumentError) {Convert.string_to_integer('.')}
assert_throws(ArgumentError) {Convert.string_to_integer('.0')}
assert_throws(ArgumentError) {Convert.string_to_integer('.9')}
end
def test_well_behaved
assert_nothing_thrown() {Convert.string_to_integer('1')}
end
def test_signs
assert_equal(1, Convert.string_to_integer('1'))
assert_equal(1, Convert.string_to_integer('+1'))
assert_equal(1, Convert.string_to_integer(' + 1 '))
assert_equal(-1, Convert.string_to_integer('-1'))
assert_equal(-1, Convert.string_to_integer(' - 1 '))
assert_equal(0, Convert.string_to_integer('0'))
assert_equal(0, Convert.string_to_integer('+0'))
assert_equal(0, Convert.string_to_integer(' + 0 '))
assert_equal(0, Convert.string_to_integer('-0'))
assert_equal(0, Convert.string_to_integer(' - 0 '))
assert_throws(ArgumentError) {Convert.string_to_integer('+-1')}
assert_throws(ArgumentError) {Convert.string_to_integer('-+1')}
end
def test_float_valid
assert_equal(1, Convert.string_to_integer('1.1'))
assert_equal(1, Convert.string_to_integer('1.8'))
assert_equal(1, Convert.string_to_integer('1.'))
assert_equal(1, Convert.string_to_integer('+1.8'))
assert_equal(-1, Convert.string_to_integer('-1.9'))
assert_equal(1, Convert.string_to_integer(' + 1.9....3 '))
end
def test_large_valid
assert_equal(123456789, Convert.string_to_integer('0123456789'))
assert_equal(9876543210, Convert.string_to_integer('9876543210'))
assert_equal(9876543210987654321098765432109876543210,
Convert.string_to_integer('9876543210987654321098765432109876543210'))
end
end