From 59565563c8eff587a0733538ea99278c8a918ecf Mon Sep 17 00:00:00 2001 From: duqingquan Date: Sat, 5 Aug 2023 15:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=89=80=E6=9C=89=E7=9A=84?= =?UTF-8?q?=20double=20=E6=B5=8B=E8=AF=95=E7=BB=93=E6=9E=9C=E5=AF=B9?= =?UTF-8?q?=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/Matchers.uts | 26 +++++++++++++++++++++++ uni_modules/uts-tests/utssdk/Math.uts | 24 ++++++++++----------- uni_modules/uts-tests/utssdk/tests.uts | 6 +++++- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/Matchers.uts b/uni_modules/uts-tests/utssdk/Matchers.uts index 5818ccd..d510892 100644 --- a/uni_modules/uts-tests/utssdk/Matchers.uts +++ b/uni_modules/uts-tests/utssdk/Matchers.uts @@ -16,6 +16,32 @@ export class Matchers { // #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 { return `expected:<${expected}> but was:<${actual}>` diff --git a/uni_modules/uts-tests/utssdk/Math.uts b/uni_modules/uts-tests/utssdk/Math.uts index e6c889e..bcfd8b7 100644 --- a/uni_modules/uts-tests/utssdk/Math.uts +++ b/uni_modules/uts-tests/utssdk/Math.uts @@ -1,4 +1,4 @@ -import { describe, test, expect, Result } from './tests.uts' +import { describe, test, expect,expectNumber, Result } from './tests.uts' export function testMath(): Result { return describe("Math", () => { @@ -68,8 +68,8 @@ export function testMath(): Result { test('acosh', () => { // 解决精度问题 expect(Math.acosh(1)).toEqual(0.0); - expect(Math.acosh(2)).toEqual(1.3169578969248166); - expect(Math.acosh(2.5)).toEqual(1.566799236972411); + expectNumber(Math.acosh(2)).toEqualDouble(1.3169578969248166); + expectNumber(Math.acosh(2.5)).toEqualDouble(1.566799236972411); }) test('asin', () => { expect(Math.asin(-1)).toEqual(-1.5707963267948966); @@ -79,10 +79,10 @@ export function testMath(): Result { expect(Math.asin(1)).toEqual(1.5707963267948966); }) test('asinh', () => { - expect(Math.asinh(1)).toEqual(0.881373587019543); - expect(Math.asinh(0)).toEqual(0); - expect(Math.asinh(-1)).toEqual(-0.881373587019543); - expect(Math.asinh(2)).toEqual(1.4436354751788103); + 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); }) test('atan', () => { expect(Math.atan(1)).toEqual(0.7853981633974483); @@ -133,8 +133,8 @@ export function testMath(): Result { expect(Math.exp(1)).toEqual(2.718281828459045); }) test('expm1', () => { - expect(Math.expm1(1)).toEqual(1.718281828459045); - expect(Math.expm1(-38)).toEqual(-1); + expectNumber(Math.expm1(1)).toEqualDouble(1.718281828459045); + expectNumber(Math.expm1(-38)).toEqualDouble(-1); }) test('floor', () => { expect(Math.floor(5.95)).toEqual(5); @@ -144,7 +144,7 @@ export function testMath(): Result { }) test('fround', () => { 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', () => { // expect(Math.hypot(3, 4)).toEqual(5); @@ -187,8 +187,8 @@ export function testMath(): Result { expect(Math.min(-2, -3, -1)).toEqual(-3); }) test('pow', () => { - expect(Math.pow(7, 3)).toEqual(343); - expect(Math.pow(4, 0.5)).toEqual(2); + expectNumber(Math.pow(7, 3)).toEqualDouble(343); + expectNumber(Math.pow(4, 0.5)).toEqualDouble(2); }) test('random', () => { function getRandomInt(max:number):number { diff --git a/uni_modules/uts-tests/utssdk/tests.uts b/uni_modules/uts-tests/utssdk/tests.uts index d647677..953d721 100644 --- a/uni_modules/uts-tests/utssdk/tests.uts +++ b/uni_modules/uts-tests/utssdk/tests.uts @@ -1,4 +1,4 @@ -import { Matchers } from './Matchers.uts' +import { Matchers,NumberMatchers } from './Matchers.uts' export const describes = new Map() @@ -32,3 +32,7 @@ export function test(name: string, fn: () => void) { export function expect(value: T): Matchers { return new Matchers(value); } + +export function expectNumber(value: number): NumberMatchers { + return new NumberMatchers(value); +} -- GitLab