
package TestFormat;
use strict;

BEGIN {
  open SAVE_STDOUT, ">&STDOUT" or die $!;
  open STDOUT, ">", "/dev/null" or die $!;
  open STDIN, "<", "/dev/null" or die $!;
  open 'main::DATA', "<", "/dev/null" or die $!;
}

END {
  my $N = 1;
  my $TEST_THESE = $ENV{CLASS} || "Basic";
  my $DIE_OK = $ENV{DIE_OK};
  local $/ = "";
  open STDOUT, ">&SAVE_STDOUT" or die $!;
TEST:
  while (<DATA>) {
    chomp;
    my ($test_classes, $in, @out) = split /\n+/;
    if ($TEST_THESE ne "ALL" && $test_classes !~ /Basic/) {
      for (split /\s+/, $test_classes) {
        unless (index($TEST_THESE, $_) >= 0) {
          print "ok $N # skipped ($_)\n";
          next TEST;
        }
      }
    }

    my @in = split /,?\s+/, $in;
    my $s = eval { main::format_number_list(@in) };
    if ($@) {
      $@ =~ s/\n+$//;
      if ($DIE_OK) {
        print "ok $N # died ($@)\n";
      } else {
        print "not ok $N # died ($@)\n";
      }
      next;
    }

    for (@out) {
      if ($s eq $_) {
        print "ok $N\n";
        next TEST;
      }
    }
    print "not ok $N # '$s'\n";
  } continue {
    $N++;
  }
}

"Cogito, ergo sum.";
__DATA__
Basic
1
1

Basic
1 3
1, 3

Basic
1 2
1-2
1, 2

Negative
-1
-1

Repeated
1 1 2 3 5 8 13
1-3, 5, 8, 13
1, 1-3, 5, 8, 13

Disordered
3 2 1
1-3

Basic
1, 2, 4, 5, 6, 7, 9, 13, 24, 25, 26, 27
1-2, 4-7, 9, 13, 24-27
1, 2, 4-7, 9, 13, 24-27

Basic
1 2 4 5 6 7 9 13 24 25 26 27
1-2, 4-7, 9, 13, 24-27
1, 2, 4-7, 9, 13, 24-27

Basic
1 17 18 99
1, 17-18, 99
1, 17, 18, 99

Disordered
1, 2, 4, 6, 5, 9, 7, 24, 13, 26, 25, 27
1-2, 4-7, 9, 13, 24-27
1, 2, 4-7, 9, 13, 24-27

Disordered
27 26 25 24 13 9 7 1 2 4 6 5
27, 26, 25, 24, 13, 9, 7, 1-2, 4, 6, 5
1-2, 4-7, 9, 13, 24-27

Negative
-1 0 1 2 13 17 28
-1-2, 13, 17, 28

Repeated
0 1 1 2 3 9
0-3, 9
0-1, 1-3, 9

Disordered Repeated
0 1 1 2 3 9 8 7 6 9
0-3, 6-9
0-1, 1-3, 6-9, 9

Negative
-4 -3 -1 0
-4--3, -1-0

Disordered Negative Repeated
-50, -10, -4, -3, -2, 0, 2, 3, 3, 3, 4, 5, 6, 7, 9, 13, 24, 25, 26, 27, 50, 49, 48, 60
-50, -10, -4--2, 0, 2-7, 9, 13, 24-27, 48-60
-50, -10, -4--2, 0, 2-3, 3, 4-7, 9, 13, 24-27, 48-60

Disordered
10 9 8 7 6 5 4 3 2 1
1-10

Repeated
1 2 4 5 5 6 7 13 13 24 24 25 26 27
1-2, 4-7, 13, 24-27
1, 2, 4-7, 13, 24-27
1-2, 4-5, 5-7, 13, 13, 24, 24-27
1, 2, 4, 5, 5-7, 13, 13, 24, 24-27

Basic
1 2 3 6 10 11 12 13 14 18 90 91 92 100
1-3, 6, 10-14, 18, 90-92, 100
