test_lru.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**************************************************************************/
  2. /* test_lru.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/templates/lru.h"
  32. #include "tests/test_macros.h"
  33. namespace TestLRU {
  34. TEST_CASE("[LRU] Store and read") {
  35. LRUCache<int, int> lru;
  36. lru.set_capacity(3);
  37. lru.insert(1, 1);
  38. lru.insert(50, 2);
  39. lru.insert(100, 5);
  40. CHECK(lru.has(1));
  41. CHECK(lru.has(50));
  42. CHECK(lru.has(100));
  43. CHECK(!lru.has(200));
  44. CHECK(lru.get(1) == 1);
  45. CHECK(lru.get(50) == 2);
  46. CHECK(lru.get(100) == 5);
  47. CHECK(lru.getptr(1) != nullptr);
  48. CHECK(lru.getptr(1000) == nullptr);
  49. lru.insert(600, 600); // Erase <50>
  50. CHECK(lru.has(600));
  51. CHECK(!lru.has(50));
  52. }
  53. TEST_CASE("[LRU] Resize and clear") {
  54. LRUCache<int, int> lru;
  55. lru.set_capacity(3);
  56. lru.insert(1, 1);
  57. lru.insert(2, 2);
  58. lru.insert(3, 3);
  59. CHECK(lru.get_capacity() == 3);
  60. lru.set_capacity(5);
  61. CHECK(lru.get_capacity() == 5);
  62. CHECK(lru.has(1));
  63. CHECK(lru.has(2));
  64. CHECK(lru.has(3));
  65. CHECK(!lru.has(4));
  66. lru.set_capacity(2);
  67. CHECK(lru.get_capacity() == 2);
  68. CHECK(!lru.has(1));
  69. CHECK(lru.has(2));
  70. CHECK(lru.has(3));
  71. CHECK(!lru.has(4));
  72. lru.clear();
  73. CHECK(!lru.has(1));
  74. CHECK(!lru.has(2));
  75. CHECK(!lru.has(3));
  76. CHECK(!lru.has(4));
  77. }
  78. } // namespace TestLRU