116 lines
2.7 KiB
Go
116 lines
2.7 KiB
Go
|
package meta_test
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"git.bit5.ru/backend/meta"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestFieldsMask(t *testing.T) {
|
||
|
t.Run("IsFilled", func(t *testing.T) {
|
||
|
t.Run("default value", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
|
||
|
actualIsFilled := mask.IsFilled()
|
||
|
require.False(t, actualIsFilled)
|
||
|
})
|
||
|
t.Run("filled value", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
mask.SetItemFromInt64(0, 1)
|
||
|
|
||
|
actualIsFilled := mask.IsFilled()
|
||
|
require.True(t, actualIsFilled)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
t.Run("FieldChanged", func(t *testing.T) {
|
||
|
t.Run("field not changed", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
var fieldIndex uint64 = 4
|
||
|
|
||
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
||
|
require.False(t, fieldChanged)
|
||
|
})
|
||
|
t.Run("filled changed #1", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
var fieldIndex uint64 = 4
|
||
|
|
||
|
mask.SetItemFromInt64(0, 16)
|
||
|
|
||
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
||
|
require.True(t, fieldChanged)
|
||
|
})
|
||
|
t.Run("filled changed #2", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
var fieldIndex uint64 = 68
|
||
|
|
||
|
mask.SetItemFromInt64(1, 16)
|
||
|
|
||
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
||
|
require.True(t, fieldChanged)
|
||
|
})
|
||
|
t.Run("filled changed #3", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
var fieldIndex uint64 = 131
|
||
|
|
||
|
mask.SetItemFromInt64(2, 8)
|
||
|
|
||
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
||
|
require.True(t, fieldChanged)
|
||
|
})
|
||
|
t.Run("filled changed #4", func(t *testing.T) {
|
||
|
var mask meta.FieldsMask
|
||
|
var fieldIndex uint64 = 194
|
||
|
|
||
|
mask.SetItemFromInt64(3, 4)
|
||
|
|
||
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
||
|
require.True(t, fieldChanged)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestFieldsMaskItem(t *testing.T) {
|
||
|
t.Run("FieldIsDirty", func(t *testing.T) {
|
||
|
cases := []struct {
|
||
|
maskItem meta.FieldsMaskItem
|
||
|
expectedDirtyIndexes []uint64
|
||
|
}{
|
||
|
{
|
||
|
maskItem: 0,
|
||
|
expectedDirtyIndexes: []uint64{},
|
||
|
},
|
||
|
{
|
||
|
maskItem: 1, // 0b0001
|
||
|
expectedDirtyIndexes: []uint64{0},
|
||
|
},
|
||
|
{
|
||
|
maskItem: 2, // 0b0010
|
||
|
expectedDirtyIndexes: []uint64{1},
|
||
|
},
|
||
|
{
|
||
|
maskItem: 10, // 0b1010
|
||
|
expectedDirtyIndexes: []uint64{1, 3},
|
||
|
},
|
||
|
{
|
||
|
maskItem: 11, // 0b1011
|
||
|
expectedDirtyIndexes: []uint64{0, 1, 3},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for i, c := range cases {
|
||
|
actualDirtyIndexes := make([]uint64, 0, meta.FieldsMaskItemBitSize)
|
||
|
for j := uint64(0); j < meta.FieldsMaskItemBitSize; j++ {
|
||
|
if c.maskItem.FieldIsDirty(j) {
|
||
|
actualDirtyIndexes = append(actualDirtyIndexes, j)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
assert.Equalf(t, c.expectedDirtyIndexes, actualDirtyIndexes, "case #%d", i)
|
||
|
}
|
||
|
})
|
||
|
}
|