Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

test: add LRU test (#3441)

Wenzhao Hu0d437d3c11c1
diff --git a/packages/core/src/shared/index.ts b/packages/core/src/shared/index.ts
index 3fd34f5fc7..a08f115f61 100644
--- a/packages/core/src/shared/index.ts
+++ b/packages/core/src/shared/index.ts
@@ -14,32 +14,32 @@
* limitations under the License.
*/
-export { checkIfMove, MOVE_BUFFER_VALUE, ROTATE_BUFFER_VALUE } from './check-if-move';
+export { afterInitApply } from './after-init-apply';
export * from './array-search';
export * from './blob';
+export { checkIfMove, MOVE_BUFFER_VALUE, ROTATE_BUFFER_VALUE } from './check-if-move';
+export * from './clipboard';
export * from './color/color';
-export { ColorKit, COLORS, RGB_PAREN, RGBA_PAREN, type IRgbColor } from './color/color-kit';
+export { ColorKit, COLORS, type IRgbColor, RGB_PAREN, RGBA_PAREN } from './color/color-kit';
export * from './command-enum';
export * from './common';
export * from './compare';
+export * from './debounce';
export * from './doc-tool';
export * from './generate';
export * from './hash-algorithm';
export * from './lifecycle';
export * from './locale';
-export * from './lru/index';
+export { LRUHelper, LRUMap } from './lru/lru-map';
+export { numfmt } from './numfmt';
export * from './object-matrix';
+export { queryObjectMatrix } from './object-matrix-query';
+export { moveRangeByOffset } from './range';
export * from './rectangle';
export { RefAlias } from './ref-alias';
export * from './row-col-iter';
export * from './sequence';
-export * from './sort-rules';
export * from './shape';
+export * from './sort-rules';
export * from './tools';
export * from './types';
-export * from './debounce';
-export * from './clipboard';
-export { numfmt } from './numfmt';
-export { queryObjectMatrix } from './object-matrix-query';
-export { moveRangeByOffset } from './range';
-export { afterInitApply } from './after-init-apply';
diff --git a/packages/core/src/shared/lru/__tests__/lru-map.spec.ts b/packages/core/src/shared/lru/__tests__/lru-map.spec.ts
new file mode 100644
index 0000000000..e5e6ffd517
--- /dev/null
+++ b/packages/core/src/shared/lru/__tests__/lru-map.spec.ts
@@ -0,0 +1,47 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+import { LRUMap } from '../lru-map';
+
+describe('Test LRUMap', () => {
+ it('Should "onShift" callback get called when LRU capacity is exceeded', () => {
+ const shiftedEntries: number[] = [];
+ const l = new LRUMap<number, number>(3);
+ l.onShift((entry) => shiftedEntries.push(entry.key));
+
+ l.set(1, 1);
+ l.set(2, 2);
+ l.set(3, 3);
+ l.set(4, 4);
+
+ expect(shiftedEntries.length).toBe(1);
+ expect(l.has(1)).toBeFalsy();
+
+ l.set(2, 2);
+ l.set(5, 5);
+
+ expect(shiftedEntries.length).toBe(2);
+ expect(l.has(1)).toBeFalsy();
+ expect(l.has(3)).toBeFalsy();
+
+ l.delete(4);
+ expect(shiftedEntries.length).toBe(2); // it won't trigger onShift
+ expect(l.has(1)).toBeFalsy();
+ expect(l.has(3)).toBeFalsy();
+ expect(l.has(4)).toBeFalsy();
+ });
+});
diff --git a/packages/core/src/shared/lru/index.ts b/packages/core/src/shared/lru/index.ts
deleted file mode 100644
index 2a7cbf9fc8..0000000000
--- a/packages/core/src/shared/lru/index.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export * from './lru-helper';
-export * from './lru-map';
diff --git a/packages/core/src/shared/lru/lru-helper.ts b/packages/core/src/shared/lru/lru-helper.ts
deleted file mode 100644
index 92112ce3c4..0000000000
--- a/packages/core/src/shared/lru/lru-helper.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export class LRUHelper {
- static hasLength(array: unknown[], size: number) {
- return array.length === size;
- }
-
- static getValueType(value: any): string {
- return Object.prototype.toString.apply(value);
- }
-
- static isObject<T = object>(value?: any): value is T {
- return this.getValueType(value) === '[object Object]';
- }
-
- static isIterable<T>(value?: any): value is Iterable<T> {
- return value[Symbol.iterator] != null;
- }
-
- static isNumber(value?: any): value is number {
- return this.getValueType(value) === '[object Number]';
- }
-}
diff --git a/packages/core/src/shared/lru/lru-map.ts b/packages/core/src/shared/lru/lru-map.ts
index 89c9e1baed..14a8cac904 100644
--- a/packages/core/src/shared/lru/lru-map.ts
+++ b/packages/core/src/shared/lru/lru-map.ts
@@ -14,11 +14,10 @@
* limitations under the License.
*/
-import type { IDisposable } from '../../common/di';
-
import { remove } from '../../common/array';
+
import { toDisposable } from '../lifecycle';
-import { LRUHelper } from './lru-helper';
+import type { IDisposable } from '../../common/di';
const NEWER = Symbol('newer');
const OLDER = Symbol('older');
@@ -382,3 +381,25 @@ export class LRUMap<K, V> {
return s;
}
}
+
+export class LRUHelper {
+ static hasLength(array: unknown[], size: number) {
+ return array.length === size;
+ }
+
+ static getValueType(value: any): string {
+ return Object.prototype.toString.apply(value);
+ }
+
+ static isObject<T = object>(value?: any): value is T {
+ return this.getValueType(value) === '[object Object]';
+ }
+
+ static isIterable<T>(value?: any): value is Iterable<T> {
+ return value[Symbol.iterator] != null;
+ }
+
+ static isNumber(value?: any): value is number {
+ return this.getValueType(value) === '[object Number]';
+ }
+}