utils: uuid: add null_uuid

and respective bool predecate and operator
and unit test.

Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
Message-Id: <20220215113438.473400-1-bhalevy@scylladb.com>
This commit is contained in:
Benny Halevy
2022-02-15 13:34:38 +02:00
committed by Nadav Har'El
parent 817d1aade8
commit 69fcc053bb
2 changed files with 36 additions and 0 deletions

View File

@@ -234,3 +234,25 @@ BOOST_AUTO_TEST_CASE(test_negate) {
BOOST_REQUIRE(original_uuid == re_negated_uuid);
}
BOOST_AUTO_TEST_CASE(test_null_uuid) {
// Verify that the default-constructed UUID is null
utils::UUID uuid;
BOOST_CHECK(uuid.is_null());
BOOST_CHECK(!uuid);
// Verify that the null_uuid is indeed null
uuid = utils::null_uuid();
BOOST_CHECK(uuid.is_null());
BOOST_CHECK(!uuid);
// Verify that a random uuid is not null
uuid = utils::make_random_uuid();
BOOST_CHECK(!uuid.is_null());
BOOST_CHECK(uuid);
// Verify that a time uuid is not null
uuid = utils::UUID_gen::get_time_UUID();
BOOST_CHECK(!uuid.is_null());
BOOST_CHECK(uuid);
}

View File

@@ -109,6 +109,16 @@ public:
return !(*this < v);
}
// Valid (non-null) UUIDs always have their version
// nibble set to a non-zero value
bool is_null() const noexcept {
return !most_sig_bits && !least_sig_bits;
}
operator bool() const noexcept {
return !is_null();
}
bytes serialize() const {
bytes b(bytes::initialized_later(), serialized_size());
auto i = b.begin();
@@ -127,6 +137,10 @@ public:
}
};
inline UUID null_uuid() noexcept {
return UUID();
}
UUID make_random_uuid();
inline std::strong_ordering uint64_t_tri_compare(uint64_t a, uint64_t b) {