2 // NSDate+SFAnalyticsTests.m
3 // KeychainAnalyticsTests
6 #import <XCTest/XCTest.h>
8 #import "../Analytics/NSDate+SFAnalytics.h"
10 @interface NSDate_SFAnalyticsTests : XCTestCase
14 @implementation NSDate_SFAnalyticsTests
16 - (void)testCurrentTimeSeconds
18 NSTimeInterval expectedTime = [[NSDate date] timeIntervalSince1970];
19 NSTimeInterval actualTimeWithWiggle = [[NSDate date] timeIntervalSince1970];
20 XCTAssertEqualWithAccuracy(actualTimeWithWiggle, expectedTime, 1, @"Expected to get roughly the same amount of seconds");
23 - (void)testCurrentTimeSecondsWithRounding
25 NSTimeInterval factor = 3; // 3 seconds
27 // Round into the same bucket
28 NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
29 NSTimeInterval expectedTime = now + factor;
30 NSTimeInterval actualTimeWithWiggle = [[NSDate date] timeIntervalSince1970WithBucket:SFAnalyticsTimestampBucketSecond];
31 XCTAssertEqualWithAccuracy(actualTimeWithWiggle, expectedTime, factor, @"Expected to get roughly the same rounded time within the rounding factor");
33 // Round into the next bucket
34 now = [[NSDate date] timeIntervalSince1970];
35 expectedTime = now + factor;
37 actualTimeWithWiggle = [[NSDate date] timeIntervalSince1970WithBucket:SFAnalyticsTimestampBucketSecond];
38 XCTAssertEqualWithAccuracy(actualTimeWithWiggle, expectedTime, factor + 1, @"Expected to get roughly the same rounded time within the rounding factor");