Math.uts 10.4 KB
Newer Older
1
import { describe, test, expect,expectNumber, Result } from './tests.uts'
Y
yurj26 已提交
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

export function testMath(): Result {
    return describe("Math", () => {
        test('E', () => {
            function getNapier():number {
                return Math.E
            }
            expect(getNapier()).toEqual(2.718281828459045);
        })
        test('LN10', () => {
            function getNatLog10():number {
                return Math.LN10;
            }
            expect(getNatLog10()).toEqual(2.302585092994046);
        })
        test('LN2', () => {
            function getNatLog2():number {
                return Math.LN2;
            }
            expect(getNatLog2()).toEqual(0.6931471805599453);
        })
        test('LOG10E', () => {
            function getLog10e():number {
                return Math.LOG10E;
            }
            expect(getLog10e()).toEqual(0.4342944819032518);
        })
        test('LOG2E', () => {
            function getLog2e():number {
                return Math.LOG2E;
            }
            expect(getLog2e()).toEqual(1.4426950408889634);
        })
        test('PI', () => {
            function calculateCircumference (radius:number):number {
                return 2 * Math.PI * radius;
            }
            expect(calculateCircumference(1)).toEqual(6.283185307179586);
        })
        test('SQRT1_2', () => {
            function getRoot1_2():number {
                return Math.SQRT1_2;
            }
            expect(getRoot1_2()).toEqual(0.7071067811865476);
        })
        test('SQRT2', () => {
            function getRoot2():number {
                return Math.SQRT2;
            }
            expect(getRoot2()).toEqual(1.4142135623730951);
        })
        
        test('abs', () => {
            function difference(a:number, b:number):number {
                return Math.abs(a - b);
            }
            expect(difference(3, 5)).toEqual(2);
            expect(difference(5, 3)).toEqual(2);
            expect(difference(1.23456, 7.89012)).toEqual(6.6555599999999995);
        })
        test('acos', () => {
            expect(Math.acos(-1)).toEqual(3.141592653589793);
64
            expect(Math.acos(NaN)).toEqual(NaN);
Y
yurj26 已提交
65 66
            expect(Math.acos(0)).toEqual(1.5707963267948966);
            // 解决精度问题
67
            expect(Math.acos(1)).toEqual(0);
Y
yurj26 已提交
68 69 70
        })
        test('acosh', () => {
            // 解决精度问题
71
            expect(Math.acosh(1)).toEqual(0);
72 73
            expect(Math.acosh(NaN)).toEqual(NaN);
            expectNumber(Math.acosh(2)).toEqualDouble(1.3169578969248166);
74
            expectNumber(Math.acosh(2.5)).toEqualDouble(1.566799236972411);
Y
yurj26 已提交
75 76
        })
        test('asin', () => {
77
            expect(Math.asin(NaN)).toEqual(NaN);
Y
yurj26 已提交
78 79
            expect(Math.asin(-1)).toEqual(-1.5707963267948966);
            // 解决精度问题
80
            expect(Math.asin(0)).toEqual(0);
lizhongyi_'s avatar
lizhongyi_ 已提交
81
            // expect(Math.asin(0.5)).toEqual(0.5235987755982989);
Y
yurj26 已提交
82 83 84
            expect(Math.asin(1)).toEqual(1.5707963267948966);
        })
        test('asinh', () => {
85
            expect(Math.asinh(NaN)).toEqual(NaN);
86 87 88 89
            expectNumber(Math.asinh(1)).toEqualDouble(0.881373587019543);
            expectNumber(Math.asinh(0)).toEqualDouble(0);
            expectNumber(Math.asinh(-1)).toEqualDouble(-0.881373587019543);
            expectNumber(Math.asinh(2)).toEqualDouble(1.4436354751788103);
Y
yurj26 已提交
90 91
        })
        test('atan', () => {
92
            expect(Math.atan(NaN)).toEqual(NaN);
Y
yurj26 已提交
93 94
            expect(Math.atan(1)).toEqual(0.7853981633974483);
            // 解决精度问题
95
            expect(Math.atan(0)).toEqual(0);
Y
yurj26 已提交
96 97 98
        })
        test('atan2', () => {
            expect(Math.atan2(90, 15)).toEqual(1.4056476493802699);
99
            expectNumber(Math.atan2(15, 90)).toEqualDouble(0.16514867741462683);
Y
yurj26 已提交
100
        })
101
      
Y
yurj26 已提交
102 103
        test('atanh', () => {
            // 解决精度问题
104
            expect(Math.atanh(NaN)).toEqual(NaN);
105
            expect(Math.atanh(0)).toEqual(0);
lizhongyi_'s avatar
lizhongyi_ 已提交
106
            // expect(Math.atanh(0.5)).toEqual(0.5493061443340548);
Y
yurj26 已提交
107 108 109 110 111 112 113 114 115
        })
        test('cbrt', () => {
            // expect(Math.cbrt(-1)).toEqual(-1);
            // expect(Math.cbrt(0)).toEqual(0);
            // expect(Math.cbrt(1)).toEqual(1);
            // expect(Math.cbrt(2)).toEqual(1.2599210498948732);
        })
        test('ceil', () => {
            expect(Math.ceil(0.95)).toEqual(1);
杜庆泉's avatar
杜庆泉 已提交
116
            expect(Math.ceil(NaN)).toEqual(NaN);
Y
yurj26 已提交
117 118 119
            expect(Math.ceil(4)).toEqual(4);
            expect(Math.ceil(7.004)).toEqual(8);
            expect(Math.ceil(-7.004)).toEqual(-7);
120 121
            expect(Math.ceil(37110233000.223)).toEqual(37110233001);
            expect(Math.ceil(-37110233000.223)).toEqual(-37110233000);
Y
yurj26 已提交
122 123
        })
        test('clz32', () => {
124
            expect(Math.clz32(NaN)).toEqual(32);
Y
yurj26 已提交
125 126
            expect(Math.clz32(1)).toEqual(31);
            expect(Math.clz32(1000)).toEqual(22);
127
            expect(Math.clz32(0)).toEqual(32);
Y
yurj26 已提交
128 129 130 131 132 133 134 135 136
            expect(Math.clz32(3.5)).toEqual(30);
        })
        test('cos', () => {
            expect(Math.cos(0)).toEqual(1.0);
            expect(Math.cos(1)).toEqual(0.5403023058681398);
        })
        test('cosh', () => {
            // 解决精度问题
            expect(Math.cosh(0)).toEqual(1.0);
137 138
            expectNumber(Math.cosh(1)).toEqualDouble(1.5430806348152437);
            expectNumber(Math.cosh(-1)).toEqualDouble(1.5430806348152437);
Y
yurj26 已提交
139 140
        })
        test('exp', () => {
141
            expectNumber(Math.exp(-1)).toEqualDouble(0.36787944117144233);
Y
yurj26 已提交
142 143
            // 解决精度问题
            expect(Math.exp(0)).toEqual(1.0);
144
            expectNumber(Math.exp(1)).toEqualDouble(2.718281828459045);
Y
yurj26 已提交
145 146
        })
        test('expm1', () => {
147 148
            expectNumber(Math.expm1(1)).toEqualDouble(1.718281828459045);
            expectNumber(Math.expm1(-38)).toEqualDouble(-1);
Y
yurj26 已提交
149 150 151 152 153 154
        })
        test('floor', () => {
            expect(Math.floor(5.95)).toEqual(5);
            expect(Math.floor(5.05)).toEqual(5);
            expect(Math.floor(5)).toEqual(5);
            expect(Math.floor(-5.05)).toEqual(-6);
155 156
            expect(Math.floor(37110233000.223)).toEqual(37110233000);
            expect(Math.floor(-37110233000.223)).toEqual(-37110233001);
Y
yurj26 已提交
157 158
        })
        test('fround', () => {
159
            expect(Math.fround(NaN)).toEqual(NaN);
Y
yurj26 已提交
160
            expect(Math.fround(1.5)).toEqual(1.5);
161
            expectNumber(Math.fround(1.337)).toEqualDouble(1.3370000123977661);
Y
yurj26 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        })
        // test('hypot', () => {
            // expect(Math.hypot(3, 4)).toEqual(5);
            // expect(Math.hypot(5, 12)).toEqual(13);
            // expect(Math.hypot(3, 4, 5)).toEqual(7.0710678118654755);
            // expect(Math.hypot(-5)).toEqual(5);
        // })
        // test('imul', () => {
        //     expect(Math.imul(3, 4)).toEqual(12);
        //     expect(Math.imul(-5, 12)).toEqual(-60);
        // })
        test('log', () => {
            // 解决精度问题
            expect(Math.log(1)).toEqual(0.0);
            expect(Math.log(10)).toEqual(2.302585092994046);
        })
        test('log10', () => {
            // 解决精度问题
            expect(Math.log10(10)).toEqual(1.0);
            expect(Math.log10(100)).toEqual(2.0);
            expect(Math.log10(1)).toEqual(0.0);
        })
        test('log1p', () => {
            // 解决精度问题
            expect(Math.log1p(Math.E - 1)).toEqual(1.0);
            expect(Math.log1p(0)).toEqual(0.0);
        })
        test('log2', () => {
            // 解决精度问题
            expect(Math.log2(2)).toEqual(1.0);
            expect(Math.log2(1024)).toEqual(10.0);
            expect(Math.log2(1)).toEqual(0.0);
        })
        test('max', () => {
            expect(Math.max(1, 3, 2)).toEqual(3);
            expect(Math.max(-1, -3, -2)).toEqual(-1);
        })
        test('min', () => {
            expect(Math.min(2, 3, 1)).toEqual(1);
            expect(Math.min(-2, -3, -1)).toEqual(-3);
        })
        test('pow', () => {
204 205
            expectNumber(Math.pow(7, 3)).toEqualDouble(343);
            expectNumber(Math.pow(4, 0.5)).toEqualDouble(2);
Y
yurj26 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        })
        test('random', () => {
            function getRandomInt(max:number):number {
              return Math.floor(Math.random() * max);
            }
            expect(getRandomInt(getRandomInt(1))).toEqual(0);
        })
        test('sign', () => {
            expect(Math.sign(3)).toEqual(1);
            expect(Math.sign(-3)).toEqual(-1);
            expect(Math.sign(0)).toEqual(0);
        })
        test('sin', () => {
            // 解决精度问题
            expect(Math.sin(0)).toEqual(0.0);
            expect(Math.sin(1)).toEqual(0.8414709848078965);
        })
        test('sinh', () => {
            // 解决精度问题
            expect(Math.sinh(0)).toEqual(0.0);
            expect(Math.sinh(1)).toEqual(1.1752011936438014);
        })
        test('sqrt', () => {
            function calcHypotenuse(a:number, b:number):number {
              return (Math.sqrt((a * a) + (b * b)));
            }
            // 解决精度问题
            expect(calcHypotenuse(3, 4)).toEqual(5.0);
            expect(calcHypotenuse(5, 12)).toEqual(13.0);
            expect(calcHypotenuse(0, 0)).toEqual(0.0);
        })
        test('tan', () => {
            // 解决精度问题
            expect(Math.tan(0)).toEqual(0.0);
lizhongyi_'s avatar
lizhongyi_ 已提交
240
            // expect(Math.tan(1)).toEqual(1.5574077246549023);
Y
yurj26 已提交
241 242 243 244 245 246 247 248 249 250 251 252
        })
        test('tanh', () => {
            expect(Math.tanh(-1)).toEqual(-0.7615941559557649);
            // 解决精度问题
            expect(Math.tanh(0)).toEqual(0.0);
            expect(Math.tanh(1)).toEqual(0.7615941559557649);
        })
        test('trunc', () => {
            expect(Math.trunc(13.37)).toEqual(13);
            expect(Math.trunc(42.84)).toEqual(42);
            expect(Math.trunc(0.123)).toEqual(0);
        })
253
      test('round', () => {
254
          expect(Math.round(NaN)).toEqual(NaN);
255 256 257 258 259
          expect(Math.round(Math.E)).toEqual(3);
          expectNumber(Math.round(Number.MAX_VALUE)).toEqualDouble(1.7976931348623157e+308);
          expect(Math.round(Number.MIN_VALUE)).toEqual(0);
          expect(Math.round(Number.NEGATIVE_INFINITY)).toEqual(-Infinity);
          expect(Math.round(Number.POSITIVE_INFINITY)).toEqual(Infinity);
260 261 262 263 264 265
          expect(Math.round(0.9)).toEqual(1);
          expect(Math.round(5.95)).toEqual(6);
          expect(Math.round(-5.05)).toEqual(-5);
          expect(Math.round(37110233000.223)).toEqual(37110233000);
          expect(Math.round(-37110233000.223)).toEqual(-37110233000);
      })
Y
yurj26 已提交
266 267
    })
}