FIRTimestampTest.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "Firestore/Source/API/FIRTimestamp+Internal.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @interface FIRTimestampTest : XCTestCase
  20. @end
  21. NSDate *TestDate(int year, int month, int day, int hour, int minute, int second) {
  22. NSDateComponents *comps = [[NSDateComponents alloc] init];
  23. comps.year = year;
  24. comps.month = month;
  25. comps.day = day;
  26. comps.hour = hour;
  27. comps.minute = minute;
  28. comps.second = second;
  29. // Force time zone to UTC to avoid these values changing due to daylight saving.
  30. comps.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  31. return [[NSCalendar currentCalendar] dateFromComponents:comps];
  32. }
  33. @implementation FIRTimestampTest
  34. - (void)testFromDate {
  35. // Use an NSDate such that its fractional seconds have an exact representation to avoid losing
  36. // precision.
  37. NSDate *input = [NSDate dateWithTimeIntervalSinceReferenceDate:1.5];
  38. FIRTimestamp *actual = [FIRTimestamp timestampWithDate:input];
  39. static const int64_t kSecondsFromEpochToReferenceDate = 978307200;
  40. XCTAssertEqual(kSecondsFromEpochToReferenceDate + 1, actual.seconds);
  41. XCTAssertEqual(actual.nanoseconds, 500000000);
  42. FIRTimestamp *expected =
  43. [[FIRTimestamp alloc] initWithSeconds:(kSecondsFromEpochToReferenceDate + 1)
  44. nanoseconds:500000000];
  45. XCTAssertEqualObjects(actual, expected);
  46. }
  47. - (void)testSO8601String {
  48. NSDate *date = TestDate(1912, 4, 14, 23, 40, 0);
  49. FIRTimestamp *timestamp =
  50. [[FIRTimestamp alloc] initWithSeconds:(int64_t)date.timeIntervalSince1970
  51. nanoseconds:543000000];
  52. XCTAssertEqualObjects(timestamp.ISO8601String, @"1912-04-14T23:40:00.543000000Z");
  53. }
  54. - (void)testISO8601String_withLowMilliseconds {
  55. NSDate *date = TestDate(1912, 4, 14, 23, 40, 0);
  56. FIRTimestamp *timestamp =
  57. [[FIRTimestamp alloc] initWithSeconds:(int64_t)date.timeIntervalSince1970
  58. nanoseconds:7000000];
  59. XCTAssertEqualObjects(timestamp.ISO8601String, @"1912-04-14T23:40:00.007000000Z");
  60. }
  61. - (void)testISO8601String_withLowNanos {
  62. FIRTimestamp *timestamp = [[FIRTimestamp alloc] initWithSeconds:0 nanoseconds:1];
  63. XCTAssertEqualObjects(timestamp.ISO8601String, @"1970-01-01T00:00:00.000000001Z");
  64. }
  65. - (void)testISO8601String_withNegativeSeconds {
  66. FIRTimestamp *timestamp = [[FIRTimestamp alloc] initWithSeconds:-1 nanoseconds:999999999];
  67. XCTAssertEqualObjects(timestamp.ISO8601String, @"1969-12-31T23:59:59.999999999Z");
  68. }
  69. - (void)testCompare {
  70. NSArray<FIRTimestamp *> *timestamps = @[
  71. [[FIRTimestamp alloc] initWithSeconds:12344 nanoseconds:999999999],
  72. [[FIRTimestamp alloc] initWithSeconds:12345 nanoseconds:0],
  73. [[FIRTimestamp alloc] initWithSeconds:12345 nanoseconds:000000001],
  74. [[FIRTimestamp alloc] initWithSeconds:12345 nanoseconds:99999999],
  75. [[FIRTimestamp alloc] initWithSeconds:12345 nanoseconds:100000000],
  76. [[FIRTimestamp alloc] initWithSeconds:12345 nanoseconds:100000001],
  77. [[FIRTimestamp alloc] initWithSeconds:12346 nanoseconds:0],
  78. ];
  79. for (int i = 0; i < timestamps.count - 1; ++i) {
  80. XCTAssertEqual([timestamps[i] compare:timestamps[i + 1]], NSOrderedAscending);
  81. XCTAssertEqual([timestamps[i + 1] compare:timestamps[i]], NSOrderedDescending);
  82. }
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END