Skip to content

Commit

Permalink
add power units
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmh committed Mar 16, 2023
1 parent b2bf4dc commit 46579e0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
40 changes: 21 additions & 19 deletions conversions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package units

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -3943,26 +3944,27 @@ var convTests = []conversionTest{
{"celsius", "kelvin", "274.15"},
}

func TestConversionValues(t *testing.T) {
func testConversions(t *testing.T, convTests []conversionTest) {
fmtOpts := FmtOptions{false, false, 6}
for _, cTest := range convTests {
// lookup both units by name
u1, err := Find(cTest.from)
if err != nil {
t.Errorf(err.Error())
continue
}

u2, err := Find(cTest.to)
if err != nil {
t.Errorf(err.Error())
continue
}

res := MustConvertFloat(1.0, u1, u2).Fmt(fmtOpts)
if res != cTest.val {
t.Errorf("%s -> %s conversion test failed", cTest.from, cTest.to)
t.Errorf("(got: %s, expected: %s)", res, cTest.val)
}
label := fmt.Sprintf("%s to %s conversion", cTest.from, cTest.to)
t.Run(label, func(t *testing.T) {
u1, err := Find(cTest.from)
if err != nil {
t.Fatal(err.Error())
}
u2, err := Find(cTest.to)
if err != nil {
t.Fatal(err.Error())
}
res := MustConvertFloat(1.0, u1, u2).Fmt(fmtOpts)
if res != cTest.val {
t.Errorf("(got: %s, expected: %s)", res, cTest.val)
}
})
}
}

func TestConversionValues(t *testing.T) {
testConversions(t, convTests)
}
27 changes: 27 additions & 0 deletions power_units.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package units

var (
Power = UnitOptionQuantity("power")

// metric
Watt = NewUnit("watt", "W", Power)
KiloWatt = Kilo(Watt)
MegaWatt = Mega(Watt)
GigaWatt = Giga(Watt)
TeraWatt = Tera(Watt)
PetaWatt = Peta(Watt)
ExaWatt = Exa(Watt)
ZettaWatt = Zetta(Watt)
YottaWatt = Yotta(Watt)
)

func init() {
NewRatioConversion(KiloWatt, Watt, 1000.0)
NewRatioConversion(MegaWatt, Watt, 1e6)
NewRatioConversion(GigaWatt, Watt, 1e9)
NewRatioConversion(TeraWatt, Watt, 1e12)
NewRatioConversion(PetaWatt, Watt, 1e15)
NewRatioConversion(ExaWatt, Watt, 1e18)
NewRatioConversion(ZettaWatt, Watt, 1e21)
NewRatioConversion(YottaWatt, Watt, 1e24)
}
20 changes: 20 additions & 0 deletions power_units_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package units

import (
"testing"
)

var powerConvTests = []conversionTest{
{from: "watt", to: "kilowatt", val: "0.001"},
{from: "watt", to: "megawatt", val: "0.000001"},
{from: "watt", to: "gigawatt", val: "0.000000001"},
{from: "watt", to: "terawatt", val: "0.000000000001"},
{from: "watt", to: "petawatt", val: "0.000000000000001"},
{from: "watt", to: "exawatt", val: "0.000000000000000001"},
{from: "watt", to: "zettawatt", val: "0.000000000000000000001"},
{from: "watt", to: "yottawatt", val: "0.000000000000000000000001"},
}

func TestPower(t *testing.T) {
testConversions(t, powerConvTests)
}

0 comments on commit 46579e0

Please sign in to comment.