test/polynomial_test.rb in polynomials-0.2.0 vs test/polynomial_test.rb in polynomials-0.3.0

- old
+ new

@@ -1,9 +1,10 @@ -require_relative 'test_helper' +require "#{File.dirname(File.expand_path(__FILE__))}/test_helper" Infinity = 1.0/0 class TestPolynomial < MiniTest::Unit::TestCase + include Polynomials def test_to_s polynomial = Polynomial.parse('5x + 2x^2 + 20') assert_equal '2x^2 + 5x + 20', polynomial.to_s polynomial = Polynomial.parse('6.5435x^234 + 5.0 + 20x') assert_equal '6.5435x^234 + 20x + 5', polynomial.to_s @@ -43,27 +44,27 @@ assert_equal 5,Polynomial.parse('3x^5 - 5 x + 3').degree end def test_extrema polynomial = Polynomial.parse('3x^2 + 2x + 1') - assert_equal({ min: Set[-2.0/6.0] }, polynomial.local_extrema) + assert_set_eql(Set[ Extremum.new(-2.0/6.0,polynomial.calculate(-2.0/6.0), :minimum) ], polynomial.local_extrema) polynomial = Polynomial.parse('5x^3 - 5x^2 + 2x - 2') end def test_extrema_with_slope_of_derivative_equal_to_zero polynomial = Polynomial.parse('1x^4') - assert_equal({ min: Set[0.0] }, polynomial.local_extrema) - assert_equal({ max: Set[1.0/0, -1.0/0], min: Set[0.0]}, polynomial.extrema) + assert_set_eql(Set[ Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.local_extrema) + assert_set_eql(Set[*[Infinity,-Infinity].map { |x| Extremum.new(x,nil, :maximum)}, Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.extrema) end def test_no_local_extrema polynomial = Polynomial.parse('6x^6 - 5x + 50') - assert_equal({}, polynomial.local_extrema) - assert_equal({max: Set[ -1.0/0, 1.0/0 ]}, polynomial.extrema) + assert_equal(Set[], polynomial.local_extrema) + assert_set_eql(Set[*[ -1.0/0, 1.0/0 ].map { |x| Extremum.new(x, nil, :maximum)}], polynomial.extrema) end - def test_no_inflection_points + def test_curvature_behaviour_no_inflection_points polynomial = Polynomial.parse('1 x^4') assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour) polynomial = Polynomial.parse('-1 x^4') assert_equal({ right: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour) @@ -72,11 +73,11 @@ assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour) end def test_no_extremums polynomial = Polynomial.parse('5') - assert_equal({}, polynomial.extrema) + assert_equal(Set[], polynomial.extrema) end def test_no_curvature polynomials = [] polynomials << Polynomial.parse('5 x + 4') @@ -89,24 +90,24 @@ def test_local_extrema_of_derivative_leads_to_no_curvature polynomial = Polynomial.parse('50x^10 - 20x^2') assert_equal({}, polynomial.curvature_behaviour) end - def test_two_inflection_points + def test_curvature_behaviour_two_inflection_points polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0') assert_equal({left:[-1.0/0..-2.5649778198, 0.0649778198..1.0/0], right: [-2.5649778198..0.0649778198] } , polynomial.curvature_behaviour) end - def test_three_inflection_points + def test_curvature_behaviour_three_inflection_points polynomial = Polynomial.new(20,4,0,-1,-200) assert_equal( {:right=>[(-1/10)..0.0], :left=>[-Infinity..(-1/10), 0.0..Infinity]}, polynomial.curvature_behaviour) end def test_efficient_roots_calculation polynomial = Polynomial.parse('200x^2342435 + 6x^20') - assert_equal(Set[0.0], polynomial.roots) + assert_set_eql(Set[Root.new(0.0)], polynomial.roots) end def test_lt polynomial = Polynomial.parse('1x^2342435 + 5x') assert_euqal Term.new(5,1), polynomial.lt @@ -118,7 +119,12 @@ end def test_initalizer polynomial = Polynomial.new(5,0,2,-1) assert_equal '5x^3 + 2x - 1', polynomial.to_s + end + + def test_inflection_points + polynomial = Polynomial.new(20,4,0,-1,-200) + assert_set_eql Set[InflectionPoint.new(-1/10,polynomial.(-1/10)),InflectionPoint.new(0,polynomial.(0))], polynomial.inflection_points end end