diff --git a/static_core/plugins/ets/sdk/api/@ohos.util.ets b/static_core/plugins/ets/sdk/api/@ohos.util.ets index d4f479ca651eb920fd7fdc4995a215008a03e36c..4877d566d55ae8061d2c90482c1580dc4486bb86 100644 --- a/static_core/plugins/ets/sdk/api/@ohos.util.ets +++ b/static_core/plugins/ets/sdk/api/@ohos.util.ets @@ -34,7 +34,7 @@ export namespace util { export class LRUCache { private cache: Map; // Default current size - private maxSize: number; + private maxSize: int; // Default maximum size private putCount: long = 0; private createCount: long = 0; @@ -42,11 +42,11 @@ export namespace util { private hitCount: long = 0; private missCount: long = 0; - get length(): number { + get length(): int { return this.cache.size; } - public constructor(capacity?: number) { + public constructor(capacity?: int) { if (capacity == undefined) { this.maxSize = 64; } else { @@ -58,8 +58,8 @@ export namespace util { this.cache = new Map(); } - private checkCapacityIsIllegal(capacity: number): boolean { - return (capacity > Int.MAX_VALUE || capacity <= 0 || (capacity % 1) != 0); + private checkCapacityIsIllegal(capacity: int): boolean { + return (capacity <= 0 || (capacity % 1) != 0); } private getLRUKey(): K { @@ -70,7 +70,7 @@ export namespace util { return this.cache.values().next().value as V; } - private reduceCapacity(newCapacity: number): void { + private reduceCapacity(newCapacity: int): void { while (this.cache.keys().next() && this.cache.size > newCapacity) { let lruKey = this.getLRUKey(); this.afterRemoval(true, lruKey, this.getLRUValue(), undefined); @@ -83,7 +83,7 @@ export namespace util { newValue: V | undefined): void { } - public updateCapacity(newCapacity: number): void { + public updateCapacity(newCapacity: int): void { if (this.checkCapacityIsIllegal(newCapacity)) { throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${newCapacity} must be small integer`) } else if (this.cache.size > newCapacity) { @@ -92,7 +92,7 @@ export namespace util { this.maxSize = newCapacity; } - public getCapacity(): number { + public getCapacity(): int { return this.maxSize; } @@ -132,8 +132,8 @@ export namespace util { } override toString(): String { - let peek: number = 0; - let hitRate: number = 0; + let peek: long = 0; + let hitRate: long = 0; peek = this.hitCount + this.missCount; if (peek != 0) { // The value is 100 times larger @@ -143,24 +143,24 @@ export namespace util { ', misses = ' + this.missCount + ', hitRate = ' + hitRate + '% ]'; } - public getCreateCount(): number { - return this.createCount.toDouble(); + public getCreateCount(): long { + return this.createCount; } - public getMissCount(): number { - return this.missCount.toDouble(); + public getMissCount(): long { + return this.missCount; } - public getRemovalCount(): number { - return this.evictionCount.toDouble(); + public getRemovalCount(): long{ + return this.evictionCount; } - public getMatchCount(): number { - return this.hitCount.toDouble(); + public getMatchCount(): long { + return this.hitCount; } - public getPutCount(): number { - return this.putCount.toDouble(); + public getPutCount(): long { + return this.putCount; } public $_iterator(): IterableIterator<[K, V]> { @@ -736,9 +736,8 @@ export namespace util { private static native nativeDestroy(ptr: long): void; } export class RationalNumber { - private mnum: number = 0; - private mden: number = 0; - private sign: number; + private mnum: long = 0; + private mden: long = 0; constructor() {} @@ -754,17 +753,14 @@ export namespace util { /** * Used to create a RationalNumber instance with a given numerator and denominator. - * @param { number } num represents the numerator. - * @param { number } den represents the denominator. + * @param { long } num represents the numerator. + * @param { long } den represents the denominator. * @returns { RationalNumber } return a RationalNumber object. */ - static parseRationalNumber(num: number, den: number): RationalNumber { - if (!Number.isInteger(num) || !Number.isInteger(den)) { - console.error('parseRationalNumber: The type of Parameter must be integer'); - } + static parseRationalNumber(num: long, den: long): RationalNumber { let sign = (num < 0) !== (den < 0) ? -1 : 1; - num = Math.abs(num); - den = Math.abs(den); + num = Math.abs(num).toLong(); + den = Math.abs(den).toLong(); let ratNum = new RationalNumber(); if (den === 0) { if (num !== 0) { @@ -808,7 +804,7 @@ export namespace util { if (!Number.isInteger(num1) || !Number.isInteger(num2)) { console.error('createRationalFromString: The type of Parameter must be integer string'); } - return RationalNumber.parseRationalNumber(num1, num2); + return RationalNumber.parseRationalNumber(num1.toLong(), num2.toLong()); } else { throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${str} must be character string`); @@ -820,7 +816,7 @@ export namespace util { * @param { RationalNumber } other RationalNumber object. * @returns { number } if equal return 0, If the givens smaller than the current return 1, otherwise return -1. */ - public compare(other: RationalNumber): number { + public compare(other: RationalNumber): int { if (this.mnum === other.mnum && this.mden === other.mden) { return 0; } else if (this.isNaN()) { @@ -858,9 +854,9 @@ export namespace util { /** * Gets the value of the current RationalNumber object as an integer or float number. * - * @returns { number } returns the value of an integer or float number. + * @returns { double } returns the value of an integer or float number. */ - public valueOf(): number { + public valueOf(): double { if (this.mnum > 0 && this.mden === 0) { return Number.POSITIVE_INFINITY; } else if (this.mnum < 0 && this.mden === 0) { @@ -868,29 +864,26 @@ export namespace util { } else if (this.isNaN()) { return Number.NaN; } else { - return this.mnum / this.mden; + return Number(this.mnum) / Number(this.mden); } } /** * Gets the greatest common divisor of two specified integers. - * @param { number } firNum the integer. - * @param { number } SecNum the integer. - * @returns { number } returns the greatest common divisor of firNum and SecNum. + * @param { long } firNum the integer. + * @param { long } SecNum the integer. + * @returns { long } returns the greatest common divisor of firNum and SecNum. */ - static getCommonFactor(firNum: number, SecNum: number): number { + static getCommonFactor(firNum: long, SecNum: long): long { if (firNum === 0 || SecNum === 0) { throw createBusinessError(TypeErrorCodeId, `Parameter error. The Parameter cannot be zero`); } - if (!Number.isInteger(firNum) || !Number.isInteger(SecNum)) { - console.error('getCommonFactor: The type of Parameter must be integer'); - } return RationalNumber.getGCD(firNum, SecNum); } - private static getGCD(firNum: number, SecNum: number): number { - let temp: number = 0; + private static getGCD(firNum: long, SecNum: long): long { + let temp: long = 0; if (firNum < SecNum) { temp = firNum; firNum = SecNum; @@ -907,18 +900,18 @@ export namespace util { /** * Gets the denominator of the current RationalNumber object. * - * @returns { number } returns the denominator. + * @returns { long } returns the denominator. */ - public getDenominator(): number { + public getDenominator(): long { return this.mden; } /** * Gets the numerator of the current RationalNumber object. * - * @returns { number } returns the numerator. + * @returns { long } returns the numerator. */ - public getNumerator(): number { + public getNumerator(): long { return this.mnum; } @@ -1699,13 +1692,13 @@ export namespace util { /** * Get the hash code of an object. * @param { RecordData } [obj] - The object that need to get hash code. - * @returns { number } Return a hash code of an object. + * @returns { long } Return a hash code of an object. */ - export function getHash(obj: RecordData): number { + export function getHash(obj: RecordData): long { if (obj === undefined || obj === null) { throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of Parameter must be object.`); } - let result: number = Runtime.getHashCodeByValue(obj!); + let result: long = Runtime.getHashCodeByValue(obj!); return result; } diff --git a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets index 3824157ca9d78ad3a88208832db6e3dc837614a1..93e57ca01e7a402d80840ba72a3828bf23b88b16 100644 --- a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets +++ b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets @@ -26,42 +26,14 @@ const expectedRangeErrors = (e: Error): boolean => { function main(): int { const suite = new arktest.ArkTestsuite("LRUCache API Error tests") - suite.addTest("LRUCache Constructor Long", testLRUCacheConstructor_Long); - suite.addTest("LRUCache Constructor Decimal", testLRUCacheConstructor_Decimal); suite.addTest("LRUCache Constructor Zero", testLRUCacheConstructor_Zero); suite.addTest("LRUCache Constructor Negative", testLRUCacheConstructor_Negative); - suite.addTest("LRUCache update&getCapacity Long", testLRUCacheUpdateGetCapacity_Long); - suite.addTest("LRUCache update&getCapacity Decimal", testLRUCacheUpdateGetCapacity_Decimal); suite.addTest("LRUCache update&getCapacity Zero", testLRUCacheUpdateGetCapacity_Zero); suite.addTest("LRUCache update&getCapacity Negative", testLRUCacheUpdateGetCapacity_Negative); - suite.addTest("LRUCache Constructor NumberMin", testLRUCacheConstructor_NumberMin); - suite.addTest("LRUCache update&getCapacity NumberMin", testLRUCacheUpdateGetCapacity_NumberMin); return suite.run() } -function testLRUCacheUpdateGetCapacity_Long() { - let that = new util.LRUCache(); - try { - that.updateCapacity(2147483648); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 2147483648 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - -function testLRUCacheUpdateGetCapacity_Decimal() { - let that = new util.LRUCache(); - try { - that.updateCapacity(2.5); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 2.5 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - function testLRUCacheUpdateGetCapacity_Zero() { let that = new util.LRUCache(); try { @@ -84,26 +56,6 @@ function testLRUCacheUpdateGetCapacity_Negative() { arktest.assertTrue(false, "LRUCache should throw an error when capacity is negative"); } -function testLRUCacheConstructor_Long() { - try { - let that = new util.LRUCache(2147483648); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 2147483648 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - -function testLRUCacheConstructor_Decimal() { - try { - let that = new util.LRUCache(0.1); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 0.1 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - function testLRUCacheConstructor_Zero() { try { let that = new util.LRUCache(0); @@ -123,27 +75,6 @@ function testLRUCacheConstructor_Negative() { arktest.assertTrue(false, "LRUCache should throw an error when capacity is negative"); } -function testLRUCacheConstructor_NumberMin() { - try { - let that = new util.LRUCache(Double.MIN_VALUE); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 5e-324 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - -function testLRUCacheUpdateGetCapacity_NumberMin() { - let that = new util.LRUCache(); - try { - that.updateCapacity(Double.MIN_VALUE); - } catch (err: BusinessError) { - arktest.assertEQ(err.message, "Parameter error. The type of 5e-324 must be small integer") - return - } - arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); -} - function testInvalidInputKeyAtPut() { let that = new util.LRUCache(); arktest.expectThrow(() => { that.put(undefined, 1) }, expectedRangeErrors) diff --git a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheTest.ets b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheTest.ets index 0881bbc8f87cab2647cc6b238669b21b997bead1..8a02a2ea099684f00d9ef3487a3c994f992f27bd 100644 --- a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheTest.ets +++ b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheTest.ets @@ -23,12 +23,10 @@ function main(): int { suite.addTest("LRUCache Constructor No Parameter", testLRUCacheConstructorNoParameter); suite.addTest("LRUCache Constructor Int", testLRUCacheConstructorInt); - suite.addTest("LRUCache Constructor x.0", testLRUCacheConstructorDotZero); suite.addTest("LRUCache update&getCapacity Int", testLRUCacheUpdateGetCapacityInt); - suite.addTest("LRUCache update&getCapacity x.0", testLRUCacheUpdateGetCapacityDotZero); suite.addTest("LRUCache Get Int", testLRUCacheGetInt); suite.addTest("LRUCache Get Str&Undefined", testLRUCacheGetStrAndUndefined); - suite.addTest("LRUCache Get CapacityChenged", testLRUCacheGetCapacityChenged); + suite.addTest("LRUCache Get CapacityChanged", testLRUCacheGetCapacityChanged); suite.addTest("LRUCache Put Once", testLRUCachePutOnce); suite.addTest("LRUCache Put Twice", testLRUCachePutTwice); suite.addTest("LRUCache Put&Get", testLRUCachePutAndGet); @@ -61,13 +59,6 @@ function testLRUCacheUpdateGetCapacityInt() { arktest.assertEQ(res2, 2147483646); } -function testLRUCacheUpdateGetCapacityDotZero() { - let that = new util.LRUCache(20); - that.updateCapacity(10.0); - let res: number = that.getCapacity(); - arktest.assertEQ(res, 10); -} - function testLRUCacheGetInt() { let that = new util.LRUCache(); that.put(1,2); @@ -88,7 +79,7 @@ function testLRUCacheGetStrAndUndefined() { arktest.assertEQ(res3, 10); } -function testLRUCacheGetCapacityChenged() { +function testLRUCacheGetCapacityChanged() { let that = new util.LRUCache(100); that.put('1111','bcjdshc') that.put(20,'cdjcaxb') @@ -299,9 +290,3 @@ function testLRUCacheConstructorInt() { let res: number = that.getCapacity(); arktest.assertEQ(res, 2147483646); } - -function testLRUCacheConstructorDotZero() { - let that = new util.LRUCache(21.0); - let res: number = that.getCapacity(); - arktest.assertEQ(res, 21); -}