Skip to content
Snippets Groups Projects
Commit 6133eda1 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

test binary module and rename things for clarity (dragoon/komodo!18)

wait for !17

## changelog
- add `bytes from_int: [int -> binary, list<int> -> binary]` to `binary.nu`
- add `bytes to_int: binary -> list<int>` to `binary.nu`
- add `tests/binary.nu` to test `binary.nu`
- run `tests/binary.nu` in the CI
- for clarity
  - rename `BYTES` to `FILE` in `tests/cli.nu`
  - rename `bytes` to `input` in `komodo.nu`
parent 016cb2dd
No related branches found
No related tags found
1 merge request!18test binary module and rename things for clarity
Pipeline #3970 passed
......@@ -55,3 +55,4 @@ test:
- cargo clippy --workspace --all-targets -- -D warnings
- cargo test --workspace --verbose
- nu tests/cli.nu
- nu tests/binary.nu
......@@ -10,3 +10,14 @@ export def "bytes encode" [encoding: string = "utf-8"]: string -> list<int> {
$bytes | bytes at ($i - 1)..($i) | into int
}
}
export def "bytes from_int" []: [int -> binary, list<int> -> binary] {
each { into binary --compact } | bytes collect
}
export def "bytes to_int" []: binary -> list<int> {
let bytes = $in
seq 1 ($bytes | bytes length) | each {|i|
$bytes | bytes at ($i - 1)..($i) | get 0
}
}
......@@ -12,7 +12,7 @@ def "nu-complete log-levels" []: nothing -> list<string> {
}
def run-komodo [
--bytes: path = "",
--input: path = "",
-k: int = 0,
-n: int = 0,
--generate-powers,
......@@ -27,7 +27,7 @@ def run-komodo [
with-env {RUST_LOG: $log_level} {
let res = do {
^$KOMODO_BINARY ...([
$bytes
$input
$k
$n
($generate_powers | into string)
......@@ -60,21 +60,21 @@ export def "komodo build" []: nothing -> nothing {
}
export def "komodo setup" [
bytes: path,
input: path,
--powers-file: path = "powers.bin",
--log-level: string@"nu-complete log-levels" = "INFO"
]: nothing -> nothing {
(
run-komodo
--log-level $log_level
--bytes $bytes
--input $input
--generate-powers
--powers-file $powers_file
)
}
export def "komodo prove" [
bytes: path,
input: path,
--fec-params: record<k: int, n: int>,
--powers-file: path = "powers.bin",
--log-level: string@"nu-complete log-levels" = "INFO"
......@@ -82,7 +82,7 @@ export def "komodo prove" [
(
run-komodo
--log-level $log_level
--bytes $bytes
--input $input
-k $fec_params.k
-n $fec_params.n
--powers-file $powers_file
......
use ../binary.nu [
"bytes decode", "bytes encode", "bytes from_int", "bytes to_int"
]
use std assert
def random-bytes [n: int]: nothing -> list<int> {
0..$n | each { random int 0..255 }
}
def main [] {
const hello_world_int = [
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33
]
const expected = 0x[68 65 6C 6C 6F 20 77 6F 72 6C 64 21]
assert equal ($hello_world_int | bytes from_int) $expected
let bytes = random-bytes 1_000
assert equal ($bytes | bytes from_int | bytes to_int) $bytes
const input = "hello world!"
assert equal ($input | bytes encode) $hello_world_int
assert equal ($input | bytes encode | bytes decode) $input
}
......@@ -6,7 +6,7 @@ use ../komodo.nu [
"komodo reconstruct",
"komodo ls",
]
use ../binary.nu [ "bytes decode" ]
use ../binary.nu [ "bytes from_int" ]
use std assert
......@@ -119,12 +119,12 @@ module math {
use math
const BYTES = "tests/dragoon_32x32.png"
const FILE = "tests/dragoon_32x32.png"
const FEC_PARAMS = {k: 3, n: 5}
def test [blocks: list<int>, --fail] {
let actual = try {
komodo reconstruct ...(komodo ls) | bytes decode
komodo reconstruct ...(komodo ls) | bytes from_int
} catch {
if not $fail {
error make --unspanned { msg: "woopsie" }
......@@ -133,15 +133,15 @@ def test [blocks: list<int>, --fail] {
}
}
let expected = open $BYTES | into binary | to text | from json | bytes decode
let expected = open $FILE | bytes from_int
assert equal $actual $expected
}
def main [] {
komodo build
komodo setup $BYTES
komodo prove $BYTES --fec-params $FEC_PARAMS
komodo setup $FILE
komodo prove $FILE --fec-params $FEC_PARAMS
komodo verify ...(komodo ls)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment