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) end local function number_test (t) SUITE:addTest(t.expect, function () assert_equal(t.expect, format(t.input, {})) end) SUITE:addTest(t.shorthand or (t.expect .. ' shorthand'), function () assert_equal(t.shorthand or t.expect, format(t.input, { math_shorthand = true })) end) 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 = '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', } -------------------------------------------------------------------------------- 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