FSTTimestampTests.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "Firestore/Source/Core/FSTTimestamp.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Util/FSTAssert.h"
  19. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FSTTimestampTests : XCTestCase
  22. @end
  23. @implementation FSTTimestampTests
  24. - (void)testFromDate {
  25. // Very carefully construct an NSDate that won't lose precision with its milliseconds.
  26. NSDate *input = [NSDate dateWithTimeIntervalSinceReferenceDate:1.5];
  27. FSTTimestamp *actual = [FSTTimestamp timestampWithDate:input];
  28. static const int64_t kSecondsFromEpochToReferenceDate = 978307200;
  29. XCTAssertEqual(kSecondsFromEpochToReferenceDate + 1, actual.seconds);
  30. XCTAssertEqual(500000000, actual.nanos);
  31. FSTTimestamp *expected =
  32. [[FSTTimestamp alloc] initWithSeconds:(kSecondsFromEpochToReferenceDate + 1) nanos:500000000];
  33. XCTAssertEqualObjects(expected, actual);
  34. }
  35. - (void)testSO8601String {
  36. NSDate *date = FSTTestDate(1912, 4, 14, 23, 40, 0);
  37. FSTTimestamp *timestamp =
  38. [[FSTTimestamp alloc] initWithSeconds:(int64_t)date.timeIntervalSince1970 nanos:543000000];
  39. XCTAssertEqualObjects(timestamp.ISO8601String, @"1912-04-14T23:40:00.543000000Z");
  40. }
  41. - (void)testISO8601String_withLowMilliseconds {
  42. NSDate *date = FSTTestDate(1912, 4, 14, 23, 40, 0);
  43. FSTTimestamp *timestamp =
  44. [[FSTTimestamp alloc] initWithSeconds:(int64_t)date.timeIntervalSince1970 nanos:7000000];
  45. XCTAssertEqualObjects(timestamp.ISO8601String, @"1912-04-14T23:40:00.007000000Z");
  46. }
  47. - (void)testISO8601String_withLowNanos {
  48. FSTTimestamp *timestamp = [[FSTTimestamp alloc] initWithSeconds:0 nanos:1];
  49. XCTAssertEqualObjects(timestamp.ISO8601String, @"1970-01-01T00:00:00.000000001Z");
  50. }
  51. - (void)testISO8601String_withNegativeSeconds {
  52. FSTTimestamp *timestamp = [[FSTTimestamp alloc] initWithSeconds:-1 nanos:999999999];
  53. XCTAssertEqualObjects(timestamp.ISO8601String, @"1969-12-31T23:59:59.999999999Z");
  54. }
  55. - (void)testCompare {
  56. NSArray<FSTTimestamp *> *timestamps = @[
  57. [[FSTTimestamp alloc] initWithSeconds:12344 nanos:999999999],
  58. [[FSTTimestamp alloc] initWithSeconds:12345 nanos:0],
  59. [[FSTTimestamp alloc] initWithSeconds:12345 nanos:000000001],
  60. [[FSTTimestamp alloc] initWithSeconds:12345 nanos:99999999],
  61. [[FSTTimestamp alloc] initWithSeconds:12345 nanos:100000000],
  62. [[FSTTimestamp alloc] initWithSeconds:12345 nanos:100000001],
  63. [[FSTTimestamp alloc] initWithSeconds:12346 nanos:0],
  64. ];
  65. for (int i = 0; i < timestamps.count - 1; ++i) {
  66. XCTAssertEqual(NSOrderedAscending, [timestamps[i] compare:timestamps[i + 1]]);
  67. XCTAssertEqual(NSOrderedDescending, [timestamps[i + 1] compare:timestamps[i]]);
  68. }
  69. }
  70. @end
  71. NS_ASSUME_NONNULL_END