File: calc_test.rb

package info (click to toggle)
ruby-citrus 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 3,417; makefile: 5
file content (120 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require File.expand_path('../../helper', __FILE__)
require 'citrus/grammars'

Citrus.require 'calc'

class CalcTest < Test::Unit::TestCase
  # A helper method that tests the successful parsing and evaluation of the
  # given mathematical expression.
  def do_test(expr)
    match = ::Calc.parse(expr)
    assert(match)
    assert_equal(expr, match)
    assert_equal(expr.length, match.length)
    assert_equal(eval(expr), match.value)
  end

  def test_int
    do_test('3')
  end

  def test_float
    do_test('1.5')
  end

  def test_addition
    do_test('1+2')
  end

  def test_addition_multi
    do_test('1+2+3')
  end

  def test_addition_float
    do_test('1.5+3')
  end

  def test_subtraction
    do_test('3-2')
  end

  def test_subtraction_float
    do_test('4.5-3')
  end

  def test_multiplication
    do_test('2*5')
  end

  def test_multiplication_float
    do_test('1.5*3')
  end

  def test_division
    do_test('20/5')
  end

  def test_division_float
    do_test('4.5/3')
  end

  def test_complex
    do_test('7*4+3.5*(4.5/3)')
  end

  def test_complex_spaced
    do_test('7 * 4 + 3.5 * (4.5 / 3)')
  end

  def test_complex_with_underscores
    do_test('(12_000 / 3) * 2.5')
  end

  def test_modulo
    do_test('3 % 2 + 4')
  end

  def test_exponent
    do_test('2**9')
  end

  def test_exponent_float
    do_test('2**2.2')
  end

  def test_negative_exponent
    do_test('2**-3')
  end

  def test_exponent_exponent
    do_test('2**2**2')
  end

  def test_exponent_group
    do_test('2**(3+1)')
  end

  def test_negative
    do_test('-5')
  end

  def test_double_negative
    do_test('--5')
  end

  def test_complement
    do_test('~4')
  end

  def test_double_complement
    do_test('~~4')
  end

  def test_mixed_unary
    do_test('~-4')
  end

  def test_complex_with_negatives
    do_test('4 * -7 / (8.0 + 1_2)**2')
  end
end