VIMediaCacheDemoTests.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // VIMediaCacheDemoTests.m
  3. // VIMediaCacheDemoTests
  4. //
  5. // Created by Vito on 5/17/16.
  6. // Copyright © 2016 Vito. All rights reserved.
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "VICacheConfiguration.h"
  10. #import "VIMediaCacheWorker.h"
  11. #import "VICacheAction.h"
  12. @interface VIMediaCacheDemoTests : XCTestCase
  13. @end
  14. @implementation VIMediaCacheDemoTests
  15. - (void)setUp {
  16. [super setUp];
  17. // Put setup code here. This method is called before the invocation of each test method in the class.
  18. }
  19. - (void)tearDown {
  20. // Put teardown code here. This method is called after the invocation of each test method in the class.
  21. [super tearDown];
  22. }
  23. - (void)testConfiguration1 {
  24. VICacheConfiguration *configuration = [[VICacheConfiguration alloc] init];
  25. NSRange range1 = NSMakeRange(10, 10);
  26. [configuration addCacheFragment:range1];
  27. NSArray *fragments = [configuration cacheFragments];
  28. XCTAssert(fragments.count == 1 && NSEqualRanges([fragments[0] rangeValue], range1) , @"add (10, 10) to [], should equal [(10, 10)]");
  29. [configuration addCacheFragment:range1];
  30. fragments = [configuration cacheFragments];
  31. XCTAssert(fragments.count == 1 && NSEqualRanges([fragments[0] rangeValue], range1) , @"add (10, 10) to [(10, 10)], should equal [(10, 10)]");
  32. NSRange range0 = NSMakeRange(5, 1);
  33. [configuration addCacheFragment:range0];
  34. fragments = [configuration cacheFragments];
  35. XCTAssert(fragments.count == 2 && NSEqualRanges([fragments[0] rangeValue], range0) && NSEqualRanges([fragments[1] rangeValue], range1), @"add (5, 1) to [(10, 10)], should equal [(5, 1), (10, 10)]");
  36. NSRange range3 = NSMakeRange(1, 1);
  37. [configuration addCacheFragment:range3];
  38. fragments = [configuration cacheFragments];
  39. XCTAssert(fragments.count == 3 &&
  40. NSEqualRanges([fragments[0] rangeValue], range3) &&
  41. NSEqualRanges([fragments[1] rangeValue], range0) &&
  42. NSEqualRanges([fragments[2] rangeValue], range1),
  43. @"add (1, 1) to [(5, 1), (10, 10)], should equal [(1, 1), (5, 1), (10, 10)]");
  44. NSRange range4 = NSMakeRange(0, 9);
  45. [configuration addCacheFragment:range4];
  46. fragments = [configuration cacheFragments];
  47. XCTAssert(fragments.count == 2 &&
  48. NSEqualRanges([fragments[0] rangeValue], NSMakeRange(0, 9)) &&
  49. NSEqualRanges([fragments[1] rangeValue], range1),
  50. @"add (0, 9) to [(1, 1), (5, 1), (10, 10)], should equal [(0, 9), (10, 10)]");
  51. }
  52. - (void)testConfiguration2 {
  53. VICacheConfiguration *configuration = [[VICacheConfiguration alloc] init];
  54. NSRange range1 = NSMakeRange(10, 10);
  55. [configuration addCacheFragment:range1];
  56. NSArray *fragments = [configuration cacheFragments];
  57. XCTAssert(fragments.count == 1 && NSEqualRanges([fragments[0] rangeValue], range1) , @"add (10, 10) to [], should equal [(10, 10)]");
  58. NSRange range2 = NSMakeRange(30, 10);
  59. [configuration addCacheFragment:range2];
  60. fragments = [configuration cacheFragments];
  61. XCTAssert(fragments.count == 2 && NSEqualRanges([fragments[0] rangeValue], range1) && NSEqualRanges([fragments[1] rangeValue], range2), @"add (30, 10) to [(10, 10)] should equal [(10, 10), (30, 10)]");
  62. NSRange range3 = NSMakeRange(50, 10);
  63. [configuration addCacheFragment:range3];
  64. fragments = [configuration cacheFragments];
  65. XCTAssert(fragments.count == 3 &&
  66. NSEqualRanges([fragments[0] rangeValue], range1) &&
  67. NSEqualRanges([fragments[1] rangeValue], range2) &&
  68. NSEqualRanges([fragments[2] rangeValue], range3),
  69. @"add (50, 10) to [(10, 10), (30, 10)] should equal [(10, 10), (30, 10), (50, 10)]");
  70. NSRange range4 = NSMakeRange(25, 26);
  71. [configuration addCacheFragment:range4];
  72. fragments = [configuration cacheFragments];
  73. XCTAssert(fragments.count == 2 &&
  74. NSEqualRanges([fragments[0] rangeValue], range1) &&
  75. NSEqualRanges([fragments[1] rangeValue], NSMakeRange(25, 35)),
  76. @"add (25, 26) to [(10, 10), (30, 10), (50, 10)] should equal [(10, 10), (25, 35)]");
  77. }
  78. - (void)testCacheWorker {
  79. VIMediaCacheWorker *cacheWorker = [[VIMediaCacheWorker alloc] initWithCacheName:@"test.mp4"];
  80. NSArray *startOffsets = @[@(50), @(80), @(200), @(708), @(1024), @(1500)];
  81. [cacheWorker setCacheResponse:nil];
  82. if (!cacheWorker.cachedResponse) {
  83. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"testUrl"]
  84. MIMEType:@"mime"
  85. expectedContentLength:2048
  86. textEncodingName:nil];
  87. [cacheWorker setCacheResponse:response];
  88. for (NSNumber *offset in startOffsets) {
  89. NSString *str = @"ddddddddddddddddddddddddddddddddddddddddd"; // 42
  90. const char *utfStr = [str UTF8String];
  91. NSData *data = [NSData dataWithBytes:utfStr length:strlen(utfStr) + 1];
  92. [cacheWorker cacheData:data forRange:NSMakeRange(offset.integerValue, data.length)];
  93. [cacheWorker save];
  94. }
  95. }
  96. NSRange range = NSMakeRange(0, 50);
  97. NSArray *cacheDataActions1 = [cacheWorker cachedDataActionsForRange:range];
  98. NSArray *expectActions1 = @[
  99. [[VICacheAction alloc] initWithActionType:VICacheAtionTypeRemote range:range]
  100. ];
  101. XCTAssert([cacheDataActions1 isEqualToArray:expectActions1], @"cacheDataActions1 count should equal to %@", expectActions1);
  102. NSRange range2 = NSMakeRange(51, 204);
  103. NSArray *cacheDataActions2 = [cacheWorker cachedDataActionsForRange:range2];
  104. XCTAssert(cacheDataActions2.count == 4, @"actions count should equal startoffsets's count");
  105. NSRange range3 = NSMakeRange(1300, 300);
  106. NSArray *cacheDataActions3 = [cacheWorker cachedDataActionsForRange:range3];
  107. NSArray *expectActions3 = @[
  108. [[VICacheAction alloc] initWithActionType:VICacheAtionTypeRemote range:NSMakeRange(1300, 200)],
  109. [[VICacheAction alloc] initWithActionType:VICacheAtionTypeLocal range:NSMakeRange(1500, 42)],
  110. [[VICacheAction alloc] initWithActionType:VICacheAtionTypeRemote range:NSMakeRange(1542, 58)]
  111. ];
  112. XCTAssert([cacheDataActions3 isEqualToArray:expectActions3], @"actions count should equal");
  113. }
  114. @end