// testSwap.cc P. Conrad for CISC181
// a testing driver for swap functions


#include <iostream>
using namespace std;

// A Template is like a "function making factory
//   We tell it what kind of function we want, and it makes it for us.
//   How do we tell it?  Just by using the function (i.e. in a function call)

template <class T>
void swap  (T &a, T &b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}


// 5 extra credit points for Tim Strab for insightful observation




bool approxEquals(double a, 
		  double b,
		  double tolerance = 1E-5)
{
  // fabs is "floating point absolute value
  // we want the abs value of the difference

  return ( fabs(a-b) < tolerance  );
}

// A longer version:
//
//  double difference = fabs(a-b);
//  if (difference < tolerance )
//    return true;
//  else
//    return false;



int main(void)
{
  int x=5; 
  int y=6;

  swap(x,y);

  if (x==6 && y==5)
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;

  double w=5.1;   
  double z=6.2;

  swap(w,z);

  // THIS WON'T WORK: if (w==6.2 && z==5.1)

  if (approxEquals(w,6.2) && approxEquals(z,5.1))
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;

  char a='?'; 
  char b='!';

  swap(a,b);

  if (a=='!' && b=='?')
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;

  bool foo = false; 
  bool bar = true;

  swap (foo, bar);

  if (foo==true && bar == false)
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;
  
  swap (first, last);

  //  if (first=="Conrad" && last == "Phill")  // NO GOOD

  if (first=="Conrad" && last == "Phill")  // NO GOOD
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;
  
  return 0;

}



