aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/memoize/memoize_test.html
blob: bcc814d3ca065ee38ef214ccccd2ab087c54aaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.memoize</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.memoize');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function testNoArgs() {
  var called = 0;
  var f = goog.memoize(function() {
    called++;
    return 10;
  });

  assertEquals('f() 1st call', 10, f());
  assertEquals('f() 2nd call', 10, f());
  assertEquals('f() 3rd call', 10, f.call());
  assertEquals('f() called once', 1, called);
}

function testOneOptionalArgSimple() {
  var called = 0;
  var f = goog.memoize(function(opt_x) {
    called++;
    return arguments.length == 0 ? "no args" : opt_x;
  });

  assertEquals('f() 1st call', "no args", f());
  assertEquals('f() 2nd call', "no args", f());
  assertEquals('f(0) 1st call', 0, f(0));
  assertEquals('f(0) 2nd call', 0, f(0));
  assertEquals('f("") 1st call', '', f(''));
  assertEquals('f("") 2nd call', '', f(''));
  assertEquals('f("0") 1st call', '0', f('0'));
  assertEquals('f("0") 1st call', '0', f('0'));
  assertEquals('f(null) 1st call', null, f(null));
  assertEquals('f(null) 2nd call', null, f(null));
  assertEquals('f(undefined) 1st call', undefined, f(undefined));
  assertEquals('f(undefined) 2nd call', undefined, f(undefined));

  assertEquals('f(opt_x) called 6 times', 6, called);
}

function testProtoFunctions() {
  var fcalled = 0;
  var gcalled = 0;
  var Class = function(x) {
    this.x = x;
    this.f = goog.memoize(function(y) {
      fcalled++;
      return this.x + y;
    });
  };
  Class.prototype.g = goog.memoize(function(z) {
    gcalled++;
    return this.x - z;
  });

  var obj1 = new Class(10);
  var obj2 = new Class(20);

  assertEquals('10+1', 11, obj1.f(1));
  assertEquals('10+2', 12, obj1.f(2));
  assertEquals('10+2 again', 12, obj1.f(2));
  assertEquals('f called twice', 2, fcalled);

  assertEquals('10-1', 9, obj1.g(1));
  assertEquals('10-2', 8, obj1.g(2));
  assertEquals('10-2 again', 8, obj1.g(2));
  assertEquals('g called twice', 2, gcalled);

  assertEquals('20+1', 21, obj2.f(1));
  assertEquals('20+2', 22, obj2.f(2));
  assertEquals('20+2 again', 22, obj2.f(2));
  assertEquals('f called 4 times', 4, fcalled);

  assertEquals('20-1', 19, obj2.g(1));
  assertEquals('20-2', 18, obj2.g(2));
  assertEquals('20-2 again', 18, obj2.g(2));
  assertEquals('g called 4 times', 4, gcalled);
}

function testCustomSerializer() {
  var called = 0;
  var serializer = function(this_context, args) {
    return String(args[0].getTime());
  }
  var getYear = goog.memoize(function(date) {
    called++;
    return date.getFullYear();
  }, serializer);

  assertEquals('getYear(2008, 0, 1), 1st', 2008, getYear(new Date(2008, 0, 1)));
  assertEquals('getYear(2008, 0, 1), 2nd', 2008, getYear(new Date(2008, 0, 1)));
  assertEquals('getYear called once', 1, called);

  assertEquals('getYear(2007, 0, 1)', 2007, getYear(new Date(2007, 0, 1)));
  assertEquals('getYear called twice', 2, called);
}

function testClearCache() {
  var computed = 0;
  var identity = goog.memoize(function(x) {
    computed++;
    return x;
  });
  assertEquals('identity(1)==1', 1, identity(1));
  assertEquals('identity(1)==1', 1, identity(1));
  assertEquals('identity(1)==1', 1, identity(1));
  assertEquals('Expected memozation', 1, computed);

  goog.memoize.clearCache(goog.global);
  assertEquals('identity(1)==1', 1, identity(1));
  assertEquals('identity(1)==1', 1, identity(1));
  assertEquals('Expected cleared memoization cache', 2, computed);
}

function testDisableMemoize() {
  var computed = 0;
  var identity = goog.memoize(function(x) {
    computed++;
    return x;
  });

  assertEquals('return value on first call', 1, identity(1));
  assertEquals('return value on second call (memoized)', 1, identity(1));
  assertEquals('computed once', 1, computed);

  goog.memoize.ENABLE_MEMOIZE = false;

  try {
    assertEquals('return value after disabled memoization', 1, identity(1));
    assertEquals('computed again', 2, computed);
  } finally {
    goog.memoize.ENABLE_MEMOIZE = true;
  }

  assertEquals('return value after reenabled memoization', 1, identity(1));
  assertEquals('not computed again', 2, computed);
}

</script>
</body>
</html>