Repository: pingcap/tidb
File: pkg/util/chunk/column_test.go (Go)

Code before:
	}
	col.ResizeGoDuration(1024, false)
	for i := range 1024 {
		require.Equal(t, time.Duration(0), col.GoDurations()[i])
	}

	col = NewColumn(types.NewFieldType(mysql.TypeDatetime), 1024)
	for range 1024 {
		gt := types.FromDate(rand.Intn(2200), rand.Intn(10)+1, rand.Intn(20)+1, rand.Intn(12), rand.Intn(60), rand.Intn(60), rand.Intn(1000000))
		t := types.NewTime(gt, 0, 0)
		col.AppendTime(t)
	}
	col.ResizeTime(1024, false)
	for i := range 1024 {
		var time types.Time
		require.Equal(t, time, col.Times()[i])
	}
}

func TestReserve(t *testing.T) {
	col := NewColumn(types.NewFieldType(mysql.TypeVarString), 0)
	require.Equal(t, 0, cap(col.data))
	require.Equal(t, 1, cap(col.offsets))
	require.Equal(t, 0, cap(col.nullBitmap))
	col.Reserve(10, 10, 10)
	require.GreaterOrEqual(t, cap(col.data), 10)
	require.GreaterOrEqual(t, cap(col.offsets), 11)
	require.GreaterOrEqual(t, cap(col.nullBitmap), 10)

	col.data = append(col.data, 12)
	col.data = append(col.data, 24)
	col.data = append(col.data, 35)
	col.data = append(col.data, 56)
	col.data = append(col.data, 78)

	col.offsets = append(col.offsets, 10)
	col.offsets = append(col.offsets, 20)
	col.offsets = append(col.offsets, 50)
	col.offsets = append(col.offsets, 66)
	col.offsets = append(col.offsets, 99)

	col.nullBitmap = append(col.nullBitmap, 1)
	col.nullBitmap = append(col.nullBitmap, 0)
	col.nullBitmap = append(col.nullBitmap, 1)
	col.nullBitmap = append(col.nullBitmap, 0)
	col.nullBitmap = append(col.nullBitmap, 1)

	col.Reserve(100, 100, 100)
	require.GreaterOrEqual(t, cap(col.data), 105)
	require.GreaterOrEqual(t, cap(col.offsets), 106)
	require.GreaterOrEqual(t, cap(col.nullBitmap), 105)

Review comment (suggestion):
The check condition should be `require.GreaterOrEqual(t, cap(col.data) - len(col.data), 10)`

Code after:
	}
	col.ResizeGoDuration(1024, false)
	for i := range 1024 {
		require.Equal(t, time.Duration(0), col.GoDurations()[i])
	}

	col = NewColumn(types.NewFieldType(mysql.TypeDatetime), 1024)
	for range 1024 {
		gt := types.FromDate(rand.Intn(2200), rand.Intn(10)+1, rand.Intn(20)+1, rand.Intn(12), rand.Intn(60), rand.Intn(60), rand.Intn(1000000))
		t := types.NewTime(gt, 0, 0)
		col.AppendTime(t)
	}
	col.ResizeTime(1024, false)
	for i := range 1024 {
		var time types.Time
		require.Equal(t, time, col.Times()[i])
	}
}

func TestReserve(t *testing.T) {
	col := NewColumn(types.NewFieldType(mysql.TypeVarString), 0)
	require.Equal(t, 0, cap(col.data))
	require.Equal(t, 1, cap(col.offsets))
	require.Equal(t, 0, cap(col.nullBitmap))
	col.Reserve(10, 10, 10)
	require.GreaterOrEqual(t, cap(col.data)-len(col.data), 10)
	require.GreaterOrEqual(t, cap(col.offsets)-len(col.offsets), 11)
	require.GreaterOrEqual(t, cap(col.nullBitmap)-len(col.nullBitmap), 10)

	col.data = append(col.data, 12)
	col.data = append(col.data, 24)
	col.data = append(col.data, 35)
	col.data = append(col.data, 56)
	col.data = append(col.data, 78)

	col.offsets = append(col.offsets, 10)
	col.offsets = append(col.offsets, 20)
	col.offsets = append(col.offsets, 50)
	col.offsets = append(col.offsets, 66)
	col.offsets = append(col.offsets, 99)

	col.nullBitmap = append(col.nullBitmap, 1)
	col.nullBitmap = append(col.nullBitmap, 0)
	col.nullBitmap = append(col.nullBitmap, 1)
	col.nullBitmap = append(col.nullBitmap, 0)
	col.nullBitmap = append(col.nullBitmap, 1)

	col.Reserve(100, 100, 100)
	require.GreaterOrEqual(t, cap(col.data)-len(col.data), 105)
	require.GreaterOrEqual(t, cap(col.offsets)-len(col.offsets), 106)
	require.GreaterOrEqual(t, cap(col.nullBitmap)-len(col.nullBitmap), 105)