R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
testClusteringEngine.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "ClusteringEngine.h"
15#include "gtest/gtest.h"
16#include <stdexcept>
17#include <vector>
18
19namespace
20{
21
22 TEST(testClusteringEngine, basic_int_clustering)
23 {
24 auto clusteringCondition = [](const int& a, const int& b) { return std::abs(b - a) <= 1; };
25
26 auto clusterer = Neuland::ClusteringEngine<int>(clusteringCondition);
27
28 std::vector<int> digis{ 1, 2, 3, 7, 8, 9, 10, 12 };
29 auto clusters = clusterer.Clusterize(digis);
30
31 std::vector<std::vector<int>> expected = { { 1, 2, 3 }, { 7, 8, 9, 10 }, { 12 } };
32
33 EXPECT_EQ(clusters, expected);
34 }
35
36 TEST(testClusteringEngine, unsorted_object_clustering)
37 {
38 struct DummyDigi
39 {
40 int a;
41 DummyDigi(const int& _a)
42 : a(_a)
43 {
44 }
45 bool operator==(const DummyDigi& rhs) const { return a == rhs.a; }
46 };
47
48 auto clusterer = Neuland::ClusteringEngine<DummyDigi>();
49 clusterer.SetClusteringCondition([](const DummyDigi& one, const DummyDigi& another)
50 { return std::abs(another.a - one.a) <= 1; });
51
52 std::vector<DummyDigi> digis{ 28, 13, 23, 22, 15, 16, 3, 6, 4, 26, 10, 11, 19, 8, 29, 12, 25, 30, 17, 18, 24 };
53 std::vector<std::vector<DummyDigi>> clusters = clusterer.Clusterize(digis);
54
55 std::vector<std::vector<DummyDigi>> expected{
56 { 28, 29, 30 }, { 22, 23, 24, 25, 26 }, { 4, 3 }, { 10, 11, 12, 13 }, { 8 }, { 19, 18, 17, 16, 15 }, { 6 }
57 };
58 EXPECT_EQ(clusters, expected);
59 }
60
61 TEST(testClusteringEngine, clustering_condition_not_set)
62 {
63 auto clusterer = Neuland::ClusteringEngine<int>();
64 std::vector<int> digis{ 1, 2, 3, 7, 8, 9, 10, 12 };
65 EXPECT_ANY_THROW(clusterer.Clusterize(digis));
66 }
67
68 TEST(testClusteringEngine, check_SatisfiesClusteringCondition)
69 {
70 auto clusterer =
71 Neuland::ClusteringEngine<int>([](const int& a, const int& b) { return std::abs(b - a) <= 1; });
72 EXPECT_TRUE(clusterer.SatisfiesClusteringCondition(1, 2));
73 EXPECT_FALSE(clusterer.SatisfiesClusteringCondition(1, 3));
74 }
75
76} // namespace