/indra/test/lltimestampcache_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 124 lines · 78 code · 11 blank · 35 comment · 0 complexity · 7ee6a9b5f7f6cfb672972363f6da61ea MD5 · raw file

  1. /**
  2. * @file lltimestampcache_tut.cpp
  3. * @author James Tess
  4. * @date 2008-12-03
  5. *
  6. * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include <tut/tut.hpp>
  28. #include "linden_common.h"
  29. #include "../mapserver/lltimestampcache.h"
  30. #include "lltut.h"
  31. namespace tut
  32. {
  33. struct LLTimestampCacheTestData
  34. {
  35. };
  36. typedef test_group<LLTimestampCacheTestData> LLTimestampCacheTestGroup;
  37. typedef LLTimestampCacheTestGroup::object LLTimestampCacheTestObject;
  38. LLTimestampCacheTestGroup timestampCacheTestGroup("LLTimestampCache");
  39. // Most common usage
  40. template<> template<>
  41. void LLTimestampCacheTestObject::test<1>()
  42. {
  43. LLTimestampCache<std::string, std::string> cache;
  44. // put in some data
  45. cache.insert("key1", "val1", 1);
  46. cache.insert("key2", "val2", 2);
  47. cache.insert("key3", "val3", 3);
  48. ensure_equals("size is 3", cache.size(), 3);
  49. // check some items
  50. ensure("has key1", cache.has("key1"));
  51. ensure("no invalid key", !cache.has("invalid key"));
  52. // get some items
  53. ensure_equals("get key1", cache.get("key1", 4), "val1");
  54. ensure_equals("get invalid key",
  55. cache.get("invalid key", 4), std::string() );
  56. // timestamps
  57. ensure_equals("key1 timestamp updated", cache.getTimestamp("key1"), 4);
  58. ensure_equals("invalid key timestamp",
  59. cache.getTimestamp("invalid key"), 0);
  60. }
  61. // New empty cache shouldn't have any entries
  62. template<> template<>
  63. void LLTimestampCacheTestObject::test<2>()
  64. {
  65. LLTimestampCache<std::string, std::string> cache;
  66. ensure_equals("starts empty", cache.size(), 0);
  67. ensure_equals("has nothing", cache.has("foo"), false);
  68. ensure_equals("gets nothing", cache.get("foo", 0), std::string() );
  69. U32 max_time = 0xFFFFFFFF;
  70. ensure_equals("erases nothing", cache.eraseBefore(max_time), 0);
  71. }
  72. // Non empty cache
  73. template<> template<>
  74. void LLTimestampCacheTestObject::test<3>()
  75. {
  76. LLTimestampCache<std::string, std::string> cache;
  77. cache.insert("foo", "bar", 123);
  78. ensure_equals("size one", cache.size(), 1);
  79. ensure_equals("has it", cache.has("foo"), true);
  80. ensure_equals("timestamp correct", cache.getTimestamp("foo"), 123);
  81. std::string value = cache.get("foo", 456);
  82. ensure_equals("get value", value, "bar");
  83. ensure_equals("timestamp updated", cache.getTimestamp("foo"), 456);
  84. ensure_equals("erase nothing", cache.eraseBefore(0), 0);
  85. ensure_equals("erase one", cache.eraseBefore(789), 1);
  86. ensure_equals("empty after erase", cache.size(), 0);
  87. }
  88. // Recache of item should update timestamp
  89. template<> template<>
  90. void LLTimestampCacheTestObject::test<4>()
  91. {
  92. LLTimestampCache<std::string, std::string> cache;
  93. cache.insert("foo", "bar", 123);
  94. cache.insert("foo", "bar", 456);
  95. ensure_equals("duplicate suppressed", cache.size(), 1);
  96. ensure_equals("timestamp replaced", cache.getTimestamp("foo"), 456);
  97. }
  98. // Erasing some items
  99. template<> template<>
  100. void LLTimestampCacheTestObject::test<5>()
  101. {
  102. LLTimestampCache<std::string, std::string> cache;
  103. cache.insert("key1", "val1", 1);
  104. cache.insert("key2", "val2", 2);
  105. cache.insert("key3", "val3", 3);
  106. cache.insert("key4", "val4", 4);
  107. size_t erased = cache.eraseBefore(3);
  108. ensure_equals("erase range", erased, 2);
  109. ensure_equals("cache post erase", cache.size(), 2);
  110. ensure_equals("has key3", cache.has("key3"), true);
  111. ensure_equals("not has key2", cache.has("key2"), false);
  112. }
  113. }