github.com/onflow/atree@v0.6.0/flag_test.go (about) 1 /* 2 * Atree - Scalable Arrays and Ordered Maps 3 * 4 * Copyright 2021 Dapper Labs, Inc. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package atree 20 21 import ( 22 "testing" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestFlagIsRoot(t *testing.T) { 28 for i := 0; i <= 255; i++ { 29 if i >= 0x80 { 30 require.True(t, isRoot(byte(i))) 31 } else { 32 require.False(t, isRoot(byte(i))) 33 } 34 } 35 } 36 37 func TestFlagSetRoot(t *testing.T) { 38 for i := 0; i <= 255; i++ { 39 require.True(t, isRoot(setRoot(byte(i)))) 40 } 41 } 42 43 func TestFlagHasPointers(t *testing.T) { 44 for i := 0; i <= 255; i++ { 45 if byte(i)&maskSlabHasPointers != 0 { 46 require.True(t, hasPointers(byte(i))) 47 } else { 48 require.False(t, hasPointers(byte(i))) 49 } 50 } 51 } 52 53 func TestFlagSetHasPointers(t *testing.T) { 54 for i := 0; i <= 255; i++ { 55 require.True(t, hasPointers(setHasPointers(byte(i)))) 56 } 57 } 58 59 func TestFlagHasSizeLimit(t *testing.T) { 60 for i := 0; i <= 255; i++ { 61 if byte(i)&maskSlabAnySize == 0 { 62 require.True(t, hasSizeLimit(byte(i))) 63 } else { 64 require.False(t, hasSizeLimit(byte(i))) 65 } 66 } 67 } 68 69 func TestFlagSetNoSizeLimit(t *testing.T) { 70 for i := 0; i <= 255; i++ { 71 f := setNoSizeLimit(byte(i)) 72 require.False(t, hasSizeLimit(f)) 73 } 74 } 75 76 func TestFlagGetSlabType(t *testing.T) { 77 for i := 0; i <= 255; i++ { 78 arrayFlag := byte(i) & 0b111_00111 79 mapFlag := arrayFlag | 0b000_01000 80 storableFlag := mapFlag | 0b000_11111 81 82 require.Equal(t, slabArray, getSlabType(arrayFlag)) 83 require.Equal(t, slabMap, getSlabType(mapFlag)) 84 require.Equal(t, slabStorable, getSlabType(storableFlag)) 85 } 86 } 87 88 func TestFlagGetSlabArrayType(t *testing.T) { 89 for i := 0; i <= 255; i++ { 90 arrayDataFlag := byte(i) & 0b111_00000 91 arrayMetaFlag := arrayDataFlag | 0b000_00001 92 arrayLargeImmutableArrayFlag := arrayDataFlag | 0b000_00010 93 basicArrayFlag := arrayDataFlag | 0b000_00011 94 95 require.Equal(t, slabArrayData, getSlabArrayType(arrayDataFlag)) 96 require.Equal(t, slabArrayMeta, getSlabArrayType(arrayMetaFlag)) 97 require.Equal(t, slabLargeImmutableArray, getSlabArrayType(arrayLargeImmutableArrayFlag)) 98 require.Equal(t, slabBasicArray, getSlabArrayType(basicArrayFlag)) 99 } 100 }