GCC Code Coverage Report


Directory: ./
File: src/util/selector.test.cc
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 46 60 76.7%
Functions: 14 15 93.3%
Branches: 63 75 84.0%

Line Branch Exec Source
1 #include "na64util/selector.hh"
2 #include "na64detID/TBName.hh"
3
4 #include <climits>
5 #include <gtest/gtest.h>
6
7 namespace na64dp {
8 namespace test {
9
10 typedef dsul::Selector<unsigned int, std::map<std::string, dsul::CodeVal_t>> TestingSelector;
11
12 static int
13 4 testing_extern_getter( const char * tok
14 , const void * resolverPtr
15 , dsul::CodeVal_t * cv ) {
16 4 const std::map<std::string, dsul::CodeVal_t> * dct =
17 reinterpret_cast<const std::map<std::string, dsul::CodeVal_t> *>(resolverPtr);
18
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 auto it = dct->find(tok);
19
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if( dct->end() == it ) {
20 4 return 0;
21 }
22 *cv = it->second;
23 return 1;
24 }
25
26 static dsul::CodeVal_t
27 8 first( dsul::Principal_t pv ) {
28 8 return ((uintptr_t) pv) & UCHAR_MAX;
29 }
30
31 static dsul::CodeVal_t
32 8 second( dsul::Principal_t pv ) {
33 8 return ((uintptr_t) pv) & (((uintptr_t) UCHAR_MAX) << 8);
34 }
35
36 static dsul::CodeVal_t
37 last( dsul::Principal_t ) {
38 return 0; // TODO
39 }
40
41 TestingSelector::SymDef gTestGetters[] = {
42 { "+", { .resolve_name = testing_extern_getter }, "" },
43 { "$first", { first }, "" },
44 { "$second", { second }, "" },
45 { "~last", { last }, "" },
46 { NULL, {NULL}, NULL }
47 };
48
49 static bool
50 8 test_f1( unsigned int pv ) {
51 8 unsigned int first_ = UCHAR_MAX & pv
52 8 , second_ = ((uintptr_t) pv) & (((uintptr_t) UCHAR_MAX) << 8);
53
1/5
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
8 return second_ == 85 && ((first_ >= 64 && first_ <= 128) || first_ == 255);
54 }
55
56 static struct expressionTest {
57 const char * expression;
58 bool (*c_func)(unsigned int);
59 unsigned int pv[8];
60 } exprTests[] = {
61 {
62 "second == 85 && ((first >= 64 && first <= 128) || first == 255)",
63 test_f1,
64 {
65 0x0,
66 0x5500,
67 0x5540,
68 0x40,
69 0x80,
70 0x5580,
71 0x55ff,
72 0x55ff,
73 }
74 },
75 #if 0 // well, whatever ....
76 {
77 "((first == 1 && second == 12) || (first == 12 && second == 1)) && last != 1",
78 test_f1,
79 {
80
81 }
82 },
83 #endif
84 { nullptr, nullptr, {0} } // sentinel
85 };
86
87 /* This test operates with principal value of four independent bytes packed
88 * into a single "unsigend int" value */
89 8 TEST(Selectors, Basic) {
90 2 std::map<std::string, dsul::CodeVal_t> externVariables;
91
92
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
4 for( auto et = exprTests; et->expression; ++et ) {
93 2 TestingSelector * selPtr = nullptr;
94 try {
95 selPtr = new TestingSelector( et->expression
96 , gTestGetters
97
3/3
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
4 , externVariables );
98 } catch( const errors::SelectorBuildupFailure & e ) {
99 std::cerr << e.what();
100 throw;
101 }
102
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
18 for( int i = 0; i < 8; ++i ) {
103
4/5
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 8 times.
16 ASSERT_EQ( selPtr->matches( et->pv[i] )
104 , et->c_func( et->pv[i] ) )
105 << " on example #" << (exprTests - et) << ":" << i
106 << ", ev. table dump:" << std::endl
107
1/2
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
16 << *selPtr;
108 }
109 }
110 2 }
111
112 /* Tests detector ID type selectors with name caching (actually, is the main
113 * purpose of DSuL). */
114
115 // Testing ficture
116 class MockNaming : public ::testing::Test {
117 protected:
118 nameutils::DetectorNaming _naming;
119 public:
120 2 virtual void SetUp() {
121 // Define chips
122 {
123 auto & ct =
124
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
12 _naming.chip_add( "CHIP1", 0x1 );
125 2 ct.description = "Testing chip #1";
126 2 ct.pathFormat = "{kin}{statNum2}/{hist}";
127 }
128 {
129 auto & ct =
130
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
12 _naming.chip_add( "CHIP2", 0x2 );
131 2 ct.description = "Testing chip #2";
132 2 ct.pathFormat = "{kin}{statNum2}/{hist}";
133 }
134 // Define kins for chips
135
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
20 _naming.define_kin( "ONE", 0x1, "CHIP1" );
136
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
20 _naming.define_kin( "TWO", 0x2, "CHIP1" );
137
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
20 _naming.define_kin( "THREE", 0x1, "CHIP2" );
138
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
20 _naming.define_kin( "FOUR", 0x2, "CHIP2" );
139 2 }
140 };
141
142 // Tests basic correspondance
143 8 TEST_F(MockNaming, BasicSelection) {
144 DetSelect s1( "chip == CHIP1 && chip == 1 && kin == TWO"
145
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
4 , util::gDetIDGetters, _naming );
146
3/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
2 ASSERT_TRUE( s1( DetID(0x1, 0x2, 1, 0) ) )
147 << "Dump of the evaluation workspace:" << std::endl
148 << s1
149
1/2
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
2 << "Dump end" << std::endl;
150 2 }
151
152 // Tests that DSuL is capable to differ kins with the same numerical ID but
153 // belonging to different chips
154 8 TEST_F(MockNaming, KinDiffers) {
155 DetSelect s1( "(kin == ONE && kin != THREE) || (kin == TWO && kin != FOUR)"
156 " || (kin == THREE && kin != ONE) || (kin == FOUR && kin != TWO)"
157
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
4 , util::gDetIDGetters, _naming );
158
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
6 for( int detChip = 1; detChip < 3; ++detChip ) {
159
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
12 for( int detKin = 1; detKin < 3; ++detKin ) {
160
3/4
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
8 EXPECT_TRUE( s1( DetID(detChip, detKin, 1, 0) ) )
161 << "Dump of the evaluation workspace:" << std::endl
162 << s1
163 8 << "Dump end" << std::endl;
164 }
165 }
166 2 }
167
168 }
169 }
170
171