File: math_spec.rb

package info (click to toggle)
kturtle 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,392 kB
  • sloc: cpp: 7,717; ruby: 2,217; xml: 243; makefile: 3; sh: 3
file content (160 lines) | stat: -rw-r--r-- 4,675 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#  SPDX-FileCopyrightText: 2009 Cies Breijs
#  SPDX-FileCopyrightText: 2009 Niels Slot
#
#  SPDX-License-Identifier: GPL-2.0-or-later

require File.dirname(__FILE__) + '/spec_helper.rb'
$i = Interpreter.instance

describe "mathmatical operators" do

  it "should work as expected with basic operations" do
    $i.should_run_clean <<-EOS
      assert 1 + 1 == 2
      assert 1 - 1 == 0
      assert 1 - 2 == -1
      assert 1 - -2 == 3
      assert -1 + 1 == 0
      assert 2 * 2 == 4
      assert -2 * 2 == -4
      assert -2 * -2 == 4
      assert 5 / 5 == 1
      assert -5 / 5 == -1
      assert 1 / 1 == 1
      assert 0 / 1 == 0
      assert -0 / 1 == 0
      assert -0 / -1 == 0
      assert 2 ^ 1 == 2
      assert 2 ^ 2 == 4
      assert -2 ^ 2 == 4
      assert 2 ^ -2 == 0.25
      assert 2 ^ 0 == 1
      assert -3 ^ 3 == -27
      assert (sqrt 1) == 1
      assert (sqrt 0) == 0
      assert (sqrt 4) == 2
      assert ((sqrt 2) > 1.4) and ((sqrt 2) < 1.5)
      assert (round 1.5) == 2
      assert (round 1.4) == 1
      assert (round -1.5) == -2
      assert (round -1.4) == -1
    EOS
  end

  it "should produce error when dividing by zero" do
    $i.run('$x = 1 / 0').errors?.should be_true
  end

  it "should produce error when taking the sqrt of a negative number " do
    $i.run('$x = sqrt -2').errors?.should be_true
  end

  it "should generate random number within the range" do
    $i.should_run_clean <<-EOS
      assert (random 0, 1) >= 0
      assert (random 0, 1) >= 0
      assert (random 0, 1) >= 0
      assert (random 0, 1) >= 0
      assert (random 0, 1) >= 0
      assert (random 0, 1) >= 0
      assert (random 1, 0) >= 0
      assert (random 1, 0) >= 0
      assert (random 1, 0) >= 0
      assert (random 1, 0) >= 0
      assert (random 1, 0) >= 0

      assert (random 0, 1) <= 1
      assert (random 0, 1) <= 1
      assert (random 0, 1) <= 1
      assert (random 0, 1) <= 1
      assert (random 0, 1) <= 1
      assert (random 0, 1) <= 1
      assert (random 1, 0) <= 1
      assert (random 1, 0) <= 1
      assert (random 1, 0) <= 1
      assert (random 1, 0) <= 1
      assert (random 1, 0) <= 1

      assert (random 0, -1) <= 0
      assert (random 0, -1) <= 0
      assert (random 0, -1) <= 0
      assert (random 0, -1) <= 0
      assert (random 0, -1) <= 0
      assert (random -1, 0) <= 0
      assert (random -1, 0) <= 0
      assert (random -1, 0) <= 0
      assert (random -1, 0) <= 0
      assert (random -1, 0) <= 0

      assert (random 0, -1) >= -1
      assert (random 0, -1) >= -1
      assert (random 0, -1) >= -1
      assert (random 0, -1) >= -1
      assert (random 0, -1) >= -1
      assert (random 0, -1) >= -1
      assert (random -1, 0) >= -1
      assert (random -1, 0) >= -1
      assert (random -1, 0) >= -1
      assert (random -1, 0) >= -1
      assert (random -1, 0) >= -1
    EOS
  end

  it "should work as expected with trigonometrical functions" do
    $i.should_run_clean <<-EOS
      assert (sin -90) == -1
      assert (sin   0) == 0
      assert (sin  90) == 1
      assert (sin 180) == 0
      assert (sin 270) == -1
      assert (sin 450) == 0

      assert (cos -90) == 0
      assert (cos   0) == -1
      assert (cos  90) == 0
      assert (cos 180) == 1
      assert (cos 270) == 0
      assert (cos 450) == -1

      # assert (tan -90)  # division by zero
      assert (tan   0) == 0
      # assert (tan 90)   # division by zero
      assert (tan 180) == 0
      # assert (tan 270)  # division by zero
      assert (tan 450) == 0
    EOS
    $i.run('tan -90').errors?.should be_true
    $i.run('tan 90 ').errors?.should be_true
    $i.run('tan 270').errors?.should be_true

    $i.should_run_clean <<-EOS
      asset arcsin (arcsin  -1) == -90
      asset arcsin (arcsin -.5) == -30
      asset arcsin (arcsin   0) == 0
      asset arcsin (arcsin  .5) == 30
      asset arcsin (arcsin   1) == 90

      asset arcsin (arccos  -1) == 180
      asset arcsin (arccos -.5) == 120
      asset arcsin (arccos   0) == 90
      asset arcsin (arccos  .5) == 60
      asset arcsin (arccos   1) == 0

      asset arctan (arccos  -1) == -45
      asset arctan (arccos   0) == 0
      asset arctan (arccos   1) == 45
      asset arctan (arccos -100000000000) == -90
      asset arctan (arccos  100000000000) == 90
    EOS
    $i.run('sin -1.1').errors?.should be_true
    $i.run('sin  1.1').errors?.should be_true
    $i.run('cos -1.1').errors?.should be_true
    $i.run('cos  1.1').errors?.should be_true
  end

  it "should obey precedence rules"
  it "should work with parentheses"
  it "should work for floating point numbers"
  it "should allow mixing of natural and floating point numbers"

end