From 01874ff167898139dc27c21a17f257298447b786 Mon Sep 17 00:00:00 2001 From: mahaifeng Date: Wed, 4 Sep 2024 18:55:03 +0800 Subject: [PATCH] =?UTF-8?q?[number]=E5=8E=BB=E9=99=A4=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E4=BB=A3=E7=A0=81(=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/Number.uts | 125 ++++++++++++++++++------ 1 file changed, 95 insertions(+), 30 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/Number.uts b/uni_modules/uts-tests/utssdk/Number.uts index 3988248..ee18275 100644 --- a/uni_modules/uts-tests/utssdk/Number.uts +++ b/uni_modules/uts-tests/utssdk/Number.uts @@ -5,9 +5,16 @@ export function testNumber() : Result { return describe("Number", () => { test('toFixed', () => { + // #TEST Number.toFixed function financial(x : Number) : String { return x.toFixed(2); } + console.log(financial(123.456)); + // expected output: "123.46" + console.log(financial(0.004)); + // expected output: "0.00" + // #END + expect(financial(123.456)).toEqual('123.46'); expect(financial(0.004)).toEqual("0.00"); expect(financial(0)).toEqual("0.00"); @@ -60,7 +67,7 @@ export function testNumber() : Result { expect(isFinite(1000 / 1)).toEqual(true); expect(isFinite(910)).toEqual(true); expect(isFinite(0)).toEqual(true); - // #ifdef APP-ANDROID + // #ifdef APP-ANDROID let aj2 = JSON.parse('{"a":1}') as UTSJSONObject; let aNumber = aj2['a'] as UTSNumber expect(isFinite(aNumber)).toEqual(true); @@ -69,12 +76,12 @@ export function testNumber() : Result { expect(UTSNumber.isFinite(Double.POSITIVE_INFINITY)).toEqual(false); // #endif }) - - - + + + test('number-from-json-parse', () => { type A = { - a:number + a : number } let aj = JSON.parse('{"a":1}'); expect(aj?.a == 1).toEqual(true); @@ -82,13 +89,13 @@ export function testNumber() : Result { expect(aj?.a == 1.0).toEqual(true); expect(aj?.a == 1.0 as number).toEqual(true); // #ifdef APP-ANDROID - expect(numberEquals(aj?.a,1)).toEqual(true); - expect(numberEquals(aj?.a,1 as number)).toEqual(true); - expect(numberEquals(aj?.a,1.0)).toEqual(true); - expect(numberEquals(aj?.a,1.0 as number)).toEqual(true); - let ki:Int = 1; - let kd:Double = 1.0; - let kf:Float = (1.0).toFloat(); + expect(numberEquals(aj?.a, 1)).toEqual(true); + expect(numberEquals(aj?.a, 1 as number)).toEqual(true); + expect(numberEquals(aj?.a, 1.0)).toEqual(true); + expect(numberEquals(aj?.a, 1.0 as number)).toEqual(true); + let ki : Int = 1; + let kd : Double = 1.0; + let kf : Float = (1.0).toFloat(); expect(aj?.a == ki).toEqual(true); expect(aj?.a == kd).toEqual(true); expect(aj?.a == kf).toEqual(true); @@ -105,7 +112,7 @@ export function testNumber() : Result { newA.a = 1.1 expect(aj?.a == newA.a).toEqual(false); expect(aj?.a === newA.a).toEqual(false); - + let a10 = JSON.parse("10") let b10 = JSON.parse("10") let c10 = JSON.parse("10.0") @@ -113,26 +120,26 @@ export function testNumber() : Result { expect(a10 == c10).toEqual(true); let aj2 = JSON.parse('{"a":1}'); expect(aj?.a == aj2?.a).toEqual(true); - + // #endif - + }) - - + + test('isInteger', () => { expect(Number.isInteger(12)).toEqual(true); expect(Number.isInteger(12.01)).toEqual(false); expect(Number.isInteger(-213123112.01)).toEqual(false); expect(Number.isInteger(-213123112)).toEqual(true); }) - + test('Unary-Operators', () => { // #ifdef APP-ANDROID let a = JSON.parse("1") as Number a++; expect(a.toString()).toEqual("2"); expect(UTSAndroid.getJavaClass(a).name).toEqual("io.dcloud.uts.UTSNumber"); - + a--; expect(a.toString()).toEqual("1"); expect(UTSAndroid.getJavaClass(a).name).toEqual("io.dcloud.uts.UTSNumber"); @@ -151,22 +158,22 @@ export function testNumber() : Result { let a2 = a % 5 expect(a2.toString()).toEqual("1"); expect(UTSAndroid.getJavaClass(a2).name).toEqual("io.dcloud.uts.UTSNumber"); - + let b = 12 let c1 = b + a2 expect(c1.toString()).toEqual("13"); expect(UTSAndroid.getJavaClass(c1).name).toEqual("io.dcloud.uts.UTSNumber"); - + let c2 = a2 + 2 expect(c2.toString()).toEqual("3"); expect(UTSAndroid.getJavaClass(c2).name).toEqual("io.dcloud.uts.UTSNumber"); - + let c3 = 2 + a2 expect(c3.toString()).toEqual("3"); expect(UTSAndroid.getJavaClass(c3).name).toEqual("io.dcloud.uts.UTSNumber"); // #endif - - + + }) test('isNaN', () => { @@ -183,12 +190,19 @@ export function testNumber() : Result { }) test('toPrecision', () => { + // #TEST Number.toPrecision + console.log(123.456.toPrecision(4))//123.5 + // #END expect(123.456.toPrecision(4)).toEqual("123.5"); expect(0.004.toPrecision(4)).toEqual("0.004000"); // expect(1.23e5.toPrecision(4)).toEqual("1.230e+5"); }) test('toString', () => { + // #TEST Number.toString + console.log((10).toString())//10 + // #END + expect((10).toString()).toEqual("10"); expect((17).toString()).toEqual("17"); expect((17.2).toString()).toEqual("17.2"); @@ -200,13 +214,13 @@ export function testNumber() : Result { expect((123456789987654).toString(16)).toEqual("7048861cc146"); expect((-0xff).toString(2)).toEqual("-11111111"); const a = 1e38 - - expect(a.toString(16).substring(0,12)).toEqual("4b3b4ca85a86"); + + expect(a.toString(16).substring(0, 12)).toEqual("4b3b4ca85a86"); expect(a.toString(16).length).toEqual(32); expect(a.toString(16).substring(23)).toEqual("000000000"); - + expect(a.toString(2).length).toEqual(127); - expect(a.toString(2).substring(0,32)).toEqual("10010110011101101001100101010000"); + expect(a.toString(2).substring(0, 32)).toEqual("10010110011101101001100101010000"); expect(a.toString(2).substring(100)).toEqual("000000000000000000000000000"); const b = 1e22 const bStr12 = b.toString(12) @@ -215,8 +229,8 @@ export function testNumber() : Result { * 浏览器端:27373a86ba1a195400000 */ expect(bStr12.length).toEqual(21); - expect(bStr12.substring(0,14)).toEqual("27373a86ba1a19"); - + expect(bStr12.substring(0, 14)).toEqual("27373a86ba1a19"); + // #ifdef APP-ANDROID expect((new UTSNumber(2709954670497349.5)).toString()).toEqual("2709954670497349.5"); expect((new UTSNumber(0.00000000000001)).toString()).toEqual("1e-14"); @@ -228,6 +242,9 @@ export function testNumber() : Result { }) test('valueOf', () => { + // #TEST Number.valueOf + console.log((10).valueOf()) //10 + // #END expect((10).valueOf()).toEqual(10); expect((-10.2).valueOf()).toEqual(-10.2); expect((0xf).valueOf()).toEqual(15); @@ -241,6 +258,54 @@ export function testNumber() : Result { expect(num.toDouble()).toEqual(10.123); }) // #endif + test('toInt', () => { + // #ifdef APP + // #TEST Number.toInt + let a = 12 + console.log(a.toInt()); + // expected output: 12 + + // Int最大值2147483647,溢出了 + let b = 2147483648 + // expected output: -2147483648 + // #END + expect(a.toInt()).toEqual(12); + // #endif + }) + + test('toByte', () => { + // #ifdef APP + // #TEST Number.toByte + let a = 12 + console.log(a.toByte()); + // expected output: 12 + // #END + expect(a.toInt()).toEqual(12); + // #endif + }) + + test('toLong', () => { + // #ifdef APP + // #TEST Number.toLong + let a = 12 + console.log(a.toLong()); + // expected output: 12 + // #END + expect(a.toLong()).toEqual(12); + // #endif + }) + + test('from', () => { + // #ifdef APP + // #TEST Number.from + let a = 12 + let b = Number.from(a) + console.log(b); + // expected output: 12 + // #END + expect(b).toEqual(12); + // #endif + }) test('numberEquals', () => { let a1 = 10.123 -- GitLab