github.com/koko1123/flow-go-1@v0.29.6/model/flow/chunk_test.go (about) 1 package flow_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/koko1123/flow-go-1/model/flow" 9 "github.com/koko1123/flow-go-1/utils/unittest" 10 ) 11 12 // TestChunkList_ByIndex evaluates reliability of ByIndex method against within and 13 // out of range indices 14 func TestChunkList_ByIndex(t *testing.T) { 15 // creates a chunk list with the size of 10 16 var chunkList flow.ChunkList = make([]*flow.Chunk, 10) 17 18 // an out of index chunk by index 19 _, ok := chunkList.ByIndex(11) 20 require.False(t, ok) 21 22 // a within range chunk by index 23 _, ok = chunkList.ByIndex(1) 24 require.True(t, ok) 25 } 26 27 // TestDistinctChunkIDs_EmptyChunks evaluates that two empty chunks 28 // with the distinct block ids would have distinct chunk ids. 29 func TestDistinctChunkIDs_EmptyChunks(t *testing.T) { 30 // generates two random block ids and requires them 31 // being distinct 32 blockIdA := unittest.IdentifierFixture() 33 blockIdB := unittest.IdentifierFixture() 34 require.NotEqual(t, blockIdA, blockIdB) 35 36 // generates a chunk associated with each block id 37 chunkA := &flow.Chunk{ 38 ChunkBody: flow.ChunkBody{ 39 BlockID: blockIdA, 40 }, 41 } 42 43 chunkB := &flow.Chunk{ 44 ChunkBody: flow.ChunkBody{ 45 BlockID: blockIdB, 46 }, 47 } 48 49 require.NotEqual(t, chunkA.ID(), chunkB.ID()) 50 } 51 52 // TestDistinctChunkIDs_FullChunks evaluates that two full chunks 53 // with completely identical fields but distinct block ids have 54 // distinct chunk ids. 55 func TestDistinctChunkIDs_FullChunks(t *testing.T) { 56 // generates two random block ids and requires them 57 // being distinct 58 blockIdA := unittest.IdentifierFixture() 59 blockIdB := unittest.IdentifierFixture() 60 require.NotEqual(t, blockIdA, blockIdB) 61 62 // generates a chunk associated with blockA 63 chunkA := unittest.ChunkFixture(blockIdA, 42) 64 65 // generates a deep copy of chunkA in chunkB 66 chunkB := *chunkA 67 68 // since chunkB is a deep copy of chunkA their 69 // chunk ids should be the same 70 require.Equal(t, chunkA.ID(), chunkB.ID()) 71 72 // changes block id in chunkB 73 chunkB.BlockID = blockIdB 74 75 // chunks with distinct block ids should have distinct chunk ids 76 require.NotEqual(t, chunkA.ID(), chunkB.ID()) 77 } 78 79 // TestChunkList_Indices evaluates the Indices method of ChunkList on lists of different sizes. 80 func TestChunkList_Indices(t *testing.T) { 81 cl := unittest.ChunkListFixture(5, unittest.IdentifierFixture()) 82 t.Run("empty chunk subset indices", func(t *testing.T) { 83 // subset of chunk list that is empty should return an empty list 84 subset := flow.ChunkList{} 85 indices := subset.Indices() 86 require.Len(t, indices, 0) 87 }) 88 89 t.Run("single chunk subset indices", func(t *testing.T) { 90 // subset of chunk list that contains chunk index of zero, should 91 // return a uint64 slice that only contains chunk index of zero. 92 subset := cl[:1] 93 indices := subset.Indices() 94 require.Len(t, indices, 1) 95 require.Contains(t, indices, uint64(0)) 96 }) 97 98 t.Run("multiple chunk subset indices", func(t *testing.T) { 99 // subset that only contains even chunk indices, should return 100 // a uint64 slice that only contains even chunk indices 101 subset := flow.ChunkList{cl[0], cl[2], cl[4]} 102 indices := subset.Indices() 103 require.Len(t, indices, 3) 104 require.Contains(t, indices, uint64(0), uint64(2), uint64(4)) 105 }) 106 }