stduuid/test/test.cpp

145 lines
3.1 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;
auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s;
uuid guid(str);
assert(guid.string() == str);
}
{
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-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
{
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;
}