summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/guuid.go247
-rw-r--r--tests/libbuild.go11
-rw-r--r--tests/main.go7
3 files changed, 265 insertions, 0 deletions
diff --git a/tests/guuid.go b/tests/guuid.go
new file mode 100644
index 0000000..6470fca
--- /dev/null
+++ b/tests/guuid.go
@@ -0,0 +1,247 @@
+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)
+ })
+}
+
+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, BadLengthError)
+
+ _, err = FromString("---000000000000000000000000000000000")
+ assertEq(err, BadDashCountError)
+
+ _, err = FromString("----00000000000000000000000000000000")
+ assertEq(err, BadDashPositionError)
+
+ _, 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/libbuild.go b/tests/libbuild.go
new file mode 100644
index 0000000..2c02686
--- /dev/null
+++ b/tests/libbuild.go
@@ -0,0 +1,11 @@
+package main
+
+import (
+ "fmt"
+
+ "guuid"
+)
+
+func main() {
+ fmt.Println(guuid.New())
+}
diff --git a/tests/main.go b/tests/main.go
new file mode 100644
index 0000000..b803727
--- /dev/null
+++ b/tests/main.go
@@ -0,0 +1,7 @@
+package main
+
+import "guuid"
+
+func main() {
+ guuid.MainTest()
+}