mirror of
https://github.com/status-im/nim-codex.git
synced 2025-02-04 15:04:54 +00:00
Add absolutely minimal implementation of chunking
This commit is contained in:
parent
2471423197
commit
5f5153820f
10
dagger/chunking.nim
Normal file
10
dagger/chunking.nim
Normal file
@ -0,0 +1,10 @@
|
||||
import ./merkledag
|
||||
|
||||
export merkledag
|
||||
|
||||
proc createChunks*(file: File): MerkleDag =
|
||||
let contents = file.readAll()
|
||||
MerkleDag(data: cast[seq[byte]](contents))
|
||||
|
||||
proc assembleChunks*(dag: MerkleDag, output: File) =
|
||||
output.write(cast[string](dag.data))
|
3
dagger/merkledag.nim
Normal file
3
dagger/merkledag.nim
Normal file
@ -0,0 +1,3 @@
|
||||
type
|
||||
MerkleDag* = object
|
||||
data*: seq[byte]
|
31
tests/testChunking.nim
Normal file
31
tests/testChunking.nim
Normal file
@ -0,0 +1,31 @@
|
||||
import std/unittest
|
||||
import std/os
|
||||
import pkg/dagger/chunking
|
||||
|
||||
suite "chunking":
|
||||
|
||||
var input, output: File
|
||||
|
||||
setup:
|
||||
input = open("tests/input.txt", fmReadWrite)
|
||||
output = open("tests/output.txt", fmReadWrite)
|
||||
input.write("foo")
|
||||
input.setFilePos(0)
|
||||
|
||||
teardown:
|
||||
input.close()
|
||||
output.close()
|
||||
removeFile("tests/input.txt")
|
||||
removeFile("tests/output.txt")
|
||||
|
||||
test "creates a Merkle DAG from a file":
|
||||
check createChunks(input) != MerkleDag.default
|
||||
|
||||
test "creates a file from a Merkle DAG":
|
||||
let dag = createChunks(input)
|
||||
assembleChunks(dag, output)
|
||||
|
||||
input.setFilePos(0)
|
||||
output.setFilePos(0)
|
||||
check output.readAll() == input.readAll()
|
||||
|
Loading…
x
Reference in New Issue
Block a user