Home | History | Annotate | Download | only in tests
      1 #include <gtest/gtest.h>
      2 
      3 #include <private/dvr/numeric.h>
      4 
      5 using TestTypes = ::testing::Types<float, double, int>;
      6 
      7 using android::dvr::RandomInRange;
      8 
      9 template <typename T>
     10 class NumericTest : public ::testing::TestWithParam<T> {
     11  public:
     12   using FT = T;
     13 };
     14 
     15 TYPED_TEST_CASE(NumericTest, TestTypes);
     16 
     17 TYPED_TEST(NumericTest, RandomInRange) {
     18   using FT = typename TestFixture::FT;
     19 
     20   const int kNumTrials = 50;
     21   const FT kLowRange = static_cast<FT>(-100);
     22   const FT kHighRange = static_cast<FT>(100);
     23 
     24   for (int i = 0; i < kNumTrials; ++i) {
     25     FT value = RandomInRange(kLowRange, kHighRange);
     26 
     27     EXPECT_LE(kLowRange, value);
     28     EXPECT_GE(kHighRange, value);
     29   }
     30 }
     31 
     32 TEST(RandomInRange, TestIntVersion) {
     33   // This checks specifically that the function does not always give the lo
     34   // value (this was previously a bug)
     35 
     36   const int kNumTrials = 50;
     37   const int kLowRange = -100;
     38   const int kHighRange = 100;
     39 
     40   for (int i = 0; i < kNumTrials; ++i) {
     41     int value = RandomInRange(kLowRange, kHighRange);
     42 
     43     if (value != kLowRange) {
     44       SUCCEED();
     45       return;
     46     }
     47   }
     48 
     49   FAIL() << "Did not produce a value other than the range minimum for "
     50          << "integers.";
     51 }
     52 
     53 TEST(RandomInRange, TestVectorVersion) {
     54   Eigen::Vector3d lo(-3.0, -4.0, -5.0);
     55   Eigen::Vector3d hi(5.0, 4.0, 3.0);
     56 
     57   const int kNumTrials = 50;
     58 
     59   for (int i = 0; i < kNumTrials; ++i) {
     60     Eigen::Vector3d result = RandomInRange(lo, hi);
     61 
     62     for (int j = 0; j < 3; ++j) {
     63       EXPECT_LE(lo[j], result[j]);
     64       EXPECT_GE(hi[j], result[j]);
     65     }
     66   }
     67 }
     68