cubefs

Форк
0
/
vuid_test.go 
55 строк · 1.6 Кб
1
// Copyright 2022 The CubeFS Authors.
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
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package proto
16

17
import (
18
	"math/rand"
19
	"testing"
20

21
	"github.com/stretchr/testify/require"
22
)
23

24
func TestVuid(t *testing.T) {
25
	_, err := NewVuid(1, 1, 0)
26
	require.Error(t, err)
27

28
	vid := Vid(rand.Uint32())
29
	index := uint8(rand.Intn(256))
30
	epoch := uint32(rand.Int31n(MaxEpoch-MinEpoch) + MinEpoch)
31
	vuid, err := NewVuid(vid, index, epoch)
32
	vuidPre := EncodeVuidPrefix(vid, index)
33
	require.NoError(t, err)
34
	require.True(t, vuid.IsValid())
35

36
	require.Equal(t, vuidPre, vuid.VuidPrefix())
37
	require.Equal(t, vid, vuidPre.Vid())
38
	require.Equal(t, index, vuidPre.Index())
39
	require.Equal(t, vid, vuid.Vid())
40
	require.Equal(t, index, vuid.Index())
41
	require.Equal(t, epoch, vuid.Epoch())
42
}
43

44
func TestVuidMulti(t *testing.T) {
45
	for range [1000]struct{}{} {
46
		TestVuid(t)
47
	}
48
}
49

50
func TestDecodeVuid(t *testing.T) {
51
	for _, vuid := range []Vuid{425335980033, 116131889167} {
52
		t.Log(vuid.Vid(), vuid.VuidPrefix(), vuid.Index(), vuid.Epoch(), vuid.ToString())
53
		require.Equal(t, vuid, EncodeVuid(vuid.VuidPrefix(), vuid.Epoch()))
54
	}
55
}
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.