file-store/pkg/erasureencode/params_test.go

154 lines
4.9 KiB
Go
Raw Normal View History

2022-08-24 02:54:01 +00:00
package erasureencode
import (
"fmt"
"testing"
)
func TestPlan_EvenStride_Full(t *testing.T) {
params := Params{
Size: 18,
Stride: 2,
Shards: 3,
Parity: 2,
}
expected := []ChunkShardMeta{
ChunkShardMeta{Shard: 3, ShardOffset: 1, Chunk: 0, ChunkOffset: 3, GlobalOffset: 7, Size: 1},
ChunkShardMeta{Shard: 4, ShardOffset: 0, Chunk: 1, ChunkOffset: 2, GlobalOffset: 8, Size: 2},
ChunkShardMeta{Shard: 5, ShardOffset: 0, Chunk: 2, ChunkOffset: 2, GlobalOffset: 10, Size: 2},
ChunkShardMeta{Shard: 6, ShardOffset: 0, Chunk: 0, ChunkOffset: 4, GlobalOffset: 12, Size: 2},
2022-08-24 02:54:01 +00:00
}
actual := params.Plan(7, 7)
if len(actual) != len(expected) {
t.Errorf(`Expected output to have length of %x, got %x`, len(expected), len(actual))
}
for i, actualItem := range actual {
if fmt.Sprintf("%#v", actualItem) != fmt.Sprintf("%#v", expected[i]) {
t.Errorf("Expected \n%#v to equal \n%#v at %d", actualItem, expected[i], i)
2022-08-24 02:54:01 +00:00
} else {
// fmt.Printf("ok: %#v\n", actualItem)
}
}
}
func TestPlan_EvenStride_Short2(t *testing.T) {
params := Params{
Size: 16,
Stride: 2,
Shards: 3,
Parity: 2,
}
expected := []ChunkShardMeta{
ChunkShardMeta{Shard: 3, ShardOffset: 1, Chunk: 0, ChunkOffset: 3, GlobalOffset: 7, Size: 1},
ChunkShardMeta{Shard: 4, ShardOffset: 0, Chunk: 1, ChunkOffset: 2, GlobalOffset: 8, Size: 2},
ChunkShardMeta{Shard: 5, ShardOffset: 0, Chunk: 2, ChunkOffset: 2, GlobalOffset: 10, Size: 2},
ChunkShardMeta{Shard: 6, ShardOffset: 0, Chunk: 0, ChunkOffset: 4, GlobalOffset: 12, Size: 2},
2022-08-24 02:54:01 +00:00
}
actual := params.Plan(7, 7)
if len(actual) != len(expected) {
t.Errorf(`Expected output to have length of %x, got %x`, len(expected), len(actual))
}
for i, actualItem := range actual {
if fmt.Sprintf("%#v", actualItem) != fmt.Sprintf("%#v", expected[i]) {
t.Errorf(`Expected %#v to equal %#v at %d`, actualItem, expected[i], i)
} else {
// fmt.Printf("ok: %#v\n", actualItem)
}
}
}
func TestPlan_EvenStride_Short1(t *testing.T) {
params := Params{
Size: 17,
Stride: 2,
Shards: 3,
Parity: 2,
}
expected := []ChunkShardMeta{
ChunkShardMeta{Shard: 3, ShardOffset: 1, Chunk: 0, ChunkOffset: 3, GlobalOffset: 7, Size: 1},
ChunkShardMeta{Shard: 4, ShardOffset: 0, Chunk: 1, ChunkOffset: 2, GlobalOffset: 8, Size: 2},
ChunkShardMeta{Shard: 5, ShardOffset: 0, Chunk: 2, ChunkOffset: 2, GlobalOffset: 10, Size: 2},
ChunkShardMeta{Shard: 6, ShardOffset: 0, Chunk: 0, ChunkOffset: 4, GlobalOffset: 12, Size: 2},
2022-08-24 02:54:01 +00:00
}
actual := params.Plan(7, 7)
if len(actual) != len(expected) {
t.Errorf(`Expected output to have length of %x, got %x`, len(expected), len(actual))
}
for i, actualItem := range actual {
if fmt.Sprintf("%#v", actualItem) != fmt.Sprintf("%#v", expected[i]) {
t.Errorf(`Expected %#v to equal %#v at %d`, actualItem, expected[i], i)
} else {
// fmt.Printf("ok: %#v\n", actualItem)
}
}
}
func TestPlan_OddStride_Short1(t *testing.T) {
params := Params{
Size: 13,
Stride: 2,
Shards: 3,
Parity: 2,
}
expected := []ChunkShardMeta{
ChunkShardMeta{Shard: 3, ShardOffset: 1, Chunk: 0, ChunkOffset: 3, GlobalOffset: 7, Size: 1},
ChunkShardMeta{Shard: 4, ShardOffset: 0, Chunk: 1, ChunkOffset: 2, GlobalOffset: 8, Size: 2},
ChunkShardMeta{Shard: 5, ShardOffset: 0, Chunk: 2, ChunkOffset: 2, GlobalOffset: 10, Size: 2},
ChunkShardMeta{Shard: 6, ShardOffset: 0, Chunk: 0, ChunkOffset: 4, GlobalOffset: 12, Size: 1},
ChunkShardMeta{Shard: 7, ShardOffset: 0, Chunk: 1, ChunkOffset: 4, GlobalOffset: 13, Size: 1},
2022-08-24 02:54:01 +00:00
}
actual := params.Plan(7, 7)
if len(actual) != len(expected) {
t.Errorf(`Expected output to have length of %x, got %x`, len(expected), len(actual))
}
for i, actualItem := range actual {
if fmt.Sprintf("%#v", actualItem) != fmt.Sprintf("%#v", expected[i]) {
t.Errorf(`Expected %#v to equal %#v at %d`, actualItem, expected[i], i)
} else {
// fmt.Printf("ok: %#v\n", actualItem)
}
}
}
func TestPlan_OddStride_Full(t *testing.T) {
params := Params{
Size: 14,
Stride: 2,
Shards: 3,
Parity: 2,
}
expected := []ChunkShardMeta{
ChunkShardMeta{Shard: 3, ShardOffset: 1, Chunk: 0, ChunkOffset: 3, GlobalOffset: 7, Size: 1},
ChunkShardMeta{Shard: 4, ShardOffset: 0, Chunk: 1, ChunkOffset: 2, GlobalOffset: 8, Size: 2},
ChunkShardMeta{Shard: 5, ShardOffset: 0, Chunk: 2, ChunkOffset: 2, GlobalOffset: 10, Size: 2},
ChunkShardMeta{Shard: 6, ShardOffset: 0, Chunk: 0, ChunkOffset: 4, GlobalOffset: 12, Size: 1},
ChunkShardMeta{Shard: 7, ShardOffset: 0, Chunk: 1, ChunkOffset: 4, GlobalOffset: 13, Size: 1},
2022-08-24 02:54:01 +00:00
}
actual := params.Plan(7, 7)
if len(actual) != len(expected) {
t.Errorf(`Expected output to have length of %x, got %x`, len(expected), len(actual))
}
for i, actualItem := range actual {
if fmt.Sprintf("%#v", actualItem) != fmt.Sprintf("%#v", expected[i]) {
t.Errorf(`Expected %#v to equal %#v at %d`, actualItem, expected[i], i)
} else {
// fmt.Printf("ok: %#v\n", actualItem)
}
}
}