1 /*
2  * Copyright (C) 2013 Tommi Maekitalo
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  */
19 
20 #include <zim/uuid.h>
21 #include <iostream>
22 #include <sstream>
23 
24 #include "gtest/gtest.h"
25 #ifdef _WIN32
26 # include <windows.h>
27 # include <synchapi.h>
28 #else
29 # include <unistd.h>
30 #endif
31 
32 namespace
33 {
TEST(UuidTest,construct)34 TEST(UuidTest, construct)
35 {
36   zim::Uuid uuid1(
37       "\x01\x23\x45\x67\x89\xab\xcd\xef\x10\x32\x54\x76\x98\xba\xdc\xfe");
38   zim::Uuid uuid2(
39       "\x01\x23\x45\x67\x89\xab\xcd\xe0\x10\x32\x54\x76\x98\xba\xdc\x0e");
40 
41   ASSERT_TRUE(uuid1 != uuid2);
42   ASSERT_TRUE(uuid1 != zim::Uuid());
43   ASSERT_TRUE(uuid2 != zim::Uuid());
44 
45   ASSERT_EQ(uuid1.data[0], '\x01');
46   ASSERT_EQ(uuid1.data[1], '\x23');
47   ASSERT_EQ(uuid1.data[2], '\x45');
48   ASSERT_EQ(uuid1.data[3], '\x67');
49   ASSERT_EQ(uuid1.data[4], '\x89');
50   ASSERT_EQ(uuid1.data[5], '\xab');
51   ASSERT_EQ(uuid1.data[6], '\xcd');
52   ASSERT_EQ(uuid1.data[7], '\xef');
53   ASSERT_EQ(uuid1.data[8], '\x10');
54   ASSERT_EQ(uuid1.data[9], '\x32');
55   ASSERT_EQ(uuid1.data[10], '\x54');
56   ASSERT_EQ(uuid1.data[11], '\x76');
57   ASSERT_EQ(uuid1.data[12], '\x98');
58   ASSERT_EQ(uuid1.data[13], '\xba');
59   ASSERT_EQ(uuid1.data[14], '\xdc');
60   ASSERT_EQ(uuid1.data[15], '\xfe');
61 
62   ASSERT_EQ(uuid2.data[0], '\x01');
63   ASSERT_EQ(uuid2.data[1], '\x23');
64   ASSERT_EQ(uuid2.data[2], '\x45');
65   ASSERT_EQ(uuid2.data[3], '\x67');
66   ASSERT_EQ(uuid2.data[4], '\x89');
67   ASSERT_EQ(uuid2.data[5], '\xab');
68   ASSERT_EQ(uuid2.data[6], '\xcd');
69   ASSERT_EQ(uuid2.data[7], '\xe0');
70   ASSERT_EQ(uuid2.data[8], '\x10');
71   ASSERT_EQ(uuid2.data[9], '\x32');
72   ASSERT_EQ(uuid2.data[10], '\x54');
73   ASSERT_EQ(uuid2.data[11], '\x76');
74   ASSERT_EQ(uuid2.data[12], '\x98');
75   ASSERT_EQ(uuid2.data[13], '\xba');
76   ASSERT_EQ(uuid2.data[14], '\xdc');
77   ASSERT_EQ(uuid2.data[15], '\x0e');
78 }
79 
TEST(UuidTest,generate)80 TEST(UuidTest, generate)
81 {
82   zim::Uuid uuid1;
83   zim::Uuid uuid2;
84   ASSERT_TRUE(uuid1 == uuid2);
85   ASSERT_TRUE(uuid1 == zim::Uuid());
86   ASSERT_TRUE(uuid2 == zim::Uuid());
87 
88   uuid1 = zim::Uuid::generate();
89   ASSERT_TRUE(uuid1 != uuid2);
90   ASSERT_TRUE(uuid1 != zim::Uuid());
91   ASSERT_TRUE(uuid2 == zim::Uuid());
92 
93   // Since GNU Mach's clock isn't precise hence the time might be
94   // same during generating uuid1 and uuid2 leading to test
95   // failure. To bring the time difference between 2 sleep for a
96   // second. Thanks to Pino Toscano.
97 #ifdef _WIN32
98   Sleep(1000);
99 #else
100   sleep(1);
101 #endif
102 
103   uuid2 = zim::Uuid::generate();
104   ASSERT_TRUE(uuid1 != uuid2);
105   ASSERT_TRUE(uuid1 != zim::Uuid());
106   ASSERT_TRUE(uuid2 != zim::Uuid());
107 }
108 
TEST(UuidTest,output)109 TEST(UuidTest, output)
110 {
111   zim::Uuid uuid(
112       "\x55\x0e\x84\x00\xe2\x9b\x41\xd4\xa7\x16\x44\x66\x55\x44\x00\x00");
113   std::ostringstream out;
114   out << uuid;
115   std::string s = out.str();
116   ASSERT_EQ(s, "550e8400-e29b-41d4-a716-446655440000");
117 }
118 };
119