-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathmath.spec.ts
More file actions
113 lines (105 loc) · 3.51 KB
/
math.spec.ts
File metadata and controls
113 lines (105 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as tstl from "../../../src";
import * as util from "../../util";
// These test are kept to a minimum,
// because we just want to confirm translation is correct
// and not test Lua's built in math functions.
// Differences in math implementations between JS & Lua cause inaccuracies
// therefore test input numbers are "carefully" selected to always match accuratly.
// Lualib implementations are tested separately.
test.each([
// log
"Math.log(42)",
"Math.log10(10)",
"Math.log2(42)",
"Math.log1p(42)",
// round
"Math.round(0.1)",
"Math.round(0.9)",
"Math.round(0.5)",
// abs
"Math.abs(-42)",
"Math.abs(42)",
// trigometric
"Math.acos(0.42)",
"Math.asin(0.42)",
"Math.atan(0.42)",
"Math.cos(42)",
"Math.sin(42)",
"Math.tan(42)",
"Math.PI",
// ceil & floor
"Math.ceil(42.42)",
"Math.floor(42.42)",
// exp
"Math.exp(42)",
// max & min
"Math.max(-42, 42)",
"Math.max(42, -42)",
"Math.max(42, 42)",
"Math.max(-42, -42)",
"Math.min(42, -42)",
"Math.min(-42, 42)",
"Math.min(42, 42)",
"Math.min(-42, -42)",
// pow
"Math.pow(4.2, 4.2)",
"Math.pow(4.2, -4.2)",
"Math.pow(-4.2, -4.2)",
// random
// "Math.random()",
// sqrt
"Math.sqrt(2)",
"Math.sqrt(-2)",
// trunc
"Math.trunc(42.42)",
"Math.trunc(-42.42)",
"Math.trunc(0)",
"Math.trunc(Infinity)",
"Math.trunc(-Infinity)",
])("%s", code => {
util.testExpression(code).expectToMatchJsResult();
});
// Hard to test properly
util.testExpression("Math.random()").expectNoExecutionError();
test.each(["E", "LN10", "LN2", "LOG10E", "LOG2E", "SQRT1_2", "SQRT2"])("Math.%s", constant => {
util.testExpression`Math.${constant}`.tap(builder => {
expect(builder.getLuaExecutionResult()).toBeCloseTo(builder.getJsExecutionResult());
});
});
// LuaLib MathSign
test.each([
"Math.sign(-42)",
"Math.sign(42)",
"Math.sign(-4.2)",
"Math.sign(4.2)",
"Math.sign(0)",
"Math.sign(-Infinity)",
])("%s", code => {
util.testExpression(code).expectToMatchJsResult();
});
// LuaLib Atan2
const expectMathAtan2: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan2(");
const expectMathAtan: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toContain("math.atan(");
const expectLualibMathAtan2: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).toContain("__TS__MathAtan2(");
util.testEachVersion("Math.atan2", () => util.testExpression`Math.atan2(4, 5)`, {
[tstl.LuaTarget.Universal]: builder => builder.tap(expectLualibMathAtan2),
[tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectMathAtan2),
[tstl.LuaTarget.Lua50]: builder => builder.tap(expectMathAtan2),
[tstl.LuaTarget.Lua51]: builder => builder.tap(expectMathAtan2),
[tstl.LuaTarget.Lua52]: builder => builder.tap(expectMathAtan2),
[tstl.LuaTarget.Lua53]: builder => builder.tap(expectMathAtan),
[tstl.LuaTarget.Lua54]: builder => builder.tap(expectMathAtan),
[tstl.LuaTarget.Lua55]: builder => builder.tap(expectMathAtan),
[tstl.LuaTarget.Luau]: builder => builder.tap(expectMathAtan2),
});
util.testEachVersion(
"Math.atan2(4, 5)",
() => util.testExpression`Math.atan2(4, 5)`,
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
);
util.testEachVersion(
"Math.pow(3, 5)",
() => util.testExpression`Math.pow(3, 5)`,
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
);