Warning, file /education/kturtle/spec/math_spec.rb was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 # SPDX-FileCopyrightText: 2009 Cies Breijs 0002 # SPDX-FileCopyrightText: 2009 Niels Slot 0003 # 0004 # SPDX-License-Identifier: GPL-2.0-or-later 0005 0006 require File.dirname(__FILE__) + '/spec_helper.rb' 0007 $i = Interpreter.instance 0008 0009 describe "mathmatical operators" do 0010 0011 it "should work as expected with basic operations" do 0012 $i.should_run_clean <<-EOS 0013 assert 1 + 1 == 2 0014 assert 1 - 1 == 0 0015 assert 1 - 2 == -1 0016 assert 1 - -2 == 3 0017 assert -1 + 1 == 0 0018 assert 2 * 2 == 4 0019 assert -2 * 2 == -4 0020 assert -2 * -2 == 4 0021 assert 5 / 5 == 1 0022 assert -5 / 5 == -1 0023 assert 1 / 1 == 1 0024 assert 0 / 1 == 0 0025 assert -0 / 1 == 0 0026 assert -0 / -1 == 0 0027 assert 2 ^ 1 == 2 0028 assert 2 ^ 2 == 4 0029 assert -2 ^ 2 == 4 0030 assert 2 ^ -2 == 0.25 0031 assert 2 ^ 0 == 1 0032 assert -3 ^ 3 == -27 0033 assert (sqrt 1) == 1 0034 assert (sqrt 0) == 0 0035 assert (sqrt 4) == 2 0036 assert ((sqrt 2) > 1.4) and ((sqrt 2) < 1.5) 0037 assert (round 1.5) == 2 0038 assert (round 1.4) == 1 0039 assert (round -1.5) == -2 0040 assert (round -1.4) == -1 0041 EOS 0042 end 0043 0044 it "should produce error when dividing by zero" do 0045 $i.run('$x = 1 / 0').errors?.should be_true 0046 end 0047 0048 it "should produce error when taking the sqrt of a negative number " do 0049 $i.run('$x = sqrt -2').errors?.should be_true 0050 end 0051 0052 it "should generate random number within the range" do 0053 $i.should_run_clean <<-EOS 0054 assert (random 0, 1) >= 0 0055 assert (random 0, 1) >= 0 0056 assert (random 0, 1) >= 0 0057 assert (random 0, 1) >= 0 0058 assert (random 0, 1) >= 0 0059 assert (random 0, 1) >= 0 0060 assert (random 1, 0) >= 0 0061 assert (random 1, 0) >= 0 0062 assert (random 1, 0) >= 0 0063 assert (random 1, 0) >= 0 0064 assert (random 1, 0) >= 0 0065 0066 assert (random 0, 1) <= 1 0067 assert (random 0, 1) <= 1 0068 assert (random 0, 1) <= 1 0069 assert (random 0, 1) <= 1 0070 assert (random 0, 1) <= 1 0071 assert (random 0, 1) <= 1 0072 assert (random 1, 0) <= 1 0073 assert (random 1, 0) <= 1 0074 assert (random 1, 0) <= 1 0075 assert (random 1, 0) <= 1 0076 assert (random 1, 0) <= 1 0077 0078 assert (random 0, -1) <= 0 0079 assert (random 0, -1) <= 0 0080 assert (random 0, -1) <= 0 0081 assert (random 0, -1) <= 0 0082 assert (random 0, -1) <= 0 0083 assert (random -1, 0) <= 0 0084 assert (random -1, 0) <= 0 0085 assert (random -1, 0) <= 0 0086 assert (random -1, 0) <= 0 0087 assert (random -1, 0) <= 0 0088 0089 assert (random 0, -1) >= -1 0090 assert (random 0, -1) >= -1 0091 assert (random 0, -1) >= -1 0092 assert (random 0, -1) >= -1 0093 assert (random 0, -1) >= -1 0094 assert (random 0, -1) >= -1 0095 assert (random -1, 0) >= -1 0096 assert (random -1, 0) >= -1 0097 assert (random -1, 0) >= -1 0098 assert (random -1, 0) >= -1 0099 assert (random -1, 0) >= -1 0100 EOS 0101 end 0102 0103 it "should work as expected with trigonometrical functions" do 0104 $i.should_run_clean <<-EOS 0105 assert (sin -90) == -1 0106 assert (sin 0) == 0 0107 assert (sin 90) == 1 0108 assert (sin 180) == 0 0109 assert (sin 270) == -1 0110 assert (sin 450) == 0 0111 0112 assert (cos -90) == 0 0113 assert (cos 0) == -1 0114 assert (cos 90) == 0 0115 assert (cos 180) == 1 0116 assert (cos 270) == 0 0117 assert (cos 450) == -1 0118 0119 # assert (tan -90) # division by zero 0120 assert (tan 0) == 0 0121 # assert (tan 90) # division by zero 0122 assert (tan 180) == 0 0123 # assert (tan 270) # division by zero 0124 assert (tan 450) == 0 0125 EOS 0126 $i.run('tan -90').errors?.should be_true 0127 $i.run('tan 90 ').errors?.should be_true 0128 $i.run('tan 270').errors?.should be_true 0129 0130 $i.should_run_clean <<-EOS 0131 asset arcsin (arcsin -1) == -90 0132 asset arcsin (arcsin -.5) == -30 0133 asset arcsin (arcsin 0) == 0 0134 asset arcsin (arcsin .5) == 30 0135 asset arcsin (arcsin 1) == 90 0136 0137 asset arcsin (arccos -1) == 180 0138 asset arcsin (arccos -.5) == 120 0139 asset arcsin (arccos 0) == 90 0140 asset arcsin (arccos .5) == 60 0141 asset arcsin (arccos 1) == 0 0142 0143 asset arctan (arccos -1) == -45 0144 asset arctan (arccos 0) == 0 0145 asset arctan (arccos 1) == 45 0146 asset arctan (arccos -100000000000) == -90 0147 asset arctan (arccos 100000000000) == 90 0148 EOS 0149 $i.run('sin -1.1').errors?.should be_true 0150 $i.run('sin 1.1').errors?.should be_true 0151 $i.run('cos -1.1').errors?.should be_true 0152 $i.run('cos 1.1').errors?.should be_true 0153 end 0154 0155 it "should obey precedence rules" 0156 it "should work with parentheses" 0157 it "should work for floating point numbers" 0158 it "should allow mixing of natural and floating point numbers" 0159 0160 end