about summary refs log tree commit diff
path: root/users/sterni/nix/float/tests/default.nix
blob: 75e2a1bfa091e4ea55830c577ea75a04be2eed51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
])