Source

units / units_test.go

package units

import (
	"testing"
)

var valueEquivTable = []struct {
	a Value
	b Value
}{
	{Value{2, Watt}, Value{2, Watt}},
	{Value{2, Watt}, Value{6, Joule}.Div(Value{3, Second})},
	{Value{10, Volt}, Value{2, Ampere}.Mul(Value{5, Ohm})},
	{Value{7, Pascal}, Value{2, Pascal}.Add(Value{5, Pascal})},
}

func TestValueMath(t *testing.T) {
	for i, equiv := range valueEquivTable {
		if equiv.a != equiv.b {
			t.Errorf("%s and %s not equal (case %d).", equiv.a, equiv.b, i)
		}
	}
}

var fmtEquivTable = []struct {
	a string
	b interface {
		String() string
	}
}{
	{"m/s", Meter.per(Second)},
	{"K", Kelvin},
	{"kg*m/s/s", Newton},
	{"kg/s/s/A", Tesla},
	{"", Unitless},

	{"4W", Value{4, Watt}},
	{"123T", Value{123, Tesla}},
	{"3.1m/s", Value{3.1, Meter.per(Second)}},
	{"1200kg*s", Value{1200, Kilogram.times(Second)}},
	{"1.21GW", Value{1.21e9, Watt}},
	{"20mm", Value{.02, Meter}},
	{"200ns", Value{200e-9, Second}},
	{"25mg", Value{25e-6, Kilogram}},
	{"4L", Value{.004, Kiloliter}},

	{"1e+36YJ", Value{1e60, Joule}},
	{"1e-36yJ", Value{1e-60, Joule}},
	{"0W", Value{0, Watt}},
}

func TestFmt(t *testing.T) {
	for i, equiv := range fmtEquivTable {
		if equiv.a != equiv.b.String() {
			t.Errorf("%q and %q not equal (case %d).", equiv.a, equiv.b, i)
		}
	}
}