aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-08-22 17:26:08 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-08-22 17:36:23 +0200
commitfebb9c275b5247df596876e4eea7fa17b7ec45e7 (patch)
tree214a192cc6b019fb25a659cfdb84601da74438bf /tests
parentf8fc81a5c9fcd986473797df9286c6c9fef683bf (diff)
Add support for UUID generation
Diffstat (limited to 'tests')
-rw-r--r--tests/uuid/buildfile8
-rw-r--r--tests/uuid/driver.cxx97
2 files changed, 105 insertions, 0 deletions
diff --git a/tests/uuid/buildfile b/tests/uuid/buildfile
new file mode 100644
index 0000000..101c41b
--- /dev/null
+++ b/tests/uuid/buildfile
@@ -0,0 +1,8 @@
+# file : tests/uuid/buildfile
+# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+# license : MIT; see accompanying LICENSE file
+
+import libs = libbutl%lib{butl}
+libs += $stdmod_lib
+
+exe{driver}: {hxx cxx}{*} $libs
diff --git a/tests/uuid/driver.cxx b/tests/uuid/driver.cxx
new file mode 100644
index 0000000..f4ee640
--- /dev/null
+++ b/tests/uuid/driver.cxx
@@ -0,0 +1,97 @@
+// file : tests/uuid/driver.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifdef _WIN32
+# include <rpc.h> // GUID
+#endif
+
+#include <cassert>
+#include <sstream>
+#include <iostream>
+
+#include <libbutl/uuid.hxx>
+#include <libbutl/uuid-io.hxx>
+
+using namespace std;
+using namespace butl;
+
+int main ()
+{
+ // Nil.
+ //
+ uuid un;
+ assert (un.nil () && !un);
+
+ // System generator.
+ //
+ uuid u1 (uuid::generate ());
+ uuid u2 (uuid::generate ());
+
+ assert (u1 && u2);
+ assert (u1 != u2);
+
+ // Binary.
+ //
+ assert (uuid (u1.binary ()) == u1);
+
+ // GUID.
+ //
+#ifdef _WIN32
+ assert (uuid (u1.guid ()) == u1);
+#endif
+
+ // String.
+ //
+ assert (uuid (u1.string ()) == u1);
+ assert (uuid (u2.c_string (false).data ()) == u2);
+
+ try {uuid ("123"); assert (false);} catch (const invalid_argument&) {}
+ try {uuid ("2cfX28ff-1a9a-451d-b953-1bb4622e810f"); assert (false);} catch (const invalid_argument&) {}
+
+ // Variant and version.
+ //
+ uuid ur ("2cf228ff-1a9a-451d-b953-1bb4622e810f");
+ uuid ut ("027bf5e8-a471-11e8-aa3f-1f0a5c55c825");
+
+ assert (ur.variant () == uuid_variant::dce &&
+ ur.version () == uuid_version::random);
+
+ assert (ut.variant () == uuid_variant::dce &&
+ ut.version () == uuid_version::time);
+
+ // Comparion.
+ //
+ assert (u1 != u2 && u1 == u1 && ur > ut);
+
+ // Input/output.
+ //
+ {
+ stringstream ss;
+ uuid u;
+ assert (ss << u1 && ss >> u && u == u1);
+ }
+
+ // Swap and move.
+ //
+ {
+ uuid un, uc (u1);
+ uc.swap (un);
+ assert (uc.nil () && un == u1);
+ }
+
+ {
+ uuid uc (u1), um (move (uc));
+ assert (uc.nil () && um == u1);
+ }
+
+ {
+ uuid uc (u1), um (u2);
+ um = move (uc);
+ assert (uc.nil () && um == u1);
+ }
+
+ // Hash.
+ //
+ assert (hash<uuid> () (ur) != hash<uuid> () (ut));
+}