|
1 | 1 | import * as tstl from "../../../src"; |
2 | 2 | import * as util from "../../util"; |
3 | 3 |
|
| 4 | +// These test are kept to a minimum, |
| 5 | +// because we just want to confirm translation is correct |
| 6 | +// and not test Lua's built in math functions. |
| 7 | +// Differences in math implementations between JS & Lua cause inaccuracies |
| 8 | +// therefore test input numbers are "carefully" selected to always match accuratly. |
| 9 | +// Lualib implementations are tested separately. |
4 | 10 | test.each([ |
5 | | - "Math.cos()", |
6 | | - "Math.sin()", |
7 | | - "Math.min()", |
8 | | - "Math.log2(3)", |
9 | | - "Math.log10(3)", |
10 | | - "const x = Math.log2(3)", |
11 | | - "const x = Math.log10(3)", |
12 | | - "Math.log1p(3)", |
13 | | - "Math.round(3.3)", |
| 11 | + // log |
| 12 | + "Math.log(42)", |
| 13 | + "Math.log10(10)", |
| 14 | + "Math.log2(42)", |
| 15 | + "Math.log1p(42)", |
| 16 | + // round |
| 17 | + "Math.round(0.1)", |
| 18 | + "Math.round(0.9)", |
| 19 | + "Math.round(0.5)", |
| 20 | + // abs |
| 21 | + "Math.abs(-42)", |
| 22 | + "Math.abs(42)", |
| 23 | + // trigometric |
| 24 | + "Math.acos(0.42)", |
| 25 | + "Math.asin(0.42)", |
| 26 | + "Math.atan(0.42)", |
| 27 | + "Math.cos(42)", |
| 28 | + "Math.sin(42)", |
| 29 | + "Math.tan(42)", |
14 | 30 | "Math.PI", |
| 31 | + // ceil & floor |
| 32 | + "Math.ceil(42.42)", |
| 33 | + "Math.floor(42.42)", |
| 34 | + // exp |
| 35 | + "Math.exp(42)", |
| 36 | + // max & min |
| 37 | + "Math.max(-42, 42)", |
| 38 | + "Math.max(42, -42)", |
| 39 | + "Math.max(42, 42)", |
| 40 | + "Math.max(-42, -42)", |
| 41 | + "Math.min(42, -42)", |
| 42 | + "Math.min(-42, 42)", |
| 43 | + "Math.min(42, 42)", |
| 44 | + "Math.min(-42, -42)", |
| 45 | + // pow |
| 46 | + "Math.pow(4.2, 4.2)", |
| 47 | + "Math.pow(4.2, -4.2)", |
| 48 | + "Math.pow(-4.2, -4.2)", |
| 49 | + // random |
| 50 | + // "Math.random()", |
| 51 | + // sqrt |
| 52 | + "Math.sqrt(2)", |
| 53 | + "Math.sqrt(-2)", |
15 | 54 | ])("%s", code => { |
16 | | - // TODO: Remove? |
17 | | - util.testFunction(code).disableSemanticCheck().expectLuaToMatchSnapshot(); |
| 55 | + util.testExpression(code).expectToMatchJsResult(); |
18 | 56 | }); |
19 | 57 |
|
| 58 | +// Hard to test properly |
| 59 | +util.testExpression("Math.random()").expectNoExecutionError(); |
| 60 | + |
20 | 61 | test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s", constant => { |
21 | 62 | util.testExpression`Math.${constant}`.tap(builder => { |
22 | 63 | expect(builder.getLuaExecutionResult()).toBeCloseTo(builder.getJsExecutionResult()); |
23 | 64 | }); |
24 | 65 | }); |
25 | 66 |
|
| 67 | +// LuaLib MathSign |
| 68 | +test.each(["Math.sign(-42)", "Math.sign(42)", "Math.sign(-4.2)", "Math.sign(4.2)", "Math.sign(0)"])("%s", code => { |
| 69 | + util.testExpression(code).expectToMatchJsResult(); |
| 70 | +}); |
| 71 | + |
| 72 | +// LuaLib Atan2 |
26 | 73 | const expectMathAtan2: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan2("); |
27 | 74 | const expectMathAtan: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan("); |
28 | 75 | const expectLualibMathAtan2: util.TapCallback = builder => |
|
0 commit comments