From a6e6896309007fa1013bd51cee125ab8a75f282d Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 3 May 2025 19:41:19 -0300 Subject: re s/guuid/uuid/g --- Makefile | 2 +- deps.mk | 32 ++-- src/guuid.go | 102 ----------- src/uuid.go | 102 +++++++++++ tests/benchmarks/string-roundtrip/guuid.go | 37 ---- tests/benchmarks/string-roundtrip/uuid.go | 37 ++++ tests/functional/string-round-trip/guuid.go | 62 ------- tests/functional/string-round-trip/uuid.go | 62 +++++++ tests/fuzz/from-string/guuid.go | 30 ---- tests/fuzz/from-string/uuid.go | 30 ++++ tests/fuzz/new-from/guuid.go | 31 ---- tests/fuzz/new-from/uuid.go | 31 ++++ tests/guuid.go | 261 ---------------------------- tests/main.go | 4 +- tests/uuid.go | 261 ++++++++++++++++++++++++++++ 15 files changed, 542 insertions(+), 542 deletions(-) delete mode 100644 src/guuid.go create mode 100644 src/uuid.go delete mode 100644 tests/benchmarks/string-roundtrip/guuid.go create mode 100644 tests/benchmarks/string-roundtrip/uuid.go delete mode 100644 tests/functional/string-round-trip/guuid.go create mode 100644 tests/functional/string-round-trip/uuid.go delete mode 100644 tests/fuzz/from-string/guuid.go create mode 100644 tests/fuzz/from-string/uuid.go delete mode 100644 tests/fuzz/new-from/guuid.go create mode 100644 tests/fuzz/new-from/uuid.go delete mode 100644 tests/guuid.go create mode 100644 tests/uuid.go diff --git a/Makefile b/Makefile index 77ddfdb..2ca519a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .POSIX: DATE = 1970-01-01 VERSION = 0.1.0 -NAME = guuid +NAME = uuid NAME_UC = $(NAME) LANGUAGES = en ## Installation prefix. Defaults to "/usr". diff --git a/deps.mk b/deps.mk index a2a936c..85a95ac 100644 --- a/deps.mk +++ b/deps.mk @@ -1,10 +1,10 @@ libs.go = \ - src/guuid.go \ - tests/benchmarks/string-roundtrip/guuid.go \ - tests/functional/string-round-trip/guuid.go \ - tests/fuzz/from-string/guuid.go \ - tests/fuzz/new-from/guuid.go \ - tests/guuid.go \ + src/uuid.go \ + tests/benchmarks/string-roundtrip/uuid.go \ + tests/functional/string-round-trip/uuid.go \ + tests/fuzz/from-string/uuid.go \ + tests/fuzz/new-from/uuid.go \ + tests/uuid.go \ mains.go = \ tests/benchmarks/string-roundtrip/main.go \ @@ -14,36 +14,36 @@ mains.go = \ tests/main.go \ functional/lib.go = \ - tests/functional/string-round-trip/guuid.go \ + tests/functional/string-round-trip/uuid.go \ functional/main.go = \ tests/functional/string-round-trip/main.go \ fuzz/lib.go = \ - tests/fuzz/from-string/guuid.go \ - tests/fuzz/new-from/guuid.go \ + tests/fuzz/from-string/uuid.go \ + tests/fuzz/new-from/uuid.go \ fuzz/main.go = \ tests/fuzz/from-string/main.go \ tests/fuzz/new-from/main.go \ benchmarks/lib.go = \ - tests/benchmarks/string-roundtrip/guuid.go \ + tests/benchmarks/string-roundtrip/uuid.go \ benchmarks/main.go = \ tests/benchmarks/string-roundtrip/main.go \ -src/guuid.a: src/guuid.go -tests/benchmarks/string-roundtrip/guuid.a: tests/benchmarks/string-roundtrip/guuid.go +src/uuid.a: src/uuid.go tests/benchmarks/string-roundtrip/main.a: tests/benchmarks/string-roundtrip/main.go -tests/functional/string-round-trip/guuid.a: tests/functional/string-round-trip/guuid.go +tests/benchmarks/string-roundtrip/uuid.a: tests/benchmarks/string-roundtrip/uuid.go tests/functional/string-round-trip/main.a: tests/functional/string-round-trip/main.go -tests/fuzz/from-string/guuid.a: tests/fuzz/from-string/guuid.go +tests/functional/string-round-trip/uuid.a: tests/functional/string-round-trip/uuid.go tests/fuzz/from-string/main.a: tests/fuzz/from-string/main.go -tests/fuzz/new-from/guuid.a: tests/fuzz/new-from/guuid.go +tests/fuzz/from-string/uuid.a: tests/fuzz/from-string/uuid.go tests/fuzz/new-from/main.a: tests/fuzz/new-from/main.go -tests/guuid.a: tests/guuid.go +tests/fuzz/new-from/uuid.a: tests/fuzz/new-from/uuid.go tests/main.a: tests/main.go +tests/uuid.a: tests/uuid.go tests/benchmarks/string-roundtrip/main.bin: tests/benchmarks/string-roundtrip/main.a tests/functional/string-round-trip/main.bin: tests/functional/string-round-trip/main.a tests/fuzz/from-string/main.bin: tests/fuzz/from-string/main.a diff --git a/src/guuid.go b/src/guuid.go deleted file mode 100644 index 22d783a..0000000 --- a/src/guuid.go +++ /dev/null @@ -1,102 +0,0 @@ -package guuid - -import ( - "crypto/rand" - "encoding/hex" - "errors" - "io" - "strings" -) - - - -const ( - ByteCount = 16 - dashCount = 4 - encodedLength = (ByteCount * 2) + dashCount -) - -var ( - dashIndexes = []int{ 8, 13, 18, 23 } - randomReader = rand.Reader - - ErrBadLength = errors.New( - "guuid: str isn't of the correct length", - ) - ErrBadDashCount = errors.New("guuid: Bad count of dashes in string") - ErrBadDashPosition = errors.New("guuid: Bad char in string") -) - - - -type UUID [ByteCount]byte - - - -func NewFrom(r io.Reader) (UUID, error) { - var uuid UUID - _, err := io.ReadFull(r, uuid[:]) - if err != nil { - return UUID{}, err - } - uuid[6] = (uuid[6] & 0x0f) | 0x40 // v4 - uuid[8] = (uuid[8] & 0x3f) | 0x80 // variant 10 - return uuid, nil -} - -func New() UUID { - uuid, err := NewFrom(randomReader) - if err != nil { - panic(err) - } - return uuid -} - -func (uuid UUID) String() string { - dst := [encodedLength]byte { - 0, 0, 0, 0, - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - } - - hex.Encode(dst[ 0:8], uuid[0:4]) - hex.Encode(dst[ 9:13], uuid[4:6]) - hex.Encode(dst[14:18], uuid[6:8]) - hex.Encode(dst[19:23], uuid[8:10]) - hex.Encode(dst[24:36], uuid[10:]) - - return string(dst[:]) -} - -func FromString(str string) (UUID, error) { - if len(str) != encodedLength { - return UUID{}, ErrBadLength - } - - if strings.Count(str, "-") != dashCount { - return UUID{}, ErrBadDashCount - } - - for _, idx := range dashIndexes { - if str[idx] != '-' { - return UUID{}, ErrBadDashPosition - } - } - - hexstr := strings.Join(strings.Split(str, "-"), "") - data, err := hex.DecodeString(hexstr) - if err != nil { - return UUID{}, err - } - - return [ByteCount]byte(data), nil -} diff --git a/src/uuid.go b/src/uuid.go new file mode 100644 index 0000000..8b953f8 --- /dev/null +++ b/src/uuid.go @@ -0,0 +1,102 @@ +package uuid + +import ( + "crypto/rand" + "encoding/hex" + "errors" + "io" + "strings" +) + + + +const ( + ByteCount = 16 + dashCount = 4 + encodedLength = (ByteCount * 2) + dashCount +) + +var ( + dashIndexes = []int{ 8, 13, 18, 23 } + randomReader = rand.Reader + + ErrBadLength = errors.New( + "uuid: str isn't of the correct length", + ) + ErrBadDashCount = errors.New("uuid: Bad count of dashes in string") + ErrBadDashPosition = errors.New("uuid: Bad char in string") +) + + + +type UUID [ByteCount]byte + + + +func NewFrom(r io.Reader) (UUID, error) { + var uuid UUID + _, err := io.ReadFull(r, uuid[:]) + if err != nil { + return UUID{}, err + } + uuid[6] = (uuid[6] & 0x0f) | 0x40 // v4 + uuid[8] = (uuid[8] & 0x3f) | 0x80 // variant 10 + return uuid, nil +} + +func New() UUID { + uuid, err := NewFrom(randomReader) + if err != nil { + panic(err) + } + return uuid +} + +func (uuid UUID) String() string { + dst := [encodedLength]byte { + 0, 0, 0, 0, + 0, 0, 0, 0, + '-', + 0, 0, 0, 0, + '-', + 0, 0, 0, 0, + '-', + 0, 0, 0, 0, + '-', + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + } + + hex.Encode(dst[ 0:8], uuid[0:4]) + hex.Encode(dst[ 9:13], uuid[4:6]) + hex.Encode(dst[14:18], uuid[6:8]) + hex.Encode(dst[19:23], uuid[8:10]) + hex.Encode(dst[24:36], uuid[10:]) + + return string(dst[:]) +} + +func FromString(str string) (UUID, error) { + if len(str) != encodedLength { + return UUID{}, ErrBadLength + } + + if strings.Count(str, "-") != dashCount { + return UUID{}, ErrBadDashCount + } + + for _, idx := range dashIndexes { + if str[idx] != '-' { + return UUID{}, ErrBadDashPosition + } + } + + hexstr := strings.Join(strings.Split(str, "-"), "") + data, err := hex.DecodeString(hexstr) + if err != nil { + return UUID{}, err + } + + return [ByteCount]byte(data), nil +} diff --git a/tests/benchmarks/string-roundtrip/guuid.go b/tests/benchmarks/string-roundtrip/guuid.go deleted file mode 100644 index 9fef55c..0000000 --- a/tests/benchmarks/string-roundtrip/guuid.go +++ /dev/null @@ -1,37 +0,0 @@ -package guuid - -import ( - "flag" - "fmt" -) - - - -var nFlag = flag.Int( - "n", - 10_000_000, - "The number of iterations to execute", -) - -func MainTest() { - flag.Parse() - n := *nFlag - - uuid := New() - - for i := 0; i < n; i++ { - derived, err := FromString(uuid.String()) - if err != nil { - panic(err) - } - - eq := uuid == derived - if !eq { - panic(fmt.Sprintf( - "bad round trip: orig (%#v); derived (%#v)", - uuid, - derived, - )) - } - } -} diff --git a/tests/benchmarks/string-roundtrip/uuid.go b/tests/benchmarks/string-roundtrip/uuid.go new file mode 100644 index 0000000..d686e04 --- /dev/null +++ b/tests/benchmarks/string-roundtrip/uuid.go @@ -0,0 +1,37 @@ +package uuid + +import ( + "flag" + "fmt" +) + + + +var nFlag = flag.Int( + "n", + 10_000_000, + "The number of iterations to execute", +) + +func MainTest() { + flag.Parse() + n := *nFlag + + uuid := New() + + for i := 0; i < n; i++ { + derived, err := FromString(uuid.String()) + if err != nil { + panic(err) + } + + eq := uuid == derived + if !eq { + panic(fmt.Sprintf( + "bad round trip: orig (%#v); derived (%#v)", + uuid, + derived, + )) + } + } +} diff --git a/tests/functional/string-round-trip/guuid.go b/tests/functional/string-round-trip/guuid.go deleted file mode 100644 index 764576e..0000000 --- a/tests/functional/string-round-trip/guuid.go +++ /dev/null @@ -1,62 +0,0 @@ -package guuid - -import ( - "fmt" - "os" - "reflect" -) - - - -func showColour() bool { - return os.Getenv("NO_COLOUR") == "" -} - -func testing(message string, body func()) { - if showColour() { - fmt.Fprintf( - os.Stderr, - "\033[0;33mtesting\033[0m: %s... ", - message, - ) - body() - fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "testing: %s... ", message) - body() - fmt.Fprintf(os.Stderr, "OK.\n") - } -} - -func assertEq(given any, expected any) { - if !reflect.DeepEqual(given, expected) { - if showColour() { - fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "ERR.\n") - } - fmt.Fprintf(os.Stderr, "given != expected\n") - fmt.Fprintf(os.Stderr, "given: %#v\n", given) - fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) - os.Exit(1) - } -} - - - -func MainTest() { - testing("string is the same after round-trip", func() { - str1 := New().String() - id, err := FromString(str1) - assertEq(err, nil) - str2 := id.String() - assertEq(str1, str2) - }) - - testing("UUID is the same after round-trip", func() { - id1 := New() - id2, err := FromString(id1.String()) - assertEq(err, nil) - assertEq(id1, id2) - }) -} diff --git a/tests/functional/string-round-trip/uuid.go b/tests/functional/string-round-trip/uuid.go new file mode 100644 index 0000000..000d3fb --- /dev/null +++ b/tests/functional/string-round-trip/uuid.go @@ -0,0 +1,62 @@ +package uuid + +import ( + "fmt" + "os" + "reflect" +) + + + +func showColour() bool { + return os.Getenv("NO_COLOUR") == "" +} + +func testing(message string, body func()) { + if showColour() { + fmt.Fprintf( + os.Stderr, + "\033[0;33mtesting\033[0m: %s... ", + message, + ) + body() + fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "testing: %s... ", message) + body() + fmt.Fprintf(os.Stderr, "OK.\n") + } +} + +func assertEq(given any, expected any) { + if !reflect.DeepEqual(given, expected) { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } + fmt.Fprintf(os.Stderr, "given != expected\n") + fmt.Fprintf(os.Stderr, "given: %#v\n", given) + fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) + os.Exit(1) + } +} + + + +func MainTest() { + testing("string is the same after round-trip", func() { + str1 := New().String() + id, err := FromString(str1) + assertEq(err, nil) + str2 := id.String() + assertEq(str1, str2) + }) + + testing("UUID is the same after round-trip", func() { + id1 := New() + id2, err := FromString(id1.String()) + assertEq(err, nil) + assertEq(id1, id2) + }) +} diff --git a/tests/fuzz/from-string/guuid.go b/tests/fuzz/from-string/guuid.go deleted file mode 100644 index 8971cbb..0000000 --- a/tests/fuzz/from-string/guuid.go +++ /dev/null @@ -1,30 +0,0 @@ -package guuid - -import ( - "os" - "testing" - "testing/internal/testdeps" -) - - - -func fn(f *testing.F) { - f.Fuzz(func(t *testing.T, str string) { - FromString(str) - }) -} - - - -func MainTest() { - fuzzTargets := []testing.InternalFuzzTarget{ - { "fn", fn }, - } - - deps := testdeps.TestDeps{} - tests := []testing.InternalTest {} - benchmarks := []testing.InternalBenchmark{} - examples := []testing.InternalExample {} - m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples) - os.Exit(m.Run()) -} diff --git a/tests/fuzz/from-string/uuid.go b/tests/fuzz/from-string/uuid.go new file mode 100644 index 0000000..5c3c787 --- /dev/null +++ b/tests/fuzz/from-string/uuid.go @@ -0,0 +1,30 @@ +package uuid + +import ( + "os" + "testing" + "testing/internal/testdeps" +) + + + +func fn(f *testing.F) { + f.Fuzz(func(t *testing.T, str string) { + FromString(str) + }) +} + + + +func MainTest() { + fuzzTargets := []testing.InternalFuzzTarget{ + { "fn", fn }, + } + + deps := testdeps.TestDeps{} + tests := []testing.InternalTest {} + benchmarks := []testing.InternalBenchmark{} + examples := []testing.InternalExample {} + m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples) + os.Exit(m.Run()) +} diff --git a/tests/fuzz/new-from/guuid.go b/tests/fuzz/new-from/guuid.go deleted file mode 100644 index a786c66..0000000 --- a/tests/fuzz/new-from/guuid.go +++ /dev/null @@ -1,31 +0,0 @@ -package guuid - -import ( - "bytes" - "os" - "testing" - "testing/internal/testdeps" -) - - - -func fn(f *testing.F) { - f.Fuzz(func(t *testing.T, payload []byte) { - NewFrom(bytes.NewReader(payload)) - }) -} - - - -func MainTest() { - fuzzTargets := []testing.InternalFuzzTarget{ - { "fn", fn }, - } - - deps := testdeps.TestDeps{} - tests := []testing.InternalTest {} - benchmarks := []testing.InternalBenchmark{} - examples := []testing.InternalExample {} - m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples) - os.Exit(m.Run()) -} diff --git a/tests/fuzz/new-from/uuid.go b/tests/fuzz/new-from/uuid.go new file mode 100644 index 0000000..298687b --- /dev/null +++ b/tests/fuzz/new-from/uuid.go @@ -0,0 +1,31 @@ +package uuid + +import ( + "bytes" + "os" + "testing" + "testing/internal/testdeps" +) + + + +func fn(f *testing.F) { + f.Fuzz(func(t *testing.T, payload []byte) { + NewFrom(bytes.NewReader(payload)) + }) +} + + + +func MainTest() { + fuzzTargets := []testing.InternalFuzzTarget{ + { "fn", fn }, + } + + deps := testdeps.TestDeps{} + tests := []testing.InternalTest {} + benchmarks := []testing.InternalBenchmark{} + examples := []testing.InternalExample {} + m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples) + os.Exit(m.Run()) +} diff --git a/tests/guuid.go b/tests/guuid.go deleted file mode 100644 index e9dd286..0000000 --- a/tests/guuid.go +++ /dev/null @@ -1,261 +0,0 @@ -package guuid - -import ( - "bytes" - "encoding/hex" - "fmt" - "os" - "reflect" - "strings" -) - - - -func showColour() bool { - return os.Getenv("NO_COLOUR") == "" -} - -func testStart(name string) { - fmt.Fprintf(os.Stderr, "%s:\n", name) -} - -func testing(message string, body func()) { - if showColour() { - fmt.Fprintf( - os.Stderr, - "\033[0;33mtesting\033[0m: %s... ", - message, - ) - body() - fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "testing: %s... ", message) - body() - fmt.Fprintf(os.Stderr, "OK.\n") - } -} - -func assertEq(given any, expected any) { - if !reflect.DeepEqual(given, expected) { - if showColour() { - fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "ERR.\n") - } - fmt.Fprintf(os.Stderr, "given != expected\n") - fmt.Fprintf(os.Stderr, "given: %#v\n", given) - fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) - os.Exit(1) - } -} - - -func test_NewFrom() { - testStart("NewFrom()") - - testing("we get the same UUID from the same input", func() { - const s = "abcdefghijklmnop" - - r1 := strings.NewReader(s) - uuid1, err := NewFrom(r1) - assertEq(err, nil) - - r2 := strings.NewReader(s) - uuid2, err := NewFrom(r2) - assertEq(err, nil) - - assertEq(uuid1, uuid2) - }) - - testing("we the bytes in UUID are what the reader gives us", func() { - input := []byte{ - 0x00, - 0x01, - 0x02, - 0x03, - 0x04, - 0x05, - 0x06, - 0x07, - 0x08, - 0x09, - 0x0a, - 0x0b, - 0x0c, - 0x0d, - 0x0e, - 0x0f, - } - - expected := UUID{ - 0x00, - 0x01, - 0x02, - 0x03, - 0x04, - 0x05, - 0x06 + 0x40, - 0x07, - 0x08 + 0x80, - 0x09, - 0x0a, - 0x0b, - 0x0c, - 0x0d, - 0x0e, - 0x0f, - } - - r := bytes.NewReader(input) - given, err := NewFrom(r) - assertEq(err, nil) - assertEq(given, expected) - }) - - testing("v4 and variant markers", func() { - input := []byte{ - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x71, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - } - - expected := UUID{ - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x41, // not 0x11 - 0x11, - 0xb1, // not 0x11 - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - 0x11, - } - - r := bytes.NewReader(input) - given, err := NewFrom(r) - assertEq(err, nil) - assertEq(given, expected) - }) -} - -func test_New() { - testStart("New()") - - testing("we can generate UUID values: ", func() { - var uuid UUID = New() - assertEq(len(uuid), 16) - }) - - testing("panic when the randomReader fails", func() { - savedReader := randomReader - randomReader = strings.NewReader("abc") - defer func() { - r := recover() - assertEq(r == nil, false) - randomReader = savedReader - }() - - New() - os.Exit(5) - }) -} - -func test_String() { - testStart("UUID.String()") - - testing("simple example values", func() { - uuids := []UUID { - UUID { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - }, - UUID { - 0, 1, 2, 3, 4, 5, 6, 7, - 8, 9, 10, 11, 12, 13, 14, 15, - }, - UUID { - 222, 222, 222, 222, 222, 222, 222, 222, - 222, 222, 222, 222, 222, 222, 222, 222, - }, - UUID { - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - }, - } - - strs := []string { - "00000000-0000-0000-0000-000000000000", - "00010203-0405-0607-0809-0a0b0c0d0e0f", - "dededede-dede-dede-dede-dededededede", - "ffffffff-ffff-ffff-ffff-ffffffffffff", - } - - for i := range uuids { - s1 := uuids[i].String() - s2 := fmt.Sprintf("%v", uuids[i]) - assertEq(s1, strs[i]) - assertEq(s2, strs[i]) - } - }) -} - -func test_FromString() { - testStart("FromString()") - - testing("UUID -> string -> UUID round trip", func() { - for i := 0; i < 100; i++ { - uuid0 := New() - uuid1, err := FromString(uuid0.String()) - assertEq(err, nil) - assertEq(uuid0, uuid1) - } - }) - - testing("errors we detect", func() { - var err error - - _, err = FromString("") - assertEq(err, ErrBadLength) - - _, err = FromString("---000000000000000000000000000000000") - assertEq(err, ErrBadDashCount) - - _, err = FromString("----00000000000000000000000000000000") - assertEq(err, ErrBadDashPosition) - - _, err = FromString("00000000-0000-0000-0000-00000000000g") - assertEq(err, hex.InvalidByteError('g')) - - _, err = FromString("00000000-0000-0000-0000-000000000000") - assertEq(err, nil) - }) -} - - - -func MainTest() { - test_NewFrom() - test_New() - test_String() - test_FromString() -} diff --git a/tests/main.go b/tests/main.go index b803727..17103ab 100644 --- a/tests/main.go +++ b/tests/main.go @@ -1,7 +1,7 @@ package main -import "guuid" +import "uuid" func main() { - guuid.MainTest() + uuid.MainTest() } diff --git a/tests/uuid.go b/tests/uuid.go new file mode 100644 index 0000000..ad9cac4 --- /dev/null +++ b/tests/uuid.go @@ -0,0 +1,261 @@ +package uuid + +import ( + "bytes" + "encoding/hex" + "fmt" + "os" + "reflect" + "strings" +) + + + +func showColour() bool { + return os.Getenv("NO_COLOUR") == "" +} + +func testStart(name string) { + fmt.Fprintf(os.Stderr, "%s:\n", name) +} + +func testing(message string, body func()) { + if showColour() { + fmt.Fprintf( + os.Stderr, + "\033[0;33mtesting\033[0m: %s... ", + message, + ) + body() + fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "testing: %s... ", message) + body() + fmt.Fprintf(os.Stderr, "OK.\n") + } +} + +func assertEq(given any, expected any) { + if !reflect.DeepEqual(given, expected) { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } + fmt.Fprintf(os.Stderr, "given != expected\n") + fmt.Fprintf(os.Stderr, "given: %#v\n", given) + fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) + os.Exit(1) + } +} + + +func test_NewFrom() { + testStart("NewFrom()") + + testing("we get the same UUID from the same input", func() { + const s = "abcdefghijklmnop" + + r1 := strings.NewReader(s) + uuid1, err := NewFrom(r1) + assertEq(err, nil) + + r2 := strings.NewReader(s) + uuid2, err := NewFrom(r2) + assertEq(err, nil) + + assertEq(uuid1, uuid2) + }) + + testing("we the bytes in UUID are what the reader gives us", func() { + input := []byte{ + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06, + 0x07, + 0x08, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + } + + expected := UUID{ + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06 + 0x40, + 0x07, + 0x08 + 0x80, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + } + + r := bytes.NewReader(input) + given, err := NewFrom(r) + assertEq(err, nil) + assertEq(given, expected) + }) + + testing("v4 and variant markers", func() { + input := []byte{ + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x71, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + } + + expected := UUID{ + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x41, // not 0x11 + 0x11, + 0xb1, // not 0x11 + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + } + + r := bytes.NewReader(input) + given, err := NewFrom(r) + assertEq(err, nil) + assertEq(given, expected) + }) +} + +func test_New() { + testStart("New()") + + testing("we can generate UUID values: ", func() { + var uuid UUID = New() + assertEq(len(uuid), 16) + }) + + testing("panic when the randomReader fails", func() { + savedReader := randomReader + randomReader = strings.NewReader("abc") + defer func() { + r := recover() + assertEq(r == nil, false) + randomReader = savedReader + }() + + New() + os.Exit(5) + }) +} + +func test_String() { + testStart("UUID.String()") + + testing("simple example values", func() { + uuids := []UUID { + UUID { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, + UUID { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + }, + UUID { + 222, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 222, 222, 222, 222, + }, + UUID { + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + }, + } + + strs := []string { + "00000000-0000-0000-0000-000000000000", + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "dededede-dede-dede-dede-dededededede", + "ffffffff-ffff-ffff-ffff-ffffffffffff", + } + + for i := range uuids { + s1 := uuids[i].String() + s2 := fmt.Sprintf("%v", uuids[i]) + assertEq(s1, strs[i]) + assertEq(s2, strs[i]) + } + }) +} + +func test_FromString() { + testStart("FromString()") + + testing("UUID -> string -> UUID round trip", func() { + for i := 0; i < 100; i++ { + uuid0 := New() + uuid1, err := FromString(uuid0.String()) + assertEq(err, nil) + assertEq(uuid0, uuid1) + } + }) + + testing("errors we detect", func() { + var err error + + _, err = FromString("") + assertEq(err, ErrBadLength) + + _, err = FromString("---000000000000000000000000000000000") + assertEq(err, ErrBadDashCount) + + _, err = FromString("----00000000000000000000000000000000") + assertEq(err, ErrBadDashPosition) + + _, err = FromString("00000000-0000-0000-0000-00000000000g") + assertEq(err, hex.InvalidByteError('g')) + + _, err = FromString("00000000-0000-0000-0000-000000000000") + assertEq(err, nil) + }) +} + + + +func MainTest() { + test_NewFrom() + test_New() + test_String() + test_FromString() +} -- cgit v1.2.3