#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 2015, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
##
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
##
##    http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorUtils;
use CondorTest;
use Cwd;
use POSIX;
use Check::SimpleJob;

my $testname = "job_basic_submit-queuespliceditemin-commalist";

TLOG "Starting $testname...\n";

sub runSliceTest {
    my $testname = shift;
    my $submitfile = shift;
    my $extraction = shift;

    my @results = ();
    my $mypid = $$;
    my $submitfilename = "$testname$mypid.cmd";
    my $return;

    open(SF,">$submitfilename") or die "failed to open submit file:$submitfilename:$!\n";
    print SF "$submitfile";
    close(SF);

    my $datafilename = CondorTest::TempFileName("$testname");
    my $cmdtorun = "condor_submit -dry-run - $submitfilename";
    runCondorTool($cmdtorun, \@results, \$return, {emit_output=>1, expect_result=>1});

    DryExtract(\@results, $extraction, "Args");
    return($return);
}

sub checkOutputAndRegisterResult {
    my $extraction = shift;
    my $allargs = shift;
    my $newarraysize = shift;

    $extractionsize = @{$extraction};

    my %seenargs = ();

    foreach my $arg (@{$extraction}){
        if($arg =~ /Args=\"(.*?)\"/) {
            $seenargs{$1} = 1;
        }
    }

    my $testfailed = 0;
    foreach my $findit (@{$allargs}) {
        if(not exists $seenargs{$findit}) {
            TLOG "Expected $findit and DID NOT GET it - Bad\n";
            $testfailed = 1;
        }
    }

    my $seenargsize = keys %seenargs;

    if ($newarraysize != $seenargsize) {
        TLOG "Expected no. of args $newarraysize, received args $seenargsize - Bad\n";
        $testfailed = 1;
    }

    if( $return != 0 ) {
        $testfailed = 1;
    }

    if( $testfailed == 1) {
        RegisterResult(0,"test_name","$testname");
    } else {
        RegisterResult(1,"test_name","$testname");
    }
}

sub generateExpectedOutput {
    $expectedoutputref = shift;
    $dataref = shift;
    $start = shift;
    $step = shift;
    $end = shift;

    @data = @{$dataref};

    if (not defined $start) {
        $start = 0;
    }

    if (not defined $step) {
        $step = 1;
    }

    if (not defined $end or $end > @data) {
        $end = @data;
    }

    if ($start < 0) {
        $start = @data + $start;
    }

    if ($end < 0) {
        $end = @data + $end;
    }

    for (my $i = $start; $i < $end; $i += $step) {
        push @{$expectedoutputref}, $data[$i];
    }

}

sub main {
    my @testcases = (
        {
            data => [1 .. 10],
            start => 0,
            step => 1,
            end => 10,
        },
        {
            data => [1 .. 10],
            start => 0,
            end => 10,
        },
        {
            data => [1 .. 10],
            start => 0,
        },
        {
            data => [1 .. 10],
            end => 10,
        },
        {
            data => [1 .. 10],
            step => 2,
        },
        {
            data => [1 .. 10],
        },
        {
            data => [1 .. 100],
            start => 17,
            step => 9,
            end => 93,
        },
        {
            data => [1 .. 100],
            start => 0,
            step => 1,
            end => 100,
        },
        {
            data => [100000 .. 110000],
            start => 9000,
            step => 1000,
            end => 110000,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => 0,
            step => 1,
            end => 4,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => 1,
            step => 2,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => 1,
            step => 2,
            end => 1,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => -1,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => 1,
            end => -1,
        },
        {
            data => ["This", "is", "a", "Test"],
            end => 100,
        },
        {
            data => ["This", "is", "a", "Test"],
            start => 5,
        },
    );

    foreach my $test (@testcases) {
        my @extraction = ();
        my @newarray = ();
        my $blehash = {};
        my $arrayref = $test->{data};
        my @array = @{$arrayref};
        my $start = $test->{start};
        my $end = $test->{end};
        my $step = $test->{step};

        generateExpectedOutput(\@newarray, \@array, $start, $step, $end);

        my $arglist = join(' ', @array);
        my $newarglist = join(' ', @newarray);
        my @allargs = split(' ', $newarglist);

        my $submitfile = "
            executable = x_print-args.pl
            should_transfer_files = yes
            when_to_transfer_output = ON_EXIT
            log = $testname$mypid.log
            Arguments = \$(ITEM)
            queue ITEM in [$start:$end:$step] (
                $arglist
            )
        ";

        my $return = runSliceTest($testname, $submitfile, \@extraction);
        checkOutputAndRegisterResult(\@extraction, \@allargs, scalar @newarray);
    }
}

main();

CondorTest::EndTest();
CondorTest::debug("$testname SUCCESS\n",1);

exit(0);
