test_lru.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef TEST_LRU_H
  31. #define TEST_LRU_H
  32. #include "core/templates/lru.h"
  33. #include "tests/test_macros.h"
  34. namespace TestLRU {
  35. TEST_CASE("[LRU] Store and read") {
  36. LRUCache<int, int> lru;
  37. lru.set_capacity(3);
  38. lru.insert(1, 1);
  39. lru.insert(50, 2);
  40. lru.insert(100, 5);
  41. CHECK(lru.has(1));
  42. CHECK(lru.has(50));
  43. CHECK(lru.has(100));
  44. CHECK(!lru.has(200));
  45. CHECK(lru.get(1) == 1);
  46. CHECK(lru.get(50) == 2);
  47. CHECK(lru.get(100) == 5);
  48. CHECK(lru.getptr(1) != nullptr);
  49. CHECK(lru.getptr(1000) == nullptr);
  50. lru.insert(600, 600); // Erase <50>
  51. CHECK(lru.has(600));
  52. CHECK(!lru.has(50));
  53. }
  54. TEST_CASE("[LRU] Resize and clear") {
  55. LRUCache<int, int> lru;
  56. lru.set_capacity(3);
  57. lru.insert(1, 1);
  58. lru.insert(2, 2);
  59. lru.insert(3, 3);
  60. CHECK(lru.get_capacity() == 3);
  61. lru.set_capacity(5);
  62. CHECK(lru.get_capacity() == 5);
  63. CHECK(lru.has(1));
  64. CHECK(lru.has(2));
  65. CHECK(lru.has(3));
  66. CHECK(!lru.has(4));
  67. lru.set_capacity(2);
  68. CHECK(lru.get_capacity() == 2);
  69. CHECK(!lru.has(1));
  70. CHECK(lru.has(2));
  71. CHECK(lru.has(3));
  72. CHECK(!lru.has(4));
  73. lru.clear();
  74. CHECK(!lru.has(1));
  75. CHECK(!lru.has(2));
  76. CHECK(!lru.has(3));
  77. CHECK(!lru.has(4));
  78. }
  79. } // namespace TestLRU
  80. #endif // TEST_LRU_H