/*
 * Test program for the class ImageInformation
 *
 * $Date$
 * $Revision$
 *
 */

#ifdef NDEBUG
#undef NDEBUG
#endif /* NDEBUG */

#include "WTools/ImageInformation.hh"
#include "WImage/StillImage.hh"
#include <cassert>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>
#include <cmath>

#if !(defined __WATCOMC__ && __WATCOMC__ < 1230)
using namespace std;
#endif

int
main (void)
{
  cout << "Testing class ImageInformation...\n";

  CoeffInformation *x = 0;
  DELETE (x);
  
  Image *img1 = NEW (StillImage (4, 4));

  for (int i = 0; i < img1->size (); i++)
    {
      img1->to (i, (coeff)i);
    }
  
  assert (img1->size () == 16);

  ImageInformation *ii1 = NEW (ImageInformation (*img1));

  assert (fabs (ii1->sdeviation () - img1->sdeviation ()) < COEFF_EPSILON);

  assert (fabs (ii1->sqvariance () - img1->sqvariance ()) < COEFF_EPSILON);

  assert (ii1->ssorted ());
  assert (ii1->psorted ());
  ii1->shuffle ();
  ii1->dump ();
  ImageInformation *ii2 = ii1->clone ();
  ii2->dump ();
  assert (ii1->equals (*ii2));
  ImageInformation *ii3 = ii1->head (8);
  ii2->shrink (8, true);
  ImageInformation *ii4 = ii1->head (8);
  ii4->resize (ii1->size ());
  assert (ii4->size () == ii1->size ());
  for (int i = 0; i < ii3->size (); i++)
    {
      assert ((ii4->at (i)).equals (ii3->at (i)));
    }
  DELETE (ii4);

  ii2->dump ();
  ii3->dump ();
  assert (ii2->equals (*ii3));
  
  DELETE (ii2);
  DELETE (ii3);

  ii2 = ii1->clone ();
  ii3 = ii1->tail (12);
  ii2->shrink (12, false);
  
  ii2->dump ();
  ii3->dump ();
  assert (ii2->equals (*ii3));
  DELETE (ii2);
  DELETE (ii3);

  ii1->ssort ();
  ii1->dump ();
  assert (ii1->ssorted ());
  ii1->shuffle ();
  ii1->dump ();
  ii1->psort ();
  ii1->dump ();

  DELETE (ii1);

  for (int i = 0; i < img1->size (); i++)
    {
      if ((i * rand () / 3) % 2)
        {
          img1->to (i, -1*img1->at (i));
        }
      
    }

  ii1 = NEW (ImageInformation (*img1));
  ii1->shrink (9, true);
  ii1->dump ();
  ii1->shuffle ();
  ii1->dump ();
  ii1->asort ();
  ii1->dump ();
  assert (ii1->asorted ());
  ii2 = ii1->head (4);
  ii3 = ii1->tail (5);
  ii4 = ii2->append (*ii3);
  assert (ii1->equals (*ii4));

  //ii1->dump ("\n", "imageinfo.txt");

  DELETE (ii4);
  DELETE (ii3);
  DELETE (ii2);
  DELETE (ii1);
  DELETE (img1);

  cout << "Test of class ImageInformation completed successfully.\n";
  return 0;
}

