Skip to content

Commit 67c7518

Browse files
CID less operator (#42)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent 3a086bb commit 67c7518

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

include/libp2p/multi/content_identifier.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace libp2p::multi {
3939
std::string toPrettyString(const std::string &base);
4040

4141
bool operator==(const ContentIdentifier &c) const;
42+
bool operator<(const ContentIdentifier &c) const;
4243

4344
Version version;
4445
MulticodecType::Code content_type;

include/libp2p/multi/multihash.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ namespace libp2p::multi {
8888

8989
bool operator==(const Multihash &other) const;
9090
bool operator!=(const Multihash &other) const;
91+
bool operator<(const Multihash &other) const;
9192

9293
private:
9394
/**

src/multi/content_identifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ namespace libp2p::multi {
3737
and content_address == c.content_address;
3838
}
3939

40+
bool ContentIdentifier::operator<(const ContentIdentifier &c) const {
41+
return version < c.version
42+
|| (version == c.version
43+
&& (content_type < c.content_type
44+
|| (content_type == c.content_type
45+
&& content_address < c.content_address)));
46+
}
47+
4048
} // namespace libp2p::multi

src/multi/multihash.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ namespace libp2p::multi {
112112
return !(this->operator==(other));
113113
}
114114

115+
bool Multihash::operator<(const class libp2p::multi::Multihash &other) const {
116+
return this->type_ < other.type_ ||
117+
(this->type_ == other.type_ && this->data_ < other.data_);
118+
}
119+
115120
} // namespace libp2p::multi
116121

117122
size_t std::hash<libp2p::multi::Multihash>::operator()(

test/libp2p/multi/cid_test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,62 @@ TEST(CidTest, PrettyString) {
4343
+ libp2p::common::hex_lower(EXAMPLE_MULTIHASH.getHash()));
4444
}
4545

46+
/**
47+
* @given CID of different versions
48+
* @when compare CIDs
49+
* @then lesser version is always less
50+
*/
51+
TEST(CidTest, CompareDifferentVersion) {
52+
ContentIdentifier c0_v0(ContentIdentifier::Version::V0, MulticodecType::IDENTITY,
53+
ZERO_MULTIHASH);
54+
ContentIdentifier c0_v1(ContentIdentifier::Version::V1, MulticodecType::IDENTITY,
55+
ZERO_MULTIHASH);
56+
ASSERT_TRUE(c0_v0 < c0_v1);
57+
ASSERT_FALSE(c0_v0 < c0_v0);
58+
ASSERT_FALSE(c0_v1 < c0_v1);
59+
60+
ContentIdentifier c1_v1(ContentIdentifier::Version::V1, MulticodecType::IDENTITY,
61+
ZERO_MULTIHASH);
62+
ASSERT_TRUE(c0_v0 < c1_v1);
63+
64+
ContentIdentifier c2_v0(ContentIdentifier::Version::V0, MulticodecType::SHA1,
65+
ZERO_MULTIHASH);
66+
ASSERT_TRUE(c0_v0 < c2_v0);
67+
ASSERT_TRUE(c0_v0 < c0_v1);
68+
}
69+
70+
/**
71+
* @given CID of different types
72+
* @when compare CIDs
73+
* @then lesser type is always less
74+
*/
75+
TEST(CidTest, CompareDifferentTypes) {
76+
ContentIdentifier c1(ContentIdentifier::Version::V1, MulticodecType::IDENTITY,
77+
ZERO_MULTIHASH);
78+
ContentIdentifier c2(ContentIdentifier::Version::V1, MulticodecType::SHA1,
79+
ZERO_MULTIHASH);
80+
ASSERT_TRUE(c1 < c2);
81+
ASSERT_FALSE(c2 < c1);
82+
ASSERT_FALSE(c1 < c1);
83+
ASSERT_FALSE(c2 < c2);
84+
}
85+
86+
/**
87+
* @given CID of different hashes
88+
* @when compare CIDs
89+
* @then lesser hash is always less
90+
*/
91+
TEST(CidTest, CompareDifferentHashes) {
92+
ContentIdentifier c1(ContentIdentifier::Version::V1, MulticodecType::IDENTITY,
93+
ZERO_MULTIHASH);
94+
ContentIdentifier c2(ContentIdentifier::Version::V1, MulticodecType::IDENTITY,
95+
EXAMPLE_MULTIHASH);
96+
ASSERT_TRUE(c1 < c2);
97+
ASSERT_FALSE(c2 < c1);
98+
ASSERT_FALSE(c1 < c1);
99+
ASSERT_FALSE(c2 < c2);
100+
}
101+
46102
class CidEncodeTest
47103
: public testing::TestWithParam<std::pair<
48104
ContentIdentifier, libp2p::outcome::result<std::vector<uint8_t>>>> {};

test/libp2p/multi/multihash_test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,35 @@ TEST(Multihash, FromToBuffer) {
8484
ASSERT_FALSE(Multihash::createFromBytes(v))
8585
<< "Length in the header does not equal actual length";
8686
}
87+
88+
/**
89+
* @given blake hash and sha256 hash with same hash
90+
* @when compare multihashes
91+
* @then sha256 hash is less then blake hash
92+
*/
93+
TEST(Multihash, CompareDifferentTypes) {
94+
std::vector<uint8_t> hash{2, 3, 4};
95+
auto sha256_hash = Multihash::create(HashType::sha256, hash).value();
96+
auto blake_hash = Multihash::create(HashType::blake2s128, hash).value();
97+
// type sha256 < blake2s128
98+
ASSERT_TRUE(sha256_hash < blake_hash);
99+
ASSERT_FALSE(blake_hash < sha256_hash);
100+
ASSERT_FALSE(sha256_hash < sha256_hash);
101+
ASSERT_FALSE(blake_hash < blake_hash);
102+
}
103+
104+
/**
105+
* @given similar hash type and different hashes
106+
* @when compare multihashes
107+
* @then lesser hash is less
108+
*/
109+
TEST(Multihash, CompareDifferentHashes) {
110+
std::vector<uint8_t> hash_lesser{2, 3, 4};
111+
std::vector<uint8_t> hash{3, 4, 5};
112+
auto hash1 = Multihash::create(HashType::sha256, hash_lesser).value();
113+
auto hash2 = Multihash::create(HashType::sha256, hash).value();
114+
ASSERT_TRUE(hash1 < hash2);
115+
ASSERT_FALSE(hash2 < hash1);
116+
ASSERT_FALSE(hash1 < hash1);
117+
ASSERT_FALSE(hash2 < hash2);
118+
}

0 commit comments

Comments
 (0)