Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/caching.js

Issue 30007559: Issue 7285 - Abstract caching logic (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Feb. 14, 2019, 5:36 a.m.
Right Patch Set: Add tests and change API and behavior slightly Created Feb. 16, 2019, 12:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« lib/caching.js ('K') | « lib/matcher.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 const {createSandbox} = require("./_common");
21
22 let Cache = null;
23
24 exports.setUp = function(callback)
25 {
26 let sandboxedRequire = createSandbox();
27 (
28 {Cache} = sandboxedRequire("../lib/caching")
29 );
30
31 callback();
32 };
33
34 exports.testCache = function(test)
35 {
36 // A capacity must be specificed and it must be coercable to a positive
37 // number greater than or equal to one.
38 test.throws(() => { new Cache(); }, "capacity must be a positive number.");
39 test.throws(() => { new Cache(0); }, "capacity must be a positive number.");
40 test.throws(() => { new Cache(-1); }, "capacity must be a positive number.");
41 test.throws(() => { new Cache(0.1); }, "capacity must be a positive number.");
42 test.throws(() => { new Cache(Number.MIN_VALUE); },
43 "capacity must be a positive number.");
44 test.throws(() => { new Cache(-Infinity); },
45 "capacity must be a positive number.");
46 test.throws(() => { new Cache("ten"); },
47 "capacity must be a positive number.");
48 test.doesNotThrow(() => { new Cache(1); },
49 "capacity must be a positive number.");
50 test.doesNotThrow(() => { new Cache(1.1); },
51 "capacity must be a positive number.");
52 test.doesNotThrow(() => { new Cache(10); },
53 "capacity must be a positive number.");
54 test.doesNotThrow(() => { new Cache(Number.MAX_VALUE); },
55 "capacity must be a positive number.");
56 test.doesNotThrow(() => { new Cache(Infinity); },
57 "capacity must be a positive number.");
58 test.doesNotThrow(() => { new Cache("10"); },
59 "capacity must be a positive number.");
60
61 let cache = new Cache(100);
62
63 cache.set("1", "one");
64 test.equal(cache.get("1"), "one");
65
66 cache.set(2, "two");
67 test.equal(cache.get(2), "two");
68
69 // No type coercion.
70 test.equal(cache.get("2"), undefined);
71
72 // Neither key nor value can be undefined.
73 test.throws(() => { cache.set(undefined, "three"); },
74 "key must not be undefined.");
75 test.throws(() => { cache.set(4, undefined); },
76 "value must not be undefined.");
77
78 // Keys and values can be null.
79 cache.set(null, "five");
80 cache.set(5, null);
81
82 cache.clear();
83
84 test.equal(cache.get("1"), undefined);
85 test.equal(cache.get(2), undefined);
86 test.equal(cache.get(null), undefined);
87 test.equal(cache.get(5), undefined);
88
89 // Fill to capacity.
90 for (let i = 0; i < 100; i++)
91 cache.set(i, i);
92
93 // All entries exist.
94 for (let i = 0; i < 100; i++)
95 test.equal(cache.get(i), i);
96
97 // Add an existing entry again.
98 cache.set(0, 0);
99
100 // All entries still exist.
101 for (let i = 0; i < 100; i++)
102 test.equal(cache.get(i), i);
103
104 // Exceed capacity.
105 cache.set(100, 100);
106
107 // Only the last entry exists.
108 for (let i = 0; i <= 100; i++)
109 test.equal(cache.get(i), i == 100 ? 100 : undefined);
110
111 test.done();
112 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld