2acbf51637
Co-authored-by: Zachary Levy <zachary@sunforge.is> Reviewed-on: #35
102 lines
2.7 KiB
Odin
102 lines
2.7 KiB
Odin
package quantity
|
|
|
|
import "base:intrinsics"
|
|
|
|
LITERS_PER_GALLON :: 3.785411784
|
|
|
|
//----- Liters ----------------------------------
|
|
Liters :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
|
|
v: V,
|
|
}
|
|
|
|
@(private = "file")
|
|
liters_to_milli_liters :: #force_inline proc "contextless" (
|
|
liters: Liters($V),
|
|
) -> Milli_Liters(V) where intrinsics.type_is_numeric(V) {
|
|
return Milli_Liters(V){liters.v * MILLI}
|
|
}
|
|
|
|
@(private = "file")
|
|
liters_to_gallons :: #force_inline proc "contextless" (
|
|
liters: Liters($V),
|
|
) -> Gallons(V) where intrinsics.type_is_float(V) {
|
|
return Gallons(V){liters.v / LITERS_PER_GALLON}
|
|
}
|
|
|
|
//----- Milliliters ----------------------------------
|
|
Milli_Liters :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
|
|
v: V,
|
|
}
|
|
|
|
@(private = "file")
|
|
milli_liters_to_liters :: #force_inline proc "contextless" (
|
|
milli_liters: Milli_Liters($V),
|
|
) -> Liters(V) where intrinsics.type_is_numeric(V) {
|
|
return Liters(V){milli_liters.v / MILLI}
|
|
}
|
|
|
|
//----- Gallons ----------------------------------
|
|
Gallons :: struct($V: typeid) where intrinsics.type_is_numeric(V) {
|
|
v: V,
|
|
}
|
|
|
|
@(private = "file")
|
|
gallons_to_liters :: #force_inline proc "contextless" (
|
|
gallons: Gallons($V),
|
|
) -> Liters(V) where intrinsics.type_is_float(V) {
|
|
return Liters(V){gallons.v * LITERS_PER_GALLON}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ----- Conversion Overloads ------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
to_liters :: proc {
|
|
milli_liters_to_liters,
|
|
gallons_to_liters,
|
|
}
|
|
|
|
to_milli_liters :: proc {
|
|
liters_to_milli_liters,
|
|
}
|
|
|
|
to_gallons :: proc {
|
|
liters_to_gallons,
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ----- Tests ------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
import "core:testing"
|
|
|
|
@(test)
|
|
test_liters_to_milli_liters :: proc(t: ^testing.T) {
|
|
liters := Liters(int){12}
|
|
milli_liters := to_milli_liters(liters)
|
|
|
|
testing.expect_value(t, milli_liters, Milli_Liters(int){12_000})
|
|
}
|
|
|
|
@(test)
|
|
test_milli_liters_to_liters :: proc(t: ^testing.T) {
|
|
milli_liters := Milli_Liters(int){12_000}
|
|
liters := to_liters(milli_liters)
|
|
|
|
testing.expect_value(t, liters, Liters(int){12})
|
|
}
|
|
|
|
@(test)
|
|
test_gallons_to_liters :: proc(t: ^testing.T) {
|
|
gallons := Gallons(f32){1}
|
|
liters := to_liters(gallons)
|
|
|
|
testing.expect(t, liters.v > 3.78 && liters.v < 3.79)
|
|
}
|
|
|
|
@(test)
|
|
test_liters_to_gallons :: proc(t: ^testing.T) {
|
|
liters := Liters(f32){3.785411784}
|
|
gallons := to_gallons(liters)
|
|
|
|
testing.expect(t, gallons.v > 0.99 && gallons.v < 1.01)
|
|
}
|