Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add divide chunk fn #40

Merged
merged 10 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/massalabs/DeWeb

go 1.22
go 1.22

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 20 additions & 0 deletions pkg/website/chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package chunk

func DivideIntoChunks(data []byte, chunkSize int) [][]byte {
if data == nil || chunkSize <= 0 {
return nil
}

var chunks [][]byte

for i := 0; i < len(data); i += chunkSize {
end := i + chunkSize
if end > len(data) {
end = len(data)
}

chunks = append(chunks, data[i:end])
}

return chunks
}
38 changes: 38 additions & 0 deletions pkg/website/chunk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package chunk

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestChunk(t *testing.T) {
tests := []struct {
name string
data []byte
chunkSize int
}{
{"Empty byte array", []byte(nil), 2},
{"Nil chunkSize", []byte("Hello"), 0},
{"Nominal test case", []byte("Hello, World!"), 1},
{"Median", []byte("Hello, World!"), 4},
{"Big byte array", []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit."), 16},
{"Small byte array", []byte("hello"), 32},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
chunks := DivideIntoChunks(test.data, test.chunkSize)

if test.data == nil || test.chunkSize <= 0 {
assert.Nil(t, chunks)

return
}

expectedLen := (len(test.data) + test.chunkSize - 1) / test.chunkSize

assert.Equal(t, len(chunks), expectedLen)
})
}
}