mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Improve docs and tests of container package
This commit is contained in:
parent
d86cccbbb6
commit
627939f7c6
2 changed files with 76 additions and 17 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
// Package container gives you a []byte slice on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation.
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -6,7 +7,7 @@ import (
|
||||||
"github.com/Safing/safing-core/formats/varint"
|
"github.com/Safing/safing-core/formats/varint"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Container is []byte array on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation. (Error transportation requires use of varints for data)
|
// Container is []byte sclie on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation. (Error transportation requires use of varints for data)
|
||||||
type Container struct {
|
type Container struct {
|
||||||
compartments [][]byte
|
compartments [][]byte
|
||||||
offset int
|
offset int
|
||||||
|
@ -15,7 +16,7 @@ type Container struct {
|
||||||
|
|
||||||
// Data Handling
|
// Data Handling
|
||||||
|
|
||||||
// NewContainer creates a new container with an optional initial []byte slice. Data will NOT be copied.
|
// NewContainer is DEPRECATED, please use New(), it's the same thing.
|
||||||
func NewContainer(data ...[]byte) *Container {
|
func NewContainer(data ...[]byte) *Container {
|
||||||
return &Container{
|
return &Container{
|
||||||
compartments: data,
|
compartments: data,
|
||||||
|
@ -120,7 +121,7 @@ func (c *Container) WriteToSlice(slice []byte) (n int, containerEmptied bool) {
|
||||||
return n, true
|
return n, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) Clean() {
|
func (c *Container) clean() {
|
||||||
if c.offset > 100 {
|
if c.offset > 100 {
|
||||||
c.renewCompartments()
|
c.renewCompartments()
|
||||||
}
|
}
|
||||||
|
@ -184,7 +185,7 @@ func (c *Container) Error() error {
|
||||||
return c.err
|
return c.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns the error.
|
// ErrString returns the error as a string.
|
||||||
func (c *Container) ErrString() string {
|
func (c *Container) ErrString() string {
|
||||||
return c.err.Error()
|
return c.err.Error()
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,10 @@ var (
|
||||||
|
|
||||||
func TestContainerDataHandling(t *testing.T) {
|
func TestContainerDataHandling(t *testing.T) {
|
||||||
|
|
||||||
c1 := NewContainer(utils.DuplicateBytes(testData))
|
c1 := New(utils.DuplicateBytes(testData))
|
||||||
c1c := c1.carbonCopy()
|
c1c := c1.carbonCopy()
|
||||||
|
|
||||||
c2 := NewContainer()
|
c2 := New()
|
||||||
for i := 0; i < len(testData); i++ {
|
for i := 0; i < len(testData); i++ {
|
||||||
oneByte := make([]byte, 1)
|
oneByte := make([]byte, 1)
|
||||||
c1c.WriteToSlice(oneByte)
|
c1c.WriteToSlice(oneByte)
|
||||||
|
@ -36,7 +36,7 @@ func TestContainerDataHandling(t *testing.T) {
|
||||||
}
|
}
|
||||||
c2c := c2.carbonCopy()
|
c2c := c2.carbonCopy()
|
||||||
|
|
||||||
c3 := NewContainer()
|
c3 := New()
|
||||||
for i := len(c2c.compartments) - 1; i >= c2c.offset; i-- {
|
for i := len(c2c.compartments) - 1; i >= c2c.offset; i-- {
|
||||||
c3.Prepend(c2c.compartments[i])
|
c3.Prepend(c2c.compartments[i])
|
||||||
}
|
}
|
||||||
|
@ -52,19 +52,19 @@ func TestContainerDataHandling(t *testing.T) {
|
||||||
c3c.WriteToSlice(d5[i : i+1])
|
c3c.WriteToSlice(d5[i : i+1])
|
||||||
}
|
}
|
||||||
|
|
||||||
c6 := NewContainer()
|
c6 := New()
|
||||||
c6.Replace(testData)
|
c6.Replace(testData)
|
||||||
|
|
||||||
c7 := NewContainer(testDataSplitted[0])
|
c7 := New(testDataSplitted[0])
|
||||||
for i := 1; i < len(testDataSplitted); i++ {
|
for i := 1; i < len(testDataSplitted); i++ {
|
||||||
c7.Append(testDataSplitted[i])
|
c7.Append(testDataSplitted[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
c8 := NewContainer(testDataSplitted...)
|
c8 := New(testDataSplitted...)
|
||||||
for i := 0; i < 110; i++ {
|
for i := 0; i < 110; i++ {
|
||||||
c8.Prepend(nil)
|
c8.Prepend(nil)
|
||||||
}
|
}
|
||||||
c8.Clean()
|
c8.clean()
|
||||||
|
|
||||||
compareMany(t, testData, c1.CompileData(), c2.CompileData(), c3.CompileData(), d4, d5, c6.CompileData(), c7.CompileData(), c8.CompileData())
|
compareMany(t, testData, c1.CompileData(), c2.CompileData(), c3.CompileData(), d4, d5, c6.CompileData(), c7.CompileData(), c8.CompileData())
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func compareMany(t *testing.T, reference []byte, other ...[]byte) {
|
||||||
|
|
||||||
func TestContainerErrorHandling(t *testing.T) {
|
func TestContainerErrorHandling(t *testing.T) {
|
||||||
|
|
||||||
c1 := NewContainer(nil)
|
c1 := New(nil)
|
||||||
|
|
||||||
if c1.HasError() {
|
if c1.HasError() {
|
||||||
t.Error("should not have error")
|
t.Error("should not have error")
|
||||||
|
@ -91,7 +91,7 @@ func TestContainerErrorHandling(t *testing.T) {
|
||||||
t.Error("should have error")
|
t.Error("should have error")
|
||||||
}
|
}
|
||||||
|
|
||||||
c2 := NewContainer(append([]byte{0}, []byte("test error")...))
|
c2 := New(append([]byte{0}, []byte("test error")...))
|
||||||
|
|
||||||
if c2.HasError() {
|
if c2.HasError() {
|
||||||
t.Error("should not have error")
|
t.Error("should not have error")
|
||||||
|
@ -109,21 +109,75 @@ func TestContainerErrorHandling(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDataFetching(t *testing.T) {
|
||||||
|
c1 := New(utils.DuplicateBytes(testData))
|
||||||
|
data := c1.GetMax(1)
|
||||||
|
if string(data[0]) != "T" {
|
||||||
|
t.Errorf("failed to GetMax(1), got %s, expected %s", string(data), "T")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := c1.Get(1000)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("should fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBlocks(t *testing.T) {
|
||||||
|
c1 := New(utils.DuplicateBytes(testData))
|
||||||
|
c1.PrependLength()
|
||||||
|
|
||||||
|
n, err := c1.GetNextN8()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetNextN8() failed: %s", err)
|
||||||
|
}
|
||||||
|
if n != 43 {
|
||||||
|
t.Errorf("n should be 43, was %d", n)
|
||||||
|
}
|
||||||
|
c1.PrependLength()
|
||||||
|
|
||||||
|
n2, err := c1.GetNextN16()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetNextN16() failed: %s", err)
|
||||||
|
}
|
||||||
|
if n2 != 43 {
|
||||||
|
t.Errorf("n should be 43, was %d", n2)
|
||||||
|
}
|
||||||
|
c1.PrependLength()
|
||||||
|
|
||||||
|
n3, err := c1.GetNextN32()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetNextN32() failed: %s", err)
|
||||||
|
}
|
||||||
|
if n3 != 43 {
|
||||||
|
t.Errorf("n should be 43, was %d", n3)
|
||||||
|
}
|
||||||
|
c1.PrependLength()
|
||||||
|
|
||||||
|
n4, err := c1.GetNextN64()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetNextN64() failed: %s", err)
|
||||||
|
}
|
||||||
|
if n4 != 43 {
|
||||||
|
t.Errorf("n should be 43, was %d", n4)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestContainerBlockHandling(t *testing.T) {
|
func TestContainerBlockHandling(t *testing.T) {
|
||||||
|
|
||||||
c1 := NewContainer(utils.DuplicateBytes(testData))
|
c1 := New(utils.DuplicateBytes(testData))
|
||||||
c1.PrependLength()
|
c1.PrependLength()
|
||||||
c1.AppendAsBlock(testData)
|
c1.AppendAsBlock(testData)
|
||||||
c1c := c1.carbonCopy()
|
c1c := c1.carbonCopy()
|
||||||
|
|
||||||
c2 := NewContainer(nil)
|
c2 := New(nil)
|
||||||
for i := 0; i < c1.Length(); i++ {
|
for i := 0; i < c1.Length(); i++ {
|
||||||
oneByte := make([]byte, 1)
|
oneByte := make([]byte, 1)
|
||||||
c1c.WriteToSlice(oneByte)
|
c1c.WriteToSlice(oneByte)
|
||||||
c2.Append(oneByte)
|
c2.Append(oneByte)
|
||||||
}
|
}
|
||||||
|
|
||||||
c3 := NewContainer(testDataSplitted[0])
|
c3 := New(testDataSplitted[0])
|
||||||
for i := 1; i < len(testDataSplitted); i++ {
|
for i := 1; i < len(testDataSplitted); i++ {
|
||||||
c3.Append(testDataSplitted[i])
|
c3.Append(testDataSplitted[i])
|
||||||
}
|
}
|
||||||
|
@ -154,9 +208,13 @@ func TestContainerBlockHandling(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerMisc(t *testing.T) {
|
func TestContainerMisc(t *testing.T) {
|
||||||
c1 := NewContainer()
|
c1 := New()
|
||||||
d1 := c1.CompileData()
|
d1 := c1.CompileData()
|
||||||
if len(d1) > 0 {
|
if len(d1) > 0 {
|
||||||
t.Fatalf("empty container should not hold any data")
|
t.Fatalf("empty container should not hold any data")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeprecated(t *testing.T) {
|
||||||
|
NewContainer(utils.DuplicateBytes(testData))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue