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