@@ -10,6 +10,73 @@ import XCTest
1010
1111import Foundation
1212
13+ #if swift(>=6) && canImport(Testing)
14+ import Testing
15+
16+ @Suite struct TimeSpecificationTests {
17+ @Test func normalization( ) {
18+ let N1 = TimeSpecification ( seconds: 0 , nanoseconds: 1_234_567_890 )
19+ let N2 = TimeSpecification ( seconds: - 1 , nanoseconds: - 1_234_567_890 )
20+
21+ #expect( N1 . seconds == 1 && N1 . nanoseconds == 234_567_890 , " Normalization Test 1 " )
22+ #expect( N2 . seconds == - 3 && N2 . nanoseconds == 765_432_110 , " Normalization Test 2 " )
23+ }
24+
25+ @Test func codable( ) throws {
26+ let spec = TimeSpecification ( seconds: 123 , nanoseconds: 456_789 )
27+ let encoder = JSONEncoder ( )
28+ encoder. outputFormatting = . sortedKeys
29+ let encoded = try encoder. encode ( spec)
30+ let encodedString = try #require( String ( data: encoded, encoding: . utf8) )
31+ #expect( encodedString == #"{"nanoseconds":456789,"seconds":123}"# )
32+
33+ let decoded = try JSONDecoder ( ) . decode ( TimeSpecification . self, from: encoded)
34+ #expect( decoded == spec)
35+ }
36+
37+ @Test func comparison( ) {
38+ let C1 = TimeSpecification ( seconds: 100 , nanoseconds: 100 )
39+ let C2 = TimeSpecification ( seconds: 98 , nanoseconds: 2_000_000_100 )
40+ let C3 = TimeSpecification ( seconds: 200 , nanoseconds: 100 )
41+ let C4 = TimeSpecification ( seconds: 100 , nanoseconds: 200 )
42+ #expect( C1 == C2, " Comparison Test 1 " )
43+ #expect( C2 < C3, " Comparison Test 2 " )
44+ #expect( C2 < C4, " Comparison Test 3 " )
45+ }
46+
47+ @Test func integerLiteral( ) {
48+ let I1 : TimeSpecification = 100
49+ let I2 : TimeSpecification = - 100
50+ #expect( I1 == TimeSpecification ( seconds: 100 , nanoseconds: 0 ) , " ExpressibleByIntegerLiteral Test 1 " )
51+ #expect( I2 == TimeSpecification ( seconds: - 100 , nanoseconds: 0 ) , " ExpressibleByIntegerLiteral Test 2 " )
52+ }
53+
54+ @Test func floatLiteral( ) {
55+ let F1 : TimeSpecification = 1.1
56+ #expect( F1 == TimeSpecification ( seconds: 1 , nanoseconds: 100_000_000 ) , " ExpressibleByFloatLiteral Test 1 " )
57+ }
58+
59+ @Test func sumAndDifference( ) {
60+ let L1 = TimeSpecification ( seconds: 100 , nanoseconds: 123_456_789 )
61+ let R1 = TimeSpecification ( seconds: 100 , nanoseconds: 987_654_321 )
62+ #expect( L1 + R1 == TimeSpecification ( seconds: 201 , nanoseconds: 111_111_110 ) , " Sum Test 1 " )
63+ #expect( L1 - R1 == TimeSpecification ( seconds: 0 , nanoseconds: - 864_197_532 ) , " Difference Test 1 " )
64+ }
65+
66+ @Test func description( ) {
67+ let spec = TimeSpecification ( seconds: 123 , nanoseconds: 456_789 )
68+ #expect( spec. description == " 123.000456789 " )
69+ }
70+
71+ @Test func date( ) {
72+ let spec = TimeSpecification ( seconds: 100 , nanoseconds: 123_456_789 )
73+ #expect(
74+ Date ( timeIntervalSinceReferenceDate: spec)
75+ == Date ( timeIntervalSinceReferenceDate: 100.123456789 )
76+ )
77+ }
78+ }
79+ #else
1380class TimeSpecificationTests : XCTestCase {
1481 func test_normalization( ) {
1582 let N1 = TimeSpecification ( seconds: 0 , nanoseconds: 1_234_567_890 )
@@ -71,3 +138,4 @@ class TimeSpecificationTests: XCTestCase {
71138 Date ( timeIntervalSinceReferenceDate: 100.123456789 ) )
72139 }
73140}
141+ #endif
0 commit comments