Rewrite tests using gocheck.

This commit is contained in:
Maxim Bublis 2018-01-03 00:26:12 +00:00
parent 95d61fadb1
commit 980eb8c1d5
7 changed files with 278 additions and 478 deletions

View File

@ -1,123 +0,0 @@
// Copyright (C) 2013-2015 by Maxim Bublis <b@codemonkey.ru>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package uuid
import (
"testing"
)
func BenchmarkFromBytes(b *testing.B) {
bytes := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
for i := 0; i < b.N; i++ {
FromBytes(bytes)
}
}
func BenchmarkFromString(b *testing.B) {
s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
for i := 0; i < b.N; i++ {
FromString(s)
}
}
func BenchmarkFromStringUrn(b *testing.B) {
s := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
for i := 0; i < b.N; i++ {
FromString(s)
}
}
func BenchmarkFromStringWithBrackets(b *testing.B) {
s := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
for i := 0; i < b.N; i++ {
FromString(s)
}
}
func BenchmarkNewV1(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV1()
}
}
func BenchmarkNewV2(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV2(DomainPerson)
}
}
func BenchmarkNewV3(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV3(NamespaceDNS, "www.example.com")
}
}
func BenchmarkNewV4(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV4()
}
}
func BenchmarkNewV5(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV5(NamespaceDNS, "www.example.com")
}
}
func BenchmarkMarshalBinary(b *testing.B) {
u := NewV4()
for i := 0; i < b.N; i++ {
u.MarshalBinary()
}
}
func BenchmarkMarshalText(b *testing.B) {
u := NewV4()
for i := 0; i < b.N; i++ {
u.MarshalText()
}
}
func BenchmarkUnmarshalBinary(b *testing.B) {
bytes := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u := UUID{}
for i := 0; i < b.N; i++ {
u.UnmarshalBinary(bytes)
}
}
func BenchmarkUnmarshalText(b *testing.B) {
bytes := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u := UUID{}
for i := 0; i < b.N; i++ {
u.UnmarshalText(bytes)
}
}
var sink string
func BenchmarkMarshalToString(b *testing.B) {
u := NewV4()
for i := 0; i < b.N; i++ {
sink = u.String()
}
}

View File

@ -23,68 +23,66 @@ package uuid
import (
"bytes"
"testing"
. "gopkg.in/check.v1"
)
func TestFromBytes(t *testing.T) {
type codecTestSuite struct{}
var _ = Suite(&codecTestSuite{})
func (s *codecTestSuite) TestFromBytes(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u1, err := FromBytes(b1)
if err != nil {
t.Errorf("Error parsing UUID from bytes: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u1, Equals, u)
b2 := []byte{}
_, err = FromBytes(b2)
if err == nil {
t.Errorf("Should return error parsing from empty byte slice, got %s", err)
c.Assert(err, NotNil)
}
func (s *codecTestSuite) BenchmarkFromBytes(c *C) {
bytes := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
for i := 0; i < c.N; i++ {
FromBytes(bytes)
}
}
func TestMarshalBinary(t *testing.T) {
func (s *codecTestSuite) TestMarshalBinary(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b2, err := u.MarshalBinary()
if err != nil {
t.Errorf("Error marshaling UUID: %s", err)
}
c.Assert(err, IsNil)
c.Assert(bytes.Equal(b1, b2), Equals, true)
}
if !bytes.Equal(b1, b2) {
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
func (s *codecTestSuite) BenchmarkMarshalBinary(c *C) {
u := NewV4()
for i := 0; i < c.N; i++ {
u.MarshalBinary()
}
}
func TestUnmarshalBinary(t *testing.T) {
func (s *codecTestSuite) TestUnmarshalBinary(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u1 := UUID{}
err := u1.UnmarshalBinary(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u1, Equals, u)
b2 := []byte{}
u2 := UUID{}
err = u2.UnmarshalBinary(b2)
if err == nil {
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
}
c.Assert(err, NotNil)
}
func TestFromString(t *testing.T) {
func (s *codecTestSuite) TestFromString(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
@ -94,88 +92,78 @@ func TestFromString(t *testing.T) {
s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
_, err := FromString("")
if err == nil {
t.Errorf("Should return error trying to parse empty string, got %s", err)
}
c.Assert(err, NotNil)
u1, err := FromString(s1)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u1, Equals, u)
u2, err := FromString(s2)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u2) {
t.Errorf("UUIDs should be equal: %s and %s", u, u2)
}
c.Assert(err, IsNil)
c.Assert(u2, Equals, u)
u3, err := FromString(s3)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u3) {
t.Errorf("UUIDs should be equal: %s and %s", u, u3)
}
c.Assert(err, IsNil)
c.Assert(u3, Equals, u)
u4, err := FromString(s4)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u4) {
t.Errorf("UUIDs should be equal: %s and %s", u, u4)
}
c.Assert(err, IsNil)
c.Assert(u4, Equals, u)
u5, err := FromString(s5)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
c.Assert(err, IsNil)
c.Assert(u5, Equals, u)
}
if !Equal(u, u5) {
t.Errorf("UUIDs should be equal: %s and %s", u, u5)
func (s *codecTestSuite) BenchmarkFromString(c *C) {
str := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
for i := 0; i < c.N; i++ {
FromString(str)
}
}
func TestFromStringShort(t *testing.T) {
func (s *codecTestSuite) BenchmarkFromStringUrn(c *C) {
str := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
for i := 0; i < c.N; i++ {
FromString(str)
}
}
func (s *codecTestSuite) BenchmarkFromStringWithBrackets(c *C) {
str := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
for i := 0; i < c.N; i++ {
FromString(str)
}
}
func (s *codecTestSuite) TestFromStringShort(c *C) {
// Invalid 35-character UUID string
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
for i := len(s1); i >= 0; i-- {
_, err := FromString(s1[:i])
if err == nil {
t.Errorf("Should return error trying to parse too short string, got %s", err)
}
c.Assert(err, NotNil)
}
}
func TestFromStringLong(t *testing.T) {
func (s *codecTestSuite) TestFromStringLong(c *C) {
// Invalid 37+ character UUID string
s := []string{
strings := []string{
"6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
"6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
"6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
}
for _, str := range s {
for _, str := range strings {
_, err := FromString(str)
if err == nil {
t.Errorf("Should return error trying to parse too long string, passed %s", str)
}
c.Assert(err, NotNil)
}
}
func TestFromStringInvalid(t *testing.T) {
func (s *codecTestSuite) TestFromStringInvalid(c *C) {
// Invalid UUID string formats
s := []string{
strings := []string{
"6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
"urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
@ -194,62 +182,67 @@ func TestFromStringInvalid(t *testing.T) {
"6ba7b8109dad11d180b4-00c04fd430c8",
}
for _, str := range s {
for _, str := range strings {
_, err := FromString(str)
if err == nil {
t.Errorf("Should return error trying to parse invalid string, passed %s", str)
}
c.Assert(err, NotNil)
}
}
func TestFromStringOrNil(t *testing.T) {
func (s *codecTestSuite) TestFromStringOrNil(c *C) {
u := FromStringOrNil("")
if u != Nil {
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
}
c.Assert(u, Equals, Nil)
}
func TestFromBytesOrNil(t *testing.T) {
func (s *codecTestSuite) TestFromBytesOrNil(c *C) {
b := []byte{}
u := FromBytesOrNil(b)
if u != Nil {
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
}
c.Assert(u, Equals, Nil)
}
func TestMarshalText(t *testing.T) {
func (s *codecTestSuite) TestMarshalText(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
b2, err := u.MarshalText()
if err != nil {
t.Errorf("Error marshaling UUID: %s", err)
}
c.Assert(err, IsNil)
c.Assert(bytes.Equal(b1, b2), Equals, true)
}
if !bytes.Equal(b1, b2) {
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
func (s *codecTestSuite) BenchmarkMarshalText(c *C) {
u := NewV4()
for i := 0; i < c.N; i++ {
u.MarshalText()
}
}
func TestUnmarshalText(t *testing.T) {
func (s *codecTestSuite) TestUnmarshalText(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u1 := UUID{}
err := u1.UnmarshalText(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u1, Equals, u)
b2 := []byte("")
u2 := UUID{}
err = u2.UnmarshalText(b2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
c.Assert(err, NotNil)
}
func (s *codecTestSuite) BenchmarkUnmarshalText(c *C) {
bytes := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u := UUID{}
for i := 0; i < c.N; i++ {
u.UnmarshalText(bytes)
}
}
var sink string
func (s *codecTestSuite) BenchmarkMarshalToString(c *C) {
u := NewV4()
for i := 0; i < c.N; i++ {
sink = u.String()
}
}

View File

@ -106,7 +106,7 @@ func (g *generator) NewV1() UUID {
copy(u[10:], hardwareAddr)
u.SetVersion(1)
u.SetVersion(V1)
u.SetVariant()
return u
@ -132,7 +132,7 @@ func (g *generator) NewV2(domain byte) UUID {
copy(u[10:], hardwareAddr)
u.SetVersion(2)
u.SetVersion(V2)
u.SetVariant()
return u
@ -141,7 +141,7 @@ func (g *generator) NewV2(domain byte) UUID {
// NewV3 returns UUID based on MD5 hash of namespace UUID and name.
func (g *generator) NewV3(ns UUID, name string) UUID {
u := newFromHash(md5.New(), ns, name)
u.SetVersion(3)
u.SetVersion(V3)
u.SetVariant()
return u
@ -151,7 +151,7 @@ func (g *generator) NewV3(ns UUID, name string) UUID {
func (g *generator) NewV4() UUID {
u := UUID{}
g.safeRandom(u[:])
u.SetVersion(4)
u.SetVersion(V4)
u.SetVariant()
return u
@ -160,7 +160,7 @@ func (g *generator) NewV4() UUID {
// NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.
func (g *generator) NewV5(ns UUID, name string) UUID {
u := newFromHash(sha1.New(), ns, name)
u.SetVersion(5)
u.SetVersion(V5)
u.SetVariant()
return u

View File

@ -1,140 +1,134 @@
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package uuid
import "testing"
import (
. "gopkg.in/check.v1"
)
func TestNewV1(t *testing.T) {
type genTestSuite struct{}
var _ = Suite(&genTestSuite{})
func (s *genTestSuite) TestNewV1(c *C) {
u := NewV1()
if u.Version() != 1 {
t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
}
if u.Variant() != VariantRFC4122 {
t.Errorf("UUIDv1 generated with incorrect variant: %d", u.Variant())
}
c.Assert(u.Version(), Equals, V1)
c.Assert(u.Variant(), Equals, VariantRFC4122)
u1 := NewV1()
u2 := NewV1()
if Equal(u1, u2) {
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
}
c.Assert(u1, Not(Equals), u2)
oldFunc := epochFunc
epochFunc = func() uint64 { return 0 }
u3 := NewV1()
u4 := NewV1()
if Equal(u3, u4) {
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
}
c.Assert(u3, Not(Equals), u4)
epochFunc = oldFunc
}
func TestNewV2(t *testing.T) {
u1 := NewV2(DomainPerson)
if u1.Version() != 2 {
t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
}
if u1.Variant() != VariantRFC4122 {
t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
}
u2 := NewV2(DomainGroup)
if u2.Version() != 2 {
t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
}
if u2.Variant() != VariantRFC4122 {
t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
func (s *genTestSuite) BenchmarkNewV1(c *C) {
for i := 0; i < c.N; i++ {
NewV1()
}
}
func TestNewV3(t *testing.T) {
func (s *genTestSuite) TestNewV2(c *C) {
u1 := NewV2(DomainPerson)
c.Assert(u1.Version(), Equals, V2)
c.Assert(u1.Variant(), Equals, VariantRFC4122)
u2 := NewV2(DomainGroup)
c.Assert(u2.Version(), Equals, V2)
c.Assert(u2.Variant(), Equals, VariantRFC4122)
}
func (s *genTestSuite) BenchmarkNewV2(c *C) {
for i := 0; i < c.N; i++ {
NewV2(DomainPerson)
}
}
func (s *genTestSuite) TestNewV3(c *C) {
u := NewV3(NamespaceDNS, "www.example.com")
if u.Version() != 3 {
t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
}
if u.Variant() != VariantRFC4122 {
t.Errorf("UUIDv3 generated with incorrect variant: %d", u.Variant())
}
if u.String() != "5df41881-3aed-3515-88a7-2f4a814cf09e" {
t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
}
c.Assert(u.Version(), Equals, V3)
c.Assert(u.Variant(), Equals, VariantRFC4122)
c.Assert(u.String(), Equals, "5df41881-3aed-3515-88a7-2f4a814cf09e")
u = NewV3(NamespaceDNS, "python.org")
if u.String() != "6fa459ea-ee8a-3ca4-894e-db77e160355e" {
t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
}
c.Assert(u.String(), Equals, "6fa459ea-ee8a-3ca4-894e-db77e160355e")
u1 := NewV3(NamespaceDNS, "golang.org")
u2 := NewV3(NamespaceDNS, "golang.org")
if !Equal(u1, u2) {
t.Errorf("UUIDv3 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
}
c.Assert(u1, Equals, u2)
u3 := NewV3(NamespaceDNS, "example.com")
if Equal(u1, u3) {
t.Errorf("UUIDv3 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
}
c.Assert(u1, Not(Equals), u3)
u4 := NewV3(NamespaceURL, "golang.org")
if Equal(u1, u4) {
t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
c.Assert(u1, Not(Equals), u4)
}
func (s *genTestSuite) BenchmarkNewV3(c *C) {
for i := 0; i < c.N; i++ {
NewV3(NamespaceDNS, "www.example.com")
}
}
func TestNewV4(t *testing.T) {
func (s *genTestSuite) TestNewV4(c *C) {
u := NewV4()
c.Assert(u.Version(), Equals, V4)
c.Assert(u.Variant(), Equals, VariantRFC4122)
}
if u.Version() != 4 {
t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
}
if u.Variant() != VariantRFC4122 {
t.Errorf("UUIDv4 generated with incorrect variant: %d", u.Variant())
func (s *genTestSuite) BenchmarkNewV4(c *C) {
for i := 0; i < c.N; i++ {
NewV4()
}
}
func TestNewV5(t *testing.T) {
func (s *genTestSuite) TestNewV5(c *C) {
u := NewV5(NamespaceDNS, "www.example.com")
if u.Version() != 5 {
t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
}
if u.Variant() != VariantRFC4122 {
t.Errorf("UUIDv5 generated with incorrect variant: %d", u.Variant())
}
c.Assert(u.Version(), Equals, V5)
c.Assert(u.Variant(), Equals, VariantRFC4122)
u = NewV5(NamespaceDNS, "python.org")
if u.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
t.Errorf("UUIDv5 generated incorrectly: %s", u.String())
}
c.Assert(u.String(), Equals, "886313e1-3b8a-5372-9b90-0c9aee199e5d")
u1 := NewV5(NamespaceDNS, "golang.org")
u2 := NewV5(NamespaceDNS, "golang.org")
if !Equal(u1, u2) {
t.Errorf("UUIDv5 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
}
c.Assert(u1, Equals, u2)
u3 := NewV5(NamespaceDNS, "example.com")
if Equal(u1, u3) {
t.Errorf("UUIDv5 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
}
c.Assert(u1, Not(Equals), u3)
u4 := NewV5(NamespaceURL, "golang.org")
if Equal(u1, u4) {
t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
c.Assert(u1, Not(Equals), u4)
}
func (s *genTestSuite) BenchmarkNewV5(c *C) {
for i := 0; i < c.N; i++ {
NewV5(NamespaceDNS, "www.example.com")
}
}

View File

@ -21,169 +21,116 @@
package uuid
import "testing"
import (
. "gopkg.in/check.v1"
)
func TestValue(t *testing.T) {
type sqlTestSuite struct{}
var _ = Suite(&sqlTestSuite{})
func (s *sqlTestSuite) TestValue(c *C) {
u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
c.Assert(err, IsNil)
val, err := u.Value()
if err != nil {
t.Errorf("Error getting UUID value: %s", err)
}
if val != u.String() {
t.Errorf("Wrong value returned, should be equal: %s and %s", val, u)
}
c.Assert(err, IsNil)
c.Assert(val, Equals, u.String())
}
func TestValueNil(t *testing.T) {
func (s *sqlTestSuite) TestValueNil(c *C) {
u := UUID{}
val, err := u.Value()
if err != nil {
t.Errorf("Error getting UUID value: %s", err)
}
if val != Nil.String() {
t.Errorf("Wrong value returned, should be equal to UUID.Nil: %s", val)
}
c.Assert(err, IsNil)
c.Assert(val, Equals, Nil.String())
}
func TestNullUUIDValueNil(t *testing.T) {
func (s *sqlTestSuite) TestNullUUIDValueNil(c *C) {
u := NullUUID{}
val, err := u.Value()
if err != nil {
t.Errorf("Error getting UUID value: %s", err)
}
if val != nil {
t.Errorf("Wrong value returned, should be nil: %s", val)
}
c.Assert(err, IsNil)
c.Assert(val, IsNil)
}
func TestScanBinary(t *testing.T) {
func (s *sqlTestSuite) TestScanBinary(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u1 := UUID{}
err := u1.Scan(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u, Equals, u1)
b2 := []byte{}
u2 := UUID{}
err = u2.Scan(b2)
if err == nil {
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
}
c.Assert(err, NotNil)
}
func TestScanString(t *testing.T) {
func (s *sqlTestSuite) TestScanString(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u1 := UUID{}
err := u1.Scan(s1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u, Equals, u1)
s2 := ""
u2 := UUID{}
err = u2.Scan(s2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
}
c.Assert(err, NotNil)
}
func TestScanText(t *testing.T) {
func (s *sqlTestSuite) TestScanText(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u1 := UUID{}
err := u1.Scan(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
c.Assert(err, IsNil)
c.Assert(u, Equals, u1)
b2 := []byte("")
u2 := UUID{}
err = u2.Scan(b2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
}
c.Assert(err, NotNil)
}
func TestScanUnsupported(t *testing.T) {
func (s *sqlTestSuite) TestScanUnsupported(c *C) {
u := UUID{}
err := u.Scan(true)
if err == nil {
t.Errorf("Should return error trying to unmarshal from bool")
}
c.Assert(err, NotNil)
}
func TestScanNil(t *testing.T) {
func (s *sqlTestSuite) TestScanNil(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
err := u.Scan(nil)
if err == nil {
t.Errorf("Error UUID shouldn't allow unmarshalling from nil")
}
c.Assert(err, NotNil)
}
func TestNullUUIDScanValid(t *testing.T) {
func (s *sqlTestSuite) TestNullUUIDScanValid(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u1 := NullUUID{}
err := u1.Scan(s1)
if err != nil {
t.Errorf("Error unmarshaling NullUUID: %s", err)
}
if !u1.Valid {
t.Errorf("NullUUID should be valid")
}
if !Equal(u, u1.UUID) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1.UUID)
}
c.Assert(err, IsNil)
c.Assert(u1.Valid, Equals, true)
c.Assert(u1.UUID, Equals, u)
}
func TestNullUUIDScanNil(t *testing.T) {
func (s *sqlTestSuite) TestNullUUIDScanNil(c *C) {
u := NullUUID{UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, true}
err := u.Scan(nil)
if err != nil {
t.Errorf("Error unmarshaling NullUUID: %s", err)
}
if u.Valid {
t.Errorf("NullUUID should not be valid")
}
if !Equal(u.UUID, Nil) {
t.Errorf("NullUUID value should be equal to Nil: %v", u)
}
c.Assert(err, IsNil)
c.Assert(u.Valid, Equals, false)
c.Assert(u.UUID, Equals, Nil)
}

26
uuid.go
View File

@ -32,9 +32,23 @@ import (
// Size of a UUID in bytes.
const Size = 16
// UUID representation compliant with specification
// described in RFC 4122.
type UUID [Size]byte
// UUID versions
const (
_ byte = iota
V1
V2
V3
V4
V5
)
// UUID layout variants.
const (
VariantNCS = iota
VariantNCS byte = iota
VariantRFC4122
VariantMicrosoft
VariantFuture
@ -53,10 +67,6 @@ var (
byteGroups = []int{8, 4, 4, 4, 12}
)
// UUID representation compliant with specification
// described in RFC 4122.
type UUID [Size]byte
// Nil is special form of UUID that is specified to have all
// 128 bits set to zero.
var Nil = UUID{}
@ -75,12 +85,12 @@ func Equal(u1 UUID, u2 UUID) bool {
}
// Version returns algorithm version used to generate UUID.
func (u UUID) Version() uint {
return uint(u[6] >> 4)
func (u UUID) Version() byte {
return u[6] >> 4
}
// Variant returns UUID layout variant.
func (u UUID) Variant() uint {
func (u UUID) Variant() byte {
switch {
case (u[8] & 0x80) == 0x00:
return VariantNCS

View File

@ -1,4 +1,4 @@
// Copyright (C) 2013, 2015 by Maxim Bublis <b@codemonkey.ru>
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@ -24,82 +24,61 @@ package uuid
import (
"bytes"
"testing"
. "gopkg.in/check.v1"
)
func TestBytes(t *testing.T) {
// Hook up gocheck into the "go test" runner.
func TestUUID(t *testing.T) { TestingT(t) }
type testSuite struct{}
var _ = Suite(&testSuite{})
func (s *testSuite) TestBytes(c *C) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
bytes1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
if !bytes.Equal(u.Bytes(), bytes1) {
t.Errorf("Incorrect bytes representation for UUID: %s", u)
}
c.Assert(bytes.Equal(u.Bytes(), bytes1), Equals, true)
}
func TestString(t *testing.T) {
if NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
t.Errorf("Incorrect string representation for UUID: %s", NamespaceDNS.String())
}
func (s *testSuite) TestString(c *C) {
c.Assert(NamespaceDNS.String(), Equals, "6ba7b810-9dad-11d1-80b4-00c04fd430c8")
}
func TestEqual(t *testing.T) {
if !Equal(NamespaceDNS, NamespaceDNS) {
t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceDNS)
}
if Equal(NamespaceDNS, NamespaceURL) {
t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceURL)
}
func (s *testSuite) TestEqual(c *C) {
c.Assert(Equal(NamespaceDNS, NamespaceDNS), Equals, true)
c.Assert(Equal(NamespaceDNS, NamespaceURL), Equals, false)
}
func TestVersion(t *testing.T) {
func (s *testSuite) TestVersion(c *C) {
u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if u.Version() != 1 {
t.Errorf("Incorrect version for UUID: %d", u.Version())
}
c.Assert(u.Version(), Equals, V1)
}
func TestSetVersion(t *testing.T) {
func (s *testSuite) TestSetVersion(c *C) {
u := UUID{}
u.SetVersion(4)
if u.Version() != 4 {
t.Errorf("Incorrect version for UUID after u.setVersion(4): %d", u.Version())
}
c.Assert(u.Version(), Equals, V4)
}
func TestVariant(t *testing.T) {
func (s *testSuite) TestVariant(c *C) {
u1 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if u1.Variant() != VariantNCS {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantNCS, u1.Variant())
}
c.Assert(u1.Variant(), Equals, VariantNCS)
u2 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if u2.Variant() != VariantRFC4122 {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantRFC4122, u2.Variant())
}
c.Assert(u2.Variant(), Equals, VariantRFC4122)
u3 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if u3.Variant() != VariantMicrosoft {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantMicrosoft, u3.Variant())
}
c.Assert(u3.Variant(), Equals, VariantMicrosoft)
u4 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if u4.Variant() != VariantFuture {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantFuture, u4.Variant())
}
c.Assert(u4.Variant(), Equals, VariantFuture)
}
func TestSetVariant(t *testing.T) {
u := new(UUID)
func (s *testSuite) TestSetVariant(c *C) {
u := UUID{}
u.SetVariant()
if u.Variant() != VariantRFC4122 {
t.Errorf("Incorrect variant for UUID after u.setVariant(): %d", u.Variant())
}
c.Assert(u.Variant(), Equals, VariantRFC4122)
}