|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Rémi Van Keisbelck |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | + |
| 27 | +import {memoize} from "./Memoize"; |
| 28 | + |
| 29 | +interface User { |
| 30 | + readonly name: string; |
| 31 | + readonly size: number; |
| 32 | +} |
| 33 | + |
| 34 | +describe('memoize function', () => { |
| 35 | + |
| 36 | + let count = 0; |
| 37 | + |
| 38 | + function invoke<T,R>(f: (t: T) => R, arg:T, expectedResult: R, expectedCount: number) { |
| 39 | + const r = f(arg); |
| 40 | + expect(r).toEqual(expectedResult); |
| 41 | + expect(count).toEqual(expectedCount); |
| 42 | + } |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + count = 0; |
| 46 | + }) |
| 47 | + |
| 48 | + test("primitive number", () => { |
| 49 | + const f = memoize((x: number) => { |
| 50 | + count++; |
| 51 | + return x + 1; |
| 52 | + }); |
| 53 | + [ |
| 54 | + [0, 1, 1], |
| 55 | + [1, 2, 2], |
| 56 | + [1, 2, 2], |
| 57 | + [0, 1, 3], |
| 58 | + [0, 1, 3] |
| 59 | + ].forEach(([v, er, ec]) => { |
| 60 | + invoke(f, v, er, ec); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + test("object ref equality", () => { |
| 65 | + const f_: (user: User) => string = u => { |
| 66 | + count++; |
| 67 | + return u.name + " " + u.size; |
| 68 | + } |
| 69 | + const f = memoize(f_); |
| 70 | + const user: User = { |
| 71 | + name: "John", |
| 72 | + size: 48, |
| 73 | + }; |
| 74 | + invoke(f, user, "John 48", 1); |
| 75 | + invoke(f, user, "John 48", 1); |
| 76 | + const user2: User = { |
| 77 | + ...user, |
| 78 | + size: 12, |
| 79 | + }; |
| 80 | + invoke(f, user2, "John 12", 2); |
| 81 | + invoke(f, user2, "John 12", 2); |
| 82 | + }); |
| 83 | + |
| 84 | + test("object with compare fn", () => { |
| 85 | + const f_: (user: User) => string = u => { |
| 86 | + count++; |
| 87 | + return u.name + " " + u.size; |
| 88 | + } |
| 89 | + const f = memoize(f_, (o1, o2) => o1.name === o2.name && o1.size === o2.size); |
| 90 | + const user: User = { |
| 91 | + name: "John", |
| 92 | + size: 48, |
| 93 | + }; |
| 94 | + invoke(f, user, "John 48", 1); |
| 95 | + invoke(f, { ...user }, "John 48", 1); |
| 96 | + invoke(f, { name: "John", size: 48 }, "John 48", 1); |
| 97 | + }); |
| 98 | + |
| 99 | + test("array ref equals", () => { |
| 100 | + const f_: (a: number[]) => string = a => { |
| 101 | + count++; |
| 102 | + return a.join("_"); |
| 103 | + } |
| 104 | + const f = memoize(f_); |
| 105 | + const a = [1, 2, 3]; |
| 106 | + invoke(f, a, "1_2_3", 1); |
| 107 | + invoke(f, a, "1_2_3", 1); |
| 108 | + const a2 = [...a, 4]; |
| 109 | + invoke(f, a2, "1_2_3_4", 2); |
| 110 | + invoke(f, a2, "1_2_3_4", 2); |
| 111 | + }); |
| 112 | + |
| 113 | +}); |
0 commit comments