From febb9c275b5247df596876e4eea7fa17b7ec45e7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Aug 2018 17:26:08 +0200 Subject: Add support for UUID generation --- tests/uuid/buildfile | 8 +++++ tests/uuid/driver.cxx | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 tests/uuid/buildfile create mode 100644 tests/uuid/driver.cxx (limited to 'tests/uuid') 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 // GUID +#endif + +#include +#include +#include + +#include +#include + +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 () (ur) != hash () (ut)); +} -- cgit v1.1