test/test_all.rb in interpolate-0.2.2 vs test/test_all.rb in interpolate-0.2.3

- old
+ new

@@ -7,11 +7,11 @@ class InterpolationTest < Test::Unit::TestCase DELTA = 1e-7 def setup - decimal_points = { + @decimal_points = { 0 => 0, 1 => 0.1, 2 => 0.2, 3 => 0.3, 4 => 0.4, @@ -21,33 +21,32 @@ 8 => 0.8, 9 => 0.9, 10 => 1 }.freeze - array_points = { + @array_points = { 100 => [1, 10, 100], 200 => [5, 50, 500], 500 => [10, 100, 1000] }.freeze - @dec_gradient = Interpolation.new(decimal_points).freeze - @array_gradient = Interpolation.new(array_points).freeze + @dec_gradient = Interpolation.new(@decimal_points).freeze + @array_gradient = Interpolation.new(@array_points).freeze end - - def test_bad_points + def test_bad_point bad_points = { 0 => 4.2, 1 => "hello", # not allowed by default 2 => 3.4, 3 => 4.8 } + gradient = Interpolation.new(bad_points) assert_raise ArgumentError do - gradient = Interpolation.new(bad_points) + gradient.at(1.5) end - end def test_lower_bounds assert_equal(@dec_gradient.at(0), 0) assert_equal(@dec_gradient.at(-1), 0) @@ -131,10 +130,34 @@ def test_array_values assert_equal(@array_gradient.at(150), [3, 30, 300]) assert_equal(@array_gradient.at(200), [5, 50, 500]) assert_equal(@array_gradient.at(350), [7.5, 75, 750]) end - + + def test_given_block_left + gradient = Interpolation.new(@decimal_points) {|l, r, bal| l} + assert_in_delta(gradient.at(1.5), 0.1, DELTA) + assert_in_delta(gradient.at(2.5), 0.2, DELTA) + assert_in_delta(gradient.at(3.5), 0.3, DELTA) + end + + def test_given_block_right + gradient = Interpolation.new(@decimal_points) {|l, r, bal| r} + assert_in_delta(gradient.at(1.5), 0.2, DELTA) + assert_in_delta(gradient.at(2.5), 0.3, DELTA) + assert_in_delta(gradient.at(3.5), 0.4, DELTA) + end + + def test_given_block_mean_squared + # squared balance, not linear + gradient = Interpolation.new(@decimal_points) do |l, r, bal| + l + ((r - l).to_f * bal * bal) + end + assert_in_delta(gradient.at(1.5), 0.12500, DELTA) + assert_in_delta(gradient.at(2.75), 0.25625, DELTA) + assert_in_delta(gradient.at(3.99), 0.39801, DELTA) + end + def test_frozen_points a = @array_gradient.at(200) assert_nothing_raised RuntimeError do a[0] = 10 a[1] = 70