FBLRUCacheTests.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import "LRUCache.h"
  11. @interface FBLRUCacheTests : XCTestCase
  12. @end
  13. @implementation FBLRUCacheTests
  14. - (void)assertArray:(NSArray *)array1 equalsTo:(NSArray *)array2
  15. {
  16. XCTAssertEqualObjects(array1, array2);
  17. }
  18. - (void)testRecentlyInsertedObjectReplacesTheOldestOne
  19. {
  20. LRUCache *cache = [[LRUCache alloc] initWithCapacity:1];
  21. [cache setObject:@"foo" forKey:@"bar"];
  22. [cache setObject:@"foo2" forKey:@"bar2"];
  23. [cache setObject:@"foo3" forKey:@"bar3"];
  24. XCTAssertEqualObjects(@[@"foo3"], cache.allObjects);
  25. }
  26. - (void)testRecentObjectReplacementAndBump
  27. {
  28. LRUCache *cache = [[LRUCache alloc] initWithCapacity:2];
  29. [cache setObject:@"foo" forKey:@"bar"];
  30. [cache setObject:@"foo2" forKey:@"bar2"];
  31. [self assertArray:@[@"foo2", @"foo"] equalsTo:cache.allObjects];
  32. XCTAssertNotNil([cache objectForKey:@"bar"]);
  33. [self assertArray:@[@"foo", @"foo2"] equalsTo:cache.allObjects];
  34. [cache setObject:@"foo3" forKey:@"bar3"];
  35. [self assertArray:@[@"foo3", @"foo"] equalsTo:cache.allObjects];
  36. [cache setObject:@"foo0" forKey:@"bar"];
  37. [self assertArray:@[@"foo0", @"foo3"] equalsTo:cache.allObjects];
  38. [cache setObject:@"foo4" forKey:@"bar4"];
  39. [self assertArray:@[@"foo4", @"foo0"] equalsTo:cache.allObjects];
  40. }
  41. - (void)testBumpFromHead
  42. {
  43. LRUCache *cache = [[LRUCache alloc] initWithCapacity:3];
  44. [cache setObject:@"foo" forKey:@"bar"];
  45. [cache setObject:@"foo2" forKey:@"bar2"];
  46. [cache setObject:@"foo3" forKey:@"bar3"];
  47. XCTAssertNotNil([cache objectForKey:@"bar3"]);
  48. [self assertArray:@[@"foo3", @"foo2", @"foo"] equalsTo:cache.allObjects];
  49. [cache setObject:@"foo4" forKey:@"bar4"];
  50. [cache setObject:@"foo5" forKey:@"bar5"];
  51. [self assertArray:@[@"foo5", @"foo4", @"foo3"] equalsTo:cache.allObjects];
  52. }
  53. - (void)testBumpFromMiddle
  54. {
  55. LRUCache *cache = [[LRUCache alloc] initWithCapacity:3];
  56. [cache setObject:@"foo" forKey:@"bar"];
  57. [cache setObject:@"foo2" forKey:@"bar2"];
  58. [cache setObject:@"foo3" forKey:@"bar3"];
  59. XCTAssertNotNil([cache objectForKey:@"bar2"]);
  60. [self assertArray:@[@"foo2", @"foo3", @"foo"] equalsTo:cache.allObjects];
  61. [cache setObject:@"foo4" forKey:@"bar4"];
  62. [cache setObject:@"foo5" forKey:@"bar5"];
  63. [self assertArray:@[@"foo5", @"foo4", @"foo2"] equalsTo:cache.allObjects];
  64. }
  65. - (void)testBumpFromTail
  66. {
  67. LRUCache *cache = [[LRUCache alloc] initWithCapacity:3];
  68. [cache setObject:@"foo" forKey:@"bar"];
  69. [cache setObject:@"foo2" forKey:@"bar2"];
  70. [cache setObject:@"foo3" forKey:@"bar3"];
  71. XCTAssertNotNil([cache objectForKey:@"bar3"]);
  72. [self assertArray:@[@"foo3", @"foo2", @"foo"] equalsTo:cache.allObjects];
  73. [cache setObject:@"foo4" forKey:@"bar4"];
  74. [cache setObject:@"foo5" forKey:@"bar5"];
  75. [self assertArray:@[@"foo5", @"foo4", @"foo3"] equalsTo:cache.allObjects];
  76. }
  77. - (void)testInsertionLoop
  78. {
  79. LRUCache *cache = [[LRUCache alloc] initWithCapacity:1];
  80. NSUInteger count = 100;
  81. for (NSUInteger i = 0; i <= count; ++i) {
  82. [cache setObject:@(i) forKey:@(i)];
  83. XCTAssertNotNil([cache objectForKey:@(i)]);
  84. }
  85. XCTAssertEqualObjects(@[@(count)], cache.allObjects);
  86. }
  87. @end