2017-04-03 18:01:36 +00:00
|
|
|
|
|
|
|
|
|
local SUITE = require('TestSuite').new('number')
|
|
|
|
|
SUITE:setEnviroment{
|
|
|
|
|
format = require('pretty')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local function format_test (t)
|
|
|
|
|
SUITE:addTest(t.expect, function ()
|
|
|
|
|
assert_equal(t.expect, format(t.input, t.options))
|
2017-04-12 13:15:03 +00:00
|
|
|
|
end, { line = debug.getinfo(2).currentline })
|
2017-04-03 18:01:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function number_test (t)
|
2017-04-30 20:40:24 +00:00
|
|
|
|
SUITE:addTest(t.name or t.expect, function ()
|
2017-04-03 18:01:36 +00:00
|
|
|
|
assert_equal(t.expect, format(t.input, {}))
|
|
|
|
|
end)
|
2017-04-30 20:40:24 +00:00
|
|
|
|
SUITE:addTest(t.name and (t.name .. ' shorthand') or t.shorthand or (t.expect .. ' shorthand'), function ()
|
2017-04-03 18:01:36 +00:00
|
|
|
|
assert_equal(t.shorthand or t.expect, format(t.input, { math_shorthand = true }))
|
2017-04-12 13:15:03 +00:00
|
|
|
|
end, { line = debug.getinfo(2).currentline })
|
2017-04-03 18:01:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Integers
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 0,
|
|
|
|
|
expect = '0',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = -0,
|
|
|
|
|
expect = '0',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 2000,
|
|
|
|
|
expect = '2000',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = -2000,
|
|
|
|
|
expect = '-2000',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Exponents
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 10^5,
|
|
|
|
|
expect = '10^5',
|
|
|
|
|
shorthand = '10⁵',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 10^-6,
|
|
|
|
|
expect = '10^-6',
|
|
|
|
|
shorthand = '10⁻⁶',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 2^17,
|
|
|
|
|
expect = '2^17',
|
|
|
|
|
shorthand = '2¹⁷',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 2^-7,
|
|
|
|
|
expect = '2^-7',
|
|
|
|
|
shorthand = '2⁻⁷',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Decimal numbers
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 0.1,
|
|
|
|
|
expect = '0.1',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 0.65,
|
|
|
|
|
expect = '0.65',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Fractions
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 1/3,
|
|
|
|
|
expect = '1/3',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 9/17,
|
|
|
|
|
expect = '9/17',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Squareroots, logarithms, and other fine functions
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = math.sqrt(8),
|
|
|
|
|
expect = 'math.sqrt(8)',
|
2017-04-12 10:46:53 +00:00
|
|
|
|
shorthand = '√̅8',
|
2017-04-03 18:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 1/math.sqrt(8),
|
|
|
|
|
expect = '1/math.sqrt(8)',
|
2017-04-12 10:46:53 +00:00
|
|
|
|
shorthand = '1/√̅8',
|
2017-04-03 18:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = math.log(8),
|
|
|
|
|
expect = 'math.log(8)',
|
|
|
|
|
shorthand = 'lg(8)',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 6227020800,
|
|
|
|
|
expect = 'fac(13)',
|
|
|
|
|
shorthand = '13!',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Constants, and multiples of constants
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = math.pi,
|
|
|
|
|
expect = 'math.pi',
|
|
|
|
|
shorthand = 'π',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 7*math.pi,
|
|
|
|
|
expect = '7*math.pi',
|
|
|
|
|
shorthand = '7π',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = math.exp(1),
|
|
|
|
|
expect = 'math.exp(1)',
|
|
|
|
|
shorthand = 'ℯ',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = math.exp(10),
|
|
|
|
|
expect = 'math.exp(10)',
|
|
|
|
|
shorthand = 'ℯ¹⁰',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 1/0,
|
|
|
|
|
expect = '1/0',
|
|
|
|
|
shorthand = '∞'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = -1/0,
|
|
|
|
|
expect = '-1/0',
|
|
|
|
|
shorthand = '-∞'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 0/0,
|
|
|
|
|
expect = '0/0',
|
|
|
|
|
shorthand = 'NaN',
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 20:40:24 +00:00
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
-- Use the expression closest to the real value
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
local sum = 0
|
|
|
|
|
for i = 1, 100 do sum = sum + math.pi/100 end
|
|
|
|
|
number_test {
|
|
|
|
|
name = 'Approx π',
|
|
|
|
|
input = sum,
|
|
|
|
|
expect = 'math.pi',
|
|
|
|
|
shorthand = 'π',
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
number_test {
|
|
|
|
|
input = 4*4,
|
|
|
|
|
expect = '16',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 18:01:36 +00:00
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2017-04-12 10:46:53 +00:00
|
|
|
|
number_test {
|
|
|
|
|
-- A half should always be represented using 1/2, never with 2⁻¹.
|
|
|
|
|
input = 1/2,
|
|
|
|
|
expect = '1/2',
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 18:01:36 +00:00
|
|
|
|
format_test {
|
|
|
|
|
input = { 1/2, 1/3, 1/4, 1/5 },
|
|
|
|
|
expect = '{ 1/2, 1/3, 1/4, 1/5 }',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
return SUITE
|