1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
private import _TestingInternals
@Suite("Clock API Tests")
struct ClockTests {
@available(_clockAPI, *)
@Test("Clock.Instant basics")
func clockInstant() async throws {
let instant1 = Test.Clock.Instant.now
try await Test.Clock.sleep(for: .nanoseconds(50_000_000))
let instant2 = Test.Clock.Instant.now
#expect(instant1 == instant1)
#expect(instant1 < instant2)
#expect(!(instant2 < instant1))
#expect(instant2 > instant1)
let instant3 = instant2.advanced(by: .seconds(5))
#expect(instant2 < instant3)
#expect(!(instant3 < instant2))
#expect(instant3 > instant2)
// Exercise the hash function.
let instants = Set([instant1, instant1, instant2])
#expect(instants.count == 2)
let now = Test.Clock.Instant.now
#expect(now.suspending.seconds > 0)
#expect(now.suspending.attoseconds >= 0)
#if !SWT_NO_UTC_CLOCK
#expect(now.wall.seconds > 0)
#expect(now.wall.attoseconds >= 0)
#endif
}
@available(_clockAPI, *)
@Test("Creating a SuspendingClock.Instant from Test.Clock.Instant")
func suspendingInstantInitializer() async throws {
let instant1 = SuspendingClock.Instant(Test.Clock.Instant.now)
try await Test.Clock.sleep(for: .milliseconds(50))
let instant2 = SuspendingClock.Instant(Test.Clock.Instant.now)
#expect(instant1 < instant2)
}
@available(_clockAPI, *)
@Test("Clock.sleep(until:tolerance:) method")
func sleepUntilTolerance() async throws {
let instant1 = SuspendingClock.Instant(Test.Clock.Instant.now)
try await Test.Clock().sleep(until: .now.advanced(by: .milliseconds(50)), tolerance: nil)
let instant2 = SuspendingClock.Instant(Test.Clock.Instant.now)
#expect(instant1 < instant2)
}
#if !SWT_NO_UTC_CLOCK
@available(_clockAPI, *)
@Test("Clock.Instant.timeComponentsSince1970 property")
func timeComponentsSince1970() async throws {
let instant1 = Test.Clock.Instant.now.timeComponentsSince1970
try await Test.Clock.sleep(for: .nanoseconds(50_000_000))
let instant2 = Test.Clock.Instant.now.timeComponentsSince1970
#expect(instant1.seconds < instant2.seconds || instant1.attoseconds < instant2.attoseconds)
}
#endif
#if !SWT_NO_UTC_CLOCK
@available(_clockAPI, *)
@Test("Clock.Instant.durationSince1970 property")
func durationSince1970() async throws {
let instant1 = Test.Clock.Instant.now.durationSince1970
try await Test.Clock.sleep(for: .milliseconds(50))
let instant2 = Test.Clock.Instant.now.durationSince1970
#expect(instant1 < instant2)
}
#endif
@available(_clockAPI, *)
@Test("Clock.now property")
func clockNowProperty() async throws {
let instant1 = Test.Clock().now
try await Test.Clock.sleep(for: .milliseconds(50))
let instant2 = Test.Clock().now
#expect(instant1 < instant2)
}
@available(_clockAPI, *)
@Test("Clock.minimumResolution property")
func clockMinimumResolutionProperty() async throws {
let minimumResolution = Test.Clock().minimumResolution
#expect(minimumResolution == SuspendingClock().minimumResolution)
}
@available(_clockAPI, *)
@Test("Clock.Instant.advanced(by:) and .duration(to:) methods")
func instantAdvancedByAndDurationTo() async throws {
let offsetNanoseconds = Int64.random(in: -1_000_000_000 ..< 1_000_000_000)
let instant1 = Test.Clock.Instant.now
let instant2 = instant1.advanced(by: .nanoseconds(offsetNanoseconds))
let duration = instant1.duration(to: instant2)
#expect(SuspendingClock.Instant(instant1).advanced(by: .nanoseconds(offsetNanoseconds)) == SuspendingClock.Instant(instant2))
#if !SWT_NO_UTC_CLOCK
#expect(instant1.durationSince1970 + .nanoseconds(offsetNanoseconds) == instant2.durationSince1970)
#endif
#expect(duration == .nanoseconds(offsetNanoseconds))
}
#if canImport(Foundation)
@available(_clockAPI, *)
@Test("Codable")
func codable() async throws {
let now = Test.Clock.Instant()
let instant = now.advanced(by: .nanoseconds(100))
let decoded = try JSON.encodeAndDecode(instant)
#expect(instant == decoded)
#expect(instant != now)
}
#endif
@available(_clockAPI, *)
@Test("Clock.Instant.nanoseconds(until:) method",
arguments: [
(Duration.zero, 0),
(.nanoseconds(1), 1),
(.seconds(1), 1_000_000_000),
(Duration(secondsComponent: 0, attosecondsComponent: 1), 0),
]
)
func nanoseconds(until offset: Duration, nanoseconds: Int) {
let now = Test.Clock.Instant.now
#expect(now.nanoseconds(until: now.advanced(by: offset)) == nanoseconds)
}
}
|