Advertisement
Guest User

distance_test.cpp

a guest
Feb 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <stan/math/fwd/scal.hpp>
  2. #include <gtest/gtest.h>
  3.  
  4. TEST(Fwd, distance) {
  5.     using stan::math::fvar;
  6.     using stan::math::distance;
  7.    
  8.     fvar<double> x1(1.0,1.0);
  9.     fvar<double> x2(4.0,1.0);
  10.    
  11.     double x1c = 1.0;
  12.     double x2c = 4.0;
  13.    
  14.     fvar<double> dx1 = distance(x1,x2c);
  15.     EXPECT_FLOAT_EQ(3, dx1.val_);
  16.     EXPECT_FLOAT_EQ(-1,dx1.d_);
  17.    
  18.     fvar<double> dx2 = distance(x1c,x2);
  19.     EXPECT_FLOAT_EQ(3, dx2.val_);
  20.     EXPECT_FLOAT_EQ(1,dx2.d_);
  21.    
  22.     dx1= distance(x1,x1c);
  23.     EXPECT_FLOAT_EQ(0,dx1.val_);
  24.     EXPECT_TRUE(stan::math::is_nan(dx1.d_));
  25.    
  26.     dx1= distance(x1c,x1);
  27.     EXPECT_FLOAT_EQ(0,dx1.val_);
  28.     EXPECT_TRUE(stan::math::is_nan(dx1.d_));
  29.    
  30.    
  31. }
  32.  
  33. TEST(Fwd, distance_nan) {
  34.   using stan::math::fvar;
  35.   using stan::math::distance;
  36.   double x1 = 1;
  37.   double nan = std::numeric_limits<double>::quiet_NaN();
  38.  
  39.   fvar<double> x2(nan,1.0);
  40.  
  41.   EXPECT_THROW(distance(x1, x2), std::domain_error);
  42.   EXPECT_THROW(distance(x2, x1), std::domain_error);
  43.   EXPECT_THROW(distance(x2, x2), std::domain_error);
  44.  
  45. }
  46.  
  47. TEST(Fwd, distance_inf) {
  48.   using stan::math::fvar;
  49.   using stan::math::distance;
  50.   double x1 = 1;
  51.   double inf = std::numeric_limits<double>::infinity();
  52.  
  53.   fvar<double> x2(inf,1.0);
  54.  
  55.   EXPECT_THROW(distance(x1, x2), std::domain_error);
  56.   EXPECT_THROW(distance(x2, x1), std::domain_error);
  57.   EXPECT_THROW(distance(x2, x2), std::domain_error);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement