Skip to content

SR-11766: Fix coefficients for UnitVolume #2561

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

Merged
merged 3 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
5BF9B8001FABD5DA00EE1A7C /* CFBundle_Main.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BF9B7F41FABD5D300EE1A7C /* CFBundle_Main.c */; };
5BF9B8011FABD5DA00EE1A7C /* CFBundle_ResourceFork.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BF9B7F61FABD5D400EE1A7C /* CFBundle_ResourceFork.c */; };
5BF9B8021FABD5DA00EE1A7C /* CFBundle_Tables.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BF9B7F71FABD5D400EE1A7C /* CFBundle_Tables.c */; };
5D0E1BDB237A1FE800C35C5A /* TestUnitVolume.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D0E1BDA237A1FE800C35C5A /* TestUnitVolume.swift */; };
5FE52C951D147D1C00F7D270 /* TestNSTextCheckingResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE52C941D147D1C00F7D270 /* TestNSTextCheckingResult.swift */; };
616068F3225DE5C2004FCC54 /* FTPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616068F2225DE5C2004FCC54 /* FTPServer.swift */; };
616068F5225DE606004FCC54 /* TestURLSessionFTP.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616068F4225DE606004FCC54 /* TestURLSessionFTP.swift */; };
Expand Down Expand Up @@ -1018,6 +1019,7 @@
5BF9B7F61FABD5D400EE1A7C /* CFBundle_ResourceFork.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFBundle_ResourceFork.c; sourceTree = "<group>"; };
5BF9B7F71FABD5D400EE1A7C /* CFBundle_Tables.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFBundle_Tables.c; sourceTree = "<group>"; };
5BF9B7F81FABD5D500EE1A7C /* CFBundle_Executable.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFBundle_Executable.c; sourceTree = "<group>"; };
5D0E1BDA237A1FE800C35C5A /* TestUnitVolume.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUnitVolume.swift; sourceTree = "<group>"; };
5E5835F31C20C9B500C81317 /* TestThread.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestThread.swift; sourceTree = "<group>"; };
5EB6A15C1C188FC40037DCB8 /* TestJSONSerialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestJSONSerialization.swift; sourceTree = "<group>"; };
5EF673AB1C28B527006212A3 /* TestNotificationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNotificationQueue.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1899,6 +1901,7 @@
5B6F17961C48631C00935030 /* TestUtils.swift */,
03B6F5831F15F339004F25AF /* TestURLProtocol.swift */,
3E55A2321F52463B00082000 /* TestUnit.swift */,
5D0E1BDA237A1FE800C35C5A /* TestUnitVolume.swift */,
7D8BD738225ED1480057CF37 /* TestMeasurement.swift */,
);
name = Tests;
Expand Down Expand Up @@ -3096,6 +3099,7 @@
D4FE895B1D703D1100DA7986 /* TestURLRequest.swift in Sources */,
684C79011F62B611005BD73E /* TestNSNumberBridging.swift in Sources */,
DAA79BD920D42C07004AF044 /* TestURLProtectionSpace.swift in Sources */,
5D0E1BDB237A1FE800C35C5A /* TestUnitVolume.swift in Sources */,
616068F5225DE606004FCC54 /* TestURLSessionFTP.swift in Sources */,
155B77AC22E63D2D00D901DE /* TestURLCredentialStorage.swift in Sources */,
B951B5EC1F4E2A2000D8B332 /* TestNSLock.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Foundation/Unit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,8 @@ public final class UnitVolume : Dimension {
static let cubicKilometers = 1e12
static let cubicMeters = 1000.0
static let cubicDecimeters = 1.0
static let cubicCentimeters = 0.01
static let cubicMillimeters = 0.001
static let cubicCentimeters = 1e-3
static let cubicMillimeters = 1e-6
static let cubicInches = 0.0163871
static let cubicFeet = 28.3168
static let cubicYards = 764.555
Expand Down
1 change: 1 addition & 0 deletions TestFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ target_sources(TestFoundation PRIVATE
TestTimer.swift
TestTimeZone.swift
TestUnitConverter.swift
TestUnitVolume.swift
TestUnit.swift
TestURLCache.swift
TestURLCredential.swift
Expand Down
76 changes: 76 additions & 0 deletions TestFoundation/TestUnitVolume.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//

class TestUnitVolume: XCTestCase {
func testMetricVolumeConversions() {
let cubicKilometers = Measurement(value: 4, unit: UnitVolume.cubicKilometers)
XCTAssertEqual(cubicKilometers, Measurement(value: 4e9, unit: UnitVolume.cubicMeters), "Conversion from cubicKilometers to cubicMeters")

let megaliters = Measurement(value: 3, unit: UnitVolume.megaliters)
XCTAssertEqual(megaliters, Measurement(value: 3_000_000, unit: UnitVolume.liters), "Conversion from megaliters to liters")

let kiloliters = Measurement(value: 2, unit: UnitVolume.kiloliters)
XCTAssertEqual(kiloliters, Measurement(value: 2000, unit: UnitVolume.liters), "Conversion from kiloliters to liters")

let cubicMeters = Measurement(value: 2, unit: UnitVolume.cubicMeters)
XCTAssertEqual(cubicMeters, Measurement(value: 2000, unit: UnitVolume.liters), "Conversion from cubicMeters to liters")
XCTAssertEqual(kiloliters, cubicMeters, "Conversion from kiloliters to cubicMeters")

let liters = Measurement(value: 5, unit: UnitVolume.liters)
XCTAssertEqual(liters, Measurement(value: 5, unit: UnitVolume.cubicDecimeters), "Conversion from liters to cubicDecimeters")
XCTAssertEqual(liters, Measurement(value: 50, unit: UnitVolume.deciliters), "Conversion from liters to deciliters")
XCTAssertEqual(liters, Measurement(value: 500, unit: UnitVolume.centiliters), "Conversion from liters to centiliters")
XCTAssertEqual(liters, Measurement(value: 5000, unit: UnitVolume.milliliters), "Conversion from liters to milliliters")
XCTAssertEqual(liters, Measurement(value: 5000, unit: UnitVolume.cubicCentimeters), "Conversion from liters to cubicCentimeters")
XCTAssertEqual(liters, Measurement(value: 5e6, unit: UnitVolume.cubicMillimeters), "Conversion from liters to cubicMillimeters")
}

func testMetricToImperialVolumeConversion() {
let liters = Measurement(value: 10, unit: UnitVolume.liters)
XCTAssertEqual(liters.converted(to: .cubicInches).value, 610.236, accuracy: 0.001, "Conversion from liters to cubicInches")
}

func testImperialVolumeConversions() {
let cubicMiles = Measurement(value: 1, unit: UnitVolume.cubicMiles)
XCTAssertEqual(cubicMiles.converted(to: .cubicYards).value, 1760 * 1760 * 1760, accuracy: 1_000_000, "Conversion from cubicMiles to cubicYards")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Admittedly, this assertion with the insanely high accuracy threshold is kind of ridiculous. I wanted the test to be intuitive, so for people who know that 1 mile = 1760 yards, it makes sense that 1 cubic mile = 1760^3 cubic yards.

Unfortunately, that's not really the case with Measurement (even after factoring in floating-point rounding errors) because the conversion factors hardcoded by Foundation often don't use the full range offered by Double. In this instance, cubicMiles is defined like this (in Unit.swift):

static let cubicMiles           = 4.168e+12 // in liters

Whereas the "accurate" conversion factor is more like 4.16818182544058e12 (= 16093.44^3; 1 mile is 16093.44 decimeters) (I think), which leads to the huge discrepancy (in absolute terms) between the actual and expected values.

This inaccuracy is not specific to corelibs-foundation, but is the same in Darwin-Foundation. Out of interest, was it a conscious decision to limit conversion factors to x significant digits or something when the Measurement types were introduced?

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as scl-foundation is concerned it just tries to match Darwin's version as closely as possible, I don't know why Foundation has such low accuracy for its conversions.

The tests should hopefully catch any accidental changes in the future so thanks for adding them, Ive kicked off a CI test for them on Linux and will merge if the tests work ok.


let cubicYards = Measurement(value: 1, unit: UnitVolume.cubicYards)
XCTAssertEqual(cubicYards.converted(to: .cubicFeet).value, 27, accuracy: 0.001, "Conversion from cubicYards to cubicFeet")

let cubicFeet = Measurement(value: 1, unit: UnitVolume.cubicFeet)
XCTAssertEqual(cubicFeet.converted(to: .cubicInches).value, 1728, accuracy: 0.01, "Conversion from cubicFeet to cubicInches")

let gallons = Measurement(value: 1, unit: UnitVolume.gallons)
XCTAssertEqual(gallons.converted(to: .quarts).value, 4, accuracy: 0.001, "Conversion from gallons to quarts")

let quarts = Measurement(value: 1, unit: UnitVolume.quarts)
XCTAssertEqual(quarts.converted(to: .pints).value, 2, accuracy: 0.001, "Conversion from quarts to pints")

let pints = Measurement(value: 1, unit: UnitVolume.pints)
XCTAssertEqual(pints.converted(to: .cups).value, 2, accuracy: 0.05, "Conversion from pints to cups")

let cups = Measurement(value: 1, unit: UnitVolume.cups)
XCTAssertEqual(cups.converted(to: .fluidOunces).value, 8.12, accuracy: 0.01, "Conversion from cups to fluidOunces")

let fluidOunces = Measurement(value: 1, unit: UnitVolume.fluidOunces)
XCTAssertEqual(fluidOunces.converted(to: .tablespoons).value, 2, accuracy: 0.001, "Conversion from fluidOunces to tablespoons")

let tablespoons = Measurement(value: 1, unit: UnitVolume.tablespoons)
XCTAssertEqual(tablespoons.converted(to: .teaspoons).value, 3, accuracy: 0.001, "Conversion from tablespoons to teaspoons")

let teaspoons = Measurement(value: 1, unit: UnitVolume.teaspoons)
XCTAssertEqual(teaspoons.converted(to: .cubicInches).value, 0.3, accuracy: 0.001, "Conversion from teaspoons to cubicInches")
}

static let allTests = [
("testMetricVolumeConversions", testMetricVolumeConversions),
("testMetricToImperialVolumeConversion", testMetricToImperialVolumeConversion),
("testImperialVolumeConversions", testImperialVolumeConversions),
]
}
1 change: 1 addition & 0 deletions TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var allTestCases = [
testCase(TestUnit.allTests),
testCase(TestDimension.allTests),
testCase(TestMeasurement.allTests),
testCase(TestUnitVolume.allTests),
testCase(TestNSLock.allTests),
testCase(TestNSSortDescriptor.allTests),
]
Expand Down