Home | History | Annotate | Download | only in test
      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 "gtest/gtest.h"
     18 
     19 #include "chre/apps/wifi_offload/channel_histogram.h"
     20 #include "include/random_generator.h"
     21 #include "include/utility.h"
     22 
     23 using wifi_offload_test::kNumFrequencies_Test;
     24 using wifi_offload_test::kAllFrequencies_Test;
     25 using wifi_offload_test::kAllChannels_Test;
     26 
     27 /**
     28  * This file includes all the unit tests for ChannelHistogram class, except ==
     29  * operator and serialize/deserialize functions which have already been covered
     30  * in offloadtypes_test.cc.
     31  */
     32 
     33 class ChannelHistogramTest : public testing::Test {
     34  public:
     35   wifi_offload_test::RandomGenerator random_gen_;
     36   wifi_offload::ChannelHistogram channel_histo_;
     37 };
     38 
     39 TEST_F(ChannelHistogramTest, UnsupportedFrequencyAndchannel_numbersAreHandled) {
     40   // some unsupported frequencies
     41   channel_histo_.IncrementScanCountForFrequency(2000);
     42   channel_histo_.IncrementScanCountForFrequency(42000);
     43 
     44   // verify no count have been increased
     45   for (size_t i = 0; i < kNumFrequencies_Test; i++) {
     46     ASSERT_EQ(0, channel_histo_.GetChannelScanCount(kAllChannels_Test[i]));
     47   }
     48 
     49   // some unsupported channel numbers
     50   EXPECT_EQ(0, channel_histo_.GetChannelScanCount(0));
     51   EXPECT_EQ(0, channel_histo_.GetChannelScanCount(15));
     52   EXPECT_EQ(0, channel_histo_.GetChannelScanCount(200));
     53 }
     54 
     55 TEST_F(ChannelHistogramTest, FrequenciesMapCorrectlyTochannel_numbers) {
     56   for (size_t i = 0; i < kNumFrequencies_Test; i++) {
     57     wifi_offload::ChannelHistogram histo;
     58     histo.IncrementScanCountForFrequency(kAllFrequencies_Test[i]);
     59 
     60     // verify only the increase channel is non-zero
     61     for (size_t j = 0; j < kNumFrequencies_Test; j++) {
     62       EXPECT_EQ(kAllChannels_Test[i] == kAllChannels_Test[j] ? 255 : 0,
     63                 histo.GetChannelScanCount(kAllChannels_Test[j]));
     64     }
     65   }
     66 }
     67 
     68 TEST_F(ChannelHistogramTest, IncreaseFrequencyScanCountAndGetChannelScanCount) {
     69   uint32_t increase = 1;
     70   for (size_t i = 0; i < 12; i++) {
     71     increase *= 2;
     72     channel_histo_.IncrementScanCountForFrequencyForTest(
     73         kAllFrequencies_Test[i], increase);
     74   }
     75 
     76   uint32_t expected = 1;
     77   for (size_t i = 0; i < 12; i++) {
     78     uint8_t scaled_value =
     79         channel_histo_.GetChannelScanCount(kAllChannels_Test[i]);
     80 
     81     expected *= 2;
     82     EXPECT_EQ(expected * 254 / increase + 1, scaled_value);
     83   }
     84 
     85   for (size_t i = 12; i < 14; i++) {
     86     EXPECT_EQ(0, channel_histo_.GetChannelScanCount(kAllChannels_Test[i]));
     87   }
     88 }
     89 
     90 TEST_F(ChannelHistogramTest, SetScanCountToMaxUInt32) {
     91   // add some nice scan counts
     92   uint32_t increase = 1;
     93   for (size_t i = 0; i < 12; i++) {
     94     increase *= 2;
     95     channel_histo_.IncrementScanCountForFrequencyForTest(
     96         kAllFrequencies_Test[i], increase);
     97   }
     98   /* set the next scan count to be 2 ^ 32 - 1, this will force all other counts
     99    * to get mapped to 1 */
    100   channel_histo_.IncrementScanCountForFrequencyForTest(kAllFrequencies_Test[12],
    101                                                        0xffffffff);
    102 
    103   for (size_t i = 0; i < 12; i++) {
    104     EXPECT_EQ(1, channel_histo_.GetChannelScanCount(kAllChannels_Test[i]));
    105   }
    106   EXPECT_EQ(255, channel_histo_.GetChannelScanCount(kAllChannels_Test[12]));
    107 }
    108