github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/id/nano_id_test.go (about) 1 package id 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestNanoID(t *testing.T) { 10 nanoID, err := ClassicNanoID(0) 11 require.Error(t, err) 12 require.Nil(t, nanoID) 13 14 nanoID, err = ClassicNanoID(256) 15 require.Error(t, err) 16 require.Nil(t, nanoID) 17 18 nanoID, err = ClassicNanoID(8) 19 require.NoError(t, err) 20 for i := 0; i < 1000; i++ { 21 require.Equal(t, 8, len(nanoID())) 22 } 23 } 24 25 func BenchmarkNanoID(b *testing.B) { 26 nanoID, err := ClassicNanoID(8) 27 require.NoError(b, err) 28 29 b.ResetTimer() 30 for i := 0; i < b.N; i++ { 31 _ = nanoID() 32 } 33 b.ReportAllocs() 34 }