Created
October 16, 2012 22:23
-
-
Save agudulin/3902440 to your computer and use it in GitHub Desktop.
Perl: Homework TEST SYSTEM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Switch; | |
use strict; | |
use warnings; | |
if ($#ARGV eq -1) { | |
print "Error! No script to test.\n"; | |
print "Usage:\n check_hw.pl [script_to_test.pl]\n"; | |
exit(0); | |
} | |
sub compare_dates { | |
my $expected = $_[0]; | |
my $real = $_[1]; | |
# everything is ok if strings equals | |
if ($expected eq $real) { | |
return 0; | |
} | |
# check input string to get into the pattern %S:%M:%H %m.%d.%Y | |
if ($real =~ m/^([0-5]\d:){2}([0-1]\d|2[0-3])\ (\d\d.){2}\d{4}/) { | |
my($ss, $mm, $hh) = $expected =~ /(\d{2}):(\d{2}):(\d{2}).*/; | |
my $timestamp_e = $ss + $mm * 60 + $hh * 3600; | |
($ss, $mm, $hh) = $real =~ /(\d{2}):(\d{2}):(\d{2}).*/; | |
my $timestamp_r = $ss + $mm * 60 + $hh * 3600; | |
# print $timestamp_e, ':', $timestamp_r, "\n"; | |
# set maximum difference eq 2 sec | |
if (abs($timestamp_r - $timestamp_e) < 2) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
# Remove all whitespaces from begin and end of string | |
sub remove_whitespaces { | |
my $str = $_[0]; | |
$str =~ s/^\s*(.*)\s*$/$1/; | |
return "$str"; | |
} | |
sub test { | |
my $expected = $_[0]; | |
my $real = $_[1]; | |
chomp($expected, $real); | |
print "expected: $expected, real: $real"; | |
print " ... ", ($expected eq $real) ? "OK\n" : "FAIL\n"; | |
} | |
my @args = ("name", "time", "nfile", "ndirs", "other"); | |
for my $to_test (@ARGV) { | |
print ">> $to_test\n"; | |
for my $arg (@args) { | |
switch ($arg) { | |
case "name" { | |
test(`whoami`, `./$to_test $arg`); | |
} | |
case "time" { | |
my $expected = `date +"%S:%M:%H %m.%d.%Y"`; | |
# `sleep 2`; | |
my $real = `./$to_test $arg`; | |
my $res = compare_dates($expected, $real); | |
chomp($expected, $real); | |
print "expected: $expected, real: $real"; | |
print " ... ", ($res == 0) ? "OK\n" : "FAIL\n"; | |
} | |
case "nfile" { | |
test(remove_whitespaces(`ls | wc -l`), | |
`./$to_test $arg`); | |
} | |
case "ndirs" { | |
test(remove_whitespaces(`ls -d */ | wc -l`), | |
`./$to_test $arg`); | |
} | |
else { | |
my $real = `./$to_test $arg`; | |
chomp($real); | |
print "expected: {Anything}, real: $real"; | |
print " ... ", (length($real) > 0) ? "OK\n" : "FAIL\n"; | |
} | |
} | |
} | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment