evaluator 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - ruby-head
5
+ - jruby
6
+ - rbx-18mode
7
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gemspec
3
+
@@ -27,7 +27,7 @@ it as follows:
27
27
  6241509647120.42 MeV
28
28
 
29
29
  The calculator loads a few natural constants at startup (calc.startup). For unit support
30
- my minad-units library is used. Units are denoted in brackets e.g. [meter], [kV] etc
30
+ my unit gem is used. Units are denoted in brackets e.g. [meter], [kV] etc
31
31
 
32
32
  Authors
33
33
  -------
data/Rakefile CHANGED
@@ -1,9 +1,6 @@
1
- require 'hoe'
1
+ task :default => :test
2
2
 
3
- $:.unshift 'lib'
4
- require 'evaluator'
5
-
6
- Hoe.new 'evaluator', Evaluator::VERSION do |evaluator|
7
- evaluator.developer 'Daniel Mendler', 'mail@daniel-mendler.de'
3
+ desc 'Run tests with bacon'
4
+ task :test => FileList['test/*_test.rb'] do |t|
5
+ sh "bacon -q -Ilib:test #{t.prerequisites.join(' ')}"
8
6
  end
9
-
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'evaluator'
6
+ require 'readline'
7
+
8
+ def process(line, vars, quiet = false)
9
+ exit if line == 'exit' || line == 'quit'
10
+ if line =~ /^(\w+)\s*:=?\s*(.*)$/
11
+ vars[$1] = Evaluator($2, vars)
12
+ else
13
+ Evaluator(line, vars)
14
+ end
15
+ rescue Exception => ex
16
+ quiet ? nil : ex.message
17
+ end
18
+
19
+ vars = {}
20
+ File.read(File.expand_path(File.join(File.dirname(__FILE__), '..', 'evaluator.constants'))).split("\n").each { |line| process(line, vars, true) }
21
+
22
+ loop do
23
+ line = Readline::readline('> ')
24
+ if !line
25
+ puts
26
+ break
27
+ end
28
+ Readline::HISTORY.push(line)
29
+ puts process(line, vars)
30
+ end
@@ -0,0 +1,18 @@
1
+ SpeedOfLight: 2.99792458e8 [m/s]
2
+ ElementaryCharge: 1.602176487e-19 [C]
3
+ ElectronMass: 9.10938215e-31 [kg]
4
+ ProtonMass: 1.67262158e-27 [kg]
5
+ NeutronMass: 1.67492716e-27 [kg]
6
+ AvogadroConstant: 6.02214179e+23 [1/mole]
7
+ BoltzmannConstant: 1.3806504e-23 [J/K]
8
+ PlanckConstant: 6.62606896e-34 [J s]
9
+ ReducedPlanckConstant: PlanckConstant / (2 * pi)
10
+ GravitationConstant: 6.67428e-11 [m^3 / (kg * s^2)]
11
+ GasConstant: AvogadroConstant * BoltzmannConstant
12
+ BohrRadius: 5.2917720859e−11 [m]
13
+ BohrMagneton: 9.27400915e-24 [J/T]
14
+ VacuumPermeability: 4 * pi * 1e-7 [N/A]
15
+ VacuumPermittivity: 1 / (VacuumPermeability * SpeedOfLight^2)
16
+ FineStructureConstant: ElementaryCharge^2 / (4 * pi * VacuumPermittivity * ReducedPlanckConstant * SpeedOfLight)
17
+ RydbergConstant: 10973731.568527 [1/m]
18
+ FluxQuantum: PlanckConstant / (2 * ElementaryCharge)
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/lib/evaluator'
3
+ require 'date'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'evaluator'
7
+ s.version = Evaluator::VERSION
8
+ s.date = Date.today.to_s
9
+ s.authors = ['Daniel Mendler']
10
+ s.email = ['mail@daniel-mendler.de']
11
+ s.summary = 'Mathematical expression evaluator'
12
+ s.description = 'Mathematical expression evaluator for infix notation'
13
+ s.homepage = 'http://github.com/minad/evaluator'
14
+ s.rubyforge_project = s.name
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = %w(lib)
19
+
20
+ s.add_development_dependency('bacon')
21
+ s.add_development_dependency('rake')
22
+ s.add_development_dependency('unit')
23
+ end
@@ -1,6 +1,11 @@
1
- require 'complex'
1
+ if RUBY_VERSION < '1.9'
2
+ require 'complex'
3
+ else
4
+ require 'cmath'
5
+ end
6
+
2
7
  begin
3
- require 'units'
8
+ require 'unit'
4
9
  rescue LoadError
5
10
  end
6
11
 
@@ -8,7 +13,7 @@ module Evaluator
8
13
  def self.infix(priority, unary = nil, &block) [false, priority, lambda(&block), unary] end
9
14
  def self.prefix(&block) [true, 1e5, lambda(&block)] end
10
15
 
11
- VERSION = '0.1.5'
16
+ VERSION = '0.1.6'
12
17
  OPERATOR = {
13
18
  'in' => infix(0) do |a,b|
14
19
  raise(RuntimeError, 'Unit support not available') if !a.respond_to? :in
@@ -112,7 +117,7 @@ module Evaluator
112
117
  HEX = /^0[xX][\dA-Fa-f]+$/
113
118
  OCT = /^0[0-7]+$/
114
119
  DEC = /^\d+$/
115
- SYMBOL = /^[a-zA-Z_][\w_]*$/
120
+ SYMBOL = /^[a-zA-Z_]\w*$/
116
121
  UNIT = /^\[[^\]]+\]$/
117
122
  VALUE_TOKENS = [UNIT, STRING, REAL, HEX, OCT, DEC, SYMBOL].map {|x| x.source[1..-2] }
118
123
  OPERATOR_TOKENS = OPERATOR.keys.flatten.sort { |a,b| b.length <=> a.length}.map { |x| Regexp.quote(x) }
@@ -153,8 +158,8 @@ module Evaluator
153
158
  else
154
159
  val = case tok
155
160
  when UNIT
156
- if tok.respond_to? :to_unit
157
- tok[1..-2].to_unit
161
+ if Object.const_defined?(:Unit)
162
+ Unit(tok[1..-2])
158
163
  else
159
164
  raise(RuntimeError, 'Unit support not available')
160
165
  end
@@ -0,0 +1,179 @@
1
+ require 'evaluator'
2
+
3
+ class Bacon::Context
4
+ def e(*args)
5
+ Evaluator(*args)
6
+ end
7
+ end
8
+
9
+ describe Evaluator do
10
+ it 'should parse binary operators' do
11
+ e('42 || false').should.equal 42
12
+ e('false || nil || true').should.equal true
13
+ e('42 or false').should.equal 42
14
+ e('false or nil or true').should.equal true
15
+
16
+ e('true && 42').should.equal 42
17
+ e('nil && 1').should.equal nil
18
+ e('true and 42').should.equal 42
19
+ e('nil and 1').should.equal nil
20
+
21
+ e('1+1==2').should.equal true
22
+ e("'abc' == 'a'+'b'+'c'").should.equal true
23
+
24
+ e('1+1 != 3').should.equal true
25
+ e("'xxx' != 'a'+'b'+'c'").should.equal true
26
+
27
+ e('1<=1').should.equal true
28
+ e('1<=2').should.equal true
29
+ e('1>=1').should.equal true
30
+ e('2>=1').should.equal true
31
+ e('1<1').should.equal false
32
+ e('1<2').should.equal true
33
+ e('1>1').should.equal false
34
+ e('2>1').should.equal true
35
+
36
+ e('1+2').should.equal 3
37
+ e('"x"+"yz"').should.equal 'xyz'
38
+ e("'a'+string 1+'b'").should.equal 'a1b'
39
+ e("'a'+string(1)+'b'").should.equal 'a1b'
40
+
41
+ e('5-2').should.equal 3
42
+ e('3-4').should.equal -1
43
+
44
+ e('3*4').should.equal 12
45
+ e('"ab"*3').should.equal 'ababab'
46
+
47
+ e('12/4').should.equal 3
48
+
49
+ e('103. div 10.').should.equal 10
50
+
51
+ e('7 mod 4').should.equal 3
52
+ e('7 % 4').should.equal 3
53
+
54
+ e('2 ** 10').should.equal 1024
55
+
56
+ e('2 << 2').should.equal 8
57
+ e('256 >> 3').should.equal 32
58
+
59
+ e('6 & 2').should.equal 2
60
+ e('1 | 2 | 4').should.equal 7
61
+ e('9 ^ 8').should.equal 1
62
+ end
63
+
64
+ it 'should parse unary operators' do
65
+ e('-1').should.equal -1
66
+ e('-(1+1)').should.equal -2
67
+ e('---42').should.equal -42
68
+ e('----42').should.equal 42
69
+ e('3*-3').should.equal -9
70
+ e('3*+3').should.equal 9
71
+ end
72
+
73
+ it 'should respect precendence' do
74
+ e('1+3*5').should.equal 16
75
+ e('3*5+1').should.equal 16
76
+ e('3*5+2**3').should.equal 23
77
+ end
78
+
79
+ it 'should parse constants' do
80
+ e('PI').should.equal Math::PI
81
+ e('E').should.equal Math::E
82
+ e('I').should.equal Complex::I
83
+ e('trUe').should.equal true
84
+ e('fAlSe').should.equal false
85
+ e('niL').should.equal nil
86
+ end
87
+
88
+ it 'should parse numeric functions' do
89
+ e('gcd(26, 39)').should.equal 13
90
+ e('lcm(26, 39)').should.equal 78
91
+ e('cos 42').should.equal Math.cos(42)
92
+ e('sin 42').should.equal Math.sin(42)
93
+ e('cos 42').should.equal Math.cos(42)
94
+ e('tan 42').should.equal Math.tan(42)
95
+ e('sinh 42').should.equal Math.sinh(42)
96
+ e('cosh 42').should.equal Math.cosh(42)
97
+ e('tanh 42').should.equal Math.tanh(42)
98
+ e('asin .5').should.equal Math.asin(0.5)
99
+ e('acos .5').should.equal Math.acos(0.5)
100
+ e('atan .5').should.equal Math.atan(0.5)
101
+ e('asinh .5').should.equal Math.asinh(0.5)
102
+ e('atanh .5').should.equal Math.atanh(0.5)
103
+ e('sqrt 42 + 1').should.equal Math.sqrt(42) + 1
104
+ e('log 42 + 3').should.equal Math.log(42) + 3
105
+ e('ln 42 + 3').should.equal Math.log(42) + 3
106
+ e('log10 42 + 3').should.equal Math.log10(42) + 3
107
+ e('log2 42 + 3').should.equal Math.log(42)/Math.log(2) + 3
108
+ e('3 * exp 42').should.equal 3 * Math.exp(42)
109
+ e('erf 2').should.equal Math.erf(2)
110
+ e('erfc 2').should.equal Math.erfc(2)
111
+ e('floor 42.3').should.equal 42
112
+ e('ceil 41.6').should.equal 42
113
+ e('float("3.5")').should.equal 3.5
114
+ e('string(3.5)').should.equal "3.5"
115
+ e('int("3.5")').should.equal 3
116
+ e('int(3.6)').should.equal 3
117
+ srand(42); x = rand; srand(42)
118
+ e('rand').should.equal x
119
+ e('conj(1+2*i)').should.equal Complex(1,-2)
120
+ e('Im(1+2*i)').should.equal 2
121
+ e('Re(1+2*i)').should.equal 1
122
+ e('round(3.4)').should.equal 3
123
+ e('round(3.5)').should.equal 4
124
+ e('abs -6').should.equal 6
125
+ e('plus 3').should.equal 3
126
+ e('minus 3').should.equal -3
127
+ e('!3').should.equal false
128
+ e('~3').should.equal ~3
129
+ end
130
+
131
+ it 'should parse string_functions' do
132
+ e('substr("abcde", 1, 3)').should.equal 'bcd'
133
+ e('len("abcd")').should.equal 4
134
+ e('strip " abc "').should.equal 'abc'
135
+ e('reverse "abc"').should.equal 'cba'
136
+ e('index("abcdefg", "cde")').should.equal 2
137
+ e('rindex("abcdefgcdef", "cde")').should.equal 7
138
+ end
139
+
140
+ it 'should parse variables' do
141
+ e('a+b*c', :a => 2, :b => 3, :c => 4).should.equal 14
142
+ e('a+b*C', 'A' => 2, 'b' => 3, :c => 4).should.equal 14
143
+ e('alpha+beta*GAMMA', 'ALPHA' => 2, 'bEtA' => 3, 'gamma' => 4).should.equal 14
144
+ end
145
+
146
+ it 'should parse errors' do
147
+ lambda { e('(((((((((((((3+3))') }.should.raise SyntaxError
148
+ lambda { e('1+2)') }.should.raise SyntaxError
149
+ lambda { e('1+2+3+') }.should.raise SyntaxError
150
+ lambda { e('1 + floor') }.should.raise SyntaxError
151
+ lambda { e('42*a+3') }.should.raise NameError
152
+ lambda { e('abc10') }.should.raise NameError
153
+ lambda { e('abc10d') }.should.raise NameError
154
+ end
155
+
156
+ it 'should parse numbers' do
157
+ e('0xABCDEF123').should.equal 0xABCDEF123
158
+ e('01234').should.equal 01234
159
+ e('234 ').should.equal 234
160
+ e('.123').should.equal 0.123
161
+ e('0.123').should.equal 0.123
162
+ e('123.').should.equal 123.0
163
+ e('123e-42').should.equal 123e-42
164
+ e('.123e-42').should.equal 0.123e-42
165
+ e('2.123e-42').should.equal 2.123e-42
166
+ end
167
+
168
+ it 'should parse strings' do
169
+ e('"abc\'a"').should.equal "abc'a"
170
+ e('"abc\"a"').should.equal 'abc"a'
171
+ e("'abc\"a'").should.equal 'abc"a'
172
+ e("'abc\\'a'").should.equal "abc'a"
173
+ end
174
+
175
+ it 'should support units' do
176
+ e('1 [km] in [m]').should.equal Unit('1000 m')
177
+ e('3 [m] / 2 [s]').should.equal Unit('3 m / 2 s')
178
+ end
179
+ end
metadata CHANGED
@@ -1,68 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: evaluator
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Daniel Mendler
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-04-13 00:00:00 +02:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bacon
16
+ requirement: &18032920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
17
22
  type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.8.3
24
- version:
25
- description:
26
- email:
23
+ prerelease: false
24
+ version_requirements: *18032920
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &18031680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18031680
36
+ - !ruby/object:Gem::Dependency
37
+ name: unit
38
+ requirement: &18030460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18030460
47
+ description: Mathematical expression evaluator for infix notation
48
+ email:
27
49
  - mail@daniel-mendler.de
28
- executables: []
29
-
50
+ executables:
51
+ - evaluator
30
52
  extensions: []
31
-
32
- extra_rdoc_files:
33
- - Manifest.txt
34
- files:
35
- - lib/evaluator.rb
36
- - test/test_evaluator.rb
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .travis.yml
56
+ - Gemfile
37
57
  - README.markdown
38
58
  - Rakefile
39
- - Manifest.txt
40
- has_rdoc: true
41
- homepage:
59
+ - bin/evaluator
60
+ - evaluator.constants
61
+ - evaluator.gemspec
62
+ - lib/evaluator.rb
63
+ - test/evaluator_test.rb
64
+ homepage: http://github.com/minad/evaluator
65
+ licenses: []
42
66
  post_install_message:
43
- rdoc_options:
44
- - --main
45
- - README.markdown
46
- require_paths:
67
+ rdoc_options: []
68
+ require_paths:
47
69
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: "0"
53
- version:
54
- required_rubygems_version: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- version: "0"
59
- version:
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
60
82
  requirements: []
61
-
62
83
  rubyforge_project: evaluator
63
- rubygems_version: 1.3.1
84
+ rubygems_version: 1.8.15
64
85
  signing_key:
65
- specification_version: 2
86
+ specification_version: 3
66
87
  summary: Mathematical expression evaluator
67
- test_files:
68
- - test/test_evaluator.rb
88
+ test_files: []
@@ -1,5 +0,0 @@
1
- lib/evaluator.rb
2
- test/test_evaluator.rb
3
- README.markdown
4
- Rakefile
5
- Manifest.txt
@@ -1,169 +0,0 @@
1
- require 'test/unit'
2
- require 'evaluator'
3
-
4
- class TestEvaluator < Test::Unit::TestCase
5
- def test_binary_operators
6
- assert_equal 42, Evaluator('42 || false')
7
- assert_equal true, Evaluator('false || nil || true')
8
- assert_equal 42, Evaluator('42 or false')
9
- assert_equal true, Evaluator('false or nil or true')
10
-
11
- assert_equal 42, Evaluator('true && 42')
12
- assert_equal nil, Evaluator('nil && 1')
13
- assert_equal 42, Evaluator('true and 42')
14
- assert_equal nil, Evaluator('nil and 1')
15
-
16
- assert Evaluator('1+1==2')
17
- assert Evaluator("'abc' == 'a'+'b'+'c'")
18
-
19
- assert Evaluator('1+1 != 3')
20
- assert Evaluator("'xxx' != 'a'+'b'+'c'")
21
-
22
- assert Evaluator('1<=1')
23
- assert Evaluator('1<=2')
24
- assert Evaluator('1>=1')
25
- assert Evaluator('2>=1')
26
- assert(!Evaluator('1<1'))
27
- assert Evaluator('1<2')
28
- assert(!Evaluator('1>1'))
29
- assert Evaluator('2>1')
30
-
31
- assert_equal 3, Evaluator('1+2')
32
- assert_equal 'xyz', Evaluator('"x"+"yz"')
33
- assert_equal 'a1b', Evaluator("'a'+string 1+'b'")
34
- assert_equal 'a1b', Evaluator("'a'+string(1)+'b'")
35
-
36
- assert_equal 3, Evaluator('5-2')
37
- assert_equal(-1, Evaluator('3-4'))
38
-
39
- assert_equal 12, Evaluator('3*4')
40
- assert_equal 'ababab', Evaluator('"ab"*3')
41
-
42
- assert_equal 3, Evaluator('12/4')
43
-
44
- assert_equal 10, Evaluator('103. div 10.')
45
-
46
- assert_equal 3, Evaluator('7 mod 4')
47
- assert_equal 3, Evaluator('7 % 4')
48
-
49
- assert_equal 1024, Evaluator('2 ** 10')
50
-
51
- assert_equal 8, Evaluator('2 << 2')
52
- assert_equal 32, Evaluator('256 >> 3')
53
-
54
- assert_equal 2, Evaluator('6 & 2')
55
- assert_equal 7, Evaluator('1 | 2 | 4')
56
- assert_equal 1, Evaluator('9 ^ 8')
57
- end
58
-
59
- def test_unary_operators
60
- assert_equal(-1, Evaluator('-1'))
61
- assert_equal(-2, Evaluator('-(1+1)'))
62
- assert_equal(-42, Evaluator('---42'))
63
- assert_equal 42, Evaluator('----42')
64
- assert_equal(-9, Evaluator('3*-3'))
65
- assert_equal(9, Evaluator('3*+3'))
66
- end
67
-
68
- def test_precendence
69
- assert_equal 16, Evaluator('1+3*5')
70
- assert_equal 16, Evaluator('3*5+1')
71
- assert_equal 23, Evaluator('3*5+2**3')
72
- end
73
-
74
- def test_constants
75
- assert_equal Math::PI, Evaluator('PI')
76
- assert_equal Math::E, Evaluator('E')
77
- assert_equal Complex::I, Evaluator('I')
78
- assert_equal true, Evaluator('trUe')
79
- assert_equal false, Evaluator('fAlSe')
80
- assert_equal nil, Evaluator('niL')
81
- end
82
-
83
- def test_numeric_functions
84
- assert_equal 13, Evaluator('gcd(26, 39)')
85
- assert_equal 78, Evaluator('lcm(26, 39)')
86
- assert_equal Math.cos(42), Evaluator('cos 42')
87
- assert_equal Math.sin(42), Evaluator('sin 42')
88
- assert_equal Math.cos(42), Evaluator('cos 42')
89
- assert_equal Math.tan(42), Evaluator('tan 42')
90
- assert_equal Math.sinh(42), Evaluator('sinh 42')
91
- assert_equal Math.cosh(42), Evaluator('cosh 42')
92
- assert_equal Math.tanh(42), Evaluator('tanh 42')
93
- assert_equal Math.asin(0.5), Evaluator('asin .5')
94
- assert_equal Math.acos(0.5), Evaluator('acos .5')
95
- assert_equal Math.atan(0.5), Evaluator('atan .5')
96
- assert_equal Math.asinh(0.5), Evaluator('asinh .5')
97
- assert_equal Math.atanh(0.5), Evaluator('atanh .5')
98
- assert_equal Math.sqrt(42) + 1, Evaluator('sqrt 42 + 1')
99
- assert_equal Math.log(42) + 3, Evaluator('log 42 + 3')
100
- assert_equal Math.log(42) + 3, Evaluator('ln 42 + 3')
101
- assert_equal Math.log10(42) + 3, Evaluator('log10 42 + 3')
102
- assert_equal Math.log(42)/Math.log(2) + 3, Evaluator('log2 42 + 3')
103
- assert_equal 3 * Math.exp(42), Evaluator('3 * exp 42')
104
- assert_equal Math.erf(2), Evaluator('erf 2')
105
- assert_equal Math.erfc(2), Evaluator('erfc 2')
106
- assert_equal 42, Evaluator('floor 42.3')
107
- assert_equal 42, Evaluator('ceil 41.6')
108
- assert_equal 3.5, Evaluator('float("3.5")')
109
- assert_equal "3.5", Evaluator('string(3.5)')
110
- assert_equal 3, Evaluator('int("3.5")')
111
- assert_equal 3, Evaluator('int(3.6)')
112
- srand(42); x = rand; srand(42)
113
- assert_equal x, Evaluator('rand')
114
- assert_equal Complex(1,-2), Evaluator('conj(1+2*i)')
115
- assert_equal 2, Evaluator('Im(1+2*i)')
116
- assert_equal 1, Evaluator('Re(1+2*i)')
117
- assert_equal 3, Evaluator('round(3.4)')
118
- assert_equal 4, Evaluator('round(3.5)')
119
- assert_equal 6, Evaluator('abs -6')
120
- assert_equal 3, Evaluator('plus 3')
121
- assert_equal(-3, Evaluator('minus 3'))
122
- assert_equal false, Evaluator('!3')
123
- assert_equal ~3, Evaluator('~3')
124
- end
125
-
126
- def test_string_functions
127
- assert_equal 'bcd', Evaluator('substr("abcde", 1, 3)')
128
- assert_equal 4, Evaluator('len("abcd")')
129
- assert_equal 'abc', Evaluator('strip " abc "')
130
- assert_equal 'cba', Evaluator('reverse "abc"')
131
- assert_equal 2, Evaluator('index("abcdefg", "cde")')
132
- assert_equal 7, Evaluator('rindex("abcdefgcdef", "cde")')
133
- end
134
-
135
- def test_variables
136
- assert_equal 14, Evaluator('a+b*c', :a => 2, :b => 3, :c => 4)
137
- assert_equal 14, Evaluator('a+b*C', 'A' => 2, 'b' => 3, :c => 4)
138
- assert_equal 14, Evaluator('alpha+beta*GAMMA', 'ALPHA' => 2, 'bEtA' => 3, 'gamma' => 4)
139
- end
140
-
141
- def test_errors
142
- assert_raise(SyntaxError) { Evaluator('(((((((((((((3+3))') }
143
- assert_raise(SyntaxError) { Evaluator('1+2)') }
144
- assert_raise(SyntaxError) { Evaluator('1+2+3+') }
145
- assert_raise(SyntaxError) { Evaluator('1 + floor') }
146
- assert_raise(NameError) { Evaluator('42*a+3') }
147
- assert_raise(NameError) { Evaluator('abc10') }
148
- assert_raise(NameError) { Evaluator('abc10d') }
149
- end
150
-
151
- def test_numbers
152
- assert_equal 0xABCDEF123, Evaluator('0xABCDEF123')
153
- assert_equal 01234, Evaluator('01234')
154
- assert_equal 234, Evaluator('234 ')
155
- assert_equal 0.123, Evaluator('.123')
156
- assert_equal 0.123, Evaluator('0.123')
157
- assert_equal 123.0, Evaluator('123.')
158
- assert_equal 123e-42, Evaluator('123e-42')
159
- assert_equal 0.123e-42, Evaluator('.123e-42')
160
- assert_equal 2.123e-42, Evaluator('2.123e-42')
161
- end
162
-
163
- def test_strings
164
- assert_equal "abc'a", Evaluator('"abc\'a"')
165
- assert_equal 'abc"a', Evaluator('"abc\"a"')
166
- assert_equal 'abc"a', Evaluator("'abc\"a'")
167
- assert_equal "abc'a", Evaluator("'abc\\'a'")
168
- end
169
- end