Skip to content

Commit 5315bbd

Browse files
author
Mike Ferris
committed
Add XCTimeUtilities.swift and factor two functions out for dealing with Doubles that represent 'time intervals'. This centralizes the weird rounding that is going on to try to format printed doubles with no more than 3 decimal places as well as encapsulating the use of timeval and gettimeofday.
1 parent 84da524 commit 5315bbd

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

XCTest.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
B108C2A71C20C15500F9F301 /* XCTimeUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B108C2A61C20C15500F9F301 /* XCTimeUtilities.swift */; };
1011
B11FFA2A1C1603B6004297C2 /* XCTAssert.swift in Sources */ = {isa = PBXBuildFile; fileRef = B11FFA291C1603B6004297C2 /* XCTAssert.swift */; };
1112
B11FFA2C1C160434004297C2 /* XCTestCaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = B11FFA2B1C160434004297C2 /* XCTestCaseProvider.swift */; };
1213
B11FFA2E1C16053C004297C2 /* XCTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B11FFA2D1C16053C004297C2 /* XCTestCase.swift */; };
@@ -15,6 +16,7 @@
1516

1617
/* Begin PBXFileReference section */
1718
5B5D86DB1BBC74AD00234F36 /* SwiftXCTest.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftXCTest.framework; sourceTree = BUILT_PRODUCTS_DIR; };
19+
B108C2A61C20C15500F9F301 /* XCTimeUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTimeUtilities.swift; sourceTree = "<group>"; };
1820
B11FFA291C1603B6004297C2 /* XCTAssert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTAssert.swift; sourceTree = "<group>"; };
1921
B11FFA2B1C160434004297C2 /* XCTestCaseProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestCaseProvider.swift; sourceTree = "<group>"; };
2022
B11FFA2D1C16053C004297C2 /* XCTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestCase.swift; sourceTree = "<group>"; };
@@ -63,6 +65,7 @@
6365
B11FFA2D1C16053C004297C2 /* XCTestCase.swift */,
6466
B11FFA2B1C160434004297C2 /* XCTestCaseProvider.swift */,
6567
B11FFA291C1603B6004297C2 /* XCTAssert.swift */,
68+
B108C2A61C20C15500F9F301 /* XCTimeUtilities.swift */,
6669
);
6770
path = XCTest;
6871
sourceTree = "<group>";
@@ -164,6 +167,7 @@
164167
isa = PBXSourcesBuildPhase;
165168
buildActionMask = 2147483647;
166169
files = (
170+
B108C2A71C20C15500F9F301 /* XCTimeUtilities.swift in Sources */,
167171
EA6E86BB1BDEA7DE007C0323 /* XCTest.swift in Sources */,
168172
B11FFA2C1C160434004297C2 /* XCTestCaseProvider.swift in Sources */,
169173
B11FFA2E1C16053C004297C2 /* XCTestCase.swift in Sources */,

XCTest/XCTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal struct XCTRun {
5858
failureSuffix = ""
5959
}
6060
let averageDuration = totalDuration / Double(XCTAllRuns.count)
61-
print("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(round(averageDuration * 1000.0) / 1000.0) (\(round(totalDuration * 1000.0) / 1000.0)) seconds")
61+
print("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(averageDuration)) (\(printableStringForTimeInterval(totalDuration))) seconds")
6262
exit(totalFailures > 0 ? 1 : 0)
6363
}
6464

XCTest/XCTestCase.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
// Base protocol (and extension with default methods) for test cases
1212
//
1313

14-
#if os(Linux)
15-
import Glibc
16-
#else
17-
import Darwin
18-
#endif
19-
2014
public protocol XCTestCase : XCTestCaseProvider {
2115

2216
}
@@ -42,17 +36,10 @@ extension XCTestCase {
4236
let method = "\(self.dynamicType).\(name)"
4337
var duration: Double = 0.0
4438
print("Test Case '\(method)' started.")
45-
var tv = timeval()
46-
let start = withUnsafeMutablePointer(&tv, { (t: UnsafeMutablePointer<timeval>) -> Double in
47-
gettimeofday(t, nil)
48-
return Double(t.memory.tv_sec) + Double(t.memory.tv_usec) / 1000000.0
49-
})
39+
let start = currentTimeIntervalSinceReferenceTime()
5040

5141
test()
52-
let end = withUnsafeMutablePointer(&tv, { (t: UnsafeMutablePointer<timeval>) -> Double in
53-
gettimeofday(t, nil)
54-
return Double(t.memory.tv_sec) + Double(t.memory.tv_usec) / 1000000.0
55-
})
42+
let end = currentTimeIntervalSinceReferenceTime()
5643
duration = end - start
5744
totalDuration += duration
5845
for failure in XCTCurrentFailures {
@@ -66,7 +53,7 @@ extension XCTestCase {
6653
if XCTCurrentFailures.count > 0 {
6754
result = "failed"
6855
}
69-
print("Test Case '\(method)' \(result) (\(round(duration * 1000.0) / 1000.0) seconds).")
56+
print("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).")
7057
XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: XCTCurrentFailures.count == 0, failures: XCTCurrentFailures))
7158
XCTCurrentFailures.removeAll()
7259
XCTCurrentTestCase = nil
@@ -81,8 +68,7 @@ extension XCTestCase {
8168
}
8269
let averageDuration = totalDuration / Double(tests.count)
8370

84-
85-
print("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) \(unexpectedFailures) unexpected) in \(round(averageDuration * 1000.0) / 1000.0) (\(round(totalDuration * 1000.0) / 1000.0)) seconds")
71+
print("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) \(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(averageDuration)) (\(printableStringForTimeInterval(totalDuration))) seconds")
8672
}
8773

8874
// This function is for the use of XCTestCase only, but we must make it public or clients will get a link failure when using XCTest (23476006)

XCTest/XCTimeUtilities.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
//
10+
// XCTimeUtilities.swift
11+
// Some simple functions for working with "time intervals".
12+
//
13+
14+
#if os(Linux)
15+
import Glibc
16+
#else
17+
import Darwin
18+
#endif
19+
20+
/// Returns the number of seconds since the reference time as a Double.
21+
internal func currentTimeIntervalSinceReferenceTime() -> Double {
22+
var tv = timeval()
23+
let currentTime = withUnsafeMutablePointer(&tv, { (t: UnsafeMutablePointer<timeval>) -> Double in
24+
gettimeofday(t, nil)
25+
return Double(t.memory.tv_sec) + Double(t.memory.tv_usec) / 1000000.0
26+
})
27+
return currentTime
28+
}
29+
30+
/// Returns a string version of the given time interval rounded to no more than 3 decimal places.
31+
internal func printableStringForTimeInterval(timeInterval: Double) -> String {
32+
return String(round(timeInterval * 1000.0) / 1000.0)
33+
}
34+

build_script.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ def main():
7878
if not os.path.exists(build_dir):
7979
run("mkdir -p {}".format(build_dir))
8080

81-
sourceFiles = ["XCTest.swift", "XCTestCaseProvider.swift", "XCTestCase.swift", "XCTAssert.swift"]
81+
sourceFiles = [
82+
"XCTest.swift",
83+
"XCTestCaseProvider.swift",
84+
"XCTestCase.swift",
85+
"XCTAssert.swift",
86+
"XCTimeUtilities.swift",
87+
]
8288
sourcePaths = []
8389
for file in sourceFiles:
8490
sourcePaths.append("{0}/XCTest/{1}".format(os.path.dirname(os.path.abspath(__file__)), file))

0 commit comments

Comments
 (0)