mirror of
https://github.com/rclone/rclone.git
synced 2025-08-07 21:18:43 +02:00
This commit modernizes Go usage. This was done with: go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... Then files needed to be `go fmt`ed and a few comments needed to be restored. The modernizations include replacing - if/else conditional assignment by a call to the built-in min or max functions added in go1.21 - sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21 - interface{} by the 'any' type added in go1.18 - append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21 - loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21 - []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19 - append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21 - a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
package mrhash_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/rclone/rclone/backend/mailru/mrhash"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func testChunk(t *testing.T, chunk int) {
|
|
data := make([]byte, chunk)
|
|
for i := range chunk {
|
|
data[i] = 'A'
|
|
}
|
|
for _, test := range []struct {
|
|
n int
|
|
want string
|
|
}{
|
|
{0, "0000000000000000000000000000000000000000"},
|
|
{1, "4100000000000000000000000000000000000000"},
|
|
{2, "4141000000000000000000000000000000000000"},
|
|
{19, "4141414141414141414141414141414141414100"},
|
|
{20, "4141414141414141414141414141414141414141"},
|
|
{21, "eb1d05e78a18691a5aa196a6c2b60cd40b5faafb"},
|
|
{22, "037e6d960601118a0639afbeff30fe716c66ed2d"},
|
|
{4096, "45a16aa192502b010280fb5b44274c601a91fd9f"},
|
|
{4194303, "fa019d5bd26498cf6abe35e0d61801bf19bf704b"},
|
|
{4194304, "5ed0e07aa6ea5c1beb9402b4d807258f27d40773"},
|
|
{4194305, "67bd0b9247db92e0e7d7e29a0947a50fedcb5452"},
|
|
{8388607, "41a8e2eb044c2e242971b5445d7be2a13fc0dd84"},
|
|
{8388608, "267a970917c624c11fe624276ec60233a66dc2c0"},
|
|
{8388609, "37b60b308d553d2732aefb62b3ea88f74acfa13f"},
|
|
} {
|
|
d := mrhash.New()
|
|
var toWrite int
|
|
for toWrite = test.n; toWrite >= chunk; toWrite -= chunk {
|
|
n, err := d.Write(data)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, chunk, n)
|
|
}
|
|
n, err := d.Write(data[:toWrite])
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, toWrite, n)
|
|
got1 := hex.EncodeToString(d.Sum(nil))
|
|
assert.Equal(t, test.want, got1, fmt.Sprintf("when testing length %d", n))
|
|
got2 := hex.EncodeToString(d.Sum(nil))
|
|
assert.Equal(t, test.want, got2, fmt.Sprintf("when testing length %d (2nd sum)", n))
|
|
}
|
|
}
|
|
|
|
func TestHashChunk16M(t *testing.T) { testChunk(t, 16*1024*1024) }
|
|
func TestHashChunk8M(t *testing.T) { testChunk(t, 8*1024*1024) }
|
|
func TestHashChunk4M(t *testing.T) { testChunk(t, 4*1024*1024) }
|
|
func TestHashChunk2M(t *testing.T) { testChunk(t, 2*1024*1024) }
|
|
func TestHashChunk1M(t *testing.T) { testChunk(t, 1*1024*1024) }
|
|
func TestHashChunk64k(t *testing.T) { testChunk(t, 64*1024) }
|
|
func TestHashChunk32k(t *testing.T) { testChunk(t, 32*1024) }
|
|
func TestHashChunk2048(t *testing.T) { testChunk(t, 2048) }
|
|
func TestHashChunk2047(t *testing.T) { testChunk(t, 2047) }
|
|
|
|
func TestSumCalledTwice(t *testing.T) {
|
|
d := mrhash.New()
|
|
assert.NotPanics(t, func() { d.Sum(nil) })
|
|
d.Reset()
|
|
assert.NotPanics(t, func() { d.Sum(nil) })
|
|
assert.NotPanics(t, func() { d.Sum(nil) })
|
|
_, _ = d.Write([]byte{1})
|
|
assert.NotPanics(t, func() { d.Sum(nil) })
|
|
}
|
|
|
|
func TestSize(t *testing.T) {
|
|
d := mrhash.New()
|
|
assert.Equal(t, 20, d.Size())
|
|
}
|
|
|
|
func TestBlockSize(t *testing.T) {
|
|
d := mrhash.New()
|
|
assert.Equal(t, 64, d.BlockSize())
|
|
}
|