github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/block_info_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package objectio 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestEncodeInfoHeader(t *testing.T) { 25 h := InfoHeader{ 26 Type: BlockInfoType, 27 Version: V1, 28 } 29 require.Equal(t, h, DecodeInfoHeader(EncodeInfoHeader(h))) 30 } 31 32 func FuzzEncodeInfoHeader(f *testing.F) { 33 f.Fuzz(func(t *testing.T, typ, v uint16) { 34 h := InfoHeader{ 35 Type: typ, 36 Version: v, 37 } 38 require.Equal(t, h, DecodeInfoHeader(EncodeInfoHeader(h))) 39 }) 40 } 41 42 func TestBlockInfoSlice_Append(t *testing.T) { 43 var s BlockInfoSlice 44 s.AppendBlockInfo(BlockInfo{BlockID: types.Blockid{1}}) 45 require.Equal(t, 1, s.Len()) 46 require.Equal(t, BlockInfoSize, s.Size()) 47 require.Equal(t, &BlockInfo{BlockID: types.Blockid{1}}, s.Get(0)) 48 49 var s2 BlockInfoSlice 50 s2.AppendBlockInfo(BlockInfo{BlockID: types.Blockid{1}}) 51 s2.AppendBlockInfo(BlockInfo{BlockID: types.Blockid{2}}) 52 require.Equal(t, 2, s2.Len()) 53 require.Equal(t, BlockInfoSize*2, s2.Size()) 54 require.Equal(t, &BlockInfo{BlockID: types.Blockid{1}}, s2.Get(0)) 55 require.Equal(t, &BlockInfo{BlockID: types.Blockid{2}}, s2.Get(1)) 56 57 var s3 BlockInfoSlice 58 s3.Append(EncodeBlockInfo(BlockInfo{BlockID: types.Blockid{1}})) 59 s3.Append(EncodeBlockInfo(BlockInfo{BlockID: types.Blockid{2}})) 60 require.Equal(t, 2, s3.Len()) 61 require.Equal(t, BlockInfoSize*2, s3.Size()) 62 require.Equal(t, &BlockInfo{BlockID: types.Blockid{1}}, s3.Get(0)) 63 require.Equal(t, &BlockInfo{BlockID: types.Blockid{2}}, s3.Get(1)) 64 } 65 66 func intToBlockid(i int32) types.Blockid { 67 return types.Blockid{ 68 byte(i >> 24), 69 byte(i >> 16), 70 byte(i >> 8), 71 byte(i), 72 } 73 } 74 75 func TestBlockInfoSliceTraverse(t *testing.T) { 76 var s BlockInfoSlice 77 for i := int32(0); i < 1000; i++ { 78 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(i)}) 79 } 80 require.Equal(t, 1000, s.Len()) 81 82 for i := 0; i < s.Len(); i++ { 83 blkInfo := s.Get(i) 84 require.Equal(t, intToBlockid(int32(i)), blkInfo.BlockID) 85 require.Equal(t, false, blkInfo.CanRemote) 86 blkInfo.CanRemote = true 87 } 88 89 for i := 0; i < s.Len(); i++ { 90 require.Equal(t, true, s.Get(i).CanRemote) 91 } 92 93 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(1000), CanRemote: true}) 94 95 for i := 0; i < s.Len(); i++ { 96 require.Equal(t, true, s.Get(i).CanRemote) 97 } 98 } 99 100 func TestBytesToBlockInfoSlice(t *testing.T) { 101 bs := make([]byte, 0) 102 for i := 0; i < 1000; i++ { 103 bs = append(bs, EncodeBlockInfo(BlockInfo{BlockID: intToBlockid(int32(i))})...) 104 } 105 106 s := BlockInfoSlice(bs) 107 require.Equal(t, 1000, s.Len()) 108 109 for i := 0; i < s.Len(); i++ { 110 blkInfo := s.Get(i) 111 require.Equal(t, intToBlockid(int32(i)), blkInfo.BlockID) 112 require.Equal(t, false, blkInfo.CanRemote) 113 blkInfo.CanRemote = true 114 } 115 116 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(1000), CanRemote: true}) 117 118 for i := 0; i < s.Len(); i++ { 119 require.Equal(t, true, s.Get(i).CanRemote) 120 } 121 122 require.Equal(t, 1000*BlockInfoSize, len(bs)) 123 require.Equal(t, s.Size(), len(bs)+BlockInfoSize) 124 bs = s 125 require.Equal(t, 1001*BlockInfoSize, len(bs)) 126 require.Equal(t, s.GetAllBytes(), bs) 127 128 s.Get(999).CanRemote = false 129 require.Equal(t, false, s.Get(999).CanRemote) 130 blkInfo := DecodeBlockInfo(bs[999*BlockInfoSize:]) 131 require.Equal(t, false, blkInfo.CanRemote) 132 } 133 134 func TestBlockInfoSlice_Slice(t *testing.T) { 135 s := make(BlockInfoSlice, 0) 136 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(0)}) 137 // Get BlockInfoSlice[:1] 138 require.Equal(t, s.GetBytes(0), []byte(s.Slice(0, 1))) 139 // Get BlockInfoSlice[1:] 140 require.Equal(t, []byte{}, []byte(s.Slice(1, s.Len()))) 141 142 s = s.Slice(1, s.Len()) 143 require.Equal(t, 0, len(s)) 144 require.Equal(t, 0, s.Len()) 145 146 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(1)}) 147 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(2)}) 148 require.Equal(t, s.GetBytes(0), []byte(s.Slice(0, 1))) 149 require.Equal(t, s.GetBytes(1), []byte(s.Slice(1, s.Len()))) 150 } 151 152 func TestBlockInfoSlice_GetBytes(t *testing.T) { 153 s := make(BlockInfoSlice, 0, 10) 154 for i := 0; i < 10; i++ { 155 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(int32(i))}) 156 } 157 158 for i := 0; i < 10; i++ { 159 require.Equal(t, EncodeBlockInfo(BlockInfo{BlockID: intToBlockid(int32(i))}), s.GetBytes(i)) 160 require.Equal(t, &BlockInfo{BlockID: intToBlockid(int32(i))}, DecodeBlockInfo(s.GetBytes(i))) 161 require.Equal(t, &BlockInfo{BlockID: intToBlockid(int32(i))}, s.Get(i)) 162 require.Equal(t, EncodeBlockInfo(BlockInfo{BlockID: intToBlockid(int32(i))}), EncodeBlockInfo(*s.Get(i))) 163 } 164 } 165 166 func TestBlockInfoSlice_Remove(t *testing.T) { 167 s := make(BlockInfoSlice, 0, 10) 168 for i := 0; i < 10; i++ { 169 s.AppendBlockInfo(BlockInfo{BlockID: intToBlockid(int32(i))}) 170 } 171 172 curr := 0 173 for i := 0; i < 10; i++ { 174 blk := s.Get(i) 175 if blk.BlockID == intToBlockid(0) { 176 // remove the first element 177 continue 178 } 179 s.Set(curr, blk) 180 curr++ 181 } 182 183 s = s.Slice(0, curr) 184 require.Equal(t, 9, s.Len()) 185 for i := 0; i < 9; i++ { 186 require.Equal(t, intToBlockid(int32(i+1)), s.Get(i).BlockID) 187 } 188 }