#!/bin/env perl

# This is a standalone testing executable for PS::MOPS::Lib.

use strict;

use Benchmark ':hireswallclock';

# Define variables for benchmarking.
our $g_start;
our $g_end;
our $g_diff;

# Testing environment.
use Test::More tests=>10; # Perl testing framework.
use PS::MOPS::Lib;

# Test definition 1.
# convert JD to MJD
# Expected result: mjd value
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_jd2mjd
{
    my $success=0;
    my $a=2454686.50000; # Aug 8 2008 CE
    my $result=mopslib_jd2mjd($a);
    if($result==54686){
        $success=1;
    }
    else {
        $success=0;
    }
    return $success;
}

# Test definition 2.
# convert MJD to JD
# Expected result: jd value
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_mjd2jd
{
    my $success=0;
    my $a=54686; # Aug 8 2008 CE
    my $result=mopslib_mjd2jd($a);
    if($result==2454686.5){ # any problems comparing fp value here?
        $success=1;
    }
    else {
        $success=0;
    }   
    return $success;
}

# Test definition 3.
# retrieve a random variable from a Gaussian distribution
# Expected result: random variable
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_rndGaussian
{
    my $success=0;
    my $mean=0.125;
    my $sigma=0.0;
    my $result=mopslib_rndGaussian($mean,$sigma);

    # test deterministic case
    if($result==0.125){ 
        $success=1;
    }
    else {
        $success=0;
    }       
    return $success;
}

# Test definition 4.
# normalize values of ra and dec
# Expected result: array containing ra and dec
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_normalizeRADEC
{
    my $success=0;
    my $ra=370;
    my $dec=-10;
    my ($r1,$r2)=mopslib_normalizeRADEC($ra,$dec);
    if($r1==10 && $r2==-10){ 
        $success=1;
    }
    else {
        $success=0;
    }          
    return $success;
}

# Test definition 5.
# compute avg and stdev of an array of numbers
# Expected result: array containing avg and stdev
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_stdev
{
    my $success=0;
    my @list_of_numbers=(0.2,1.3,2.4,3.1,4.2,5.8,6.65,7.21,8.3);
    my ($avg,$stdev)=mopslib_stdev(@list_of_numbers);
    if(between($avg,4.35110,4.35112) &&
        between($stdev,2.64756,2.64758)) { $success=1; }
    else { $success=0; }
    return $success;
}

# Test definition 6.
# calculate a histogram for data
# Expected result: a hash containing min, max, hist, bins, binsize, and numpts
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_histogram
{
    my $success=0;
    my @data=qw(1 2 3 4 5);
    my $bins=3;
    my $hmax=5;
    my $result=mopslib_histogram(data=>\@data,bins=>$bins,hmax=>$hmax);
    if($result->{hist}->[0]==1 &&
       $result->{hist}->[1]==2 &&
       $result->{hist}->[2]==2){$success=1;}
    else {$success=0;}
    return $success;
}

# Test definition 7.
# determine if a point falls within a square field
# Expected result: 1 if point is in field
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_inSquareField
{
    my $success=0;
    my $ra=0;
    my $dec=0;
    my $fov=1;
    my $pt_ra=0;
    my $pt_dec=0;
    my $result=mopslib_inSquareField($ra,$dec,$fov,$pt_ra,$pt_dec);
    if($result){$success=1;}
    else{$success=0;}
    return $success;
}

# Test definition 8.
# determine if a point falls within a circular field, uses slaDsep()
# Expected result: boolean value
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_inField
{
    my $success=0;
    my $ra=0;
    my $dec=0;
    my $fov=1;
    my $pt_ra=0;
    my $pt_dec=0;
    my $result=mopslib_inField($ra,$dec,$fov,$pt_ra,$pt_dec);
    if($result){$success=1;}
    else{$success=0;}
    return $success;
}

# Test definition 9.
# convert an integer to base 62
# Expected result: base 62 value
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_toB62
{
    my $success=0;
    my $num=1001;
    my $template="00000000";
    my $result=mopslib_toB62($num,$template);
    if($result eq "000000g9"){$success=1;}
    else{$success=0};
    return $success;
}

# Test definition 10.
# convert a jd to a mops oc number
# Expected result: oc number
#
# @return {1|0} Success is 1, failure is 0.
sub test_mopslib_jd2ocnum
{
    my $success=0;
    my $jd=2454686.50000;
    my $result=mopslib_jd2ocnum($jd);
    if($result==105){$success=1;}
    return $success;
}

sub between {
    my($v,$x1,$x2)=@_;
    my $diff=abs($x1-$x2);
    my $half=$diff/2;
    
    if($v<($v+$half) && $v>($v-$half)){
        return 1;
    }
    return 0;
}

# Test 1: test_mopslib_jd2mjd.
$g_start=new Benchmark;
ok(test_mopslib_jd2mjd, "PS-MOPS-Lib_1->test_mopslib_jd2mjd");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_jd2mjd: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 2: test_mopslib_mjd2jd.
$g_start=new Benchmark;
ok(test_mopslib_mjd2jd, "PS-MOPS-Lib_1->test_mopslib_mjd2jd");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_mjd2jd: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 3: test_mopslib_rndGaussian.
$g_start=new Benchmark;
ok(test_mopslib_rndGaussian, "PS-MOPS-Lib_1->test_mopslib_rndGaussian");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_rndGaussian: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 4: test_mopslib_normalizeRADEC.
$g_start=new Benchmark;
ok(test_mopslib_normalizeRADEC, "PS-MOPS-Lib_1->test_mopslib_normalizeRADEC");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_normalizeRADEC: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 5: test_mopslib_stdev.
$g_start=new Benchmark;
ok(test_mopslib_stdev, "PS-MOPS-Lib_1->test_mopslib_stdev");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_stdev: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 6: test_mopslib_histogram.
$g_start=new Benchmark;
ok(test_mopslib_histogram, "PS-MOPS-Lib_1->test_mopslib_histogram");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_histogram: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 7: test_mopslib_inSquareField.
$g_start=new Benchmark;
ok(test_mopslib_inSquareField, "PS-MOPS-Lib_1->test_mopslib_inSquareField");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_inSquareField: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 8: test_mopslib_inField.
$g_start=new Benchmark;
ok(test_mopslib_inField, "PS-MOPS-Lib_1->test_mopslib_inField");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_inField: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 9: test_mopslib_toB62.
$g_start=new Benchmark;
ok(test_mopslib_toB62, "PS-MOPS-Lib_1->test_mopslib_toB62");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_toB62: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 10: test_mopslib_jd2ocnum.
$g_start=new Benchmark;
ok(test_mopslib_jd2ocnum, "PS-MOPS-Lib_1->test_mopslib_jd2ocnum");
$g_end=new Benchmark;
print "time: PS-MOPS-Lib_1->test_mopslib_jd2ocnum: " . timestr(timediff($g_end,$g_start),'all') . "\n";
