Skip to content

Implement NSMeasurement #1425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ There is no _Complete_ status for test coverage because there are always additio
| `NSConcreteValue` | N/A | N/A | For internal use only |
| `NSSpecialValue` | N/A | N/A | For internal use only |
| `NSValue` | Complete | Substantial | |
| `NSMeasurement` | Unimplemented | None | |
| `NSMeasurement` | Complete | Incomplete | |
| `Measurement` | Complete | None | |
| `UnitConverter` | Complete | Incomplete | |
| `UnitConverterLinear` | Complete | Incomplete | |
Expand Down
42 changes: 10 additions & 32 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
159884921DCC877700E3314C /* TestHTTPCookieStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 159884911DCC877700E3314C /* TestHTTPCookieStorage.swift */; };
231503DB1D8AEE5D0061694D /* TestDecimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 231503DA1D8AEE5D0061694D /* TestDecimal.swift */; };
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */; };
2E60F151202CFA960037435B /* TestNSMeasurement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E60F150202CFA960037435B /* TestNSMeasurement.swift */; };
2EBE67A51C77BF0E006583D5 /* TestDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBE67A31C77BF05006583D5 /* TestDateFormatter.swift */; };
3E55A2331F52463B00082000 /* TestUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E55A2321F52463B00082000 /* TestUnit.swift */; };
3EA9D6701EF0532D00B362D6 /* TestJSONEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EA9D66F1EF0532D00B362D6 /* TestJSONEncoder.swift */; };
Expand Down Expand Up @@ -506,6 +507,7 @@
22B9C1E01C165D7A00DECFF9 /* TestDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDate.swift; sourceTree = "<group>"; };
231503DA1D8AEE5D0061694D /* TestDecimal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDecimal.swift; sourceTree = "<group>"; };
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSAttributedString.swift; sourceTree = "<group>"; };
2E60F150202CFA960037435B /* TestNSMeasurement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSMeasurement.swift; sourceTree = "<group>"; };
2EBE67A31C77BF05006583D5 /* TestDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDateFormatter.swift; sourceTree = "<group>"; };
3E55A2321F52463B00082000 /* TestUnit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUnit.swift; sourceTree = "<group>"; };
3EA9D66F1EF0532D00B362D6 /* TestJSONEncoder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestJSONEncoder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1469,6 +1471,7 @@
EA66F65A1BF1976100136161 /* Tests */ = {
isa = PBXGroup;
children = (
2E60F150202CFA960037435B /* TestNSMeasurement.swift */,
6E203B8C1C1303BB003B2576 /* TestBundle.swift */,
A5A34B551C18C85D00FD972B /* TestByteCountFormatter.swift */,
2EBE67A31C77BF05006583D5 /* TestDateFormatter.swift */,
Expand Down Expand Up @@ -2447,6 +2450,7 @@
5B13B32A1C582D4C00651CE2 /* TestCharacterSet.swift in Sources */,
BF8E65311DC3B3CB005AB5C3 /* TestNotification.swift in Sources */,
63DCE9D41EAA432400E9CB02 /* TestISO8601DateFormatter.swift in Sources */,
2E60F151202CFA960037435B /* TestNSMeasurement.swift in Sources */,
EA01AAEC1DA839C4008F4E07 /* TestProgress.swift in Sources */,
03B6F5841F15F339004F25AF /* TestURLProtocol.swift in Sources */,
5B13B3411C582D4C00651CE2 /* TestNSRegularExpression.swift in Sources */,
Expand Down Expand Up @@ -2668,11 +2672,7 @@
INFOPLIST_FILE = Foundation/Info.plist;
INIT_ROUTINE = "___CFInitialize";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
OTHER_CFLAGS = (
"-DCF_BUILDING_CF",
"-DDEPLOYMENT_TARGET_MACOSX",
Expand Down Expand Up @@ -2744,11 +2744,7 @@
INFOPLIST_FILE = Foundation/Info.plist;
INIT_ROUTINE = "___CFInitialize";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
OTHER_CFLAGS = (
"-DCF_BUILDING_CF",
"-DDEPLOYMENT_TARGET_MACOSX",
Expand Down Expand Up @@ -2896,11 +2892,7 @@
/usr/include/libxml2,
);
INFOPLIST_FILE = TestFoundation/Resources/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
LIBRARY_SEARCH_PATHS = "$(inherited)";
MACH_O_TYPE = mh_execute;
OTHER_SWIFT_FLAGS = "-DDEPLOYMENT_ENABLE_LIBDISPATCH -swift-version 4";
Expand All @@ -2926,11 +2918,7 @@
/usr/include/libxml2,
);
INFOPLIST_FILE = TestFoundation/Resources/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
LIBRARY_SEARCH_PATHS = "$(inherited)";
MACH_O_TYPE = mh_execute;
OTHER_SWIFT_FLAGS = "-DDEPLOYMENT_ENABLE_LIBDISPATCH -swift-version 4";
Expand All @@ -2954,12 +2942,7 @@
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
INFOPLIST_FILE = TestFoundation/xdgTestHelper/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../../..",
"@loader_path/../../..",
"@executable_path/../Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../../.. @loader_path/../../.. @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_BUNDLE_IDENTIFIER = org.swift.xdgTestHelper;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -2981,12 +2964,7 @@
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
INFOPLIST_FILE = TestFoundation/xdgTestHelper/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../../..",
"@loader_path/../../..",
"@executable_path/../Frameworks",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../../.. @loader_path/../../.. @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_BUNDLE_IDENTIFIER = org.swift.xdgTestHelper;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
86 changes: 79 additions & 7 deletions Foundation/NSMeasurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,93 @@ open class NSMeasurement : NSObject, NSCopying, NSSecureCoding {
self.unit = unit
}

open func canBeConverted(to unit: Unit) -> Bool { NSUnimplemented() }
open func canBeConverted(to otherUnit: Unit) -> Bool {
#if DEPLOYMENT_RUNTIME_OBJC //|| os(Linux)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the stuff inside a compilation conditional should be indented as though there weren't a conditional surrounding it; in most files, the conditional statement itself should be outdented to the left margin, but the specific style can vary depending on the file.

return otherUnit.isKind(of: type(of: unit))
#else
// just check conversion
if unit is Dimension && otherUnit is Dimension {
return true
} else {
return unit.isEqual(otherUnit)
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this implementation match Darwin Foundation? It seems to be very limited.

}

open func converting(to unit: Unit) -> Measurement<Unit> { NSUnimplemented() }
open func converting(to otherUnit: Unit) -> Measurement<Unit> {
if canBeConverted(to: otherUnit) {
if unit.isEqual(otherUnit) {
return Measurement(value: doubleValue, unit: otherUnit)
} else {
guard let dimension = unit as? Dimension,
let otherDimension = otherUnit as? Dimension else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the stuff inside the braces should be indented only four spaces as compared to guard; it's fine to indent the second line of the condition but it doesn't affect how the inside stuff is indented (the same applies elsewhere below).

fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: otherUnit))")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to align these error messages as closely as possible with Darwin Foundation.

}
let valueInTermsOfBase = dimension.converter.baseUnitValue(fromValue: doubleValue)
if otherDimension.isEqual(type(of: dimension).baseUnit()) {
return Measurement(value: valueInTermsOfBase, unit: otherDimension)
} else {
let otherValueFromTermsOfBase = otherDimension.converter.value(fromBaseUnitValue: valueInTermsOfBase)
return Measurement(value: otherValueFromTermsOfBase, unit: otherDimension)
}
}
} else {
fatalError("Cannot convert measurements of differing unit types! self: \(type(of: unit)) unit: \(type(of: otherUnit))")
}
}

open func adding(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() }
open func adding(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
if self.unit.isEqual(rhs.unit) {
return Measurement(value: self.doubleValue + rhs.value, unit: self.unit)
} else {
guard let dimension = unit as? Dimension,
let otherDimension = rhs.unit as? Dimension else {
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: rhs.unit))")
}
let selfValueInTermsOfBase = dimension.converter.baseUnitValue(fromValue: self.doubleValue)
let rhsValueInTermsOfBase = otherDimension.converter.baseUnitValue(fromValue: rhs.value)
return Measurement(value: selfValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: dimension).baseUnit())
}
}

open func subtracting(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() }
open func subtracting(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
if self.unit.isEqual(rhs.unit) {
return Measurement(value: self.doubleValue - rhs.value, unit: self.unit)
} else {
guard let dimension = unit as? Dimension,
let otherDimension = rhs.unit as? Dimension else {
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: rhs.unit))")
}
let selfValueInTermsOfBase = dimension.converter.baseUnitValue(fromValue: self.doubleValue)
let rhsValueInTermsOfBase = otherDimension.converter.baseUnitValue(fromValue: rhs.value)
return Measurement(value: selfValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: dimension).baseUnit())
}
}

open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
open func copy(with zone: NSZone? = nil) -> Any { return self }

open class var supportsSecureCoding: Bool { return true }

open func encode(with aCoder: NSCoder) { NSUnimplemented() }
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.doubleValue, forKey:"NS.value")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space after forKey:; likewise one line below.

aCoder.encode(self.unit, forKey:"NS.unit")
}

public required init?(coder aDecoder: NSCoder) { NSUnimplemented() }
public required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let doubleValue = aDecoder.decodeDouble(forKey: "NS.value")
let possibleUnit = aDecoder.decodeObject(forKey: "NS.unit")
guard let unit = possibleUnit as? Unit else {
return nil // or should we `fatalError()`?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does Darwin Foundation do?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing it does is validation, e.g. @"Cannot add measurements of differing unit types!"

}
self.doubleValue = doubleValue
self.unit = unit
}
}

extension NSMeasurement : _StructTypeBridgeable {
Expand Down
177 changes: 177 additions & 0 deletions TestFoundation/TestNSMeasurement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif

class TestNSMeasurement : XCTestCase {

static var allTests: [(String, (TestNSMeasurement) -> () throws -> Void)] {
return [
("test_addition_subtraction", test_addition_subtraction),
("test_conversion_dimensions", test_conversion_dimensions),
("test_conversion_units", test_conversion_units),
]
}

func test_addition_subtraction() {
func testAddSub<U : Unit>(for unit: U) {
for i in -2...10 {
for j in 0...5 {
let mi = NSMeasurement(doubleValue: Double(i), unit: unit)
let mj = Measurement(value: Double(i), unit: unit)

XCTAssertEqual(Measurement(value: Double(i + j), unit: unit), mi.adding(mj), "\(NSMeasurement(doubleValue: Double(i + j), unit: unit)) == \(mi)+\(mj)")
XCTAssertEqual(Measurement(value: Double(i - j), unit: unit), mi.subtracting(mj), "\(NSMeasurement(doubleValue: Double(i - j), unit: unit)) == \(mi)-\(mj)")
}
}
}

let s1 = "a"
let s2 = "ab"

let u1 = Unit(symbol: s1)
let u2 = Unit(symbol: s2)

testAddSub(for: u1)
testAddSub(for: u2)

let uc1 = UnitConverterLinear(coefficient: 1, constant: 2)
let uc2 = UnitConverterLinear(coefficient: 1, constant: 3)

let d1 = Dimension(symbol: s1, converter: uc1)
let d2 = Dimension(symbol: s1, converter: uc1)
let d3 = Dimension(symbol: s2, converter: uc1)
let d4 = Dimension(symbol: s1, converter: uc2)

testAddSub(for: d1)
testAddSub(for: d2)
testAddSub(for: d3)
testAddSub(for: d4)
}

func test_conversion_dimensions() {
let u1 = UnitLength.inches
let u2 = UnitLength.centimeters
let u3 = UnitLength.baseUnit()
let u4 = UnitArea.baseUnit()

let m1 = NSMeasurement(doubleValue: 1, unit: u1)
let m2 = NSMeasurement(doubleValue: 1, unit: u2)
let m3 = NSMeasurement(doubleValue: 1, unit: u3)
let m4 = NSMeasurement(doubleValue: 1, unit: u4)

XCTAssertTrue(m1.canBeConverted(to: u1))
XCTAssertTrue(m2.canBeConverted(to: u2))
XCTAssertTrue(m3.canBeConverted(to: u3))
XCTAssertTrue(m4.canBeConverted(to: u4))

XCTAssertTrue(m1.canBeConverted(to: u2))
XCTAssertTrue(m1.canBeConverted(to: u3))
XCTAssertFalse(m1.canBeConverted(to: u4))

XCTAssertTrue(m2.canBeConverted(to: u1))
XCTAssertTrue(m2.canBeConverted(to: u3))
XCTAssertFalse(m2.canBeConverted(to: u4))

XCTAssertTrue(m3.canBeConverted(to: u1))
XCTAssertTrue(m3.canBeConverted(to: u2))
XCTAssertFalse(m3.canBeConverted(to: u4))

XCTAssertFalse(m4.canBeConverted(to: u1))
XCTAssertFalse(m4.canBeConverted(to: u2))
XCTAssertFalse(m4.canBeConverted(to: u3))

XCTAssertEqual(m1.converting(to: u1), m1._bridgeToSwift())
XCTAssertEqual(m2.converting(to: u2), m2._bridgeToSwift())
XCTAssertEqual(m3.converting(to: u3), m3._bridgeToSwift())
XCTAssertEqual(m4.converting(to: u4), m4._bridgeToSwift())

// XCTAssertEqual(m1.converting(to: u2), /*TODO*/)
// XCTAssertEqual(m1.converting(to: u3), /*TODO*/)
// XCTAssertEqual(m2.converting(to: u1), /*TODO*/)
// XCTAssertEqual(m2.converting(to: u3), /*TODO*/)
// XCTAssertEqual(m3.converting(to: u1), /*TODO*/)
// XCTAssertEqual(m3.converting(to: u2), /*TODO*/)

// What function should this be calling?
// AssertCrash() {
// m1.converting(to: u4)
// }
}

class Unit1: Unit {
convenience init() {
self.init(symbol: "a")
}
required init(symbol: String) {
super.init(symbol)
}
required public init?(coder aDecoder: Foundation.NSCoder)
return nil
}
}
// class Unit2: Unit1 {
// convenience init() {
// super.init()
// }
// required init(symbol: String) {
// super.init(symbol)
// }
// required public init?(coder aDecoder: Foundation.NSCoder)
// return nil
// }
// }
class Unit3: Unit {
init() {
self.init(symbol: "ab")
}
required init(symbol: String) {
super.init(symbol)
}
required public init?(coder aDecoder: Foundation.NSCoder)
return nil
}
}

func test_conversion_units() {

let u1 = Unit1()
let u2 = Unit1()
let u3 = Unit3()

let m1 = NSMeasurement(doubleValue: 1, unit: u1)
let m2 = NSMeasurement(doubleValue: 1, unit: u2)
let m3 = NSMeasurement(doubleValue: 1, unit: u3)

XCTAssertTrue(m1.canBeConverted(to: u1))
XCTAssertTrue(m2.canBeConverted(to: u2))
XCTAssertTrue(m1.canBeConverted(to: u2))
XCTAssertTrue(m2.canBeConverted(to: u1))

XCTAssertFalse(m1.canBeConverted(to: u3))
XCTAssertFalse(m2.canBeConverted(to: u3))
XCTAssertFalse(m3.canBeConverted(to: u1))
XCTAssertFalse(m3.canBeConverted(to: u2))

XCTAssertEqual(m1.converting(to: u1), m1._bridgeToSwift())
XCTAssertEqual(m2.converting(to: u2), m2._bridgeToSwift())

// What function should this be calling?
// AssertCrash() {
// m1.converting(to: u3)
// }
}
}
Loading