memoize_test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. goog.provide('goog.memoizeTest');
  15. goog.setTestOnly('goog.memoizeTest');
  16. goog.require('goog.memoize');
  17. goog.require('goog.testing.jsunit');
  18. function testNoArgs() {
  19. var called = 0;
  20. var f = goog.memoize(function() {
  21. called++;
  22. return 10;
  23. });
  24. assertEquals('f() 1st call', 10, f());
  25. assertEquals('f() 2nd call', 10, f());
  26. assertEquals('f() 3rd call', 10, f.call());
  27. assertEquals('f() called once', 1, called);
  28. }
  29. function testOneOptionalArgSimple() {
  30. var called = 0;
  31. var f = goog.memoize(function(opt_x) {
  32. called++;
  33. return arguments.length == 0 ? 'no args' : opt_x;
  34. });
  35. assertEquals('f() 1st call', 'no args', f());
  36. assertEquals('f() 2nd call', 'no args', f());
  37. assertEquals('f(0) 1st call', 0, f(0));
  38. assertEquals('f(0) 2nd call', 0, f(0));
  39. assertEquals('f("") 1st call', '', f(''));
  40. assertEquals('f("") 2nd call', '', f(''));
  41. assertEquals('f("0") 1st call', '0', f('0'));
  42. assertEquals('f("0") 1st call', '0', f('0'));
  43. assertEquals('f(null) 1st call', null, f(null));
  44. assertEquals('f(null) 2nd call', null, f(null));
  45. assertEquals('f(undefined) 1st call', undefined, f(undefined));
  46. assertEquals('f(undefined) 2nd call', undefined, f(undefined));
  47. assertEquals('f(opt_x) called 6 times', 6, called);
  48. }
  49. function testProtoFunctions() {
  50. var fcalled = 0;
  51. var gcalled = 0;
  52. var Class = function(x) {
  53. this.x = x;
  54. this.f = goog.memoize(function(y) {
  55. fcalled++;
  56. return this.x + y;
  57. });
  58. };
  59. Class.prototype.g = goog.memoize(function(z) {
  60. gcalled++;
  61. return this.x - z;
  62. });
  63. var obj1 = new Class(10);
  64. var obj2 = new Class(20);
  65. assertEquals('10+1', 11, obj1.f(1));
  66. assertEquals('10+2', 12, obj1.f(2));
  67. assertEquals('10+2 again', 12, obj1.f(2));
  68. assertEquals('f called twice', 2, fcalled);
  69. assertEquals('10-1', 9, obj1.g(1));
  70. assertEquals('10-2', 8, obj1.g(2));
  71. assertEquals('10-2 again', 8, obj1.g(2));
  72. assertEquals('g called twice', 2, gcalled);
  73. assertEquals('20+1', 21, obj2.f(1));
  74. assertEquals('20+2', 22, obj2.f(2));
  75. assertEquals('20+2 again', 22, obj2.f(2));
  76. assertEquals('f called 4 times', 4, fcalled);
  77. assertEquals('20-1', 19, obj2.g(1));
  78. assertEquals('20-2', 18, obj2.g(2));
  79. assertEquals('20-2 again', 18, obj2.g(2));
  80. assertEquals('g called 4 times', 4, gcalled);
  81. }
  82. function testCustomSerializer() {
  83. var called = 0;
  84. var serializer = function(this_context, args) {
  85. return String(args[0].getTime());
  86. };
  87. var getYear = goog.memoize(function(date) {
  88. called++;
  89. return date.getFullYear();
  90. }, serializer);
  91. assertEquals('getYear(2008, 0, 1), 1st', 2008, getYear(new Date(2008, 0, 1)));
  92. assertEquals('getYear(2008, 0, 1), 2nd', 2008, getYear(new Date(2008, 0, 1)));
  93. assertEquals('getYear called once', 1, called);
  94. assertEquals('getYear(2007, 0, 1)', 2007, getYear(new Date(2007, 0, 1)));
  95. assertEquals('getYear called twice', 2, called);
  96. }
  97. function testClearCache() {
  98. var computed = 0;
  99. var identity = goog.memoize(function(x) {
  100. computed++;
  101. return x;
  102. });
  103. assertEquals('identity(1)==1', 1, identity(1));
  104. assertEquals('identity(1)==1', 1, identity(1));
  105. assertEquals('identity(1)==1', 1, identity(1));
  106. assertEquals('Expected memozation', 1, computed);
  107. goog.memoize.clearCache(goog.global);
  108. assertEquals('identity(1)==1', 1, identity(1));
  109. assertEquals('identity(1)==1', 1, identity(1));
  110. assertEquals('Expected cleared memoization cache', 2, computed);
  111. }
  112. function testDisableMemoize() {
  113. var computed = 0;
  114. var identity = goog.memoize(function(x) {
  115. computed++;
  116. return x;
  117. });
  118. assertEquals('return value on first call', 1, identity(1));
  119. assertEquals('return value on second call (memoized)', 1, identity(1));
  120. assertEquals('computed once', 1, computed);
  121. goog.memoize.ENABLE_MEMOIZE = false;
  122. try {
  123. assertEquals('return value after disabled memoization', 1, identity(1));
  124. assertEquals('computed again', 2, computed);
  125. } finally {
  126. goog.memoize.ENABLE_MEMOIZE = true;
  127. }
  128. assertEquals('return value after reenabled memoization', 1, identity(1));
  129. assertEquals('not computed again', 2, computed);
  130. }