Commit 65e38aa9 authored by Adam Goins's avatar Adam Goins
Browse files

PROG: Improved test coverage and coding standards as a training exercise.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@7894 41f8697f-d340-4b68-9986-7bafba869bb8
parent 1df3f54a
Loading
Loading
Loading
Loading
+70 −64
Original line number Diff line number Diff line
@@ -3,11 +3,11 @@
#include <cmath>
#include <float.h>

#include <QRegularExpression>

#include "IException.h"
#include "PvlContainer.h"
#include "Pvl.h"
#include "IException.h"
#include "QRegularExpression"


using namespace std;
using namespace Isis;
@@ -15,19 +15,19 @@ using namespace Isis;
bool filesMatch;
QString differenceReason;
PvlGroup tolerances;
PvlGroup ignorekeys;
PvlGroup ignorefilepaths;
PvlGroup ignoreKeys;
PvlGroup ignoreFilePaths;

void CompareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2);
void CompareObjects(const PvlObject &pvl1, const PvlObject &pvl2);
void CompareGroups(const PvlGroup &pvl1, const PvlGroup &pvl2);
void compareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2);
void compareObjects(const PvlObject &pvl1, const PvlObject &pvl2);
void compareGroups(const PvlGroup &pvl1, const PvlGroup &pvl2);

void IsisMain() {
  UserInterface &ui = Application::GetUserInterface();

  tolerances = PvlGroup();
  ignorekeys = PvlGroup();
  ignorefilepaths = PvlGroup();
  ignoreKeys = PvlGroup();
  ignoreFilePaths = PvlGroup();
  differenceReason = "";
  filesMatch = true;

@@ -42,15 +42,15 @@ void IsisMain() {
    }

    if (diffFile.hasGroup("IgnoreKeys") ) {
      ignorekeys = diffFile.findGroup("IgnoreKeys");
      ignoreKeys = diffFile.findGroup("IgnoreKeys");
    }

    if (diffFile.hasGroup("IgnoreFilePaths") ) {
      ignorefilepaths = diffFile.findGroup("IgnoreFilePaths");
      ignoreFilePaths = diffFile.findGroup("IgnoreFilePaths");
    }
  }

  CompareObjects(file1, file2);
  compareObjects(file1, file2);

  PvlGroup differences("Results");
  if (filesMatch) {
@@ -72,7 +72,7 @@ void IsisMain() {
  differenceReason = "";
}

void CompareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2) {
void compareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2) {
  if ( pvl1.name().compare(pvl2.name()) != 0 ) {
    filesMatch = false;
    differenceReason = "Keyword '" + pvl1.name() + "' does not match keyword '" + pvl2.name() + "'";
@@ -89,25 +89,31 @@ void CompareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2) {
  if ( tolerances.hasKeyword(pvl1.name() ) &&
       tolerances[pvl1.name()].size() > 1 &&
       pvl1.size() != tolerances[pvl1.name()].size() ) {

    QString msg = "Size of keyword '" + pvl1.name() + "' does not match with ";
    msg += "its number of tolerances in the DIFF file.";
    throw IException(IException::User, msg, _FILEINFO_);

  }

  if(ignorekeys.hasKeyword(pvl1.name()) &&
      ignorekeys[pvl1.name()].size() > 1 &&
      pvl1.size() != ignorekeys[pvl1.name()].size()) {
  if ( ignoreKeys.hasKeyword(pvl1.name()) &&
       ignoreKeys[pvl1.name()].size() > 1 &&
       pvl1.size() != ignoreKeys[pvl1.name()].size() ) {

    QString msg = "Size of keyword '" + pvl1.name() + "' does not match with ";
    msg += "its number of ignore keys in the DIFF file.";
    throw IException(IException::User, msg, _FILEINFO_);

  }

  if (ignorefilepaths.hasKeyword(pvl1.name()) &&
      ignorefilepaths[pvl1.name()].size() > 1 &&
      pvl1.size() != ignorefilepaths[pvl1.name()].size()) {
  if ( ignoreFilePaths.hasKeyword(pvl1.name()) &&
       ignoreFilePaths[pvl1.name()].size() > 1 &&
       pvl1.size() != ignoreFilePaths[pvl1.name()].size() ) {

    QString msg = "Size of keyword '" + pvl1.name() + "' does not match with ";
    msg += "its number of filepath ignores in the DIFF file.";
    throw IException(IException::User, msg, _FILEINFO_);

  }

  for (int i = 0; i < pvl1.size() && filesMatch; i++) {
@@ -118,23 +124,23 @@ void CompareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2) {
    QString unit2 = pvl2.unit(i);

    int ignoreIndex = 0;
    if(ignorekeys.hasKeyword(pvl1.name()) && ignorekeys[pvl1.name()].size() > 1) {
    if ( ignoreKeys.hasKeyword(pvl1.name()) && ignoreKeys[pvl1.name()].size() > 1 ) {
      ignoreIndex = i;
    }

    // If we only have one value for the keyword, use it for all paths in the keyword's array,
    // otherwise use the corresponding IgnoreFilePaths value
    if (ignorefilepaths.hasKeyword(pvl1.name()) &&
      (((ignorefilepaths[pvl1.name()].size() > 1 && ignorefilepaths[pvl1.name()][i] == "true") ||
        (ignorefilepaths[pvl1.name()].size() == 1 && ignorefilepaths[pvl1.name()][0] == "true")))) {
    if ( ignoreFilePaths.hasKeyword(pvl1.name()) &&
         (((ignoreFilePaths[pvl1.name()].size() > 1 && ignoreFilePaths[pvl1.name()][i] == "true") ||
         (ignoreFilePaths[pvl1.name()].size() == 1 && ignoreFilePaths[pvl1.name()][0] == "true"))) ) {
      val1 =  val1.replace(QRegularExpression("(\\/[\\w\\-\\. ]*)+\\/"), "");
      val2 =  val2.replace(QRegularExpression("(\\/[\\w\\-\\. ]*)+\\/"), "");
    }


    try {
      if(!ignorekeys.hasKeyword(pvl1.name()) ||
          ignorekeys[pvl1.name()][ignoreIndex] == "false") {
      if ( !ignoreKeys.hasKeyword(pvl1.name()) ||
          ignoreKeys[pvl1.name()][ignoreIndex] == "false" ) {

        if ( unit1.toLower() != unit2.toLower() ) {
          filesMatch = false;
@@ -175,7 +181,7 @@ void CompareKeywords(const PvlKeyword &pvl1, const PvlKeyword &pvl2) {
  }
}

void CompareObjects(const PvlObject &pvl1, const PvlObject &pvl2) {
void compareObjects(const PvlObject &pvl1, const PvlObject &pvl2) {
  if ( pvl1.name().compare(pvl2.name()) != 0 ) {
    filesMatch = false;
    differenceReason = "Object " + pvl1.name() + " does not match " +
@@ -202,15 +208,15 @@ void CompareObjects(const PvlObject &pvl1, const PvlObject &pvl2) {
  }

  for (int keyword = 0; keyword < pvl1.keywords() && filesMatch; keyword++) {
    CompareKeywords(pvl1[keyword], pvl2[keyword]);
    compareKeywords(pvl1[keyword], pvl2[keyword]);
  }

  for (int object = 0; object < pvl1.objects() && filesMatch; object++) {
    CompareObjects(pvl1.object(object), pvl2.object(object));
    compareObjects(pvl1.object(object), pvl2.object(object));
  }

  for (int group = 0; group < pvl1.groups() && filesMatch; group++) {
    CompareGroups(pvl1.group(group), pvl2.group(group));
    compareGroups(pvl1.group(group), pvl2.group(group));
  }

  if ( !filesMatch && pvl1.name().compare("Root") != 0 ) {
@@ -218,14 +224,14 @@ void CompareObjects(const PvlObject &pvl1, const PvlObject &pvl2) {
  }
}

void CompareGroups(const PvlGroup &pvl1, const PvlGroup &pvl2) {
void compareGroups(const PvlGroup &pvl1, const PvlGroup &pvl2) {
  if ( pvl1.keywords() != pvl2.keywords() ) {
    filesMatch = false;
    return;
  }

  for (int keyword = 0; keyword < pvl1.keywords() && filesMatch; keyword++) {
    CompareKeywords(pvl1[keyword], pvl2[keyword]);
    compareKeywords(pvl1[keyword], pvl2[keyword]);
  }

  if (!filesMatch) {
+10 −9
Original line number Diff line number Diff line
#include "Isis.h"

#include "ProcessByLine.h"
#include "SpecialPixel.h"

using namespace std;
using namespace Isis;

void do_ratio(vector<Buffer *> &in,
void doRatio(vector<Buffer*> &in,
           vector<Buffer*> &out);

void IsisMain() {
@@ -13,12 +14,12 @@ void IsisMain() {
  p.SetInputCube("NUMERATOR");
  p.SetInputCube("DENOMINATOR");
  p.SetOutputCube("TO");
  p.StartProcess(do_ratio);
  p.StartProcess(doRatio);
  p.EndProcess();
}

// Line processing routine
void do_ratio(vector<Buffer *> &in,
void doRatio(vector<Buffer*> &in,
           vector<Buffer*> &out) {
  Buffer &num = *in[0];
  Buffer &den = *in[1];
+29 −27
Original line number Diff line number Diff line
@@ -14,10 +14,10 @@ using namespace Isis;
void IsisMain() {

  UserInterface &ui = Application::GetUserInterface();
  Process p;
  Process process;

  // Get the histogram
  Cube *icube = p.SetInputCube("FROM");
  Cube *inputCube = process.SetInputCube("FROM");

  double validMin = Isis::ValidMinimum;
  double validMax = Isis::ValidMaximum;
@@ -31,18 +31,18 @@ void IsisMain() {
  }
  
  // Set a global Pvl for storing results
  Pvl mainpvl;
  Pvl mainPvl;
  
  // Get the number of bands to process
  int bandcount = icube->bandCount();
  int bandCount = inputCube->bandCount();
  
  for (int i = 1; i <= bandcount; i++) {
    Histogram *stats = icube->histogram(i, validMin, validMax);
  for (int i = 1; i <= bandCount; i++) {
    Histogram *stats = inputCube->histogram(i, validMin, validMax);

    // Construct a label with the results
    PvlGroup results("Results");  
    results += PvlKeyword("From", icube->fileName());
    results += PvlKeyword("Band", toString(icube->physicalBand(i)));
    results += PvlKeyword("From", inputCube->fileName());
    results += PvlKeyword("Band", toString(inputCube->physicalBand(i)));
    if ( stats->ValidPixels() != 0 ) {
      results += PvlKeyword("Average", toString(stats->Average()));
      results += PvlKeyword("StandardDeviation", toString(stats->StandardDeviation()));
@@ -65,9 +65,11 @@ void IsisMain() {
    results += PvlKeyword("HisPixels", toString(stats->HisPixels()));
    results += PvlKeyword("HrsPixels", toString(stats->HrsPixels()));
    
    mainpvl.addGroup(results);
    mainPvl.addGroup(results);
    
    delete stats;
    stats = NULL;

    // Write the results to the log
    Application::Log(results);
  }
@@ -82,10 +84,10 @@ void IsisMain() {
    //write the results in the requested format.
    if ( ui.GetString("FORMAT") == "PVL" ) {
      if (append) {
        mainpvl.append(outFile);
        mainPvl.append(outFile);
      }
      else {
        mainpvl.write(outFile);
        mainPvl.write(outFile);
      }
    }
    else {
@@ -102,19 +104,19 @@ void IsisMain() {
      }

      if (writeHeader) {
        for(int i = 0; i < mainpvl.group(0).keywords(); i++) {
          os << mainpvl.group(0)[i].name();
          if( i < mainpvl.group(0).keywords() - 1 ) {
        for (int i = 0; i < mainPvl.group(0).keywords(); i++) {
          os << mainPvl.group(0)[i].name();
          if ( i < mainPvl.group(0).keywords() - 1 ) {
            os << ",";
          }
        }
        os << endl;
      }
      
      for(int i = 0; i < mainpvl.groups(); i++) {
        for (int j = 0; j < mainpvl.group(i).keywords(); j++) {
          os << (QString)mainpvl.group(i)[j];
          if(j < mainpvl.group(i).keywords() - 1) {
      for (int i = 0; i < mainPvl.groups(); i++) {
        for (int j = 0; j < mainPvl.group(i).keywords(); j++) {
          os << (QString) mainPvl.group(i)[j];
          if ( j < mainPvl.group(i).keywords() - 1 ) {
            os << ",";
          }
        }
+14 −2
Original line number Diff line number Diff line
@@ -6,4 +6,16 @@ commands:
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	           to=$(OUTPUT)/stats.pvl > /dev/null;
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	  validmin=-20 validmax=20 to=$(OUTPUT)/stats2.pvl > /dev/null;
	           validmin=-20 \
	           validmax=20 \
	           to=$(OUTPUT)/stats2.pvl \
	           > /dev/null;
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	           to=$(OUTPUT)/stats3.pvl \
	           append=false \
	           > /dev/null;
	  
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	           | grep -v stats \
	           | grep -v Processed \
	           >& $(OUTPUT)/output.pvl;
 No newline at end of file
+14 −3
Original line number Diff line number Diff line
@@ -5,8 +5,19 @@ include $(ISISROOT)/make/isismake.tsts
commands:
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	to=$(OUTPUT)/tmp.txt format=flat > /dev/null;
	cat $(OUTPUT)/tmp.txt | \
	  sed 's/\/[^,]*isisTruth.cub/PATH/g' | \
	  sed 's/\([0-9]*\.[0-9]\{11\}\)[0-9]*/\1/g' \
	
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	to=$(OUTPUT)/tmp.txt format=flat append=true > /dev/null;
	$(CAT) $(OUTPUT)/tmp.txt | \
	  $(SED) 's+.*\/[^,]*isisTruth.cub+PATH+' | \
	  $(SED) 's/\([0-9]*\.[0-9]\{11\}\)[0-9]*/\1/g' \
	  > $(OUTPUT)/stats.txt;
	$(RM) $(OUTPUT)/tmp.txt;
	
	$(APPNAME) from=$(INPUT)/isisTruth.cub \
	 to=$(OUTPUT)/tmp2.txt format=flat append=false > /dev/null;
	$(CAT) $(OUTPUT)/tmp2.txt | \
	  $(SED) 's+.*\/[^,]*isisTruth.cub+PATH+' | \
	  $(SED) 's/\([0-9]*\.[0-9]\{11\}\)[0-9]*/\1/g' \
	  > $(OUTPUT)/stats2.txt;
	$(RM) $(OUTPUT)/tmp2.txt;