Skip to content

Commit

Permalink
Add Temp/Reaumur conversion, also looks like power was not yet add, s…
Browse files Browse the repository at this point in the history
…o now added.
  • Loading branch information
putridparrot committed Jan 27, 2024
1 parent 0f98606 commit a72e921
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"image": "swift",
"forwardPorts": [3000]
}
6 changes: 6 additions & 0 deletions Build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
If running on Windows, open as devcontainer

# Build

swift build
swift test
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ Each unit of measure then includes functions to convert to each for example, con
* Stones (stone)
* Tonnes (tonne)

### Power

* Mechanical Horse Power (hp)
* Metric Horse Power (ps)

### Pressure

* Atmospheres (atm)
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftUnits/Power.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <auto-generated>
// This code was generated by the UnitCodeGenerator tool
//
// Changes to this file will be lost if the code is regenerated
// </auto-generated>

public final class Power {
@available(*, unavailable) private init() {}

public final class MechanicalHorsePower {
@available(*, unavailable) private init() {}

public static func toMetricHorsePower(_ value : Double) -> Double {
return value * 1.013869665424;
}
}

public final class MetricHorsePower {
@available(*, unavailable) private init() {}

public static func toMechanicalHorsePower(_ value : Double) -> Double {
return value / 1.013869665424;
}
}
}
29 changes: 29 additions & 0 deletions Sources/SwiftUnits/Temperature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public final class Temperature {
public static func toRankine(_ value : Double) -> Double {
return value * 9.0/5.0 + 491.67;
}
public static func toReaumur(_ value : Double) -> Double {
return value / 1.25;
}
}

public final class Fahrenheit {
Expand All @@ -33,6 +36,9 @@ public final class Temperature {
public static func toRankine(_ value : Double) -> Double {
return value + 459.67;
}
public static func toReaumur(_ value : Double) -> Double {
return (value - 32.0) / 2.25;
}
}

public final class Kelvin {
Expand All @@ -47,6 +53,9 @@ public final class Temperature {
public static func toRankine(_ value : Double) -> Double {
return value * 1.8;
}
public static func toReaumur(_ value : Double) -> Double {
return (value - 273.15) / 1.25;
}
}

public final class Rankine {
Expand All @@ -61,5 +70,25 @@ public final class Temperature {
public static func toKelvin(_ value : Double) -> Double {
return value / 1.8;
}
public static func toReaumur(_ value : Double) -> Double {
return (value - 491.67) / 2.25;
}
}

public final class Reaumur {
@available(*, unavailable) private init() {}

public static func toKelvin(_ value : Double) -> Double {
return value * 1.25 + 273.15;
}
public static func toCelsius(_ value : Double) -> Double {
return value * 1.25;
}
public static func toFahrenheit(_ value : Double) -> Double {
return value * 2.25 + 32.0;
}
public static func toRankine(_ value : Double) -> Double {
return value * 2.25 + 491.67;
}
}
}
45 changes: 45 additions & 0 deletions Tests/SwiftUnitsTests/PowerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <auto-generated>
// This code was generated by the UnitCodeGenerator tool
//
// Changes to this file will be lost if the code is regenerated
// </auto-generated>

import XCTest
@testable import SwiftUnits

final class MechanicalHorsePowerTests: XCTestCase {
func testConvertKnownMechanicalHorsePowerToMetricHorsePower_1() throws {
let result = Power.MechanicalHorsePower.toMetricHorsePower(65.0971)
XCTAssertEqual(65.9999749, result, accuracy: 0.01)
}

func testConvertKnownMechanicalHorsePowerToMetricHorsePower_2() throws {
let result = Power.MechanicalHorsePower.toMetricHorsePower(121.317)
XCTAssertEqual(122.9996, result, accuracy: 0.01)
}

func testConvertKnownMechanicalHorsePowerToMetricHorsePower_3() throws {
let result = Power.MechanicalHorsePower.toMetricHorsePower(86.7962)
XCTAssertEqual(88.0, result, accuracy: 0.01)
}

}

final class MetricHorsePowerTests: XCTestCase {
func testConvertKnownMetricHorsePowerToMechanicalHorsePower_1() throws {
let result = Power.MetricHorsePower.toMechanicalHorsePower(126.734)
XCTAssertEqual(125.0, result, accuracy: 0.01)
}

func testConvertKnownMetricHorsePowerToMechanicalHorsePower_2() throws {
let result = Power.MetricHorsePower.toMechanicalHorsePower(91.2483)
XCTAssertEqual(90.0, result, accuracy: 0.01)
}

func testConvertKnownMetricHorsePowerToMechanicalHorsePower_3() throws {
let result = Power.MetricHorsePower.toMechanicalHorsePower(425.825)
XCTAssertEqual(419.9997, result, accuracy: 0.01)
}

}

123 changes: 123 additions & 0 deletions Tests/SwiftUnitsTests/TemperatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ final class CelsiusTests: XCTestCase {
XCTAssertEqual(486.27, result, accuracy: 0.01)
}

func testConvertKnownCelsiusToReaumur_1() throws {
let result = Temperature.Celsius.toReaumur(123.0)
XCTAssertEqual(98.4, result, accuracy: 0.01)
}

func testConvertKnownCelsiusToReaumur_2() throws {
let result = Temperature.Celsius.toReaumur(15.67)
XCTAssertEqual(12.536, result, accuracy: 0.01)
}

func testConvertKnownCelsiusToReaumur_3() throws {
let result = Temperature.Celsius.toReaumur(87.0)
XCTAssertEqual(69.6, result, accuracy: 0.01)
}

}

final class FahrenheitTests: XCTestCase {
Expand Down Expand Up @@ -101,6 +116,21 @@ final class FahrenheitTests: XCTestCase {
XCTAssertEqual(459.87, result, accuracy: 0.01)
}

func testConvertKnownFahrenheitToReaumur_1() throws {
let result = Temperature.Fahrenheit.toReaumur(67.0)
XCTAssertEqual(15.555555556, result, accuracy: 0.01)
}

func testConvertKnownFahrenheitToReaumur_2() throws {
let result = Temperature.Fahrenheit.toReaumur(1.6)
XCTAssertEqual(-13.511111111, result, accuracy: 0.01)
}

func testConvertKnownFahrenheitToReaumur_3() throws {
let result = Temperature.Fahrenheit.toReaumur(900.0)
XCTAssertEqual(385.77777778, result, accuracy: 0.01)
}

}

final class KelvinTests: XCTestCase {
Expand Down Expand Up @@ -149,6 +179,21 @@ final class KelvinTests: XCTestCase {
XCTAssertEqual(1.44, result, accuracy: 0.01)
}

func testConvertKnownKelvinToReaumur_1() throws {
let result = Temperature.Kelvin.toReaumur(900.0)
XCTAssertEqual(501.48, result, accuracy: 0.01)
}

func testConvertKnownKelvinToReaumur_2() throws {
let result = Temperature.Kelvin.toReaumur(1.3)
XCTAssertEqual(-217.48, result, accuracy: 0.01)
}

func testConvertKnownKelvinToReaumur_3() throws {
let result = Temperature.Kelvin.toReaumur(60.0)
XCTAssertEqual(-170.52, result, accuracy: 0.01)
}

}

final class RankineTests: XCTestCase {
Expand Down Expand Up @@ -197,5 +242,83 @@ final class RankineTests: XCTestCase {
XCTAssertEqual(12.777777778, result, accuracy: 0.01)
}

func testConvertKnownRankineToReaumur_1() throws {
let result = Temperature.Rankine.toReaumur(900.0)
XCTAssertEqual(181.48, result, accuracy: 0.01)
}

func testConvertKnownRankineToReaumur_2() throws {
let result = Temperature.Rankine.toReaumur(34.9)
XCTAssertEqual(-203.00888889, result, accuracy: 0.01)
}

func testConvertKnownRankineToReaumur_3() throws {
let result = Temperature.Rankine.toReaumur(0.7)
XCTAssertEqual(-218.20888889, result, accuracy: 0.01)
}

}

final class ReaumurTests: XCTestCase {
func testConvertKnownReaumurToKelvin_1() throws {
let result = Temperature.Reaumur.toKelvin(128.0)
XCTAssertEqual(433.15, result, accuracy: 0.01)
}

func testConvertKnownReaumurToKelvin_2() throws {
let result = Temperature.Reaumur.toKelvin(7.4)
XCTAssertEqual(282.4, result, accuracy: 0.01)
}

func testConvertKnownReaumurToKelvin_3() throws {
let result = Temperature.Reaumur.toKelvin(1.5)
XCTAssertEqual(275.025, result, accuracy: 0.01)
}

func testConvertKnownReaumurToCelsius_1() throws {
let result = Temperature.Reaumur.toCelsius(1.5)
XCTAssertEqual(1.875, result, accuracy: 0.01)
}

func testConvertKnownReaumurToCelsius_2() throws {
let result = Temperature.Reaumur.toCelsius(23.9)
XCTAssertEqual(29.875, result, accuracy: 0.01)
}

func testConvertKnownReaumurToCelsius_3() throws {
let result = Temperature.Reaumur.toCelsius(0.3)
XCTAssertEqual(0.375, result, accuracy: 0.01)
}

func testConvertKnownReaumurToFahrenheit_1() throws {
let result = Temperature.Reaumur.toFahrenheit(0.3)
XCTAssertEqual(32.675, result, accuracy: 0.01)
}

func testConvertKnownReaumurToFahrenheit_2() throws {
let result = Temperature.Reaumur.toFahrenheit(87.0)
XCTAssertEqual(227.75, result, accuracy: 0.01)
}

func testConvertKnownReaumurToFahrenheit_3() throws {
let result = Temperature.Reaumur.toFahrenheit(34.1)
XCTAssertEqual(108.725, result, accuracy: 0.01)
}

func testConvertKnownReaumurToRankine_1() throws {
let result = Temperature.Reaumur.toRankine(34.1)
XCTAssertEqual(568.395, result, accuracy: 0.01)
}

func testConvertKnownReaumurToRankine_2() throws {
let result = Temperature.Reaumur.toRankine(10.6)
XCTAssertEqual(515.52, result, accuracy: 0.01)
}

func testConvertKnownReaumurToRankine_3() throws {
let result = Temperature.Reaumur.toRankine(1.9)
XCTAssertEqual(495.945, result, accuracy: 0.01)
}

}

0 comments on commit a72e921

Please sign in to comment.