From 717317dbca3818ec3733e9f58c9d386da72ee84e Mon Sep 17 00:00:00 2001 From: Tadashi Saito Date: Tue, 8 Aug 2023 00:02:12 +0900 Subject: [PATCH] WIP --- lib/int.rb | 31 ----------------- test/int_test.rb | 88 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 33 deletions(-) diff --git a/lib/int.rb b/lib/int.rb index f993cf0..7647c03 100644 --- a/lib/int.rb +++ b/lib/int.rb @@ -106,34 +106,3 @@ def inspect = self.class.name + to_s.sub('..', "..(#{median})..") end def Int(...) = Int.new(...) - -if $0 == __FILE__ - x, y = Int(1, 2), Int(2, 3) - pp(x:, y:) - - pp [x == y, x != y] - pp [x >= y, x <= y] - pp [y >= x, y <= x] - pp [x.comparable?(y), y.comparable?(x)] - - z = Int(1.1, 1.4) - pp z - pp [x.comparable?(z), z.comparable?(x)] - pp z.comparable?(:foo) - pp z.round(1) - pp Int.error(0.2) - - pp [x.with_error(0.01), x.with_error_percent(1)].map{_1.round(2)} - - pp [z / 2, z / z].map{_1.round(2)} - - i = Int(0, 1.2) - j = Int(-1.2, 0) - pp [x/i, x/j] - ji = j.send(:invert) - pp ji - pp [i, ji] - #pp i*ji - #pp i/j - #pp j/i -end diff --git a/test/int_test.rb b/test/int_test.rb index 155eaf4..0322987 100644 --- a/test/int_test.rb +++ b/test/int_test.rb @@ -7,7 +7,91 @@ class IntTest < Test::Unit::TestCase end end - test "something useful" do - assert_equal(true, !!!false) + test 'new' do + assert_nothing_raised do + _, _ = Int(1, 2), Int(2, 3) + end + end + + setup do + @x = Int(1, 2) + @y = Int(2, 3) + @z = Int(1.1, 1.4) + end + attr_reader :x, :y, :z + + test 'comparison' do + assert_not_equal(x, y) + assert_not_operator(x, :==, y) + + assert_operator(x, :<=, y) + assert_not_operator(x, :>=, y) + + assert_operator(y, :>=, x) + assert_not_operator(y, :<=, x) + + assert_true(x.comparable?(y)) + assert_true(y.comparable?(x)) + end + + test 'cmp2' do + assert_false(x.comparable?(z)) + assert_false(z.comparable?(x)) + assert_false(z.comparable?(:foo)) + end + + test 'rounding and error' do + z.round(1) # todo + e = Int.error(0.2) + assert_in_delta(0.8, e.begin, 0.001) + assert_in_delta(1.2, e.end, 0.001) + + x1, x2 = x.with_error(0.01), x.with_error_percent(1) + #assert_in_delta(x1, x2, 0.001) + assert_in_delta(x1.begin, x2.begin, 0.001) + assert_in_delta(x1.end, x2.end, 0.001) + end + + test '/' do + [z / 2, z / z].map{_1.round(2)} #todo + + i = Int(0, 1.2) + j = Int(-1.2, 0) + [x/i, x/j] #todo + z.round(1) #todo end end + + +=begin +if $0 == __FILE__ + x, y = Int(1, 2), Int(2, 3) + pp(x:, y:) + + pp [x == y, x != y] + pp [x >= y, x <= y] + pp [y >= x, y <= x] + pp [x.comparable?(y), y.comparable?(x)] + + z = Int(1.1, 1.4) + pp z + pp [x.comparable?(z), z.comparable?(x)] + pp z.comparable?(:foo) + pp z.round(1) + pp Int.error(0.2) + + pp [x.with_error(0.01), x.with_error_percent(1)].map{_1.round(2)} + + pp [z / 2, z / z].map{_1.round(2)} + + i = Int(0, 1.2) + j = Int(-1.2, 0) + pp [x/i, x/j] + ji = j.send(:invert) + pp ji + pp [i, ji] + #pp i*ji + #pp i/j + #pp j/i +end +=end