提交 59565563 编写于 作者: 杜庆泉's avatar 杜庆泉

兼容所有的 double 测试结果对比

上级 03b0b7c4
...@@ -16,6 +16,32 @@ export class Matchers<T> { ...@@ -16,6 +16,32 @@ export class Matchers<T> {
// #endif // #endif
} }
} }
/**
* 数值对比器
*/
export class NumberMatchers {
private actual: number
constructor(actual: number) {
this.actual = actual
}
/**
* 用于浮点数对比,只比较小数点后5位,web/app 浮点储存位数不同
*/
toEqualDouble(expected:number) {
let absDiff = Math.abs(this.actual - expected)
if (absDiff < 0.00001) {
return
}
// #ifndef APP-IOS
throw new Error(format(expected, this.actual))
// #endif
}
}
function format(expected: any | null, actual: any | null): string { function format(expected: any | null, actual: any | null): string {
return `expected:<${expected}> but was:<${actual}>` return `expected:<${expected}> but was:<${actual}>`
......
import { describe, test, expect, Result } from './tests.uts' import { describe, test, expect,expectNumber, Result } from './tests.uts'
export function testMath(): Result { export function testMath(): Result {
return describe("Math", () => { return describe("Math", () => {
...@@ -68,8 +68,8 @@ export function testMath(): Result { ...@@ -68,8 +68,8 @@ export function testMath(): Result {
test('acosh', () => { test('acosh', () => {
// 解决精度问题 // 解决精度问题
expect(Math.acosh(1)).toEqual(0.0); expect(Math.acosh(1)).toEqual(0.0);
expect(Math.acosh(2)).toEqual(1.3169578969248166); expectNumber(Math.acosh(2)).toEqualDouble(1.3169578969248166);
expect(Math.acosh(2.5)).toEqual(1.566799236972411); expectNumber(Math.acosh(2.5)).toEqualDouble(1.566799236972411);
}) })
test('asin', () => { test('asin', () => {
expect(Math.asin(-1)).toEqual(-1.5707963267948966); expect(Math.asin(-1)).toEqual(-1.5707963267948966);
...@@ -79,10 +79,10 @@ export function testMath(): Result { ...@@ -79,10 +79,10 @@ export function testMath(): Result {
expect(Math.asin(1)).toEqual(1.5707963267948966); expect(Math.asin(1)).toEqual(1.5707963267948966);
}) })
test('asinh', () => { test('asinh', () => {
expect(Math.asinh(1)).toEqual(0.881373587019543); expectNumber(Math.asinh(1)).toEqualDouble(0.881373587019543);
expect(Math.asinh(0)).toEqual(0); expectNumber(Math.asinh(0)).toEqualDouble(0);
expect(Math.asinh(-1)).toEqual(-0.881373587019543); expectNumber(Math.asinh(-1)).toEqualDouble(-0.881373587019543);
expect(Math.asinh(2)).toEqual(1.4436354751788103); expectNumber(Math.asinh(2)).toEqualDouble(1.4436354751788103);
}) })
test('atan', () => { test('atan', () => {
expect(Math.atan(1)).toEqual(0.7853981633974483); expect(Math.atan(1)).toEqual(0.7853981633974483);
...@@ -133,8 +133,8 @@ export function testMath(): Result { ...@@ -133,8 +133,8 @@ export function testMath(): Result {
expect(Math.exp(1)).toEqual(2.718281828459045); expect(Math.exp(1)).toEqual(2.718281828459045);
}) })
test('expm1', () => { test('expm1', () => {
expect(Math.expm1(1)).toEqual(1.718281828459045); expectNumber(Math.expm1(1)).toEqualDouble(1.718281828459045);
expect(Math.expm1(-38)).toEqual(-1); expectNumber(Math.expm1(-38)).toEqualDouble(-1);
}) })
test('floor', () => { test('floor', () => {
expect(Math.floor(5.95)).toEqual(5); expect(Math.floor(5.95)).toEqual(5);
...@@ -144,7 +144,7 @@ export function testMath(): Result { ...@@ -144,7 +144,7 @@ export function testMath(): Result {
}) })
test('fround', () => { test('fround', () => {
expect(Math.fround(1.5)).toEqual(1.5); expect(Math.fround(1.5)).toEqual(1.5);
expect(Math.fround(1.337)).toEqual(1.3370000123977661); expectNumber(Math.fround(1.337)).toEqualDouble(1.3370000123977661);
}) })
// test('hypot', () => { // test('hypot', () => {
// expect(Math.hypot(3, 4)).toEqual(5); // expect(Math.hypot(3, 4)).toEqual(5);
...@@ -187,8 +187,8 @@ export function testMath(): Result { ...@@ -187,8 +187,8 @@ export function testMath(): Result {
expect(Math.min(-2, -3, -1)).toEqual(-3); expect(Math.min(-2, -3, -1)).toEqual(-3);
}) })
test('pow', () => { test('pow', () => {
expect(Math.pow(7, 3)).toEqual(343); expectNumber(Math.pow(7, 3)).toEqualDouble(343);
expect(Math.pow(4, 0.5)).toEqual(2); expectNumber(Math.pow(4, 0.5)).toEqualDouble(2);
}) })
test('random', () => { test('random', () => {
function getRandomInt(max:number):number { function getRandomInt(max:number):number {
......
import { Matchers } from './Matchers.uts' import { Matchers,NumberMatchers } from './Matchers.uts'
export const describes = new Map<string, Result>() export const describes = new Map<string, Result>()
...@@ -32,3 +32,7 @@ export function test(name: string, fn: () => void) { ...@@ -32,3 +32,7 @@ export function test(name: string, fn: () => void) {
export function expect<T>(value: T): Matchers<T> { export function expect<T>(value: T): Matchers<T> {
return new Matchers(value); return new Matchers(value);
} }
export function expectNumber(value: number): NumberMatchers {
return new NumberMatchers(value);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册