github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/twinj/uuid/rfc4122_test.go (about)

     1  package uuid
     2  
     3  /****************
     4   * Date: 16/02/14
     5   * Time: 11:29 AM
     6   ***************/
     7  
     8  import (
     9  	"fmt"
    10  	"net/url"
    11  	"testing"
    12  )
    13  
    14  var (
    15  	goLang Name = "https://google.com/golang.org?q=golang"
    16  )
    17  
    18  const (
    19  	generate = 1000000
    20  )
    21  
    22  func TestUUID_NewV1(t *testing.T) {
    23  	u := NewV1()
    24  	if u.Version() != 1 {
    25  		t.Errorf("Expected correct version %d, but got %d", 1, u.Version())
    26  	}
    27  	if u.Variant() != ReservedRFC4122 {
    28  		t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
    29  	}
    30  	if !parseUUIDRegex.MatchString(u.String()) {
    31  		t.Errorf("Expected string representation to be valid, given: %s", u.String())
    32  	}
    33  }
    34  
    35  func TestUUID_NewV1Bulk(t *testing.T) {
    36  	for i := 0; i < generate; i++ {
    37  		NewV1()
    38  	}
    39  }
    40  
    41  // Tests NewV3
    42  func TestUUID_NewV3(t *testing.T) {
    43  	u := NewV3(NamespaceURL, goLang)
    44  	if u.Version() != 3 {
    45  		t.Errorf("Expected correct version %d, but got %d", 3, u.Version())
    46  	}
    47  	if u.Variant() != ReservedRFC4122 {
    48  		t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
    49  	}
    50  	if !parseUUIDRegex.MatchString(u.String()) {
    51  		t.Errorf("Expected string representation to be valid, given: %s", u.String())
    52  	}
    53  	ur, _ := url.Parse(string(goLang))
    54  
    55  	// Same NS same name MUST be equal
    56  	u2 := NewV3(NamespaceURL, ur)
    57  	if !Equal(u2, u) {
    58  		t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
    59  	}
    60  
    61  	// Different NS same name MUST NOT be equal
    62  	u3 := NewV3(NamespaceDNS, ur)
    63  	if Equal(u3, u) {
    64  		t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
    65  	}
    66  
    67  	// Same NS different name MUST NOT be equal
    68  	u4 := NewV3(NamespaceURL, u)
    69  	if Equal(u4, u) {
    70  		t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
    71  	}
    72  
    73  	ids := []UUID{
    74  		u, u2, u3, u4,
    75  	}
    76  	for j, id := range ids {
    77  		i := NewV3(NamespaceURL, NewName(string(j), id))
    78  		if Equal(id, i) {
    79  			t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
    80  		}
    81  	}
    82  }
    83  
    84  func TestUUID_NewV3Bulk(t *testing.T) {
    85  	for i := 0; i < generate; i++ {
    86  		NewV3(NamespaceDNS, goLang)
    87  	}
    88  }
    89  
    90  func TestUUID_NewV4(t *testing.T) {
    91  	u := NewV4()
    92  	if u.Version() != 4 {
    93  		t.Errorf("Expected correct version %d, but got %d", 4, u.Version())
    94  	}
    95  	if u.Variant() != ReservedRFC4122 {
    96  		t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
    97  	}
    98  	if !parseUUIDRegex.MatchString(u.String()) {
    99  		t.Errorf("Expected string representation to be valid, given: %s", u.String())
   100  	}
   101  }
   102  
   103  func TestUUID_NewV4Bulk(t *testing.T) {
   104  	for i := 0; i < generate; i++ {
   105  		NewV4()
   106  	}
   107  }
   108  
   109  // Tests NewV5
   110  func TestUUID_NewV5(t *testing.T) {
   111  	u := NewV5(NamespaceURL, goLang)
   112  	if u.Version() != 5 {
   113  		t.Errorf("Expected correct version %d, but got %d", 5, u.Version())
   114  	}
   115  	if u.Variant() != ReservedRFC4122 {
   116  		t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
   117  	}
   118  	if !parseUUIDRegex.MatchString(u.String()) {
   119  		t.Errorf("Expected string representation to be valid, given: %s", u.String())
   120  	}
   121  	ur, _ := url.Parse(string(goLang))
   122  
   123  	// Same NS same name MUST be equal
   124  	u2 := NewV5(NamespaceURL, ur)
   125  	if !Equal(u2, u) {
   126  		t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
   127  	}
   128  
   129  	// Different NS same name MUST NOT be equal
   130  	u3 := NewV5(NamespaceDNS, ur)
   131  	if Equal(u3, u) {
   132  		t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
   133  	}
   134  
   135  	// Same NS different name MUST NOT be equal
   136  	u4 := NewV5(NamespaceURL, u)
   137  	if Equal(u4, u) {
   138  		t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
   139  	}
   140  
   141  	ids := []UUID{
   142  		u, u2, u3, u4,
   143  	}
   144  	for j, id := range ids {
   145  		i := NewV5(NamespaceURL, NewName(string(j), id))
   146  		if Equal(id, i) {
   147  			t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
   148  		}
   149  	}
   150  }
   151  
   152  func TestUUID_NewV5Bulk(t *testing.T) {
   153  	for i := 0; i < generate; i++ {
   154  		NewV5(NamespaceDNS, goLang)
   155  	}
   156  }
   157  
   158  // A small test to test uniqueness across all UUIDs created
   159  func TestUUID_EachIsUnique(t *testing.T) {
   160  	s := 1000
   161  	ids := make([]UUID, s)
   162  	for i := 0; i < s; i++ {
   163  		u := NewV1()
   164  		ids[i] = u
   165  		for j := 0; j < i; j++ {
   166  			if Equal(ids[j], u) {
   167  				t.Error("Should not create the same V1 UUID", u, ids[j])
   168  			}
   169  		}
   170  	}
   171  	ids = make([]UUID, s)
   172  	for i := 0; i < s; i++ {
   173  		u := NewV3(NamespaceDNS, NewName(string(i), Name(goLang)))
   174  		ids[i] = u
   175  		for j := 0; j < i; j++ {
   176  			if Equal(ids[j], u) {
   177  				t.Error("Should not create the same V3 UUID", u, ids[j])
   178  			}
   179  		}
   180  	}
   181  	ids = make([]UUID, s)
   182  	for i := 0; i < s; i++ {
   183  		u := NewV4()
   184  		ids[i] = u
   185  		for j := 0; j < i; j++ {
   186  			if Equal(ids[j], u) {
   187  				t.Error("Should not create the same V4 UUID", u, ids[j])
   188  			}
   189  		}
   190  	}
   191  	ids = make([]UUID, s)
   192  	for i := 0; i < s; i++ {
   193  		u := NewV5(NamespaceDNS, NewName(string(i), Name(goLang)))
   194  		ids[i] = u
   195  		for j := 0; j < i; j++ {
   196  			if Equal(ids[j], u) {
   197  				t.Error("Should not create the same V5 UUID", u, ids[j])
   198  			}
   199  		}
   200  	}
   201  }
   202  
   203  // Not really a test but used for visual verification of the defaults
   204  func UUID_NamespaceDefaults() {
   205  	fmt.Println(NamespaceDNS)
   206  	fmt.Println(NamespaceURL)
   207  	fmt.Println(NamespaceOID)
   208  	fmt.Println(NamespaceX500)
   209  }