# CmpDay.t compare weekday names, find name order 24jul22waw use strict; use warnings; use Test::More; use Test::NoWarnings; use List::Util qw(shuffle sum); use Data::Dump qw(dd pp); note "\n=== testing under perl version $] ===\n\n"; use lib '.'; BEGIN { use_ok 'CmpDay'; } # test name to wday number conversion. my @Test_wday_to_n = ( 'invalid weekday names (no wday numbers)', [ undef, '', qw(x xyz xsun sunx xsunx mondaytuesday mondayxtuesday sunsun) ], 'valid weekday names -> wday numbers', [ 0, qw(su sun sunday sU Su SU sUnDaY) ], [ 3, qw(we wed wednesday wE We WE WednEsDaY) ], [ 6, qw(sa sat saturday sA Sa SA sAtUrDaY) ], ); # end @Test_wday_to_n # test wday number to name conversion. my @Test_n_to_wday = ( 'invalid weekday numbers (no corresponding wday name)', [ 7, undef ], [ -8, undef ], 'valid weekday numbers -> wday names', [ 0, 'sunday' ], [ -7, 'sunday' ], [ 3, 'wednesday' ], [ -4, 'wednesday' ], [ 6, 'saturday' ], [ -1, 'saturday' ], ); # end @Test_n_to_wday my @additional = qw(use_ok Test::NoWarnings @unsorted_days); plan 'tests' => (sum map $#$_, grep { ref eq 'ARRAY' } @Test_wday_to_n) + (sum map $#$_, grep { ref eq 'ARRAY' } @Test_n_to_wday) + @additional ; VECTOR: for my $ar_vector (@Test_wday_to_n) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($expected, @days) = @$ar_vector; for my $day (@days) { my $wday = CmpDay::day_to_i($day); is $wday, $expected, "'$day' -> " . pp $wday; } } # end for VECTOR VECTOR: for my $ar_vector (@Test_n_to_wday) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($wday_n, $expected_day) = @$ar_vector; my $got_day = CmpDay::i_to_day($wday_n); is $got_day, $expected_day, "$wday_n -> " . pp $expected_day; } # end for VECTOR note "\ntest sorting of mixed short/long day names\n\n"; # define standard and variant short/long weekday names for testing. my @days = qw( mo mon monday tu tue tues tuesday we wed wednesday th thu thur thurs thursday fr fri friday sa sat saturday su sun sunday ); # prepare randomized weekday list with uc/lc variations for sort testing. my @unsorted_days = shuffle @days, map(ucfirst, @days), map(uc, @days), qw(mO mOn tU TuE tUeS wEdNeSdAy tH tHuR ThUr ThurS tHuRs sA sU suN), ; # # works # # sort by day-name order (ascending) then lexicographic (ascending) by day-name. # my @sorted_days = # map $_->[0], # sort { # $a->[1] <=> $b->[1] # sort by day order ascending # or # $a->[0] cmp $b->[0] # then lexicographic ascending # } # map [ $_, defined CmpDay::day_to_i($_) ? CmpDay::day_to_i($_) : die "bad day '$_'" ], # @unsorted_days # ; # works # sort by day-name order (ascending) then lexicographic (ascending) by day-name. my @sorted_days = map unpack('x[C] a*', $_), sort map pack('C a*', CmpDay::day_to_i($_), $_), map { defined CmpDay::day_to_i($_) ? $_ : die "bad day '$_'" } @unsorted_days ; # dd \@sorted_days; # for debug is_deeply \@sorted_days, [ qw( SU SUN SUNDAY Su Sun Sunday sU su suN sun sunday MO MON MONDAY Mo Mon Monday mO mOn mo mon monday TU TUE TUES TUESDAY Tu TuE Tue Tues Tuesday tU tUeS tu tue tues tuesday WE WED WEDNESDAY We Wed Wednesday wEdNeSdAy we wed wednesday TH THU THUR THURS THURSDAY Th ThUr Thu Thur ThurS Thurs Thursday tH tHuR tHuRs th thu thur thurs thursday FR FRI FRIDAY Fr Fri Friday fr fri friday SA SAT SATURDAY Sa Sat Saturday sA sa sat saturday )], 'shuffled day names'; note "\n=== testing done ===\n\n";