-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
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), | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 byDouble
. In this instance,cubicMiles
is defined like this (inUnit.swift
):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?
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.
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.