import std/random import std/sets import pkg/unittest2 import pkg/stew/results import ../leopard import ./helpers randomize() suite "Leopard Parametrization": test "Should not allow invalid buffer multiples": check: LeoEncoder.init(63, 4, 2).error == "bufSize should be multiples of 64 bytes!" LeoEncoder.init(65, 4, 2).error == "bufSize should be multiples of 64 bytes!" test "Should not allow invalid data/parity buffer counts": check: LeoEncoder.init(64, 1, 2).error == "number of parity buffers cannot exceed number of data buffers!" test "Should not allow data + parity to exceed 65536": check: LeoEncoder.init(64, 65536 + 1, 0).error == "number of parity and data buffers cannot exceed 65536!" LeoEncoder.init(64, 32768 + 1, 32768).error == "number of parity and data buffers cannot exceed 65536!" test "Should not allow encoding with invalid data buffer counts": var leo = LeoEncoder.init(64, 4, 2).tryGet() data = newSeq[seq[byte]](3) parity = newSeq[seq[byte]](2) check: leo.encode(data, parity).error == "Number of data buffers should match!" test "Should not allow encoding with invalid parity buffer counts": var leo = LeoEncoder.init(64, 4, 2).tryGet() data = newSeq[seq[byte]](4) parity = newSeq[seq[byte]](3) check: leo.encode(data, parity).error == "Number of parity buffers should match!" test "Should not allow decoding with invalid data buffer counts": var leo = LeoDecoder.init(64, 4, 2).tryGet() data = newSeq[seq[byte]](3) parity = newSeq[seq[byte]](2) recovered = newSeq[seq[byte]](3) check: leo.decode(data, parity, recovered).error == "Number of data buffers should match!" test "Should not allow decoding with invalid data buffer counts": var leo = LeoDecoder.init(64, 4, 2).tryGet() data = newSeq[seq[byte]](4) parity = newSeq[seq[byte]](1) recovered = newSeq[seq[byte]](3) check: leo.decode(data, parity, recovered).error == "Number of parity buffers should match!" test "Should not allow decoding with invalid data buffer counts": var leo = LeoDecoder.init(64, 4, 2).tryGet() data = newSeq[seq[byte]](4) parity = newSeq[seq[byte]](2) recovered = newSeq[seq[byte]](3) check: leo.decode(data, parity, recovered).error == "Number of recovered buffers should match buffers!" suite "Leopard simple Encode/Decode": const TestString = "Hello World!" DataCount = 4 ParityCount = 2 BufferSize = 64 var encoder: LeoEncoder decoder: LeoDecoder data: seq[seq[byte]] parity: seq[seq[byte]] recovered: seq[seq[byte]] setup: encoder = LeoEncoder.init(BufferSize, DataCount, ParityCount).tryGet() decoder = LeoDecoder.init(BufferSize, DataCount, ParityCount).tryGet() data = newSeq[seq[byte]](DataCount) parity = newSeq[seq[byte]](ParityCount) recovered = newSeq[seq[byte]](DataCount) teardown: encoder.free() decoder.free() test "Test 2 data loses out of 4 possible": for i in 0..