From d4976912beceb70a110050f212d5a1f17f974f17 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 28 Feb 2025 15:23:05 -0500 Subject: volume unit conversion functions --- test_volume.c | 16 ++++++++-------- unit.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/test_volume.c b/test_volume.c index 1dac319..84b29a0 100644 --- a/test_volume.c +++ b/test_volume.c @@ -6,40 +6,40 @@ void test_cubic_centimetre(void) { - assert(0); + test(cubic_centimetre(123.456), 0.000123456); } void test_litre(void) { - assert(0); + test(litre(123.456), 0.123456); } void test_cubic_metre(void) { - assert(0); + test(cubic_metre(123.456), 123.456); } void test_cubic_inch(void) { - assert(0); + test(cubic_inch(123.456), 0.0020230814); } void test_as_cubic_centimetre(void) { - assert(0); + test(as_cubic_centimetre(cubic_centimetre(123.456)), 123.456); } void test_as_litre(void) { - assert(0); + test(as_litre(litre(123.456)), 123.456); } void test_as_cubic_metre(void) { - assert(0); + test(as_cubic_metre(cubic_metre(123.456)), 123.456); } void test_as_cubic_inch(void) { - assert(0); + test(as_cubic_inch(cubic_inch(123.456)), 123.456); } diff --git a/unit.c b/unit.c index 702f56c..fde768f 100644 --- a/unit.c +++ b/unit.c @@ -45,11 +45,42 @@ double as_bar(Pressure x); double as_psi(Pressure x); -Volume cubic_centimetre(double x); -Volume litre(double x); -Volume cubic_metre(double x); -Volume cubic_inch(double x); -double as_cubic_centimetre(Volume x); -double as_litre(Volume x); -double as_cubic_metre(double x); -double as_cubic_inch(double x); +Volume +cubic_centimetre(double x) { + return x * 1e-6; +} + +Volume +litre(double x) { + return x * 1e-3; +} + +Volume +cubic_metre(double x) { + return x; +} + +Volume +cubic_inch(double x) { + return x * 1.6387064e-5; +} + +double +as_cubic_centimetre(Volume x) { + return x * 1e6; +} + +double +as_litre(Volume x) { + return x * 1e3; +} + +double +as_cubic_metre(double x) { + return x; +} + +double +as_cubic_inch(double x) { + return x / 1.6387064e-5; +} -- cgit v1.2.3