diff --git a/tests/dagger/testleopard.nim b/tests/dagger/testleopard.nim index 522a014b..0dc4bfb9 100644 --- a/tests/dagger/testleopard.nim +++ b/tests/dagger/testleopard.nim @@ -1,7 +1,6 @@ -# import std/os -# import std/random import std/sequtils import std/strformat +import std/strutils import pkg/dagger/leopard import pkg/stew/byteutils @@ -11,8 +10,6 @@ type original_count: cuint recovery_count: cuint buffer_bytes : cuint - loss_count : cuint - seed : cuint Vec = seq[pointer] @@ -20,75 +17,45 @@ proc init( T: type TestParameters, original_count: cuint = 100, recovery_count: cuint = 10, - buffer_bytes : cuint = 64000, - loss_count : cuint = 32768, - seed : cuint = 2): T = + buffer_bytes : cuint = 64000): T = T(original_count: original_count, recovery_count: recovery_count, - buffer_bytes : buffer_bytes, - loss_count : loss_count, - seed : seed) + buffer_bytes : buffer_bytes) proc new(T: type Vec, input: seq[seq[byte]]): T = input.mapIt(cast[pointer](unsafeAddr it[0])) -proc benchmark(params: TestParameters) = +proc test(params: TestParameters) = + let + deadbeef64 = @[ + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef, + 0xde, 0xad, 0xbe, 0xef + ].mapIt(it.byte) + var - # original_data = newSeqWith(params.original_count.int, - # newSeq[byte](params.buffer_bytes)) - - # original_data_0 = newSeqWith(params.original_count.int, hello.toBytes) - # original_data = Vec.new(original_data_0) - - hello01 = "hello world01 " - hello02 = "hello world02 " - hello03 = "hello world03 " - hello04 = "hello world04 " - hello05 = "hello world05 " - hello06 = "hello world06 " - hello07 = "hello world07 " - hello08 = "hello world08 " - hello09 = "hello world09 " - hello10 = "hello world10 " - hello11 = "hello world11 " - hello12 = "hello world12 " - hello13 = "hello world13 " - hello14 = "hello world14 " - hello15 = "hello world15 " - hello16 = "hello world16 " - hello17 = "hello world17 " - hello18 = "hello world18 " - hello19 = "hello world19 " - hello20 = "hello world20 " - - original_data_0 = @[ - hello01.toBytes, - hello02.toBytes, - hello03.toBytes, - hello04.toBytes, - hello05.toBytes, - hello06.toBytes, - hello07.toBytes, - hello08.toBytes, - hello09.toBytes, - hello10.toBytes, - hello11.toBytes, - hello12.toBytes, - hello13.toBytes, - hello14.toBytes, - hello15.toBytes, - hello16.toBytes, - hello17.toBytes, - hello18.toBytes, - hello19.toBytes, - hello20.toBytes - ] - + original_data_0 = newSeqWith(params.original_count.int, deadbeef64) original_data = Vec.new(original_data_0) - # debugEcho $original_data_0.mapIt(repr it) - # debugEcho $original_data.mapIt(repr cast[seq[byte]](cast[pointer](cast[int](it) - 16))) - debugEcho $original_data.mapIt(cast[int](it)) + debugEcho "\nORIGINAL DATA" + debugEcho "-------------\n" + debugEcho $original_data_0.mapIt(it.mapIt(it.toHex(2).toLowerAscii)) + + # debugEcho $original_data.mapIt("0x" & (cast[int](it) - 16).toHex(9).toLowerAscii) + # debugEcho $original_data.mapIt((cast[seq[byte]](cast[pointer](cast[int](it) - 16))).mapIt(it.toHex(2).toLowerAscii)) let encode_work_count = leo_encode_work_count(params.original_count, @@ -96,7 +63,7 @@ proc benchmark(params: TestParameters) = decode_work_count = leo_decode_work_count(params.original_count, params.recovery_count) - debugEcho "encode_work_count: " & $encode_work_count + debugEcho "\nencode_work_count: " & $encode_work_count debugEcho "decode_work_count: " & $decode_work_count let @@ -127,19 +94,25 @@ proc benchmark(params: TestParameters) = debugEcho "encodeResult: " & $leo_result_string(encodeResult) - original_data[0] = nil - original_data[17] = nil + original_data[53] = nil + original_data[199] = nil - encode_work_data[0] = nil - encode_work_data[1] = nil - # encode_work_data[2] = nil - encode_work_data[3] = nil - encode_work_data[4] = nil - encode_work_data[5] = nil - encode_work_data[6] = nil - encode_work_data[7] = nil - encode_work_data[8] = nil - # encode_work_data[9] = nil + encode_work_data[0] = nil + encode_work_data[1] = nil + encode_work_data[2] = nil + encode_work_data[3] = nil + encode_work_data[4] = nil + encode_work_data[5] = nil + encode_work_data[6] = nil + # encode_work_data[7] = nil + encode_work_data[8] = nil + encode_work_data[9] = nil + encode_work_data[10] = nil + # encode_work_data[11] = nil + encode_work_data[12] = nil + encode_work_data[13] = nil + encode_work_data[14] = nil + encode_work_data[15] = nil let decodeResult = leo_decode( @@ -154,27 +127,34 @@ proc benchmark(params: TestParameters) = debugEcho "decodeResult: " & $leo_result_string(decodeResult) - debugEcho $original_data.mapIt((if it.isNil: @[] else: cast[seq[byte]](cast[pointer](cast[int](it) - 16))).len) - debugEcho $encode_work_data.mapIt((if it.isNil: @[] else: cast[seq[byte]](cast[pointer](cast[int](it) - 16))).len) - debugEcho $decode_work_data.mapIt(cast[seq[byte]](cast[pointer](cast[int](it) - 16)).len) - # debugEcho $decode_work_data.mapIt(cast[seq[byte]](cast[pointer](cast[int](it) - 16))) + debugEcho "\nDECODED DATA" + debugEcho "------------\n" + debugEcho $decode_work_data.mapIt((cast[seq[byte]](cast[pointer](cast[int](it) - 16))).mapIt(it.toHex(2).toLowerAscii)) proc main() = if leo_init() != 0: raise (ref Defect)(msg: "Leopard failed to initialize") - var params = TestParameters.init(buffer_bytes = 64, original_count = 20) + # https://github.com/catid/leopard/issues/12 + # https://www.cs.cmu.edu/~guyb/realworld/reedsolomon/reed_solomon_codes.html + + # RS(255, 239) + # ------------ + # original_count = 239 + # recovery_count = 255 - 239 = 16 + + var params = TestParameters.init( + original_count = 239, + recovery_count = 16, + buffer_bytes = 64 + ) debugEcho fmt( "Parameters: " & "[original count={params.original_count}] " & "[recovery count={params.recovery_count}] " & - "[buffer bytes={params.buffer_bytes}] " & - "[loss count={params.loss_count}] " & - "[random seed={params.seed}]" + "[buffer bytes={params.buffer_bytes}] " ) - benchmark params + test params -when true: # isMainModule: - # randomize() - main() +main()