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.12 examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/examples/lambda_calculus/arithmetic_test.rb