-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement NSMeasurement #1425
Changes from all commits
3a135ea
5346f25
da6abf6
04de52e
902e1a9
ccfeece
fa8fe19
2f4e5a1
fff371d
8deb185
f34df89
0b2f8d5
a6a7651
1e50a23
2f195f0
6161d7b
e9986ee
0f084a8
b4bc5a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: otherUnit))") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: space after |
||
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()`? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does Darwin Foundation do? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
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) | ||
// } | ||
} | ||
} |
There was a problem hiding this comment.
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.