Commit e6fb02c5 authored by paarongiroux's avatar paarongiroux Committed by Kelvin Rodriguez
Browse files

Added LineEqationTests.cpp (#3152)

parent ce26475f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ print.prt
*/tsts/*/output/*
*/build/
build/
*/install/
install/

# Created by https://www.gitignore.io/api/macos
# Edit at https://www.gitignore.io/?templates=macos
+2 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ namespace Isis {
    }
    else if(!p_slopeDefined) {
      p_slope = (p_y[0] - p_y[1]) / (p_x[0] - p_x[1]);
      p_slopeDefined = true;
    }
    return p_slope;
  }
@@ -110,6 +111,7 @@ namespace Isis {
    }
    else if(!p_interceptDefined) {
      p_intercept = p_y[0] - Slope() * p_x[0];
      p_interceptDefined = true;
    }

    return p_intercept;
+0 −7
Original line number Diff line number Diff line
Unit test for LineEquation
Testing first constructor...
     Slope = 2.5
     Intercept = -1.5
Testing second constructor...
     Slope = -0.5
     Intercept = 0.5
+0 −30
Original line number Diff line number Diff line
#include <iostream>
#include <iomanip>
#include "LineEquation.h"
#include "FileName.h"
#include "Preference.h"
#include "Table.h"



using namespace std;

int main(int argc, char *argv[]) {
  Isis::Preference::Preferences(true);

  cout << setprecision(8);
  cout << "Unit test for LineEquation" << endl;

  cout << "Testing first constructor..." << endl;
  Isis::LineEquation line1;
  line1.AddPoint(1., 1.);
  line1.AddPoint(3., 6.);
  cout << "     Slope = " << line1.Slope() << endl;
  cout << "     Intercept = " << line1.Intercept() << endl;

  cout << "Testing second constructor..." << endl;
  Isis::LineEquation line2(-1., 1., -3., 2.);
  cout << "     Slope = " << line2.Slope() << endl;
  cout << "     Intercept = " << line2.Intercept() << endl;

}
+173 −0
Original line number Diff line number Diff line
#include <gtest/gtest.h>
#include "LineEquation.h"
#include "IException.h"
#include "TestUtilities.h"

TEST(LineEquation, DefaultConstructor)
{
  Isis::LineEquation testEquation;

  EXPECT_FALSE(testEquation.Defined());
  EXPECT_FALSE(testEquation.HaveSlope());
  EXPECT_FALSE(testEquation.HaveIntercept());
  EXPECT_EQ(testEquation.Points(), 0);
}

TEST(LineEquation, InitConstructor)
{
  Isis::LineEquation testEquation(1.0, 2.0, 3.0, 4.0);

  EXPECT_TRUE(testEquation.Defined());
  EXPECT_TRUE(testEquation.HaveSlope());
  EXPECT_TRUE(testEquation.HaveIntercept());

  EXPECT_DOUBLE_EQ(testEquation.Slope(), 1.0);
  EXPECT_DOUBLE_EQ(testEquation.Intercept(), 1.0);
  EXPECT_EQ(testEquation.Points(), 2);
}

TEST(LineEquation, AddingPoints)
{
  Isis::LineEquation testEquation;

  testEquation.AddPoint(1.0,2.0);

  EXPECT_EQ(testEquation.Points(), 1);
  EXPECT_FALSE(testEquation.HaveSlope());
  EXPECT_FALSE(testEquation.HaveIntercept());
  EXPECT_FALSE(testEquation.Defined());

  testEquation.AddPoint(3.0,4.0);

  EXPECT_EQ(testEquation.Points(), 2);
  EXPECT_FALSE(testEquation.HaveSlope());
  EXPECT_FALSE(testEquation.HaveIntercept());
  EXPECT_TRUE(testEquation.Defined());
  EXPECT_DOUBLE_EQ(testEquation.Slope(), 1.0);
  EXPECT_DOUBLE_EQ(testEquation.Intercept(), 1.0);
  EXPECT_TRUE(testEquation.HaveSlope());
  EXPECT_TRUE(testEquation.HaveIntercept());

  try
  {
    testEquation.AddPoint(5.0, 6.0);
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Line equation is already defined"))
      << e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
      "\"Line equation is already defined with 2 points\"";
  }

  EXPECT_EQ(testEquation.Points(), 2);
  EXPECT_TRUE(testEquation.HaveSlope());
  EXPECT_TRUE(testEquation.HaveIntercept());
  EXPECT_TRUE(testEquation.Defined());
  EXPECT_DOUBLE_EQ(testEquation.Slope(), 1.0);
  EXPECT_DOUBLE_EQ(testEquation.Intercept(), 1.0);
}

TEST(LineEquation, UndefinedSlope)
{
  Isis::LineEquation testEquation;

  try
  {
    testEquation.Slope();
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Line equation undefined"))
      <<e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
      "\"Line equation undefined:  2 points are required\"";
  }
}

TEST(LineEquation, UndefinedIntercept)
{
  Isis::LineEquation testEquation;

  try
  {
    testEquation.Intercept();
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Line equation undefined"))
      <<e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
     " \"Line equation undefined:  2 points are required\"";
  }
}

TEST(LineEquation, AddSamePoints)
{
  Isis::LineEquation testEquation;
  testEquation.AddPoint(1.0,1.0);
  testEquation.AddPoint(1.0,1.0);

  try
  {
    testEquation.Intercept();
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Points have identical"))
      <<e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
      "\"Points have identical independent variables -- no intercept\"";
  }

  try
  {
    testEquation.Slope();
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Points have identical"))
      <<e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
      "\"Points have identical independent variables -- no slope\"";
  }
}

TEST(LineEquation, InitSamePoints)
{
  try
  {
    Isis::LineEquation testEquation(1.0,1.0,1.0,1.0);
    FAIL() << "Expected an exception to be thrown";
  }
  catch(Isis::IException &e)
  {
    EXPECT_TRUE(e.toString().toLatin1().contains("Points have identical"))
      <<e.toString().toStdString();
  }
  catch(...)
  {
    FAIL() << "Expected an IException with message:"
      "\"Points have identical independent variables -- no intercept\"";
  }
}