git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/guid/uuid_test.go (about)

     1  package guid_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"git.sr.ht/~pingoo/stdx/guid"
     7  	"git.sr.ht/~pingoo/stdx/uuid"
     8  )
     9  
    10  func TestParseUuidString(t *testing.T) {
    11  	for i := 0; i < 10000; i += 1 {
    12  		id := guid.NewRandom()
    13  		if id.Equal(guid.Empty) {
    14  			t.Error("GUID is empty")
    15  		}
    16  
    17  		parsed, err := guid.ParseUuidString(id.ToUuidString())
    18  		if err != nil {
    19  			t.Errorf("parsing GUID: %s", err)
    20  		}
    21  		if !id.Equal(parsed) {
    22  			t.Errorf("parsed (%s) != original GUID (%s)", parsed.String(), id.String())
    23  		}
    24  	}
    25  }
    26  
    27  func TestNewRandomIsValidUuidV4(t *testing.T) {
    28  	for i := 0; i < 10000; i += 1 {
    29  		id := guid.NewRandom()
    30  		if id.Equal(guid.Empty) {
    31  			t.Error("GUID is empty")
    32  		}
    33  
    34  		parsedUuid, err := uuid.FromBytes(id.Bytes())
    35  		if err != nil {
    36  			t.Errorf("parsing UUID: %s", err)
    37  		}
    38  
    39  		if parsedUuid.Version() != 4 {
    40  			t.Error("UUID is not v4")
    41  		}
    42  
    43  		if parsedUuid.Variant() != uuid.RFC4122 {
    44  			t.Error("UUID is not RFC4122")
    45  		}
    46  	}
    47  }
    48  
    49  func TestNewTimebasedIsValidUuidV7(t *testing.T) {
    50  	for i := 0; i < 10000; i += 1 {
    51  		id := guid.NewTimeBased()
    52  		if id.Equal(guid.Empty) {
    53  			t.Error("GUID is empty")
    54  		}
    55  
    56  		parsedUuid, err := uuid.FromBytes(id.Bytes())
    57  		if err != nil {
    58  			t.Errorf("parsing UUID: %s", err)
    59  		}
    60  
    61  		if parsedUuid.Version() != 7 {
    62  			t.Error("UUID is not v7")
    63  		}
    64  
    65  		if parsedUuid.Variant() != uuid.RFC4122 {
    66  			t.Error("UUID is not RFC4122")
    67  		}
    68  	}
    69  }