2020-05-07 20:37:46 +00:00
|
|
|
{.used.}
|
|
|
|
|
2023-05-18 08:24:17 +00:00
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
import unittest2
|
2020-05-07 20:37:46 +00:00
|
|
|
import stew/byteutils
|
|
|
|
import ../libp2p/stream/streamseq
|
|
|
|
|
|
|
|
suite "StreamSeq":
|
|
|
|
test "basics":
|
|
|
|
var s: StreamSeq
|
|
|
|
|
|
|
|
check:
|
|
|
|
s.data().len == 0
|
|
|
|
|
|
|
|
s.add([byte 0, 1, 2, 3])
|
|
|
|
|
|
|
|
check:
|
|
|
|
@(s.data()) == [byte 0, 1, 2, 3]
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
s.prepare(10)[0 ..< 3] = [byte 4, 5, 6]
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
@(s.data()) == [byte 0, 1, 2, 3]
|
|
|
|
|
|
|
|
s.commit(3)
|
|
|
|
|
|
|
|
check:
|
|
|
|
@(s.data()) == [byte 0, 1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
s.consume(1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
@(s.data()) == [byte 1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
s.consume(6)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
check:
|
|
|
|
@(s.data()) == []
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
s.add([])
|
2024-06-11 15:18:06 +00:00
|
|
|
check:
|
|
|
|
@(s.data()) == []
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
var o: seq[byte]
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
check:
|
|
|
|
0 == s.consumeTo(o)
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
s.add([byte 1, 2, 3])
|
|
|
|
|
|
|
|
o.setLen(2)
|
|
|
|
o.setLen(s.consumeTo(o))
|
|
|
|
check:
|
|
|
|
o == [byte 1, 2]
|
|
|
|
|
|
|
|
o.setLen(s.consumeTo(o))
|
|
|
|
|
|
|
|
check:
|
|
|
|
o == [byte 3]
|