Skip to content
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
14 changes: 14 additions & 0 deletions .github/workflows/check-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,17 @@ jobs:
run: |
ls code_linter
bash ./tools/scripts/code_checker.sh

cpp-header-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Identifier equality in release and checked-iterator builds
run: |
c++ -std=c++17 -O2 -DNDEBUG -Wall -Wextra -Werror -Iinclude \
tools/testers/identifiers_test.cpp -o /tmp/identifiers_test
/tmp/identifiers_test
c++ -std=c++17 -O0 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -Iinclude \
tools/testers/identifiers_test.cpp -o /tmp/identifiers_test_debug
/tmp/identifiers_test_debug
2 changes: 1 addition & 1 deletion include/cpp_common/identifiers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Identifiers {
* @param [in] rhs set of identifiers to be compared
*/
bool operator==(const Identifiers<T> &rhs) const {
return std::equal(m_ids.begin(), m_ids.end(), rhs.m_ids.begin());
return m_ids == rhs.m_ids;
}

//! @name set UNION
Expand Down
62 changes: 62 additions & 0 deletions tools/testers/identifiers_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2026 Suyash Bhattarai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#include <array>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>

#include "cpp_common/identifiers.hpp"

namespace {

template <typename T, std::size_t N>
pgrouting::Identifiers<T> identifiers_from_mask(
const std::array<T, N>& values, std::size_t mask) {
pgrouting::Identifiers<T> result;
for (std::size_t bit = 0; bit < N; ++bit) {
if (mask & (std::size_t{1} << bit)) result += values[bit];
}
return result;
}

template <typename T, std::size_t N>
bool check_equality(const std::array<T, N>& values) {
const auto subsets = std::size_t{1} << N;
for (std::size_t left_mask = 0; left_mask < subsets; ++left_mask) {
const auto left = identifiers_from_mask(values, left_mask);
for (std::size_t right_mask = 0; right_mask < subsets; ++right_mask) {
const auto right = identifiers_from_mask(values, right_mask);
// Each distinct bit mask represents a distinct set of identifiers.
if ((left == right) != (left_mask == right_mask)) {
std::cerr << "Incorrect equality for " << left << " and " << right << '\n';
return false;
}
}
}
return true;
}

} // namespace

int main() {
const std::array<int64_t, 5> signed_values{
-7, 0, 3, 42, (std::numeric_limits<int64_t>::max)()};
const std::array<std::size_t, 5> unsigned_values{
0, 3, 42, 1000, (std::numeric_limits<std::size_t>::max)()};
if (!check_equality(signed_values) || !check_equality(unsigned_values)) return 1;
std::cout << "All 2048 identifier-set comparisons passed.\n";
return 0;
}