aboutsummaryrefslogtreecommitdiff
path: root/tests/uuid/driver.cxx
blob: f4ee640a65e23dfac54cd3f334afcfedfa0b221e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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));
}