From 69fcc053bbb9156b8b88116a8dc8cc976148d86d Mon Sep 17 00:00:00 2001 From: Benny Halevy Date: Tue, 15 Feb 2022 13:34:38 +0200 Subject: [PATCH] utils: uuid: add null_uuid and respective bool predecate and operator and unit test. Signed-off-by: Benny Halevy Message-Id: <20220215113438.473400-1-bhalevy@scylladb.com> --- test/boost/UUID_test.cc | 22 ++++++++++++++++++++++ utils/UUID.hh | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/boost/UUID_test.cc b/test/boost/UUID_test.cc index 16c6f8512d..4288912a38 100644 --- a/test/boost/UUID_test.cc +++ b/test/boost/UUID_test.cc @@ -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); +} diff --git a/utils/UUID.hh b/utils/UUID.hh index ff9bac98b9..f27b64c7a3 100644 --- a/utils/UUID.hh +++ b/utils/UUID.hh @@ -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) {