Skip to content

Commit

Permalink
Add divide chunk fn (#40)
Browse files Browse the repository at this point in the history
* add divide chunk file

* add readZipContent fn

* fmt

* change zip pkg name

* review

* review changes

* add tests

* lint

* fix syntax

* improve test
  • Loading branch information
pivilartisant authored Jul 23, 2024
1 parent 3a19751 commit c3520d7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
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)
})
}
}

0 comments on commit c3520d7

Please sign in to comment.