lin
2025-04-23 399353eb5dc7e9c1db94cc97c380dc7f66c51a4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <broadcastradio-utils-2x/Utils.h>
#include <gtest/gtest.h>
 
namespace {
 
namespace V2_0 = android::hardware::broadcastradio::V2_0;
namespace utils = android::hardware::broadcastradio::utils;
 
using V2_0::IdentifierType;
using V2_0::ProgramSelector;
 
TEST(IdentifierIteratorTest, singleSecondary) {
    // clang-format off
    V2_0::ProgramSelector sel {
        utils::make_identifier(IdentifierType::RDS_PI, 0xBEEF),
        {utils::make_identifier(IdentifierType::AMFM_FREQUENCY, 100100)}
    };
    // clang-format on
 
    auto it = V2_0::begin(sel);
    auto end = V2_0::end(sel);
 
    ASSERT_NE(end, it);
    EXPECT_EQ(sel.primaryId, *it);
    ASSERT_NE(end, ++it);
    EXPECT_EQ(sel.secondaryIds[0], *it);
    ASSERT_EQ(end, ++it);
}
 
TEST(IdentifierIteratorTest, empty) {
    V2_0::ProgramSelector sel{};
 
    auto it = V2_0::begin(sel);
    auto end = V2_0::end(sel);
 
    ASSERT_NE(end, it++);  // primary id is always present
    ASSERT_EQ(end, it);
}
 
TEST(IdentifierIteratorTest, twoSelectors) {
    V2_0::ProgramSelector sel1{};
    V2_0::ProgramSelector sel2{};
 
    auto it1 = V2_0::begin(sel1);
    auto it2 = V2_0::begin(sel2);
 
    EXPECT_NE(it1, it2);
}
 
TEST(IdentifierIteratorTest, increments) {
    V2_0::ProgramSelector sel{{}, {{}, {}}};
 
    auto it = V2_0::begin(sel);
    auto end = V2_0::end(sel);
    auto pre = it;
    auto post = it;
 
    EXPECT_NE(++pre, post++);
    EXPECT_EQ(pre, post);
    EXPECT_EQ(pre, it + 1);
    ASSERT_NE(end, pre);
}
 
TEST(IdentifierIteratorTest, findType) {
    using namespace std::placeholders;
 
    uint64_t rds_pi1 = 0xDEAD;
    uint64_t rds_pi2 = 0xBEEF;
    uint64_t freq1 = 100100;
    uint64_t freq2 = 107900;
 
    // clang-format off
    V2_0::ProgramSelector sel {
        utils::make_identifier(IdentifierType::RDS_PI, rds_pi1),
        {
            utils::make_identifier(IdentifierType::AMFM_FREQUENCY, freq1),
            utils::make_identifier(IdentifierType::RDS_PI, rds_pi2),
            utils::make_identifier(IdentifierType::AMFM_FREQUENCY, freq2),
        }
    };
    // clang-format on
 
    auto typeEquals = [](const V2_0::ProgramIdentifier& id, V2_0::IdentifierType type) {
        return utils::getType(id) == type;
    };
    auto isRdsPi = std::bind(typeEquals, _1, IdentifierType::RDS_PI);
    auto isFreq = std::bind(typeEquals, _1, IdentifierType::AMFM_FREQUENCY);
 
    auto end = V2_0::end(sel);
    auto it = std::find_if(V2_0::begin(sel), end, isRdsPi);
    ASSERT_NE(end, it);
    EXPECT_EQ(rds_pi1, it->value);
 
    it = std::find_if(it + 1, end, isRdsPi);
    ASSERT_NE(end, it);
    EXPECT_EQ(rds_pi2, it->value);
 
    it = std::find_if(V2_0::begin(sel), end, isFreq);
    ASSERT_NE(end, it);
    EXPECT_EQ(freq1, it->value);
 
    it = std::find_if(++it, end, isFreq);
    ASSERT_NE(end, it);
    EXPECT_EQ(freq2, it->value);
}
 
TEST(IdentifierIteratorTest, rangeLoop) {
    V2_0::ProgramSelector sel{{}, {{}, {}, {}}};
 
    unsigned count = 0;
    for (auto&& id : sel) {
        ASSERT_EQ(0u, id.type);
        count++;
    }
 
    const auto expectedCount = 1 + sel.secondaryIds.size();
    ASSERT_EQ(expectedCount, count);
}
 
}  // anonymous namespace