]> git.saurik.com Git - apple/security.git/blob - supd/Tests/NSDate+SFAnalyticsTests.m
Security-59754.80.3.tar.gz
[apple/security.git] / supd / Tests / NSDate+SFAnalyticsTests.m
1 //
2 // NSDate+SFAnalyticsTests.m
3 // KeychainAnalyticsTests
4 //
5
6 #import <XCTest/XCTest.h>
7
8 #import "../Analytics/NSDate+SFAnalytics.h"
9
10 @interface NSDate_SFAnalyticsTests : XCTestCase
11
12 @end
13
14 @implementation NSDate_SFAnalyticsTests
15
16 - (void)testCurrentTimeSeconds
17 {
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");
21 }
22
23 - (void)testCurrentTimeSecondsWithRounding
24 {
25 NSTimeInterval factor = 3; // 3 seconds
26
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");
32
33 // Round into the next bucket
34 now = [[NSDate date] timeIntervalSince1970];
35 expectedTime = now + factor;
36 sleep(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");
39 }
40
41 @end