From 875a0ad8ca9134aaeea59ca438472bd2d9d608b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Tue, 17 Sep 2024 22:19:08 +0200 Subject: [PATCH] Add tests and documentation --- book/src/list-functions-other.md | 8 ++++++++ examples/tests/mixed_units.nbt | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/book/src/list-functions-other.md b/book/src/list-functions-other.md index a8a97830..17142c35 100644 --- a/book/src/list-functions-other.md +++ b/book/src/list-functions-other.md @@ -105,6 +105,14 @@ More information [here](https://en.wikipedia.org/wiki/Pound_(mass)). fn pounds_and_ounces(mass: Mass) -> String ``` +### `unit_list` (Unit list) +Converts a value to a mixed unit representation. +More information [here](https://www.gnu.org/software/units/manual/html_node/Unit-Lists.html) + +```nbt +fn unit_list(units: List, value: D) -> List +``` + ## Temperature conversion Defined in: `physics::temperature_conversion` diff --git a/examples/tests/mixed_units.nbt b/examples/tests/mixed_units.nbt index 6aa98d2a..e6c2326a 100644 --- a/examples/tests/mixed_units.nbt +++ b/examples/tests/mixed_units.nbt @@ -37,7 +37,17 @@ assert_eq(-5.5 lb -> pounds_and_ounces, "-5 lb 8 oz") # Unit list -assert_eq(unit_list([m, cm, mm], 1m + 23cm + 4mm), [1m, 23cm, 4mm]) -assert_eq(unit_list([deg, arcmin], 1deg + 23arcmin + 4arcsec), [1deg, 23arcmin, 4arcsec]) -assert_eq(unit_list([hour], 1.5hour), [1.5hour]) +fn _add(a: D, b: D) -> D = a + b + +let test1 = 12 m + 34 cm + 5 mm + 678 µm +assert_eq(test1 |> unit_list([m]) |> head, test1) +assert_eq(test1 |> unit_list([m, cm]) |> foldl(_add, 0), test1) +assert_eq(test1 |> unit_list([m, cm, mm]) |> foldl(_add, 0), test1) +assert_eq(test1 |> unit_list([m, cm, mm, µm]) |> foldl(_add, 0), test1) + +let test2 = 12 degree + 34 arcminute + 5 arcsec +assert_eq(test2 |> unit_list([degree]) |> head, test2) +assert_eq(test2 |> unit_list([degree, arcmin]) |> foldl(_add, 0), test2) +assert_eq(test2 |> unit_list([degree, arcmin, arcsec]) |> foldl(_add, 0), test2) +