Skip to content

Commit

Permalink
use alecthomas asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanEngelbrecht committed Apr 14, 2024
1 parent be456c3 commit 3ea7a72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 52 deletions.
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ cloud.google.com/go/vpcaccess v1.7.6/go.mod h1:BV6tTobbojd2AhrEOBLfywFUJlFU63or5
cloud.google.com/go/webrisk v1.9.6/go.mod h1:YzrDCXBOpnC64+GRRpSXPMQSvR8I4r5YO78y7A/T0Ac=
cloud.google.com/go/websecurityscanner v1.6.6/go.mod h1:zjsc4h9nV1sUxuSMurR2v3gJwWKYorJ+Nanm+1/w6G0=
cloud.google.com/go/workflows v1.12.5/go.mod h1:KbK5/Ef28G8MKLXcsvt/laH1Vka4CKeQj0I1/wEiByo=
github.com/alecthomas/assert/v2 v2.8.1 h1:YCxnYR6jjpfnEK5AK5SysALKdUEBPGH4Y7As6tBnDw0=
github.com/alecthomas/assert/v2 v2.8.1/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
10 changes: 9 additions & 1 deletion longtaillib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ module github.com/DanEngelbrecht/golongtail/longtaillib

go 1.22.0

require github.com/pkg/errors v0.9.1
require (
github.com/alecthomas/assert/v2 v2.8.1
github.com/pkg/errors v0.9.1
)

require (
github.com/alecthomas/repr v0.4.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
)

retract v0.0.22
6 changes: 6 additions & 0 deletions longtaillib/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
github.com/alecthomas/assert/v2 v2.8.1 h1:YCxnYR6jjpfnEK5AK5SysALKdUEBPGH4Y7As6tBnDw0=
github.com/alecthomas/assert/v2 v2.8.1/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
76 changes: 25 additions & 51 deletions longtaillib/longtaillib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sort"
"sync"
"testing"

"github.com/alecthomas/assert/v2"
)

type testProgress struct {
Expand Down Expand Up @@ -128,18 +130,13 @@ func TestInMemStorage(t *testing.T) {
defer storageAPI.Dispose()
myString := "my string"
err := storageAPI.WriteToStorage("folder", "file", []byte(myString))
if err != nil {
t.Errorf("WriteToStorage() %s", err)
}
assert.NoError(t, err)

rbytes, err := storageAPI.ReadFromStorage("folder", "file")
if err != nil {
t.Errorf("ReadFromStorage() %s", err)
}
assert.NoError(t, err)

testString := string(rbytes)
if myString != testString {
t.Errorf("ReadFromStorage() %s != %s", rbytes, testString)
}
assert.Equal(t, myString, testString)
}

func TestAPICreate(t *testing.T) {
Expand Down Expand Up @@ -204,70 +201,47 @@ func createStoredBlock(chunkCount uint32, hashIdentifier uint32, hashOffset uint
}

func validateStoredBlock(t *testing.T, storedBlock Longtail_StoredBlock, hashIdentifier uint32) {
if !storedBlock.IsValid() {
t.Error("validateStoredBlock() is invalid")
}
if storedBlock.cStoredBlock == nil {
t.Errorf("validateStoredBlock() %p == %p", storedBlock, Longtail_StoredBlock{cStoredBlock: nil})
}
assert.True(t, storedBlock.IsValid())
assert.True(t, storedBlock.cStoredBlock != nil)
blockIndex := storedBlock.GetBlockIndex()

b, _ := WriteBlockIndexToBuffer(blockIndex)
defer b.Dispose()
bi2, _ := ReadBlockIndexFromBuffer(b.ToBuffer())
bi2.Dispose()

if blockIndex.GetHashIdentifier() != hashIdentifier {
t.Errorf("validateStoredBlock() %d == %d", blockIndex.GetHashIdentifier(), hashIdentifier)
}
assert.Equal(t, blockIndex.GetHashIdentifier(), hashIdentifier)
chunkCount := blockIndex.GetChunkCount()
if blockIndex.GetBlockHash() != uint64(0xdeadbeef500177aa)+uint64(chunkCount) {
t.Errorf("validateStoredBlock() %d != %d", uint64(0xdeadbeef500177aa)+uint64(chunkCount), blockIndex.GetBlockHash())
}
if blockIndex.GetBlockHash() != storedBlock.GetBlockHash() {
t.Errorf("validateStoredBlock() %d != %d", storedBlock.GetBlockHash(), blockIndex.GetBlockHash())
}
if blockIndex.GetTag() != chunkCount+uint32(10000) {
t.Errorf("validateStoredBlock() %d != %d", chunkCount+uint32(10000), blockIndex.GetTag())
}

assert.Equal(t, blockIndex.GetBlockHash(), uint64(0xdeadbeef500177aa)+uint64(chunkCount))
assert.Equal(t, blockIndex.GetBlockHash(), storedBlock.GetBlockHash())

assert.Equal(t, blockIndex.GetTag(), chunkCount+uint32(10000))

chunkHashes := blockIndex.GetChunkHashes()
if uint32(len(chunkHashes)) != chunkCount {
t.Errorf("validateStoredBlock() %d != %d", chunkCount, uint32(len(chunkHashes)))
}
assert.Equal(t, uint32(len(chunkHashes)), chunkCount)

chunkSizes := blockIndex.GetChunkSizes()
if uint32(len(chunkSizes)) != chunkCount {
t.Errorf("validateStoredBlock() %d != %d", chunkCount, uint32(len(chunkSizes)))
}
assert.Equal(t, uint32(len(chunkSizes)), chunkCount)

blockOffset := uint32(0)
for index := range chunkHashes {
if chunkHashes[index] != uint64(index+1)*4711 {
t.Errorf("validateStoredBlock() %d != %d", uint64(index)*4711, chunkHashes[index])
}
if chunkSizes[index] != uint32(index+1)*10 {
t.Errorf("validateStoredBlock() %d != %d", uint32(index)*10, chunkSizes[index])
}
assert.Equal(t, chunkHashes[index], uint64(index+1)*4711)
assert.Equal(t, chunkSizes[index], uint32(index+1)*10)
blockOffset += uint32(chunkSizes[index])
}
blockData := storedBlock.GetChunksBlockData()
if uint32(len(blockData)) != blockOffset {
t.Errorf("validateStoredBlock() %d != %d", uint32(len(blockData)), blockOffset)
}
assert.Equal(t, uint32(len(blockData)), blockOffset)
blockOffset = 0
for chunkIndex := range chunkHashes {
for index := uint32(0); index < uint32(chunkSizes[chunkIndex]); index++ {
if blockData[blockOffset+index] != uint8(chunkIndex+1) {
t.Errorf("validateStoredBlock() %d != %d", uint8(chunkIndex+1), blockData[blockOffset+index])
}
assert.Equal(t, blockData[blockOffset+index], uint8(chunkIndex+1))
}
blockOffset += uint32(chunkSizes[chunkIndex])
}
if blockOffset > uint32(storedBlock.GetBlockSize()) {
t.Errorf("validateStoredBlock() %d < %d", blockOffset, storedBlock.GetBlockSize())
}
assert.True(t, blockOffset <= uint32(storedBlock.GetBlockSize()))
indexCopy, err := blockIndex.Copy()
if err != nil {
t.Errorf("validateStoredBlock() blockIndex.Copy() %s", err)
}
assert.NoError(t, err)
indexCopy.Dispose()
}

Expand Down

0 comments on commit 3ea7a72

Please sign in to comment.