From fb68031848e07ee1c9d0185051380484ab9b2415 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 15 Apr 2025 10:47:11 +0200 Subject: [PATCH] fix: tests fix: arithmeticRight --- lib/netu.nix | 5 ++- lib/shift.nix | 2 +- tests/default.nix | 1 + tests/net.nix | 93 +++++++++++++++++++++++++++++++++++++++++++++++ tests/shift.nix | 3 +- 5 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tests/net.nix diff --git a/lib/netu.nix b/lib/netu.nix index a88bae2..47ad35f 100644 --- a/lib/netu.nix +++ b/lib/netu.nix @@ -1160,7 +1160,10 @@ let # coshadow :: integer -> (ip | mac | integer) -> (ip | mac | integer) coshadow = n: a: and (not (right n (left n (coerce a (-1))))) a; - # coerce :: (ip | mac | integer) -> (ip | mac | integer) -> (ip | mac | integer) + # coerce target value + # coerce value to the type of target + # coerce :: A -> (ip | mac | integer) -> A + # where A :: (ip | mac | integer) coerce = target: value: if target ? ipv6 then diff --git a/lib/shift.nix b/lib/shift.nix index 0fd3417..d686d0b 100644 --- a/lib/shift.nix +++ b/lib/shift.nix @@ -109,7 +109,7 @@ let arithmeticRight = a: b: if a >= 64 then - 0 + if b < 0 then -1 else 0 else if a == 0 then b else if a < 0 then diff --git a/tests/default.nix b/tests/default.nix index 03d273d..a8520ce 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -5,4 +5,5 @@ }: [ (import ./shift.nix { inherit pkgs nixt; }) + (import ./net.nix { inherit pkgs nixt; }) ] diff --git a/tests/net.nix b/tests/net.nix new file mode 100644 index 0000000..ab7928b --- /dev/null +++ b/tests/net.nix @@ -0,0 +1,93 @@ +{ + nixt, + pkgs, + ... +}: +let + inherit (nixt.lib) block describe it; + inherit (pkgs) lib; + expected = { + cidrv6 = { + empty = { + base = { + ipv6 = { + a = 0; + b = 0; + c = 0; + d = 0; + }; + }; + length = 0; + }; + base4 = { + base = { + ipv6 = { + a = 65536; + b = 0; + c = 0; + d = 0; + }; + }; + length = 4; + }; + }; + }; +in +block ./net.nix [ + (describe "cidrv6" [ + (it "child 1" (lib.net.cidr.child "1::/4" "0::/0")) + (it "child 2" (lib.net.cidr.child "1::/4" "::/0")) + (it "child 3" (!lib.net.cidr.child "::/0" "::/4")) + (it "child 4" (lib.net.cidr.child "1100::/8" "1000::/4")) + (it "child 5" (!lib.net.cidr.child "1100::/16" "1000::/8")) + (it "child 6" (lib.net.cidr.child "0:1::/24" "0:1::/8")) + + (it "typechecks 1" (lib.typechecks.cidr "" "" "::/0" == expected.cidrv6.empty)) + (it "typechecks 2" (lib.typechecks.cidr "" "" "0::/0" == expected.cidrv6.empty)) + (it "typechecks 3" (lib.typechecks.cidr "" "" "1::/4" == expected.cidrv6.empty // { length = 4; })) + + (it "impl child" (lib.implementations.cidr.child expected.cidrv6.base4 expected.cidrv6.empty)) + + (it "cidr length 0" (lib.implementations.cidr.length expected.cidrv6.empty == 0)) + (it "cidr length 4" (lib.implementations.cidr.length expected.cidrv6.base4 == 4)) + + (it "contains 4" ( + lib.implementations.cidr.contains expected.cidrv6.base4.base expected.cidrv6.empty + )) + + (it "host 4" (lib.implementations.cidr.host 0 expected.cidrv6.base4 == expected.cidrv6.base4.base)) + (it "host 0" (lib.implementations.cidr.host 0 expected.cidrv6.empty == expected.cidrv6.empty.base)) + + (it "make len 0" ( + lib.implementations.cidr.make 0 expected.cidrv6.base4.base == expected.cidrv6.empty + )) + (it "make base 4 len 0 length" ( + (lib.implementations.cidr.make 0 expected.cidrv6.base4.base).length == 0 + )) + (it "make base 4 len 0 base" ( + (lib.implementations.cidr.make 0 expected.cidrv6.base4.base).base == expected.cidrv6.empty.base + )) + ]) + (describe "cidrv4" [ + (it "child 1" (lib.net.cidr.child "10.10.10.0/24" "10.0.0.0/8")) + (it "child 2" (!lib.net.cidr.child "127.0.0.0/24" "10.0.0.0/8")) + ]) + (describe "arithmetic" [ + (it "coshadow 1" ( + (lib.arithmetic.coshadow 0 expected.cidrv6.base4.base) == expected.cidrv6.empty.base + )) + (it "coerce 1" ( + (lib.arithmetic.coerce expected.cidrv6.base4.base (-1)) == { + ipv6 = { + a = 4294967295; + b = 4294967295; + c = 4294967295; + d = 4294967295; + }; + } + )) + ]) + (describe "bit" [ + (it "mask 32 " ((lib.bit.mask 32 (-1)) == 4294967295)) + ]) +] diff --git a/tests/shift.nix b/tests/shift.nix index 53d3c07..f814f19 100644 --- a/tests/shift.nix +++ b/tests/shift.nix @@ -393,6 +393,7 @@ block ./shift.nix [ (it "negative logical right shift 21068 >> 0" (logicalRight 0 (-21068) == -21068)) (it "arithmetic right shift 21068 >> 0" (arithmeticRight 0 21068 == 21068)) (it "negative arithmetic right shift 21068 >> 0" (arithmeticRight 0 (-21068) == -21068)) - + (it "arithmetic right shift -1 >> 32" (arithmeticRight 32 (-1) == (-1))) + (it "arithmetic right shift -1 >> 102" (arithmeticRight 102 (-1) == (-1))) ]) ]