Skip to content

Commit

Permalink
Add tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Goju-Ryu committed Sep 25, 2024
1 parent 29d5d64 commit 875a0ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions book/src/list-functions-other.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: Dim>(units: List<D>, value: D) -> List<D>
```

## Temperature conversion

Defined in: `physics::temperature_conversion`
Expand Down
16 changes: 13 additions & 3 deletions examples/tests/mixed_units.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: Dim>(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)


0 comments on commit 875a0ad

Please sign in to comment.