1
0
pretty/test/test_number.lua

206 lines
3.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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))
end, { line = debug.getinfo(2).currentline })
end
local function number_test (t)
SUITE:addTest(t.name or t.expect, function ()
assert_equal(t.expect, format(t.input, {}))
end)
SUITE:addTest(t.name and (t.name .. ' shorthand') or t.shorthand or (t.expect .. ' shorthand'), function ()
assert_equal(t.shorthand or t.expect, format(t.input, { math_shorthand = true }))
end, { line = debug.getinfo(2).currentline })
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)',
shorthand = '√̅8',
}
number_test {
input = 1/math.sqrt(8),
expect = '1/math.sqrt(8)',
shorthand = '1/√̅8',
}
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 = '',
}
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',
}
--------------------------------------------------------------------------------
-- 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',
}
--------------------------------------------------------------------------------
number_test {
-- A half should always be represented using 1/2, never with 2⁻¹.
input = 1/2,
expect = '1/2',
}
format_test {
input = { 1/2, 1/3, 1/4, 1/5 },
expect = '{ 1/2, 1/3, 1/4, 1/5 }',
}
--------------------------------------------------------------------------------
return SUITE