Debian Codegraph @

closure-library/closure/goog/memoize/memoize_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.memoizeTest');
16 goog.setTestOnly('goog.memoizeTest');
17
18 goog.require('goog.memoize');
19 goog.require('goog.testing.jsunit');
20
21 function testNoArgs() {
22 var called = 0;
23 var f = goog.memoize(function() {
24 called++;
25 return 10;
26 });
27
28 assertEquals('f() 1st call', 10, f());
29 assertEquals('f() 2nd call', 10, f());
30 assertEquals('f() 3rd call', 10, f.call());
31 assertEquals('f() called once', 1, called);
32 }
33
34 function testOneOptionalArgSimple() {
35 var called = 0;
36 var f = goog.memoize(function(opt_x) {
37 called++;
38 return arguments.length == 0 ? 'no args' : opt_x;
39 });
40
41 assertEquals('f() 1st call', 'no args', f());
42 assertEquals('f() 2nd call', 'no args', f());
43 assertEquals('f(0) 1st call', 0, f(0));
44 assertEquals('f(0) 2nd call', 0, f(0));
45 assertEquals('f("") 1st call', '', f(''));
46 assertEquals('f("") 2nd call', '', f(''));
47 assertEquals('f("0") 1st call', '0', f('0'));
48 assertEquals('f("0") 1st call', '0', f('0'));
49 assertEquals('f(null) 1st call', null, f(null));
50 assertEquals('f(null) 2nd call', null, f(null));
51 assertEquals('f(undefined) 1st call', undefined, f(undefined));
52 assertEquals('f(undefined) 2nd call', undefined, f(undefined));
53
54 assertEquals('f(opt_x) called 6 times', 6, called);
55 }
56
57 function testProtoFunctions() {
58 var fcalled = 0;
59 var gcalled = 0;
60 var Class = function(x) {
61 this.x = x;
62 this.f = goog.memoize(function(y) {
63 fcalled++;
64 return this.x + y;
65 });
66 };
67 Class.prototype.g = goog.memoize(function(z) {
68 gcalled++;
69 return this.x - z;
70 });
71
72 var obj1 = new Class(10);
73 var obj2 = new Class(20);
74
75 assertEquals('10+1', 11, obj1.f(1));
76 assertEquals('10+2', 12, obj1.f(2));
77 assertEquals('10+2 again', 12, obj1.f(2));
78 assertEquals('f called twice', 2, fcalled);
79
80 assertEquals('10-1', 9, obj1.g(1));
81 assertEquals('10-2', 8, obj1.g(2));
82 assertEquals('10-2 again', 8, obj1.g(2));
83 assertEquals('g called twice', 2, gcalled);
84
85 assertEquals('20+1', 21, obj2.f(1));
86 assertEquals('20+2', 22, obj2.f(2));
87 assertEquals('20+2 again', 22, obj2.f(2));
88 assertEquals('f called 4 times', 4, fcalled);
89
90 assertEquals('20-1', 19, obj2.g(1));
91 assertEquals('20-2', 18, obj2.g(2));
92 assertEquals('20-2 again', 18, obj2.g(2));
93 assertEquals('g called 4 times', 4, gcalled);
94 }
95
96 function testCustomSerializer() {
97 var called = 0;
98 var serializer = function(this_context, args) {
99 return String(args[0].getTime());
100 };
101 var getYear = goog.memoize(function(date) {
102 called++;
103 return date.getFullYear();
104 }, serializer);
105
106 assertEquals('getYear(2008, 0, 1), 1st', 2008, getYear(new Date(2008, 0, 1)));
107 assertEquals('getYear(2008, 0, 1), 2nd', 2008, getYear(new Date(2008, 0, 1)));
108 assertEquals('getYear called once', 1, called);
109
110 assertEquals('getYear(2007, 0, 1)', 2007, getYear(new Date(2007, 0, 1)));
111 assertEquals('getYear called twice', 2, called);
112 }
113
114 function testClearCache() {
115 var computed = 0;
116 var identity = goog.memoize(function(x) {
117 computed++;
118 return x;
119 });
120 assertEquals('identity(1)==1', 1, identity(1));
121 assertEquals('identity(1)==1', 1, identity(1));
122 assertEquals('identity(1)==1', 1, identity(1));
123 assertEquals('Expected memozation', 1, computed);
124
125 goog.memoize.clearCache(goog.global);
126 assertEquals('identity(1)==1', 1, identity(1));
127 assertEquals('identity(1)==1', 1, identity(1));
128 assertEquals('Expected cleared memoization cache', 2, computed);
129 }
130
131 function testDisableMemoize() {
132 var computed = 0;
133 var identity = goog.memoize(function(x) {
134 computed++;
135 return x;
136 });
137
138 assertEquals('return value on first call', 1, identity(1));
139 assertEquals('return value on second call (memoized)', 1, identity(1));
140 assertEquals('computed once', 1, computed);
141
142 goog.memoize.ENABLE_MEMOIZE = false;
143
144 try {
145 assertEquals('return value after disabled memoization', 1, identity(1));
146 assertEquals('computed again', 2, computed);
147 } finally {
148 goog.memoize.ENABLE_MEMOIZE = true;
149 }
150
151 assertEquals('return value after reenabled memoization', 1, identity(1));
152 assertEquals('not computed again', 2, computed);
153 }