Skip to content

Commit 5a968a5

Browse files
authored
Merge pull request #9 from YOCKOW/development
Support Swift 6.
2 parents 8e4de4e + edaba07 commit 5a968a5

File tree

7 files changed

+124
-29
lines changed

7 files changed

+124
-29
lines changed

.github/workflows/ci.yml

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,73 @@ on:
1414
- '.github/workflows/*.yml'
1515
jobs:
1616
test:
17+
defaults:
18+
run:
19+
working-directory: '.'
1720
strategy:
1821
matrix:
1922
os:
20-
- ubuntu-latest
21-
- macOS-latest
23+
- ubuntu-22.04
24+
- ubuntu-24.04
25+
- macOS-14
26+
- macOS-15
27+
swift-version:
28+
- '6.0.1'
29+
- '5.10.1'
30+
- '5.10'
2231
swift-compat-ver:
32+
- '6'
2333
- '5'
2434
- '4.2'
2535
- '4'
36+
exclude:
37+
- os: ubuntu-22.04
38+
swift-version: '5.10'
39+
- os: ubuntu-24.04
40+
swift-version: '5.10'
41+
- os: macOS-14
42+
swift-version: '5.10.1'
43+
- os: macOS-15
44+
swift-version: '5.10.1'
45+
- os: macOS-15
46+
swift-version: '5.10'
47+
- swift-version: '5.10.1'
48+
swift-compat-ver: '6'
49+
- swift-version: '5.10'
50+
swift-compat-ver: '6'
2651
runs-on: ${{ matrix.os }}
2752
steps:
28-
- uses: actions/checkout@v2
53+
- uses: actions/checkout@v4
2954
- name: Use a cache for ".build" directory.
30-
uses: actions/cache@v1
55+
uses: actions/cache@v4
3156
with:
3257
path: .build
33-
key: build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-compat-ver }}-${{ hashFiles('**/*.swift') }}
58+
key: build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-${{ matrix.swift-compat-ver }}-${{ hashFiles('**/*.swift') }}
3459
restore-keys: |
35-
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-compat-ver }}-
60+
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-${{ matrix.swift-compat-ver }}-
61+
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-
3662
build-${{ github.workspace }}-${{ runner.os }}-
3763
build-${{ github.workspace }}-
38-
- uses: YOCKOW/Action-setup-swift@master
64+
- uses: YOCKOW/Action-setup-swift@main
3965
with:
40-
swift-version: '5.4'
66+
swift-version: ${{ matrix.swift-version }}
4167
# DEBUG mode
42-
- name: Try to build products with debug mode.
43-
id: debug_build
44-
run: swift build --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
45-
continue-on-error: true
46-
- name: Clean debug build directory if necessary
47-
if: steps.debug_build.outcome == 'failure'
48-
run: rm -rf $(cd .build/debug && pwd -P)
4968
- name: Test with debug mode.
69+
id: debug_test
5070
run: swift test --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
51-
# RELEASE mode
52-
- name: Try to build products with release mode.
53-
id: release_build
54-
run: swift build --configuration release -Xswiftc -enable-testing -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
5571
continue-on-error: true
56-
- name: Clean release build directory if necessary
57-
if: steps.release_build.outcome == 'failure'
58-
run: rm -rf $(cd .build/release && pwd -P)
72+
- name: Retry test with debug mode if necessary
73+
if: steps.debug_test.outcome == 'failure'
74+
run: |
75+
rm -rf $(cd .build/debug && pwd -P)
76+
swift test --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
77+
# RELEASE mode
5978
- name: Test with release mode.
79+
id: release_test
6080
run: swift test --configuration release -Xswiftc -enable-testing -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
81+
continue-on-error: true
82+
- name: Retry test with release mode if necessary
83+
if: steps.release_test.outcome == 'failure'
84+
run: |
85+
rm -rf $(cd .build/release && pwd -P)
86+
swift test --configuration release -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
build/
55
y-build/
66

7+
/.swiftpm
78
*.xcodeproj/

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2016-2017 YOCKOW
2+
Copyright (c) 2016-2017,2024 YOCKOW
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
55
and associated documentation files (the "Software"), to deal in the Software without restriction,

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ let package = Package(
1818
.target(name: "TimeSpecification", dependencies: []),
1919
.testTarget(name: "TimeSpecificationTests", dependencies: ["TimeSpecification"]),
2020
],
21-
swiftLanguageVersions: [.v4, .v4_2, .v5]
21+
swiftLanguageVersions: [.v4, .v4_2, .v5, .version("6")]
2222
)
2323

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let dateNow = Date(timeIntervalSince1970: now) // -> Almost same with Date(timeI
2727

2828
# Requirements
2929

30-
- Swift 5 (including compatibility mode for 4, 4.2)
30+
- Swift 5, 6 (including language mode for 4, 4.2)
3131
- macOS or Linux
3232

3333

Sources/TimeSpecification/TimeSpecification.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/***************************************************************************************************
22
TimeSpecification.swift
3-
© 2016-2020 YOCKOW.
3+
© 2016-2020,2024 YOCKOW.
44
Licensed under MIT License.
55
See "LICENSE.txt" for more information.
66
**************************************************************************************************/
77

88
#if canImport(Darwin)
9-
import Darwin
10-
private let mach_task_self:() -> mach_port_t = { mach_task_self_ }
9+
@preconcurrency import Darwin
10+
private let mach_task_self: @Sendable () -> mach_port_t = { mach_task_self_ }
1111
private typealias _CTimeSpec = mach_timespec_t
1212
#else
1313
import Glibc
@@ -17,7 +17,7 @@ private typealias _CTimeSpec = timespec
1717
import Foundation
1818

1919
/// The representation for the time in nanoseconds.
20-
public struct TimeSpecification {
20+
public struct TimeSpecification: Sendable {
2121
public var seconds: Int64 = 0
2222

2323
private var _nanoseconds: Int32 = 0

Tests/TimeSpecificationTests/TimeSpecificationTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,73 @@ import XCTest
1010

1111
import 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
1380
class 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

Comments
 (0)