stduuid/test/test.cpp

211 lines
4.6 KiB
C++
Raw Normal View History

2017-12-08 23:03:06 +00:00
#include "..\include\uuid.h"
#include <assert.h>
#include <iostream>
2017-12-09 21:56:40 +00:00
#include <set>
#include <unordered_set>
2017-12-08 23:03:06 +00:00
int main()
{
using namespace uuids;
{
2017-12-09 21:56:40 +00:00
std::cout << "Test default constructor" << std::endl;
2017-12-08 23:03:06 +00:00
uuid empty;
2017-12-18 12:23:52 +00:00
assert(empty.nil());
2017-12-08 23:03:06 +00:00
assert(empty.size() == 16);
}
2017-12-09 21:56:40 +00:00
{
std::cout << "Test string constructor" << std::endl;
using namespace std::string_literals;
2017-12-18 14:13:04 +00:00
{
auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s;
uuid guid(str);
assert(guid.string() == str);
}
{
uuid guid("47183823-2574-4bfd-b411-99ed177d3e43");
2017-12-18 15:39:22 +00:00
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43");
assert(guid.wstring() == L"47183823-2574-4bfd-b411-99ed177d3e43");
2017-12-18 14:13:04 +00:00
}
2017-12-09 21:56:40 +00:00
}
{
std::cout << "Test wstring constructor" << std::endl;
using namespace std::string_literals;
auto str = L"47183823-2574-4bfd-b411-99ed177d3e43"s;
uuid guid(str);
assert(guid.wstring() == str);
}
2017-12-18 14:13:04 +00:00
{
std::cout << "Test std::array constructor" << std::endl;
2017-12-18 21:33:44 +00:00
std::array<uint8_t, 16> arr{{
2017-12-18 14:13:04 +00:00
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
2017-12-18 21:33:44 +00:00
}};
2017-12-18 14:13:04 +00:00
using namespace std::string_literals;
uuid guid(arr);
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
}
{
std::cout << "Test array constructor" << std::endl;
using namespace std::string_literals;
uint8_t arr[16] = {
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
};
uuid guid(arr);
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
}
2017-12-08 23:03:06 +00:00
{
std::cout << "Test make" << std::endl;
uuid const guid = uuids::make_uuid();
2017-12-18 12:23:52 +00:00
assert(!guid.nil());
2017-12-08 23:03:06 +00:00
assert(guid.size() == 16);
2017-12-09 21:56:40 +00:00
assert(guid.version() == uuids::uuid_version::random_number_based);
assert(guid.variant() == uuids::uuid_variant::rfc);
2017-12-08 23:03:06 +00:00
}
{
2017-12-09 21:56:40 +00:00
std::cout << "Test equality" << std::endl;
2017-12-08 23:03:06 +00:00
uuid empty;
uuid guid = uuids::make_uuid();
assert(empty == empty);
assert(guid == guid);
assert(empty != guid);
}
2017-12-09 21:56:40 +00:00
{
std::cout << "Test comparison" << std::endl;
auto empty = uuid{};
auto id = make_uuid();
assert(empty < id);
std::set<uuids::uuid> ids{
uuid{},
uuids::make_uuid(),
uuids::make_uuid(),
uuids::make_uuid(),
uuids::make_uuid()
};
assert(ids.size() == 5);
assert(ids.find(uuid{}) != ids.end());
}
{
std::cout << "Test hashing" << std::endl;
using namespace std::string_literals;
auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s;
auto guid = uuid{ str };
auto h1 = std::hash<std::string>{};
auto h2 = std::hash<uuid>{};
assert(h1(str) == h2(guid));
std::unordered_set<uuids::uuid> ids{
uuid{},
uuids::make_uuid(),
uuids::make_uuid(),
uuids::make_uuid(),
uuids::make_uuid()
};
assert(ids.size() == 5);
assert(ids.find(uuid{}) != ids.end());
}
2017-12-08 23:03:06 +00:00
{
std::cout << "Test swap" << std::endl;
uuid empty;
uuid guid = uuids::make_uuid();
2017-12-18 12:23:52 +00:00
assert(empty.nil());
assert(!guid.nil());
2017-12-08 23:03:06 +00:00
std::swap(empty, guid);
2017-12-18 12:23:52 +00:00
assert(!empty.nil());
assert(guid.nil());
2017-12-08 23:03:06 +00:00
empty.swap(guid);
2017-12-18 12:23:52 +00:00
assert(empty.nil());
assert(!guid.nil());
2017-12-08 23:03:06 +00:00
}
{
std::cout << "Test string conversion" << std::endl;
uuid empty;
assert(empty.string() == "00000000-0000-0000-0000-000000000000");
assert(empty.wstring() == L"00000000-0000-0000-0000-000000000000");
2017-12-09 21:56:40 +00:00
}
2017-12-08 23:03:06 +00:00
2017-12-18 15:39:22 +00:00
{
std::cout << "Test iterators" << std::endl;
2017-12-18 21:33:44 +00:00
std::array<uint8_t, 16> arr{{
2017-12-18 15:39:22 +00:00
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
2017-12-18 21:33:44 +00:00
}};
2017-12-18 15:39:22 +00:00
uuid guid;
assert(guid.nil());
std::copy(std::cbegin(arr), std::cend(arr), std::begin(guid));
assert(!guid.nil());
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43");
size_t i = 0;
for (auto const & b : guid)
{
assert(arr[i++] == b);
}
}
2017-12-08 23:03:06 +00:00
{
std::cout << "Test constexpr" << std::endl;
constexpr uuid empty;
2017-12-18 12:23:52 +00:00
constexpr bool isnil = empty.nil();
2017-12-08 23:03:06 +00:00
constexpr size_t size = empty.size();
2017-12-09 21:56:40 +00:00
constexpr uuid_variant variant = empty.variant();
constexpr uuid_version version = empty.version();
2017-12-08 23:03:06 +00:00
}
std::cout << "ALL PASSED" << std::endl;
return 0;
}