Skip to content

nevra: Add bool Nevra::operator<(const Nevra &) const #2192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/libdnf5/rpm/nevra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ struct LIBDNF_API Nevra {
/// @return `true` if all Nevra attributes (`name`, `epoch`, `version`, `release` and `arch`) match.
bool operator==(const Nevra & other) const;

/// Compares Nevra attributes in order: `name`, `epoch`, `version`, `release`, `arch`, and checks
/// if any is less than the corresponding attribute.
/// @return `true` if any of the attributes is less than the corresponding one. Return `false` otherwise.

bool operator>(const Nevra & other) const { return other < *this; }
bool operator<=(const Nevra & other) const { return !(*this > other); }
bool operator>=(const Nevra & other) const { return !(*this < other); }

// NOTE: required by cppunit asserts
//friend std::ostringstream & operator<<(std::ostringstream & out, const Nevra & nevra);

Expand Down
5 changes: 5 additions & 0 deletions libdnf5/rpm/nevra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ bool Nevra::operator==(const Nevra & other) const {
}


bool Nevra::operator<(const Nevra & other) const {
return cmp_nevra(*this, other);
}


std::ostringstream & operator<<(std::ostringstream & out, const Nevra & nevra) {
out << to_full_nevra_string(nevra);
return out;
Expand Down
15 changes: 15 additions & 0 deletions test/libdnf5/rpm/test_nevra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,18 @@ void NevraTest::test_cmp_naevr() {
std::sort(actual.begin(), actual.end(), libdnf5::rpm::cmp_naevr<TestPackage>);
CPPUNIT_ASSERT_EQUAL(std::vector<TestPackage>({foo_0_1_4_noarch, foo_0_1_1_1_noarch}), actual);
}


void NevraTest::test_nevra_operator_less() {
TestPackage foo_0_1_1_noarch("foo-1-1.noarch");
TestPackage foo_1_1_1_noarch("foo-1:1-1.noarch");
TestPackage foo_0_2_1_noarch("foo-2-1.noarch");
TestPackage foo_0_1_1_1_noarch("foo-1.1-1.noarch");
TestPackage foo_0_1_1_s390x("foo-1-1.s390x");

CPPUNIT_ASSERT(!(foo_0_1_1_noarch < foo_0_1_1_noarch));
CPPUNIT_ASSERT(foo_0_1_1_noarch < foo_1_1_1_noarch);
CPPUNIT_ASSERT(foo_0_1_1_noarch < foo_0_2_1_noarch);
CPPUNIT_ASSERT(foo_0_1_1_noarch < foo_0_1_1_1_noarch);
CPPUNIT_ASSERT(foo_0_1_1_noarch < foo_0_1_1_s390x);
}
2 changes: 2 additions & 0 deletions test/libdnf5/rpm/test_nevra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class NevraTest : public CppUnit::TestCase {
CPPUNIT_TEST(test_evrcmp);
CPPUNIT_TEST(test_cmp_nevra);
CPPUNIT_TEST(test_cmp_naevr);
CPPUNIT_TEST(test_nevra_operator_less);
CPPUNIT_TEST_SUITE_END();

public:
Expand All @@ -41,6 +42,7 @@ class NevraTest : public CppUnit::TestCase {
void test_evrcmp();
void test_cmp_nevra();
void test_cmp_naevr();
void test_nevra_operator_less();
};

#endif