Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include "catch.hpp"

#include <algorithm>
#include <ranges>
#include <vector>

#include "DataFormats/Provenance/interface/Hash.h"
#include "FWCore/Utilities/interface/Digest.h"

namespace {
  using TestHash = edm::Hash<100>;
}

TEST_CASE("Hash", "[Hash]") {
  SECTION("Default construction is invalid") { REQUIRE(TestHash{}.isValid() == false); }

  SECTION("Basic operations") {
    cms::Digest d("foo");
    auto result = d.digest().toString();

    TestHash id{result};
    REQUIRE(id.isValid() == true);
    {
      std::string idString;
      id.toString(idString);
      REQUIRE(idString == result);
    }

    TestHash id2 = id;
    REQUIRE(id2.isValid() == true);
    {
      std::string id2String;
      id2.toString(id2String);
      REQUIRE(id2String == result);
    }

    cms::Digest b("bar");
    auto diffResult = b.digest().toString();
    REQUIRE(id2 == TestHash{result});
    REQUIRE(id2 != TestHash{diffResult});

    REQUIRE(id2 > TestHash{diffResult});
    REQUIRE(TestHash{diffResult} < id2);

    REQUIRE(not(id2 < id2));
  }

  SECTION("std::ranges::sort") {
    std::vector<TestHash> container{TestHash{cms::Digest("foo").digest().toString()},
                                    TestHash{cms::Digest("bar").digest().toString()},
                                    TestHash{cms::Digest("fred").digest().toString()},
                                    TestHash{cms::Digest("wilma").digest().toString()}};
    CHECK(container[0] > container[1]);
    CHECK(container[1] < container[2]);
    CHECK(container[2] > container[3]);

    std::ranges::sort(container);
    CHECK(container[0] < container[1]);
    CHECK(container[1] < container[2]);
    CHECK(container[2] < container[3]);
  }
}