Skip to content

Commit

Permalink
Fixes #3
Browse files Browse the repository at this point in the history
- Renamed Unit -> Units to avoid confusion with Kotlin Unit
  • Loading branch information
pusolito committed Jun 15, 2020
1 parent 26ab776 commit 117e546
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 319 deletions.
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/io/nacular/measured/units/Angle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package io.nacular.measured.units
import kotlin.math.PI

/**
* Unit to measure geometric angles.
* Units to measure geometric angles.
*/
open class Angle(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
open class Angle(suffix: String, ratio: Double = 1.0): Units(suffix, ratio) {
operator fun div(other: Angle) = ratio / other.ratio

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.nacular.measured.units

/**
* Unit for measuring graphics display distances (i.e. distances on the surface of a display).
* Units for measuring graphics display distances (i.e. distances on the surface of a display).
*/
open class GraphicsLength(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
open class GraphicsLength(suffix: String, ratio: Double = 1.0): Units(suffix, ratio) {
operator fun div(other: GraphicsLength) = ratio / other.ratio

companion object {
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/io/nacular/measured/units/Length.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.nacular.measured.units
/**
* Units for length or distance.
*/
open class Length(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
open class Length(suffix: String, ratio: Double = 1.0): Units(suffix, ratio) {
operator fun div(other: Length) = ratio / other.ratio

companion object {
Expand All @@ -19,6 +19,6 @@ open class Length(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
* Sort Length before Time which is conventional.
*/
operator fun Time.times(other: Length) = other * this
operator fun Measure<Time>.times(other: Length) = amount * other * unit
operator fun Measure<Time>.times(other: Length) = amount * other * units

typealias Distance = Length
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/io/nacular/measured/units/Mass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.nacular.measured.units
/**
* Units to measure how much matter is in an object.
*/
open class Mass(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
open class Mass(suffix: String, ratio: Double = 1.0): Units(suffix, ratio) {
operator fun div(other: Mass) = ratio / other.ratio

companion object {
Expand All @@ -13,4 +13,4 @@ open class Mass(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
}

operator fun Length.times(mass: Mass) = mass * this
operator fun Measure<Length>.times(mass: Mass) = amount * (unit * mass)
operator fun Measure<Length>.times(mass: Mass) = amount * (units * mass)
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/io/nacular/measured/units/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.nacular.measured.units
/**
* Units to measure time durations.
*/
open class Time(suffix: String, ratio: Double = 1.0): Unit(suffix, ratio) {
open class Time(suffix: String, ratio: Double = 1.0): Units(suffix, ratio) {
operator fun div(other: Time) = ratio / other.ratio

companion object {
Expand Down
267 changes: 0 additions & 267 deletions src/commonMain/kotlin/io/nacular/measured/units/Unit.kt

This file was deleted.

267 changes: 267 additions & 0 deletions src/commonMain/kotlin/io/nacular/measured/units/Units.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.expect
* Created by Nicholas Eddy on 2/22/18.
*/

fun testUnit(units: Map<Unit, Map<Double, Unit>>) {
fun testUnit(units: Map<Units, Map<Double, Units>>) {
units.forEach { (unit, mappings) ->
mappings.forEach { (multiplier, target) ->
val first = 1 * unit
Expand Down
84 changes: 42 additions & 42 deletions src/commonTest/kotlin/io/nacular/measured/units/UnitTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,48 @@ import kotlin.test.expect
* Created by Nicholas Eddy on 2/18/18.
*/

private class UnitA: Unit {
private class UnitsA: Units {
constructor(suffix: String ): super(suffix )
constructor(suffix: String, ratio: Double): super(suffix, ratio)

operator fun div(other: UnitA) = ratio / other.ratio
operator fun div(other: UnitsA) = ratio / other.ratio
}

private class UnitB: Unit {
private class UnitsB: Units {
constructor(suffix: String ): super(suffix )
constructor(suffix: String, ratio: Double): super(suffix, ratio)

operator fun div(other: UnitA) = ratio / other.ratio
operator fun div(other: UnitsA) = ratio / other.ratio
}

class UnitTests {
@Test @JsName("defaultRatioIs1")
fun `default ratio is 1`() {
val a = object: Unit("a") {}
val a = object: Units("a") {}

expect(1.0, "$a.ratio") { a.ratio }
}

@Test @JsName("divWorks")
fun `div works`() {
val a = UnitA("a", 10.0)
val b = UnitA("b", 1.0)
val a = UnitsA("a", 10.0)
val b = UnitsA("b", 1.0)

expect(10.0, "$a / $b") { a / b }
}

@Test @JsName("toStringWorks")
fun `toString works`() {
val a = object: Unit("description", 10.0) {}
val a = object: Units("description", 10.0) {}

expect("description", "$a.toString()") { a.toString() }
}

@Test @JsName("comparisonsWork")
fun `comparisons work`() {
val a = UnitA("a", 10.0)
val b = UnitA("b", 1.0)
val c = UnitA("a", 10.0)
val a = UnitsA("a", 10.0)
val b = UnitsA("b", 1.0)
val c = UnitsA("a", 10.0)

expect(true, "$a > $b" ) { a > b }
expect(false, "$a < $b" ) { a < b }
Expand All @@ -64,63 +64,63 @@ class UnitTests {
class UnitRatioTests {
@Test @JsName("defaultMultiplierIs1")
fun `default ratio is 1`() {
val a = UnitRatio(UnitA("a"), UnitB("b"))
val a = UnitsRatio(UnitsA("a"), UnitsB("b"))

expect(1.0, "$a.ratio") { a.ratio }
}

@Test @JsName("doubleReciprocal")
fun `1 ÷ (1 ÷ a) == a`() {
val a = UnitRatio(UnitA("a"), UnitB("b"))
val a = UnitsRatio(UnitsA("a"), UnitsB("b"))

expect(a, "$a.reciprocal.reciprocal") { a.reciprocal.reciprocal }
}

// @Test @JsName("divAWorks")
// fun `(A÷B) ÷ A = 1÷B`() {
// val a = UnitRatio(UnitA("a", 10.0), UnitB("b"))
// val b = UnitB("a", 10.0)
// val a = UnitsRatio(UnitsA("a", 10.0), UnitsB("b"))
// val b = UnitsB("a", 10.0)
//
// expect(1 * Unit("b"), "$a / $b") { a / b }
// expect(1 * Units("b"), "$a / $b") { a / b }
// }

@Test @JsName("timesBWorks")
fun `(A÷B) * B = A`() {
val a = UnitRatio(UnitA("a", 10.0), UnitB("b"))
val b = UnitB("b", 1.0)
val a = UnitsRatio(UnitsA("a", 10.0), UnitsB("b"))
val b = UnitsB("b", 1.0)

expect(1 * UnitA("a", 10.0), "$a * $b") { a * b }
expect(1 * UnitsA("a", 10.0), "$a * $b") { a * b }
}

@Test @JsName("timesInverseWorks")
fun `(A÷B) * (B÷A) = 1`() {
val a = UnitRatio(UnitA("a", 10.0), UnitB("b"))
val a = UnitsRatio(UnitsA("a", 10.0), UnitsB("b"))

expect(1.0, "$a * 1/$a") { a * a.reciprocal }
}

@Test @JsName("divSelfWorks")
fun `(A÷B) ÷ (A÷B) = 1`() {
val a = UnitRatio(UnitA("a", 10.0), UnitB("b"))
val a = UnitsRatio(UnitsA("a", 10.0), UnitsB("b"))

expect(1.0, "$a / $a") { a / a }
}

@Test @JsName("toStringWorks")
fun `toString works`() {
val a = UnitA("a", 10.0)
val b = UnitB("b" )
val ratio = UnitRatio(a, b)
val a = UnitsA("a", 10.0)
val b = UnitsB("b" )
val ratio = UnitsRatio(a, b)

ratio.let { expect("$a/$b", "$it.toString()") { it.toString() } }
ratio.reciprocal.let { expect("$b/$a", "$it.toString()") { it.toString() } }
}

@Test @JsName("comparisonsWork")
fun `comparisons work`() {
val a = UnitRatio(UnitA("a1", 10.0), UnitB("b"))
val b = UnitRatio(UnitA("a2", 1.0), UnitB("b"))
val c = UnitRatio(UnitA("a1", 10.0), UnitB("b"))
val a = UnitsRatio(UnitsA("a1", 10.0), UnitsB("b"))
val b = UnitsRatio(UnitsA("a2", 1.0), UnitsB("b"))
val c = UnitsRatio(UnitsA("a1", 10.0), UnitsB("b"))

expect(true, "$a > $b" ) { a > b }
expect(false, "$a < $b" ) { a < b }
Expand All @@ -133,8 +133,8 @@ class UnitRatioTests {
class MeasureTests {
@Test @JsName("zeroWorks")
fun `zero works`() {
val unitA = UnitA("a", 10.0)
val unitB = UnitA("b", 1.0)
val unitA = UnitsA("a", 10.0)
val unitB = UnitsA("b", 1.0)

val zero = 0 * unitA
val measureA = Measure(10.0, unitA)
Expand All @@ -150,9 +150,9 @@ class MeasureTests {

@Test @JsName("comparisonsWork")
fun `comparisons work`() {
val unitA = UnitA("a", 10.0)
val unitB = UnitA("b", 1.0)
val unitC = UnitA("c" )
val unitA = UnitsA("a", 10.0)
val unitB = UnitsA("b", 1.0)
val unitC = UnitsA("c" )

val measureA = Measure(10.0, unitA)
val measureB = Measure(10.0, unitB)
Expand All @@ -170,8 +170,8 @@ class MeasureTests {

@Test @JsName("plusMinusOperatorsWork")
fun `+ -`() {
val unitA = UnitA("a", 10.0)
val unitB = UnitA("b", 1.0)
val unitA = UnitsA("a", 10.0)
val unitB = UnitsA("b", 1.0)

val measureA = Measure(10.0, unitA)
val measureB = Measure(10.0, unitB)
Expand All @@ -182,7 +182,7 @@ class MeasureTests {

@Test @JsName("unaryMinusOperatorsWork")
fun `unary -`() {
val unit = UnitA("a")
val unit = UnitsA("a")

val measure = Measure(10.0, unit)

Expand All @@ -191,8 +191,8 @@ class MeasureTests {

@Test @JsName("timesDivideOperatorsWork")
fun `* ÷`() {
val op: (Operation<UnitA>) -> kotlin.Unit = {
val unit = UnitA("a")
val op: (Operation<UnitsA>) -> kotlin.Unit = {
val unit = UnitsA("a")
val start = 10.0
val value = 2.3
val measure = Measure(start, unit)
Expand All @@ -204,19 +204,19 @@ class MeasureTests {
}
}

interface Operation<T: Unit> {
interface Operation<T: Units> {
operator fun invoke(first: Double, second: Double): Double
operator fun invoke(first: Measure<T>, second: Double): Measure<T>
}

private val times = object:
Operation<UnitA> {
Operation<UnitsA> {
override fun invoke(first: Double, second: Double) = first * second
override fun invoke(first: Measure<UnitA>, second: Double) = first * second
override fun invoke(first: Measure<UnitsA>, second: Double) = first * second
}

private val divide = object:
Operation<UnitA> {
Operation<UnitsA> {
override fun invoke(first: Double, second: Double) = first / second
override fun invoke(first: Measure<UnitA>, second: Double) = first / second
override fun invoke(first: Measure<UnitsA>, second: Double) = first / second
}

0 comments on commit 117e546

Please sign in to comment.