feat(sterni/nix): add trivial float library
Only implements the different conversion types from and to ints for now.
Unfortunately very reliant on builtins.{floor,ceil} which can't be
implemented purely except very inefficiently (to my knowledge), so it
only really works for C++ Nix >= 2.4. Tests are thus skipped for
C++ Nix 2.3.
Change-Id: Idcb1a11df11e214cdba3f2a0715472b370daa7dc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9008
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
parent
984ea69386
commit
55a3b3eb81
3 changed files with 73 additions and 0 deletions
49
users/sterni/nix/float/tests/default.nix
Normal file
49
users/sterni/nix/float/tests/default.nix
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
{ depot, lib, ... }:
|
||||
|
||||
let
|
||||
|
||||
inherit (depot.nix.runTestsuite)
|
||||
runTestsuite
|
||||
it
|
||||
assertEq
|
||||
;
|
||||
|
||||
inherit (depot.users.sterni.nix)
|
||||
float
|
||||
;
|
||||
|
||||
testsBuiltins = it "tests builtin operations" [
|
||||
(assertEq "ceil pos" (float.ceil 1.5) 2)
|
||||
(assertEq "ceil neg" (float.ceil (-1.5)) (-1))
|
||||
(assertEq "floor pos" (float.floor 1.5) 1)
|
||||
(assertEq "floor neg" (float.floor (-1.5)) (-2))
|
||||
];
|
||||
|
||||
testsConversionFrom = it "tests integer to float conversion" [
|
||||
(assertEq "float.intToFloat is identity for floats" (float.intToFloat 1.3) 1.3)
|
||||
(assertEq "float.intToFloat converts ints"
|
||||
(builtins.all
|
||||
(val: builtins.isFloat val)
|
||||
(builtins.map float.intToFloat (builtins.genList (i: i - 500) 1000)))
|
||||
true)
|
||||
];
|
||||
|
||||
exampleFloats = [ 0.5 0.45 0.3 0.1 200 203.457847 204.65547 (-1.5) (-2) (-1.3) (-0.45) ];
|
||||
testsConversionTo = it "tests float to integer conversion" [
|
||||
(assertEq "round"
|
||||
(builtins.map float.round exampleFloats)
|
||||
[ 1 0 0 0 200 203 205 (-2) (-2) (-1) 0 ])
|
||||
(assertEq "truncate towards zero"
|
||||
(builtins.map float.truncate exampleFloats)
|
||||
[ 0 0 0 0 200 203 204 (-1) (-2) (-1) 0 ])
|
||||
];
|
||||
in
|
||||
|
||||
runTestsuite "nix.num" ([
|
||||
testsConversionFrom
|
||||
]
|
||||
# Skip for e.g. C++ Nix < 2.4
|
||||
++ lib.optionals (builtins ? ceil && builtins ? floor) [
|
||||
testsConversionTo
|
||||
testsBuiltins
|
||||
])
|
||||
Loading…
Add table
Add a link
Reference in a new issue