github.com/awirix/lua@v1.6.0/_lua5.1-tests/math.lua (about)

     1  print("testing numbers and math lib")
     2  
     3  do
     4    local a,b,c = "2", " 3e0 ", " 10  "
     5    assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0)
     6    assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string')
     7    assert(a == "2" and b == " 3e0 " and c == " 10  " and -c == -"  10 ")
     8    assert(c%a == 0 and a^b == 8)
     9  end
    10  
    11  
    12  do
    13    local a,b = math.modf(3.5)
    14    assert(a == 3 and b == 0.5)
    15    assert(math.huge > 10e30)
    16    assert(-math.huge < -10e30)
    17  end
    18  
    19  function f(...)
    20    if select('#', ...) == 1 then
    21      return (...)
    22    else
    23      return "***"
    24    end
    25  end
    26  
    27  assert(tonumber{} == nil)
    28  assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
    29         tonumber'.01' == 0.01    and tonumber'-1.' == -1 and
    30         tonumber'+1.' == 1)
    31  assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
    32         tonumber'1e' == nil     and tonumber'1.0e+' == nil and
    33         tonumber'.' == nil)
    34  assert(tonumber('-12') == -10-2)
    35  assert(tonumber('-1.2e2') == - - -120)
    36  assert(f(tonumber('1  a')) == nil)
    37  assert(f(tonumber('e1')) == nil)
    38  assert(f(tonumber('e  1')) == nil)
    39  assert(f(tonumber(' 3.4.5 ')) == nil)
    40  assert(f(tonumber('')) == nil)
    41  assert(f(tonumber('', 8)) == nil)
    42  assert(f(tonumber('  ')) == nil)
    43  assert(f(tonumber('  ', 9)) == nil)
    44  assert(f(tonumber('99', 8)) == nil)
    45  assert(tonumber('  1010  ', 2) == 10)
    46  assert(tonumber('10', 36) == 36)
    47  assert(tonumber('\n  -10  \n', 36) == -36)
    48  assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15)))))))
    49  assert(tonumber('fFfa', 15) == nil)
    50  assert(tonumber(string.rep('1', 42), 2) + 1 == 2^42)
    51  assert(tonumber(string.rep('1', 32), 2) + 1 == 2^32)
    52  assert(tonumber('-fffffFFFFF', 16)-1 == -2^40)
    53  assert(tonumber('ffffFFFF', 16)+1 == 2^32)
    54  assert(tonumber('0xF') == 15)
    55  
    56  assert(1.1 == 1.+.1)
    57  print(100.0, 1E2, .01)
    58  assert(100.0 == 1E2 and .01 == 1e-2)
    59  assert(1111111111111111-1111111111111110== 1000.00e-03)
    60  --     1234567890123456
    61  assert(1.1 == '1.'+'.1')
    62  assert('1111111111111111'-'1111111111111110' == tonumber"  +0.001e+3 \n\t")
    63  
    64  function eq (a,b,limit)
    65    if not limit then limit = 10E-10 end
    66    return math.abs(a-b) <= limit
    67  end
    68  
    69  assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31)
    70  
    71  assert(0.123456 > 0.123455)
    72  
    73  assert(tonumber('+1.23E30') == 1.23*10^30)
    74  
    75  -- testing order operators
    76  assert(not(1<1) and (1<2) and not(2<1))
    77  assert(not('a'<'a') and ('a'<'b') and not('b'<'a'))
    78  assert((1<=1) and (1<=2) and not(2<=1))
    79  assert(('a'<='a') and ('a'<='b') and not('b'<='a'))
    80  assert(not(1>1) and not(1>2) and (2>1))
    81  assert(not('a'>'a') and not('a'>'b') and ('b'>'a'))
    82  assert((1>=1) and not(1>=2) and (2>=1))
    83  assert(('a'>='a') and not('a'>='b') and ('b'>='a'))
    84  
    85  -- testing mod operator
    86  assert(-4%3 == 2)
    87  assert(4%-3 == -2)
    88  assert(math.pi - math.pi % 1 == 3)
    89  assert(math.pi - math.pi % 0.001 == 3.141)
    90  
    91  local function testbit(a, n)
    92    return a/2^n % 2 >= 1
    93  end
    94  
    95  assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1))
    96  assert(eq(math.tan(math.pi/4), 1))
    97  assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0))
    98  assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and
    99         eq(math.asin(1), math.pi/2))
   100  assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2))
   101  assert(math.abs(-10) == 10)
   102  assert(eq(math.atan2(1,0), math.pi/2))
   103  assert(math.ceil(4.5) == 5.0)
   104  assert(math.floor(4.5) == 4.0)
   105  assert(math.mod(10,3) == 1)
   106  assert(eq(math.sqrt(10)^2, 10))
   107  assert(eq(math.log10(2), math.log(2)/math.log(10)))
   108  assert(eq(math.exp(0), 1))
   109  assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
   110  local v,e = math.frexp(math.pi)
   111  assert(eq(math.ldexp(v,e), math.pi))
   112  
   113  assert(eq(math.tanh(3.5), math.sinh(3.5)/math.cosh(3.5)))
   114  
   115  assert(tonumber(' 1.3e-2 ') == 1.3e-2)
   116  assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
   117  
   118  -- testing constant limits
   119  -- 2^23 = 8388608
   120  assert(8388609 + -8388609 == 0)
   121  assert(8388608 + -8388608 == 0)
   122  assert(8388607 + -8388607 == 0)
   123  
   124  if rawget(_G, "_soft") then return end
   125  
   126  f = io.tmpfile()
   127  assert(f)
   128  f:write("a = {")
   129  i = 1
   130  repeat
   131    f:write("{", math.sin(i), ", ", math.cos(i), ", ", i/3, "},\n")
   132    i=i+1
   133  until i > 1000
   134  f:write("}")
   135  f:seek("set", 0)
   136  assert(loadstring(f:read('*a')))()
   137  assert(f:close())
   138  
   139  assert(eq(a[300][1], math.sin(300)))
   140  assert(eq(a[600][1], math.sin(600)))
   141  assert(eq(a[500][2], math.cos(500)))
   142  assert(eq(a[800][2], math.cos(800)))
   143  assert(eq(a[200][3], 200/3))
   144  assert(eq(a[1000][3], 1000/3, 0.001))
   145  print('+')
   146  
   147  do   -- testing NaN
   148    local NaN = 10e500 - 10e400
   149    assert(NaN ~= NaN)
   150    assert(not (NaN < NaN))
   151    assert(not (NaN <= NaN))
   152    assert(not (NaN > NaN))
   153    assert(not (NaN >= NaN))
   154    assert(not (0 < NaN))
   155    assert(not (NaN < 0))
   156    local a = {}
   157    assert(not pcall(function () a[NaN] = 1 end))
   158    assert(a[NaN] == nil)
   159    a[1] = 1
   160    assert(not pcall(function () a[NaN] = 1 end))
   161    assert(a[NaN] == nil)
   162  end
   163  
   164  require "checktable"
   165  stat(a)
   166  
   167  a = nil
   168  
   169  -- testing implicit convertions
   170  
   171  local a,b = '10', '20'
   172  assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
   173  assert(a == '10' and b == '20')
   174  
   175  
   176  math.randomseed(0)
   177  
   178  local i = 0
   179  local Max = 0
   180  local Min = 2
   181  repeat
   182    local t = math.random()
   183    Max = math.max(Max, t)
   184    Min = math.min(Min, t)
   185    i=i+1
   186    flag = eq(Max, 1, 0.001) and eq(Min, 0, 0.001)
   187  until flag or i>10000
   188  assert(0 <= Min and Max<1)
   189  assert(flag);
   190  
   191  for i=1,10 do
   192    local t = math.random(5)
   193    assert(1 <= t and t <= 5)
   194  end
   195  
   196  i = 0
   197  Max = -200
   198  Min = 200
   199  repeat
   200    local t = math.random(-10,0)
   201    Max = math.max(Max, t)
   202    Min = math.min(Min, t)
   203    i=i+1
   204    flag = (Max == 0 and Min == -10)
   205  until flag or i>10000
   206  assert(-10 <= Min and Max<=0)
   207  assert(flag);
   208  
   209  
   210  print('OK')