fixed_ratio_sampler_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/common/fixed_ratio_sampler.h"
00018 
00019 #include "gtest/gtest.h"
00020 
00021 namespace cartographer {
00022 namespace common {
00023 namespace {
00024 
00025 TEST(FixedRatioSamplerTest, AlwaysTrue) {
00026   FixedRatioSampler fixed_ratio_sampler(1.);
00027   for (int i = 0; i < 100; ++i) {
00028     EXPECT_TRUE(fixed_ratio_sampler.Pulse());
00029   }
00030 }
00031 
00032 TEST(FixedRatioSamplerTest, AlwaysFalse) {
00033   FixedRatioSampler fixed_ratio_sampler(0.);
00034   for (int i = 0; i < 100; ++i) {
00035     EXPECT_FALSE(fixed_ratio_sampler.Pulse());
00036   }
00037 }
00038 
00039 TEST(FixedRatioSamplerTest, NonSensicalRatio) {
00040   EXPECT_DEATH(FixedRatioSampler(2.), "ratio");
00041   EXPECT_DEATH(FixedRatioSampler(-0.1), "ratio");
00042 }
00043 
00044 TEST(FixedRatioSamplerTest, SometimesTrue) {
00045   FixedRatioSampler fixed_ratio_sampler(0.5);
00046   for (int i = 0; i < 100; ++i) {
00047     EXPECT_EQ(i % 2 == 0, fixed_ratio_sampler.Pulse());
00048   }
00049 }
00050 
00051 TEST(FixedRatioSamplerTest, FirstPulseIsTrue) {
00052   // Choose a very very small positive number for the ratio.
00053   FixedRatioSampler fixed_ratio_sampler(1e-20);
00054   EXPECT_TRUE(fixed_ratio_sampler.Pulse());
00055   for (int i = 0; i < 100; ++i) {
00056     EXPECT_FALSE(fixed_ratio_sampler.Pulse());
00057   }
00058 }
00059 
00060 }  // namespace
00061 }  // namespace common
00062 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35