From cf6dcaf143d5ca832c0fff39adcc3ca27dd793ce Mon Sep 17 00:00:00 2001 From: Zhangyanrui123 <2844259537@qq.com> Date: Sat, 28 Nov 2026 12:05:49 +0800 Subject: [PATCH] Add equality for policy collection iterators --- casbin/model/policy_collection.cpp | 8 ++++++++ include/casbin/model/policy_collection.hpp | 2 ++ tests/management_api_test.cpp | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/casbin/model/policy_collection.cpp b/casbin/model/policy_collection.cpp index d141af50..e1feba8c 100644 --- a/casbin/model/policy_collection.cpp +++ b/casbin/model/policy_collection.cpp @@ -87,6 +87,10 @@ PoliciesValues::iterator PoliciesValues::iterator::operator++() { return *this; } +bool PoliciesValues::iterator::operator==(const PoliciesValues::iterator& other) const { + return !(*this != other); +} + bool PoliciesValues::iterator::operator!=(const PoliciesValues::iterator& other) const { return opt_vector_iterator != other.opt_vector_iterator || opt_hashset_iterator != other.opt_hashset_iterator; } @@ -146,6 +150,10 @@ PoliciesValues::const_iterator PoliciesValues::const_iterator::operator++() { return *this; } +bool PoliciesValues::const_iterator::operator==(const const_iterator& other) const { + return !(*this != other); +} + bool PoliciesValues::const_iterator::operator!=(const const_iterator& other) const { return opt_vector_iterator != other.opt_vector_iterator || opt_hashset_iterator != other.opt_hashset_iterator; } diff --git a/include/casbin/model/policy_collection.hpp b/include/casbin/model/policy_collection.hpp index a9455980..6a271cf9 100644 --- a/include/casbin/model/policy_collection.hpp +++ b/include/casbin/model/policy_collection.hpp @@ -73,6 +73,7 @@ using PoliciesHashset = std::unordered_set; using reference = value_type&; PolicyValues& operator*() const; iterator operator++(); + bool operator==(const iterator& other) const; bool operator!=(const iterator& other) const; }; @@ -95,6 +96,7 @@ using PoliciesHashset = std::unordered_set; using reference = value_type&; const PolicyValues& operator*() const; const_iterator operator++(); + bool operator==(const const_iterator& other) const; bool operator!=(const const_iterator& other) const; }; diff --git a/tests/management_api_test.cpp b/tests/management_api_test.cpp index cf0b6874..e2370351 100644 --- a/tests/management_api_test.cpp +++ b/tests/management_api_test.cpp @@ -35,6 +35,18 @@ TEST(TestManagementAPI, TestGetList) { ASSERT_TRUE(casbin::ArrayEquals({"data2_admin"}, e.GetAllRoles())); } +TEST(TestManagementAPI, TestPolicyIteratorEquality) { + const PolicyValues policy = {"alice", "data1", "read"}; + for (auto policies : {PoliciesValues::createWithVector({policy}), PoliciesValues::createWithHashset({policy})}) { + const auto& const_policies = policies; + + ASSERT_NE(policies.begin(), policies.end()); + ASSERT_EQ(++policies.begin(), policies.end()); + ASSERT_NE(const_policies.begin(), const_policies.end()); + ASSERT_EQ(++const_policies.begin(), const_policies.end()); + } +} + void TestGetPolicy(casbin::Enforcer& e, const PoliciesValues& res) { PoliciesValues my_res; my_res = e.GetPolicy();