FPRCounterListTest.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/FIRPerformance+Internal.h"
  16. #import "FirebasePerformance/Sources/FPRClient.h"
  17. #import "FirebasePerformance/Sources/Public/FIRPerformance.h"
  18. #import "FirebasePerformance/Sources/Timer/FPRCounterList.h"
  19. @interface FPRCounterListTest : XCTestCase
  20. @end
  21. @implementation FPRCounterListTest
  22. + (void)setUp {
  23. [super setUp];
  24. FIRPerformance *performance = [FIRPerformance sharedInstance];
  25. [performance setDataCollectionEnabled:YES];
  26. [[FPRClient sharedInstance] disableInstrumentation];
  27. }
  28. + (void)tearDown {
  29. [super tearDown];
  30. FIRPerformance *performance = [FIRPerformance sharedInstance];
  31. [performance setDataCollectionEnabled:NO];
  32. [[FPRClient sharedInstance] disableInstrumentation];
  33. }
  34. /** Validates counterlist object creation. */
  35. - (void)testInit {
  36. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  37. XCTAssertNotNil(counterList);
  38. }
  39. /** Validates the initial state of counter list. */
  40. - (void)testInitialState {
  41. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  42. XCTAssertTrue(counterList.counters.count == 0);
  43. }
  44. /** Validates that the counter values are incremented correctly. */
  45. - (void)testCounters {
  46. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  47. [counterList incrementCounterNamed:@"testing" by:1];
  48. [counterList incrementCounterNamed:@"testing" by:5];
  49. [counterList incrementCounterNamed:@"testing 2" by:1];
  50. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  51. XCTAssertEqual(counterValue1, 6);
  52. NSUInteger counterValue2 = [[counterList.counters objectForKey:@"testing 2"] integerValue];
  53. XCTAssertEqual(counterValue2, 1);
  54. NSUInteger counterValue3 = [[counterList.counters objectForKey:@"Random"] integerValue];
  55. XCTAssertEqual(counterValue3, 0);
  56. }
  57. /** Tests that metrics are set correctly. */
  58. - (void)testSetMetric {
  59. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  60. [counterList setIntValue:10 forMetric:@"testing"];
  61. [counterList setIntValue:12 forMetric:@"testing 2"];
  62. int64_t metricValue1 = [[counterList.counters objectForKey:@"testing"] longLongValue];
  63. XCTAssertEqual(metricValue1, 10);
  64. int64_t metricValue2 = [[counterList.counters objectForKey:@"testing 2"] longLongValue];
  65. XCTAssertEqual(metricValue2, 12);
  66. }
  67. /** Tests that metrics are incremented correctly. */
  68. - (void)testIncrementMetric {
  69. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  70. [counterList setIntValue:10 forMetric:@"testing"];
  71. [counterList incrementMetric:@"testing" byInt:10];
  72. int64_t metricValue1 = [[counterList.counters objectForKey:@"testing"] longLongValue];
  73. XCTAssertEqual(metricValue1, 20);
  74. }
  75. /** Tests getting metric after having set it. */
  76. - (void)testGetMetric {
  77. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  78. [counterList setIntValue:10 forMetric:@"testing"];
  79. [counterList incrementMetric:@"testing" byInt:10];
  80. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 20);
  81. }
  82. /** Validates deleting a non existent metric doesnt affect other metrics. */
  83. - (void)testDeleteNonExistentMetricDoesntAffectOtherMetrics {
  84. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  85. [counterList setIntValue:10 forMetric:@"testing"];
  86. [counterList deleteMetric:@"testing2"];
  87. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 10);
  88. }
  89. /** Validates deleteMetric deletes a metric. */
  90. - (void)testDeleteExistingMetric {
  91. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  92. [counterList setIntValue:10 forMetric:@"testing"];
  93. [counterList deleteMetric:@"testing"];
  94. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 0);
  95. XCTAssertNil(counterList.counters[@"testing"]);
  96. }
  97. /** Validates deleting existing metric only deletes that metric. */
  98. - (void)testDeleteExistingMetricDoesntDeleteOtherMetric {
  99. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  100. [counterList setIntValue:10 forMetric:@"testing"];
  101. [counterList setIntValue:10 forMetric:@"testing2"];
  102. [counterList deleteMetric:@"testing"];
  103. XCTAssertNil(counterList.counters[@"testing"]);
  104. XCTAssertEqual([counterList valueForIntMetric:@"testing2"], 10);
  105. }
  106. /** Validates passing nil to metricName doesn't do anything. */
  107. - (void)testDeleteMetricWithNilNameDoesntDoAnything {
  108. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  109. [counterList setIntValue:10 forMetric:@"testing"];
  110. #pragma clang diagnostic push
  111. #pragma clang diagnostic ignored "-Wnonnull"
  112. [counterList deleteMetric:nil];
  113. #pragma clang diagnostic pop
  114. XCTAssertEqual([counterList valueForIntMetric:@"testing"], 10);
  115. }
  116. /** Validates that the counters are valid when then have valid data. */
  117. - (void)testCounterValidity {
  118. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  119. [counterList incrementCounterNamed:@"testing" by:1];
  120. [counterList incrementCounterNamed:@"testing" by:1];
  121. [counterList incrementCounterNamed:@"testing 2" by:1];
  122. [counterList setIntValue:-44 forMetric:@"testing"];
  123. XCTAssertTrue([counterList isValid]);
  124. }
  125. /** Validates if the counter increment with negative value reduces the value. */
  126. - (void)testCounterIncrementWithNegativeValue {
  127. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  128. [counterList incrementCounterNamed:@"testing" by:5];
  129. [counterList incrementCounterNamed:@"testing" by:-1];
  130. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  131. XCTAssertEqual(counterValue1, 4);
  132. }
  133. /** Validates if the counter initialize with negative value works. */
  134. - (void)testCounterInitializeWithNegativeValue {
  135. FPRCounterList *counterList = [[FPRCounterList alloc] init];
  136. [counterList incrementCounterNamed:@"testing" by:-1];
  137. NSUInteger counterValue1 = [[counterList.counters objectForKey:@"testing"] integerValue];
  138. XCTAssertEqual(counterValue1, -1);
  139. }
  140. @end