github.com/koko1123/flow-go-1@v0.29.6/ledger/ledger_test.go (about) 1 package ledger 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 // this benchmark can run with this command: 12 // go test -run=CanonicalForm -bench=. 13 14 // nolint 15 func BenchmarkCanonicalForm(b *testing.B) { 16 17 constant := 10 18 19 keyParts := make([]KeyPart, 0, 200) 20 21 for i := 0; i < 16; i++ { 22 keyParts = append(keyParts, KeyPart{}) 23 keyParts[i].Value = []byte("somedomain1") 24 keyParts[i].Type = 1234 25 } 26 27 requiredLen := constant * len(keyParts) 28 for _, kp := range keyParts { 29 requiredLen += len(kp.Value) 30 } 31 32 retval := make([]byte, 0, requiredLen) 33 34 for _, kp := range keyParts { 35 typeNumber := strconv.Itoa(int(kp.Type)) 36 37 retval = append(retval, byte('/')) 38 retval = append(retval, []byte(typeNumber)...) 39 retval = append(retval, byte('/')) 40 retval = append(retval, kp.Value...) 41 } 42 } 43 44 func BenchmarkOriginalCanonicalForm(b *testing.B) { 45 keyParts := make([]KeyPart, 0, 200) 46 47 for i := 0; i < 16; i++ { 48 keyParts = append(keyParts, KeyPart{}) 49 keyParts[i].Value = []byte("somedomain1") 50 keyParts[i].Type = 1234 51 } 52 53 ret := "" 54 55 for _, kp := range keyParts { 56 ret += fmt.Sprintf("/%d/%v", kp.Type, string(kp.Value)) 57 } 58 } 59 60 // TestKeyEquals tests whether keys are equal. 61 // It tests equality of empty, nil, and not-empty keys. 62 // Empty key and nil key should be equal. 63 func TestKeyEquals(t *testing.T) { 64 65 nilKey := (*Key)(nil) 66 emptyKey := &Key{} 67 68 t.Run("nil vs empty", func(t *testing.T) { 69 require.True(t, nilKey.Equals(emptyKey)) 70 require.True(t, emptyKey.Equals(nilKey)) 71 }) 72 73 t.Run("nil vs nil", func(t *testing.T) { 74 require.True(t, nilKey.Equals(nilKey)) 75 }) 76 77 t.Run("empty vs empty", func(t *testing.T) { 78 require.True(t, emptyKey.Equals(emptyKey)) 79 }) 80 81 t.Run("empty vs not-empty", func(t *testing.T) { 82 k := &Key{ 83 KeyParts: []KeyPart{ 84 {0, []byte{0x01, 0x02}}, 85 }, 86 } 87 require.False(t, emptyKey.Equals(k)) 88 require.False(t, k.Equals(emptyKey)) 89 }) 90 91 t.Run("nil vs not-empty", func(t *testing.T) { 92 k := &Key{ 93 KeyParts: []KeyPart{ 94 {0, []byte{0x01, 0x02}}, 95 }, 96 } 97 require.False(t, nilKey.Equals(k)) 98 require.False(t, k.Equals(nilKey)) 99 }) 100 101 t.Run("num of KeyParts different", func(t *testing.T) { 102 k1 := &Key{ 103 KeyParts: []KeyPart{ 104 {0, []byte{0x01, 0x02}}, 105 }, 106 } 107 k2 := &Key{ 108 KeyParts: []KeyPart{ 109 {0, []byte{0x01, 0x02}}, 110 {1, []byte{0x03, 0x04}}, 111 }, 112 } 113 require.False(t, k1.Equals(k2)) 114 require.False(t, k2.Equals(k1)) 115 }) 116 117 t.Run("KeyPart different", func(t *testing.T) { 118 k := &Key{ 119 KeyParts: []KeyPart{ 120 {0, []byte{0x01, 0x02}}, 121 {1, []byte{0x03, 0x04}}, 122 }, 123 } 124 // k1.KeyParts[1].Type is different. 125 k1 := &Key{ 126 KeyParts: []KeyPart{ 127 {0, []byte{0x01, 0x02}}, 128 {2, []byte{0x03, 0x04}}, 129 }, 130 } 131 // k2.KeyParts[1].Value is different (same length). 132 k2 := &Key{ 133 KeyParts: []KeyPart{ 134 {0, []byte{0x01, 0x02}}, 135 {1, []byte{0x03, 0x05}}, 136 }, 137 } 138 // k3.KeyParts[1].Value is different (different length). 139 k3 := &Key{ 140 KeyParts: []KeyPart{ 141 {0, []byte{0x01, 0x02}}, 142 {1, []byte{0x03}}, 143 }, 144 } 145 // k4.KeyParts has different order. 146 k4 := &Key{ 147 KeyParts: []KeyPart{ 148 {1, []byte{0x03, 0x04}}, 149 {0, []byte{0x01, 0x02}}, 150 }, 151 } 152 require.False(t, k.Equals(k1)) 153 require.False(t, k.Equals(k2)) 154 require.False(t, k.Equals(k3)) 155 require.False(t, k.Equals(k4)) 156 }) 157 158 t.Run("same", func(t *testing.T) { 159 k1 := &Key{ 160 KeyParts: []KeyPart{ 161 {0, []byte{0x01, 0x02}}, 162 }, 163 } 164 k2 := &Key{ 165 KeyParts: []KeyPart{ 166 {0, []byte{0x01, 0x02}}, 167 }, 168 } 169 require.True(t, k1.Equals(k2)) 170 require.True(t, k2.Equals(k1)) 171 172 k3 := &Key{ 173 KeyParts: []KeyPart{ 174 {0, []byte{0x01, 0x02}}, 175 {1, []byte{0x03, 0x04}}, 176 }, 177 } 178 k4 := &Key{ 179 KeyParts: []KeyPart{ 180 {0, []byte{0x01, 0x02}}, 181 {1, []byte{0x03, 0x04}}, 182 }, 183 } 184 require.True(t, k3.Equals(k4)) 185 require.True(t, k4.Equals(k3)) 186 }) 187 } 188 189 // TestValueEquals tests whether values are equal. 190 // It tests equality of empty, nil, and not-empty values. 191 // Empty value and nil value should be equal. 192 func TestValueEquals(t *testing.T) { 193 194 nilValue := (Value)(nil) 195 emptyValue := Value{} 196 197 t.Run("nil vs empty", func(t *testing.T) { 198 require.True(t, nilValue.Equals(emptyValue)) 199 require.True(t, emptyValue.Equals(nilValue)) 200 }) 201 202 t.Run("nil vs nil", func(t *testing.T) { 203 require.True(t, nilValue.Equals(nilValue)) 204 }) 205 206 t.Run("empty vs empty", func(t *testing.T) { 207 require.True(t, emptyValue.Equals(emptyValue)) 208 }) 209 210 t.Run("empty vs non-empty", func(t *testing.T) { 211 v := Value{0x01, 0x02} 212 require.False(t, emptyValue.Equals(v)) 213 require.False(t, v.Equals(emptyValue)) 214 }) 215 216 t.Run("nil vs non-empty", func(t *testing.T) { 217 v := Value{0x01, 0x02} 218 require.False(t, nilValue.Equals(v)) 219 require.False(t, v.Equals(nilValue)) 220 }) 221 222 t.Run("length different", func(t *testing.T) { 223 v1 := Value{0x01, 0x02} 224 v2 := Value{0x01, 0x02, 0x03} 225 require.False(t, v1.Equals(v2)) 226 require.False(t, v2.Equals(v1)) 227 }) 228 229 t.Run("data different", func(t *testing.T) { 230 v1 := Value{0x01, 0x02} 231 v2 := Value{0x01, 0x03} 232 require.False(t, v1.Equals(v2)) 233 require.False(t, v2.Equals(v1)) 234 }) 235 236 t.Run("same", func(t *testing.T) { 237 v1 := Value{0x01, 0x02} 238 v2 := Value{0x01, 0x02} 239 require.True(t, v1.Equals(v2)) 240 require.True(t, v2.Equals(v1)) 241 }) 242 }