Sha256: 9c85220908937b9aa6b70c33c9fb8fb1a9a0318b9b8092166885abdbba98c3ab

Contents?: true

Size: 1.29 KB

Versions: 201

Compression:

Stored size: 1.29 KB

Contents

dir = File.dirname(__FILE__)
require File.expand_path("#{dir}/test_helper")

require File.expand_path("#{dir}/arithmetic_node_classes")
Treetop.load File.expand_path("#{dir}/arithmetic")

class ArithmeticParserTest < Test::Unit::TestCase
  include ParserTestHelper
  
  def setup
    @parser = ArithmeticParser.new
  end
  
  def test_number
    assert_equal 0, parse('0').eval
    assert_equal 1, parse('1').eval
    assert_equal 123, parse('123').eval
  end
  
  def test_variable
    assert_equal 0, parse('x').eval('x' => 0)
    assert_equal 3, parse('x').eval('x' => 3)
    assert_equal 10, parse('y').eval('y' => 10)
  end
  
  def test_addition
    assert_equal 10, parse('x + 5').eval('x' => 5)
  end
  
  def test_subtraction
    assert_equal 0, parse('x - 5').eval('x' => 5)
  end
  
  def test_multiplication
    assert_equal 6, parse('x * 2').eval('x' => 3)
  end
  
  def test_division
    assert_equal 3, parse('x / 2').eval('x' => 6)
  end
  
  def test_order_of_operations
    assert_equal 11, parse('1 + 2 * 3 + 4').eval
  end
  
  def test_left_to_right
    assert_equal 2, parse('5 - 2 - 1').eval
  end
  
  def test_parentheses
    assert_equal 25, parse('(5 + x) * (10 - y)').eval('x' => 0, 'y' => 5)
  end
  
  def test_equality
    assert parse('4 == 4').eval
    assert !parse('4 == 3').eval
  end
end

Version data entries

201 entries across 186 versions & 30 rubygems

Version Path
treetop-1.6.3 examples/lambda_calculus/arithmetic_test.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
treetop-1.6.2 examples/lambda_calculus/arithmetic_test.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
treetop-1.5.3 examples/lambda_calculus/arithmetic_test.rb
treetop-1.5.1 examples/lambda_calculus/arithmetic_test.rb
treetop-1.4.15 examples/lambda_calculus/arithmetic_test.rb
challah-1.0.0 vendor/bundle/gems/treetop-1.4.14/examples/lambda_calculus/arithmetic_test.rb
classiccms-0.7.2 vendor/bundle/gems/treetop-1.4.10/examples/lambda_calculus/arithmetic_test.rb
classiccms-0.7.1 vendor/bundle/gems/treetop-1.4.10/examples/lambda_calculus/arithmetic_test.rb
swipe-rails-0.0.5 vendor/bundle/gems/treetop-1.4.14/examples/lambda_calculus/arithmetic_test.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
treetop-1.4.14 examples/lambda_calculus/arithmetic_test.rb
classiccms-0.7.0 vendor/bundle/gems/treetop-1.4.10/examples/lambda_calculus/arithmetic_test.rb
challah-1.0.0.beta3 vendor/bundle/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb
challah-1.0.0.beta2 vendor/bundle/gems/treetop-1.4.12/examples/lambda_calculus/arithmetic_test.rb