Skip to content

Commit

Permalink
Merge pull request #9 from YOCKOW/development
Browse files Browse the repository at this point in the history
Support Swift 6.
  • Loading branch information
YOCKOW authored Oct 4, 2024
2 parents 8e4de4e + edaba07 commit 5a968a5
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 29 deletions.
70 changes: 48 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,73 @@ on:
- '.github/workflows/*.yml'
jobs:
test:
defaults:
run:
working-directory: '.'
strategy:
matrix:
os:
- ubuntu-latest
- macOS-latest
- ubuntu-22.04
- ubuntu-24.04
- macOS-14
- macOS-15
swift-version:
- '6.0.1'
- '5.10.1'
- '5.10'
swift-compat-ver:
- '6'
- '5'
- '4.2'
- '4'
exclude:
- os: ubuntu-22.04
swift-version: '5.10'
- os: ubuntu-24.04
swift-version: '5.10'
- os: macOS-14
swift-version: '5.10.1'
- os: macOS-15
swift-version: '5.10.1'
- os: macOS-15
swift-version: '5.10'
- swift-version: '5.10.1'
swift-compat-ver: '6'
- swift-version: '5.10'
swift-compat-ver: '6'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use a cache for ".build" directory.
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: .build
key: build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-compat-ver }}-${{ hashFiles('**/*.swift') }}
key: build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-${{ matrix.swift-compat-ver }}-${{ hashFiles('**/*.swift') }}
restore-keys: |
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-compat-ver }}-
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-${{ matrix.swift-compat-ver }}-
build-${{ github.workspace }}-${{ runner.os }}-${{ matrix.swift-version }}-
build-${{ github.workspace }}-${{ runner.os }}-
build-${{ github.workspace }}-
- uses: YOCKOW/Action-setup-swift@master
- uses: YOCKOW/Action-setup-swift@main
with:
swift-version: '5.4'
swift-version: ${{ matrix.swift-version }}
# DEBUG mode
- name: Try to build products with debug mode.
id: debug_build
run: swift build --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
continue-on-error: true
- name: Clean debug build directory if necessary
if: steps.debug_build.outcome == 'failure'
run: rm -rf $(cd .build/debug && pwd -P)
- name: Test with debug mode.
id: debug_test
run: swift test --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
# RELEASE mode
- name: Try to build products with release mode.
id: release_build
run: swift build --configuration release -Xswiftc -enable-testing -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
continue-on-error: true
- name: Clean release build directory if necessary
if: steps.release_build.outcome == 'failure'
run: rm -rf $(cd .build/release && pwd -P)
- name: Retry test with debug mode if necessary
if: steps.debug_test.outcome == 'failure'
run: |
rm -rf $(cd .build/debug && pwd -P)
swift test --configuration debug -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
# RELEASE mode
- name: Test with release mode.
id: release_test
run: swift test --configuration release -Xswiftc -enable-testing -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
continue-on-error: true
- name: Retry test with release mode if necessary
if: steps.release_test.outcome == 'failure'
run: |
rm -rf $(cd .build/release && pwd -P)
swift test --configuration release -Xswiftc -swift-version -Xswiftc ${{ matrix.swift-compat-ver }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
build/
y-build/

/.swiftpm
*.xcodeproj/
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2016-2017 YOCKOW
Copyright (c) 2016-2017,2024 YOCKOW

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ let package = Package(
.target(name: "TimeSpecification", dependencies: []),
.testTarget(name: "TimeSpecificationTests", dependencies: ["TimeSpecification"]),
],
swiftLanguageVersions: [.v4, .v4_2, .v5]
swiftLanguageVersions: [.v4, .v4_2, .v5, .version("6")]
)

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let dateNow = Date(timeIntervalSince1970: now) // -> Almost same with Date(timeI

# Requirements

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


Expand Down
8 changes: 4 additions & 4 deletions Sources/TimeSpecification/TimeSpecification.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/***************************************************************************************************
TimeSpecification.swift
© 2016-2020 YOCKOW.
© 2016-2020,2024 YOCKOW.
Licensed under MIT License.
See "LICENSE.txt" for more information.
**************************************************************************************************/

#if canImport(Darwin)
import Darwin
private let mach_task_self:() -> mach_port_t = { mach_task_self_ }
@preconcurrency import Darwin
private let mach_task_self: @Sendable () -> mach_port_t = { mach_task_self_ }
private typealias _CTimeSpec = mach_timespec_t
#else
import Glibc
Expand All @@ -17,7 +17,7 @@ private typealias _CTimeSpec = timespec
import Foundation

/// The representation for the time in nanoseconds.
public struct TimeSpecification {
public struct TimeSpecification: Sendable {
public var seconds: Int64 = 0

private var _nanoseconds: Int32 = 0
Expand Down
68 changes: 68 additions & 0 deletions Tests/TimeSpecificationTests/TimeSpecificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,73 @@ import XCTest

import Foundation

#if swift(>=6) && canImport(Testing)
import Testing

@Suite struct TimeSpecificationTests {
@Test func normalization() {
let N1 = TimeSpecification(seconds:0, nanoseconds:1_234_567_890)
let N2 = TimeSpecification(seconds:-1, nanoseconds:-1_234_567_890)

#expect(N1.seconds == 1 && N1.nanoseconds == 234_567_890, "Normalization Test 1")
#expect(N2.seconds == -3 && N2.nanoseconds == 765_432_110, "Normalization Test 2")
}

@Test func codable() throws {
let spec = TimeSpecification(seconds: 123, nanoseconds: 456_789)
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let encoded = try encoder.encode(spec)
let encodedString = try #require(String(data: encoded, encoding: .utf8))
#expect(encodedString == #"{"nanoseconds":456789,"seconds":123}"#)

let decoded = try JSONDecoder().decode(TimeSpecification.self, from: encoded)
#expect(decoded == spec)
}

@Test func comparison() {
let C1 = TimeSpecification(seconds:100, nanoseconds:100)
let C2 = TimeSpecification(seconds: 98, nanoseconds:2_000_000_100)
let C3 = TimeSpecification(seconds:200, nanoseconds:100)
let C4 = TimeSpecification(seconds:100, nanoseconds:200)
#expect(C1 == C2, "Comparison Test 1")
#expect(C2 < C3, "Comparison Test 2")
#expect(C2 < C4, "Comparison Test 3")
}

@Test func integerLiteral() {
let I1: TimeSpecification = 100
let I2: TimeSpecification = -100
#expect(I1 == TimeSpecification(seconds:100, nanoseconds:0), "ExpressibleByIntegerLiteral Test 1")
#expect(I2 == TimeSpecification(seconds:-100, nanoseconds:0), "ExpressibleByIntegerLiteral Test 2")
}

@Test func floatLiteral() {
let F1: TimeSpecification = 1.1
#expect(F1 == TimeSpecification(seconds:1, nanoseconds:100_000_000), "ExpressibleByFloatLiteral Test 1")
}

@Test func sumAndDifference() {
let L1 = TimeSpecification(seconds:100, nanoseconds:123_456_789)
let R1 = TimeSpecification(seconds:100, nanoseconds:987_654_321)
#expect(L1 + R1 == TimeSpecification(seconds:201, nanoseconds:111_111_110), "Sum Test 1")
#expect(L1 - R1 == TimeSpecification(seconds:0, nanoseconds:-864_197_532), "Difference Test 1")
}

@Test func description() {
let spec = TimeSpecification(seconds: 123, nanoseconds: 456_789)
#expect(spec.description == "123.000456789")
}

@Test func date() {
let spec = TimeSpecification(seconds: 100, nanoseconds: 123_456_789)
#expect(
Date(timeIntervalSinceReferenceDate: spec)
== Date(timeIntervalSinceReferenceDate: 100.123456789)
)
}
}
#else
class TimeSpecificationTests: XCTestCase {
func test_normalization() {
let N1 = TimeSpecification(seconds:0, nanoseconds:1_234_567_890)
Expand Down Expand Up @@ -71,3 +138,4 @@ class TimeSpecificationTests: XCTestCase {
Date(timeIntervalSinceReferenceDate: 100.123456789))
}
}
#endif

0 comments on commit 5a968a5

Please sign in to comment.