Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Jan 9, 2024
1 parent d4ae16a commit 717317d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
31 changes: 0 additions & 31 deletions lib/int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
88 changes: 86 additions & 2 deletions test/int_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 717317d

Please sign in to comment.