# Starter file for tests for Assignment 8 (trees and symbolic differentiation) # There should be enough tests here for symbolic differentiation -- you # should add another class TestTree for unit tests for the tree classes. require 'test/unit' require 'assign8' # put definition here for tree tests: # class TestTree < Test::Unit::TestCase class TestDeriv < Test::Unit::TestCase def test_simplify x = Variable.new("x") assert_equal(10, (0*(x+3)+10).simplify) assert_equal(3.5, (0*(x+2.8)+3.5).simplify) end def test_to_s x = Variable.new("x") assert_equal("x", x.to_s) # these tests assume that you turn expressions into fully-parenthesized # strings -- change these if you remove unneeded parentheses assert_equal("(3*(10+x))", (3*(10+x)).to_s) assert_equal("((10*x)+3)", (10*x+3).to_s) end def test_const_and_var x = Variable.new("x") y = Variable.new("y") assert_equal(0, 3.deriv(x)) assert_equal(1, x.deriv(x)) assert_equal(0, y.deriv(x)) end def test_plus x = Variable.new("x") y = Variable.new("y") assert_equal(1, (x+y).deriv(x)) assert_equal(1, (x+3).deriv(x)) assert_equal(1, (3+x).deriv(x)) end def test_times x = Variable.new("x") y = Variable.new("y") assert_equal(y, (x*y).deriv(x)) assert_equal(3, (x*3).deriv(x)) assert_equal(3, (3*x).deriv(x)) assert_equal(2.8, (x*2.8).deriv(x)) end def test_complicated x = Variable.new("x") y = Variable.new("y") assert_equal(x*y+(x+3)*y, (x*y*(x+3)).deriv(x)) assert_equal(50, (10*(2*x+3*x+5)).deriv(x)) assert_equal(57.2, (10*(2.52*x+3.2*x+5.99)).deriv(x)) end def test_unary # test unary + and - x = Variable.new("x") assert_equal(-1*x, -x) assert_equal(x, +x) end # change the name to test_trig to enable this test (if you do the # extra credit question) def skip_test_trig x = Variable.new("x") y = Variable.new("y") assert_equal(x.cos, x.sin.deriv(x)) assert_equal(-x.sin, x.cos.deriv(x)) end end