github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/util/random/id_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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 implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package random
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  type testReader byte
    24  
    25  func (r *testReader) Read(dest []byte) (int, error) {
    26  	for i := 0; i < len(dest); i++ {
    27  		dest[i] = byte(*r)
    28  	}
    29  	return len(dest), nil
    30  }
    31  
    32  func TestBasic(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	func() {
    36  		var r testReader
    37  		oldReader := reader
    38  		reader = &r
    39  		defer func() {
    40  			reader = oldReader
    41  		}()
    42  
    43  		r = testReader(byte(0x00))
    44  		assert.Equal("00000000000000000000000000000000", Id())
    45  		r = testReader(byte(0x01))
    46  		assert.Equal("01010101010101010101010101010101", Id())
    47  		r = testReader(byte(0xFF))
    48  		assert.Equal("ffffffffffffffffffffffffffffffff", Id())
    49  	}()
    50  
    51  	one := Id()
    52  	two := Id()
    53  	assert.NotEqual(one, two)
    54  }