Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-math/test/math.rb

    r321 r439  
    22# Math Test
    33
    4 ##
    5 # Performs fuzzy check for equality on methods returning floats
    6 # on the basis of the Math::TOLERANCE constant.
    7 def check_float(a, b)
    8   tolerance = Math::TOLERANCE
    9   a = a.to_f
    10   b = b.to_f
    11   if a.finite? and b.finite?
    12     (a-b).abs < tolerance
    13   else
    14     true
     4def assert_float_and_int(exp_ary, act_ary)
     5  assert('assert_float_and_int') do
     6    flo_exp, int_exp, flo_act, int_act = *exp_ary, *act_ary
     7    assert_float(flo_exp, flo_act)
     8    assert_operator(int_exp, :eql?, int_act)
    159  end
    1610end
    1711
    1812assert('Math.sin 0') do
    19   check_float(Math.sin(0), 0)
     13  assert_float(0, Math.sin(0))
    2014end
    2115
    2216assert('Math.sin PI/2') do
    23   check_float(Math.sin(Math::PI / 2), 1)
     17  assert_float(1, Math.sin(Math::PI / 2))
    2418end
    2519
    2620assert('Math.cos 0') do
    27   check_float(Math.cos(0), 1)
     21  assert_float(1, Math.cos(0))
    2822end
    2923
    3024assert('Math.cos PI/2') do
    31   check_float(Math.cos(Math::PI / 2), 0)
     25  assert_float(0, Math.cos(Math::PI / 2))
    3226end
    3327
    3428assert('Math.tan 0') do
    35   check_float(Math.tan(0), 0)
     29  assert_float(0, Math.tan(0))
    3630end
    3731
    3832assert('Math.tan PI/4') do
    39   check_float(Math.tan(Math::PI / 4), 1)
     33  assert_float(1, Math.tan(Math::PI / 4))
    4034end
    4135
    4236assert('Fundamental trig identities') do
    43   result = true
    4437  N = 13
    4538  N.times do |i|
     
    4942    c  = Math.cos(a)
    5043    t  = Math.tan(a)
    51     result &= check_float(s, Math.cos(ca))
    52     result &= check_float(t, 1 / Math.tan(ca))
    53     result &= check_float(s ** 2 + c ** 2, 1)
    54     result &= check_float(t ** 2 + 1, (1/c) ** 2)
    55     result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
    56   end
    57   result
     44    assert_float(Math.cos(ca), s)
     45    assert_float(1 / Math.tan(ca), t)
     46    assert_float(1, s ** 2 + c ** 2)
     47    assert_float((1/c) ** 2, t ** 2 + 1)
     48    assert_float((1/s) ** 2, (1/t) ** 2 + 1)
     49  end
    5850end
    5951
    6052assert('Math.erf 0') do
    61   check_float(Math.erf(0), 0)
     53  assert_float(0, Math.erf(0))
    6254end
    6355
    6456assert('Math.exp 0') do
    65   check_float(Math.exp(0), 1.0)
     57  assert_float(1.0, Math.exp(0))
    6658end
    6759
    6860assert('Math.exp 1') do
    69   check_float(Math.exp(1), 2.718281828459045)
     61  assert_float(2.718281828459045, Math.exp(1))
    7062end
    7163
    7264assert('Math.exp 1.5') do
    73   check_float(Math.exp(1.5), 4.4816890703380645)
     65  assert_float(4.4816890703380645, Math.exp(1.5))
    7466end
    7567
    7668assert('Math.log 1') do
    77   check_float(Math.log(1), 0)
     69  assert_float(0, Math.log(1))
    7870end
    7971
    8072assert('Math.log E') do
    81   check_float(Math.log(Math::E), 1.0)
     73  assert_float(1.0, Math.log(Math::E))
    8274end
    8375
    8476assert('Math.log E**3') do
    85   check_float(Math.log(Math::E**3), 3.0)
     77  assert_float(3.0, Math.log(Math::E**3))
    8678end
    8779
    8880assert('Math.log2 1') do
    89   check_float(Math.log2(1), 0.0)
     81  assert_float(0.0, Math.log2(1))
    9082end
    9183
    9284assert('Math.log2 2') do
    93   check_float(Math.log2(2), 1.0)
     85  assert_float(1.0, Math.log2(2))
    9486end
    9587
    9688assert('Math.log10 1') do
    97   check_float(Math.log10(1), 0.0)
     89  assert_float(0.0, Math.log10(1))
    9890end
    9991
    10092assert('Math.log10 10') do
    101   check_float(Math.log10(10), 1.0)
     93  assert_float(1.0, Math.log10(10))
    10294end
    10395
    10496assert('Math.log10 10**100') do
    105   check_float(Math.log10(10**100), 100.0)
     97  assert_float(100.0, Math.log10(10**100))
    10698end
    10799
     
    109101  num = [0.0, 1.0, 2.0, 3.0, 4.0]
    110102  sqr = [0, 1, 4, 9, 16]
    111   result = true
    112103  sqr.each_with_index do |v,i|
    113     result &= check_float(Math.sqrt(v), num[i])
    114   end
    115   result
     104    assert_float(num[i], Math.sqrt(v))
     105  end
    116106end
    117107
     
    119109  num = [-2.0, -1.0, 0.0, 1.0, 2.0]
    120110  cub = [-8, -1, 0, 1, 8]
    121   result = true
    122111  cub.each_with_index do |v,i|
    123     result &= check_float(Math.cbrt(v), num[i])
    124   end
    125   result
     112    assert_float(num[i], Math.cbrt(v))
     113  end
    126114end
    127115
    128116assert('Math.hypot') do
    129   check_float(Math.hypot(3, 4), 5.0)
    130 end
    131 
    132 assert('Math.frexp 1234') do
    133   n = 1234
    134   fraction, exponent = Math.frexp(n)
    135   check_float(Math.ldexp(fraction, exponent), n)
     117  assert_float(5.0, Math.hypot(3, 4))
    136118end
    137119
    138120assert('Math.erf 1') do
    139   check_float(Math.erf(1), 0.842700792949715)
     121  assert_float(0.842700792949715, Math.erf(1))
    140122end
    141123
    142124assert('Math.erfc 1') do
    143   check_float(Math.erfc(1), 0.157299207050285)
     125  assert_float(0.157299207050285, Math.erfc(1))
    144126end
    145127
    146128assert('Math.erf -1') do
    147   check_float(Math.erf(-1), -0.8427007929497148)
     129  assert_float(-0.8427007929497148, Math.erf(-1))
    148130end
    149131
    150132assert('Math.erfc -1') do
    151   check_float(Math.erfc(-1), 1.8427007929497148)
    152 end
     133  assert_float(1.8427007929497148, Math.erfc(-1))
     134end
     135
     136assert('Math.acos') do
     137  assert_float(0 * Math::PI / 4, Math.acos( 1.0))
     138  assert_float(1 * Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
     139  assert_float(2 * Math::PI / 4, Math.acos( 0.0))
     140  assert_float(4 * Math::PI / 4, Math.acos(-1.0))
     141  assert_raise(Math::DomainError) { Math.acos(+1.1) }
     142  assert_raise(Math::DomainError) { Math.acos(-1.1) }
     143end
     144
     145assert('Math.asin') do
     146  assert_float( 0 * Math::PI / 4, Math.asin( 0.0))
     147  assert_float( 1 * Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
     148  assert_float( 2 * Math::PI / 4, Math.asin( 1.0))
     149  assert_float(-2 * Math::PI / 4, Math.asin(-1.0))
     150  assert_raise(Math::DomainError) { Math.asin(+1.1) }
     151  assert_raise(Math::DomainError) { Math.asin(-1.1) }
     152  assert_raise(Math::DomainError) { Math.asin(2.0) }
     153end
     154
     155assert('Math.atan') do
     156  assert_float( 0 * Math::PI / 4, Math.atan( 0.0))
     157  assert_float( 1 * Math::PI / 4, Math.atan( 1.0))
     158  assert_float( 2 * Math::PI / 4, Math.atan(1.0 / 0.0))
     159  assert_float(-1 * Math::PI / 4, Math.atan(-1.0))
     160end
     161
     162assert('Math.cosh') do
     163  assert_float(1, Math.cosh(0))
     164  assert_float((Math::E ** 1 + Math::E ** -1) / 2, Math.cosh(1))
     165  assert_float((Math::E ** 2 + Math::E ** -2) / 2, Math.cosh(2))
     166end
     167
     168assert('Math.sinh') do
     169  assert_float(0, Math.sinh(0))
     170  assert_float((Math::E ** 1 - Math::E ** -1) / 2, Math.sinh(1))
     171  assert_float((Math::E ** 2 - Math::E ** -2) / 2, Math.sinh(2))
     172end
     173
     174assert('Math.tanh') do
     175  assert_float(Math.sinh(0) / Math.cosh(0), Math.tanh(0))
     176  assert_float(Math.sinh(1) / Math.cosh(1), Math.tanh(1))
     177  assert_float(Math.sinh(2) / Math.cosh(2), Math.tanh(2))
     178  assert_float(+1.0, Math.tanh(+1000.0))
     179  assert_float(-1.0, Math.tanh(-1000.0))
     180end
     181
     182assert('Math.acosh') do
     183  assert_float(0, Math.acosh(1))
     184  assert_float(1, Math.acosh((Math::E ** 1 + Math::E ** -1) / 2))
     185  assert_float(2, Math.acosh((Math::E ** 2 + Math::E ** -2) / 2))
     186  assert_raise(Math::DomainError) { Math.acosh(0.9) }
     187  assert_raise(Math::DomainError) { Math.acosh(0) }
     188end
     189
     190assert('Math.asinh') do
     191  assert_float(0, Math.asinh(0))
     192  assert_float(1, Math.asinh((Math::E ** 1 - Math::E ** -1) / 2))
     193  assert_float(2, Math.asinh((Math::E ** 2 - Math::E ** -2) / 2))
     194end
     195
     196assert('Math.atanh') do
     197  assert_float(0, Math.atanh(Math.sinh(0) / Math.cosh(0)))
     198  assert_float(1, Math.atanh(Math.sinh(1) / Math.cosh(1)))
     199  assert_float(2, Math.atanh(Math.sinh(2) / Math.cosh(2)))
     200  assert_float(Float::INFINITY, Math.atanh(1))
     201  assert_float(-Float::INFINITY, Math.atanh(-1))
     202  assert_raise(Math::DomainError) { Math.atanh(+1.1) }
     203  assert_raise(Math::DomainError) { Math.atanh(-1.1) }
     204end
     205
     206assert('Math.atan2') do
     207  assert_float(+0.0, Math.atan2(+0.0, +0.0))
     208  assert_float(-0.0, Math.atan2(-0.0, +0.0))
     209  assert_float(+Math::PI, Math.atan2(+0.0, -0.0))
     210  assert_float(-Math::PI, Math.atan2(-0.0, -0.0))
     211
     212  inf = Float::INFINITY
     213  expected = 3.0 * Math::PI / 4.0
     214  assert_float(+expected, Math.atan2(+inf, -inf))
     215  assert_float(-expected, Math.atan2(-inf, -inf))
     216  expected = Math::PI / 4.0
     217  assert_float(+expected, Math.atan2(+inf, +inf))
     218  assert_float(-expected, Math.atan2(-inf, +inf))
     219
     220  assert_float(0, Math.atan2(0, 1))
     221  assert_float(Math::PI / 4, Math.atan2(1, 1))
     222  assert_float(Math::PI / 2, Math.atan2(1, 0))
     223end
     224
     225assert('Math.ldexp') do
     226  assert_float(0.0, Math.ldexp(0.0, 0.0))
     227  assert_float(0.5, Math.ldexp(0.5, 0.0))
     228  assert_float(1.0, Math.ldexp(0.5, 1.0))
     229  assert_float(2.0, Math.ldexp(0.5, 2.0))
     230  assert_float(3.0, Math.ldexp(0.75, 2.0))
     231end
     232
     233assert('Math.frexp') do
     234  assert_float_and_int([0.0,  0], Math.frexp(0.0))
     235  assert_float_and_int([0.5,  0], Math.frexp(0.5))
     236  assert_float_and_int([0.5,  1], Math.frexp(1.0))
     237  assert_float_and_int([0.5,  2], Math.frexp(2.0))
     238  assert_float_and_int([0.75, 2], Math.frexp(3.0))
     239end
Note: See TracChangeset for help on using the changeset viewer.