summaryrefslogtreecommitdiffstats
path: root/storage/maria/unittest/ma_test_all-t
blob: 8858649fb5d474b3018ff3b67e3002dfb7470580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#!/usr/bin/env perl
#
# Run various unit tests.
#

use Getopt::Long;
use File::Basename;

$|= 1;
$^W = 1; # warnings, because env cannot parse 'perl -w'
$VER= "1.5";

$opt_version=         0;
$opt_help=            0;
$opt_verbose=         0;
$opt_abort_on_error=  0;
$opt_valgrind=        "valgrind --alignment=8 --leak-check=yes";
$opt_silent=          "-s";
$opt_number_of_tests= 0;
$opt_run_tests=       undef();

my $maria_path;       # path to "storage/maria"
my $maria_exe_path;   # path to executables (ma_test1, aria_chk etc)
my $my_progname= $0;
$my_progname=~ s/.*[\/]//;
my $runtime_error= 0; # Return 1 if error(s) occur during run
my $NEW_TEST= 0;      # Test group separator in an array of tests
my $test_begin= 0;
my $test_end= 0;
my $test_counter= 0;
my $using_internal_tmpdir= 0;
my $full_tmpdir;
my $tmpdir="tmp";
my $exec_dir="TMP-ma_test_all"; # Run test in this directory
run_tests();

####
#### Initialise variables, clean temporary files and run the tests
####

sub run_tests
{
  my $nr_tests= 0;
  my $flag_exit= 0;

  if (!GetOptions("help" => \$opt_help,
                  "version" => \$opt_version,
                  "verbose" => \$opt_verbose,
                  "abort-on-error" => \$opt_abort_on_error,
                  "valgrind=s" => \$opt_valgrind,
                  "silent=s" => \$opt_silent,
                  "tmpdir=s" => \$full_tmpdir,
                  "number-of-tests" => \$opt_number_of_tests,
                  "run-tests=s" => \$opt_run_tests,
                  "start-from=s" => \$opt_run_tests))
  {
    $flag_exit= 1;
  }
  if ($opt_version)
  {
    print "$my_progname version $VER\n";
    exit(0);
  }

  if (! -d $exec_dir)
  {
    die if (!mkdir("$exec_dir"));
  }
  chdir($exec_dir);

  $maria_path= "../" . dirname($0) . "/..";

  my $suffix= ( $^O =~ /win/i  && $^O !~ /darwin/i ) ? ".exe" : "";
  $maria_exe_path= "$maria_path/release";
  # we use -f, sometimes -x is unexpectedly false in Cygwin
  if ( ! -f "$maria_exe_path/ma_test1$suffix" )
  {
    $maria_exe_path= "$maria_path/relwithdebinfo";
    if ( ! -f "$maria_exe_path/ma_test1$suffix" )
    {
      $maria_exe_path= "$maria_path/debug";
      if ( ! -f "$maria_exe_path/ma_test1$suffix" )
      {
        $maria_exe_path= $maria_path;
        if ( ! -f "$maria_exe_path/ma_test1$suffix" )
        {
           die("Cannot find ma_test1 executable in $maria_path\n");
        }
      }
    }
  }

  usage() if ($opt_help || $flag_exit);

  if (defined($full_tmpdir))
  {
    $tmpdir= $full_tmpdir;
  }
  else
  {
    $full_tmpdir= $tmpdir;
    $using_internal_tmpdir= 1;
    if (! -d "$full_tmpdir")
    {
      die if (!mkdir("$full_tmpdir"));
    }
  }

  #
  # IMPORTANT: If you modify this file, please read this:
  #
  # Count total number of tests. Make sure that the functions return
  # number of unit tests correctly, e.g. calls to ok(). The last argument
  # for each function is a flag counter and will return the number of
  # unit tests in each. Please see comments on function ok() at the end.
  #
  # If you modify any functions or add any new ones, please make sure the
  # unit tests are appropriately detected here. A wrong count will
  # make the unit test fail during 'make test'. $nr_tests must be right.
  #

  $nr_tests+= run_check_tests(0, 0, 0, 0, 1) * 5;  #
  $nr_tests+= run_repair_tests(0, 0, 0, 0, 1) * 5; # called 4 times
  $nr_tests+= run_pack_tests(0, 0, 0, 0, 1) * 5;   #
  $nr_tests+= run_tests_on_warnings_and_errors(0, 0, 0, 1);
  $nr_tests+= run_ma_test_recovery(0, 1);
  $nr_tests+= run_tests_on_clrs(0, 0, 1);

  if ($opt_number_of_tests)
  {
    print "Total number of tests is $nr_tests\n";
    exit(0);
  }

  if (defined($opt_run_tests))
  {
    if ($opt_run_tests =~ m/^(\d+)$/ ||
        $opt_run_tests =~ m/^(\d+)\.+$/)
    {
      $test_begin= $1;
    }
    elsif ($opt_run_tests =~ m/^(\d+)\.+(\d+)$/)
    {
      $test_begin= $1;
      $test_end= $2;
    }
    else
    {
      print "Wrong syntax for option --run-tests=$opt_run_tests\n";
      print "Please use --run-tests=<begin>..<end>\nwhere 'begin' is the ";
      print "first test to be run and 'end' is the last.\n";
      exit(1);
    }
    if ($test_end > $nr_tests)
    {
      print "Test range ($test_begin..$test_end) out of range. ";
      print "There are only $nr_tests in the test suite.\n";
      exit(1);
    }
    $test_begin++ if (!$test_begin); # Handle zero, if user gave that
    if ($test_end && $test_begin > $test_end)
    {
      print "Bad test range ($test_begin..$test_end)\n";
      exit(1);
    }
    # Now adjust number of tests
    $nr_tests= ($test_end ? $test_end : $nr_tests) - $test_begin + 1;
  }

  #
  # clean-up
  #

  unlink_all_possible_tmp_files();

  #
  # Run tests
  #

  if (!$opt_verbose)
  {
    print "1..$nr_tests\n";
  }
  else
  {
    print "Total tests: $nr_tests\n";
  }

  if ($opt_verbose)
  {
    print "Running tests with dynamic row format\n"
  }
  run_check_tests($suffix, $opt_silent, "", $opt_verbose, 0);
  run_repair_tests($suffix, $opt_silent, "", $opt_verbose, 0);
  run_pack_tests($suffix, $opt_silent, "", $opt_verbose, 0);

  if ($opt_verbose)
  {
    print "\nRunning tests with static row format\n";
  }
  run_check_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);
  run_repair_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);
  run_pack_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);

  if ($opt_verbose)
  {
    print "\nRunning tests with block row format\n";
  }
  run_check_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);
  run_repair_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);
  run_pack_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);

  if ($opt_verbose)
  {
    print "\nRunning tests with block row format and transactions\n";
  }
  run_check_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);
  run_repair_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);
  run_pack_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);

  if ($opt_verbose)
  {
    print "\nRunning tests with block row format, transactions and versioning\n";
  }
  run_check_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);
  run_repair_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);
  run_pack_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);


  if ($opt_verbose)
  {
    print "\nRunning tests with warnings and recovery\n";
  }
  run_tests_on_warnings_and_errors($suffix, $opt_silent, $opt_verbose, 0);
  run_ma_test_recovery($opt_verbose, 0);
  run_tests_on_clrs($suffix, $opt_verbose, 0);

  unlink_all_possible_tmp_files();
  if ($using_internal_tmpdir)
  {
    rmdir($tmpdir);
  }
  rmdir($exec_dir);
  chdir("..");
  rmdir($exec_dir);
  exit($runtime_error);
}

####
#### regular tests
####

sub run_check_tests
{
  my ($suffix, $silent, $row_type, $verbose, $count)= @_;
  my ($i, $nr_tests);
  my @ma_test1_opt= ( ["","-se"],
                      ["-N","-se"],
                      ["-P --checksum","-se"],
                      ["-P -N","-se"],
                      ["-B -N -R2","-sm"],
                      ["-a -k 480 --unique","-sm"],
                      ["-a -N -R1 ","-sm"],
                      ["-p","-sm"],
                      ["-p -N --unique","-sm"],
                      ["-p -N --key_length=127 --checksum","-sm"],
                      ["-p -N --key_length=128","-sm"],
                      ["-p --key_length=480","-sm"],
                      ["-a -B","-sm"],
                      ["-a -B --key_length=64 --unique","-sm"],
                      ["-a -B -k 480 --checksum","-sm"],
                      ["-a -B -k 480 -N --unique --checksum","-sm"],
                      ["-a -m","-sm"],
                      ["-a -m -P --unique --checksum","-sm"],
                      ["-a -m -P --key_length=480 --key_cache","-sm"],
                      ["-m -p","-sm"],
                      ["-w --unique","-sm"],
                      ["-a -w --key_length=64 --checksum","-sm"],
                      ["-a -w -N --key_length=480","-sm"],
                      ["-a -w --key_length=480 --checksum","-sm"],
                      ["-a -b -N","-sm"],
                      ["-a -b --key_length=480","-sm"],
                      ["-p -B --key_length=480","-sm"],
                      ["--checksum --unique","-se"],
                      ["--unique","-se"],
                      ["--rows-no-data", "-s"],
                      ["--key_multiple -N -S","-sm"],
                      ["--key_multiple -a -p --key_length=480","-sm"],
                      ["--key_multiple -a -B --key_length=480","-sm"],
                      ["--key_multiple -P -S","-sm"] );
  my @ma_test2_opt= ( ["-L -K -W -P","-sm"],
                      ["-L -K -W -P -A","-sm"],
                      ["-L -K -W -P -b32768", "-sm"],
                      ["-L -K -W -P -M -T -c -b32768 -t4 -m300", "-sm"],
                      ["-L -K -P -R3 -m50 -b1000000", "-sm"],
                      ["-L -B","-sm"],
                      ["-D -B -c","-sm"],
                      ["-m10000 -e4096 -K","-sm"],
                      ["-m10000 -e8192 -K","-sm"],
                      ["-m10000 -e16384 -E16384 -K -L","-sm"],
                      ["-L -K -W -P -b32768", "-se"],
                      ["-c -b65000","-se"] );
  my @ma_rt_test_opt= ( ); # (["--checksum", "-se"] );


  if ($count)
  {
    $nr_tests= 2;                # Number of tests outside loops
    for ($i= 0; defined($ma_test1_opt[$i]); $i++) { $nr_tests+=2; }
    for ($i= 0; defined($ma_test2_opt[$i]); $i++) { $nr_tests+=2; }
    for ($i= 0; defined($ma_rt_test_opt[$i]); $i++) { $nr_tests+=2; }
    return $nr_tests;
  }

  for ($i= 0; defined($ma_test1_opt[$i]); $i++)
  {
    unlink_log_files();
    ok("$maria_exe_path/ma_test1$suffix $silent -h$tmpdir $ma_test1_opt[$i][0] $row_type",
       $verbose, $i + 1);
    ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_test1_opt[$i][1] $tmpdir/test1",
       $verbose, $i + 1);
  }
  #
  # These tests are outside the loops. Make sure to include them in
  # nr_tests manually
  #
  ok("$maria_exe_path/aria_pack$suffix --force -s $tmpdir/test1", $verbose, 0);
  ok("$maria_exe_path/aria_chk$suffix -ess $tmpdir/test1", $verbose, 0);

  for ($i= 0; defined($ma_test2_opt[$i]); $i++)
  {
    unlink_log_files();
    ok("$maria_exe_path/ma_test2$suffix $silent -h$tmpdir $ma_test2_opt[$i][0] $row_type",
       $verbose, $i + 1);
    ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_test2_opt[$i][1] $tmpdir/test2",
       $verbose, $i + 1);
  }

  for ($i= 0; defined($ma_rt_test_opt[$i]); $i++)
  {
    unlink_log_files();
    ok("$maria_exe_path/ma_rt_test$suffix $silent -h$tmpdir $ma_rt_test_opt[$i][0] $row_type",
       $verbose, $i + 1);
    ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_rt_test_opt[$i][1] $tmpdir/rt_test",
       $verbose, $i + 1);
  }

  unlink_log_files();

  return 0;
}

####
#### repair tests
####

sub run_repair_tests()
{
  my ($suffix, $silent, $row_type, $verbose, $count)= @_;
  my ($i);

  my @t= ($NEW_TEST,
          "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix --silent -re --transaction-log test1",
          "$maria_exe_path/aria_chk$suffix -rs test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -rqs test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -rs --correct-checksum test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -rqs --correct-checksum test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -ros --correct-checksum test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -rqos --correct-checksum test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -sz test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/ma_test2$suffix $silent -c -d1 $row_type",
          "$maria_exe_path/aria_chk$suffix -s --parallel-recover test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/aria_chk$suffix -s --parallel-recover --quick test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/ma_test2$suffix $silent -c $row_type",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/aria_chk$suffix -sr test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/ma_test2$suffix $silent -c -t4 -b32768 $row_type",
          "$maria_exe_path/aria_chk$suffix -s --zerofill test1",
          "$maria_exe_path/aria_chk$suffix -se test1"
         );

  return &count_tests(\@t) if ($count);
  &run_test_bunch(\@t, $verbose, 0);
  return 0;
}

####
#### pack tests
####

sub run_pack_tests()
{
  my ($suffix, $silent, $row_type, $verbose, $count)= @_;
  my ($i);

  my @t= ($NEW_TEST,
          "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
          "$maria_exe_path/aria_pack$suffix --force -s test1",
          "$maria_exe_path/aria_chk$suffix -ess test1",
          "$maria_exe_path/aria_chk$suffix -rqs test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          "$maria_exe_path/aria_chk$suffix -rs test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          "$maria_exe_path/aria_chk$suffix -rus test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          $NEW_TEST,
          "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
          "$maria_exe_path/aria_pack$suffix --force -s test1",
          "$maria_exe_path/aria_chk$suffix -rus --safe-recover test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          $NEW_TEST,
          "$maria_exe_path/ma_test1$suffix $silent --checksum -S $row_type",
          "$maria_exe_path/aria_chk$suffix -se test1",
          "$maria_exe_path/aria_chk$suffix -ros test1",
          "$maria_exe_path/aria_chk$suffix -rqs test1",
          "$maria_exe_path/aria_chk$suffix -se test1",
          $NEW_TEST,
          "$maria_exe_path/aria_pack$suffix --force -s test1",
          "$maria_exe_path/aria_chk$suffix -rqs test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          "$maria_exe_path/aria_chk$suffix -rus test1",
          "$maria_exe_path/aria_chk$suffix -es test1",
          $NEW_TEST,
          "$maria_exe_path/ma_test2$suffix $silent -c -d1 $row_type",
          "$maria_exe_path/aria_chk$suffix -s --parallel-recover test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/aria_chk$suffix -s --unpack --parallel-recover test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          "$maria_exe_path/aria_pack$suffix --force -s test1",
          "$maria_exe_path/aria_chk$suffix -s --unpack --parallel-recover test2",
          "$maria_exe_path/aria_chk$suffix -se test2",
          $NEW_TEST,
          "$maria_exe_path/ma_test1$suffix $silent -c $row_type",
          "cp test1.MAD test2.MAD",
          "cp test1.MAI test2.MAI",
          "$maria_exe_path/aria_pack$suffix --force -s --join=test3 test1 test2",
         );

  return (&count_tests(\@t) + 3) if ($count);
  &run_test_bunch(\@t, $verbose, 0);

  ok("$maria_exe_path/aria_chk -s test3", $verbose, 0, 1);
  @t= ("$maria_exe_path/aria_chk -s --safe-recover test3",
       "$maria_exe_path/aria_chk -s test3");
  &run_test_bunch(\@t, $verbose, 0);

  return 0;
}

####
#### Tests that gives warnings or errors
####

sub run_tests_on_warnings_and_errors
{
  my ($suffix, $silent, $verbose, $count)= @_;
  my ($com);

  return 9 if ($count);  # Number of tests in this function, e.g. calls to ok()

  ok("$maria_exe_path/ma_test2$suffix -h$tmpdir $silent -L -K -W -P -S -R1 -m500",
     $verbose, 0);
  ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2", $verbose, 0);
  # ma_test2$suffix $silent -L -K -R1 -m2000 ;  Should give error 135\n
  # In the following a failure is a success and success is a failure
  $com=  "$maria_exe_path/ma_test2$suffix -h$tmpdir $silent -L -K -R1 -m2000 ";
  $com.= ">ma_test2_message.txt 2>&1";
  ok($com, $verbose, 0, 1);
  ok("cat ma_test2_message.txt", $verbose, 0);
  ok("grep \"Error: 135\" ma_test2_message.txt > /dev/null", $verbose, 0);
  # maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2 will warn that
  # Datafile is almost full
  ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2 >ma_test2_message.txt 2>&1",
     $verbose, 0, 1);
  ok("cat ma_test2_message.txt", $verbose, 0);
  ok("grep \"warning: Datafile is almost full\" ma_test2_message.txt>/dev/null",
     $verbose, 0);
  unlink <ma_test2_message.txt>;
  ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -ssm $tmpdir/test2", $verbose, 0);

  return 0;
}

####
#### Test that removing tables and applying the log leads to identical tables
####

sub run_ma_test_recovery
{
  my ($verbose, $count)= @_;

  return 1 if ($count);                # Number of tests in this function
  ok("$maria_path/unittest/ma_test_recovery.pl", $verbose, 0);
  return 0;
}

####
#### Tests on CLR's
####

sub run_tests_on_clrs
{
  my ($suffix, $verbose, $count)= @_;
  my ($i);

  my @t= ($NEW_TEST,
          "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b -t2 -A1",
          "cp $tmpdir/aria_log_control $tmpdir/aria_log_control.backup",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
          "mv $tmpdir/aria_log_control.backup $tmpdir/aria_log_control",
          "rm $tmpdir/test2.MA?",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
          "rm $tmpdir/test2.MA?",
          $NEW_TEST,
          "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b -t2 -A1",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir ",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
          "rm $tmpdir/test2.MA?",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -e -s $tmpdir/test2",
          "rm $tmpdir/test2.MA?",
          $NEW_TEST,
          "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b32768 -t4 -A1",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir ",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
          "rm $tmpdir/test2.MA?",
          "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
          "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
          "rm $tmpdir/test2.MA?"
         );

  return &count_tests(\@t) if ($count);
  &run_test_bunch(\@t, $verbose, 1);
  return 0;
}

#
# Print "ok" on success and "not ok" on error
#
# Note: Every time this function is called it will be counted
# as a unit test.
#
# Args: $com:            The actual command run. Will be printed on a failure
#       $verbose:        Be more verbose.
#       $iteration:      Number of iterations in a loop when the error
#                        occurred. If not in loop, this should be blank
#                        (e.g. send zero).
#       $expected_error: Optional; put here expected error code. Test
#                        will pass with this result only.
#
# Return value:          Will return 1 on success and 0 on an error
#

sub ok
{
  my ($com, $verbose, $iteration, $expected_error)= @_;
  my ($msg, $output, $err, $errcode, $len);

  $test_counter++;
  if ($test_begin > $test_counter)
  {
    return 0;
  }
  if ($test_end && $test_end < $test_counter)
  {
    exit(0);
  }

  $msg= "";
  $expected_error= 0 if (!defined($expected_error));

  if ($verbose)
  {
    # Print command with out the long unittest/../ prefix 
    my $tmp;
    $tmp= $com;
    $tmp =~ s|^unittest/../||;
    print "$tmp ";
    $len= length($tmp);
  }
  $output= `$com 2>&1`;
  if ($verbose)
  {
    print " " x (62 - $len);
  }
  $err= $?;
  $errcode= ($? >> 8);
  if ((!$err && !$expected_error) ||
      ($errcode == $expected_error && $expected_error))
  {
    print "[ " if ($verbose);
    print "ok";
    if ($verbose)
    {
      print " ]";
      print " " x (5 - length("$test_counter"));
      print "$test_counter";
    }
    else
    {
      print " $test_counter - $com"
    }
    print "\n";
    return 1;
  }
  print "[ " if ($verbose);
  print "not ok";
  print " ]" if ($verbose);
  print " $test_counter - $com" unless $verbose;
  print "\n";
  if ($verbose && defined($output) && length($output))
  {
    print "$output\n";
  }
  if (!$verbose)
  {
    $msg= "\n"; # Get a nicer output in perl unit test mode
  }
  $msg.= "Failed test '$com' ";
  if ($iteration)
  {
    $msg.= "(loop iteration $iteration.) ";
  }
  $msg.= "at line ";
  $msg.= (caller)[2];
  $msg.= "\n(errcode: $errcode, test: $test_counter)\n";
  if ($expected_error)
  {
    $msg.= "Was expecting errcode: $expected_error\n";
  }
  warn $msg;
  $runtime_error= 1;
  if ($opt_abort_on_error)
  {
    exit 1;
  }
  # Unlink all files so that we can continue on error
  unlink_all_possible_tmp_files();
  return 0;
}

#
# Print "skip" and the reason
#
# Note: Every time this function is called it will be counted
# as a unit test.
#
# Args: $com:            The actual command run. Will be printed on a failure
#       $reason:         The reason to skip a test
#       $verbose:        Be more verbose.
#

sub skip
{
  my ($com, $reason, $verbose)= @_;

  $test_counter++;
  return 0 if $test_begin > $test_counter;
  exit 0 if $test_end && $test_end < $test_counter;
  printf '%-64s[ skipped ]%5d', $com, $test_counter if $verbose;
  print "ok $test_counter # skip $reason" unless $verbose;
  print "\n";
  return 1;
}

####
#### Count tests
#### Arguments: $t:    an array of the tests
####

sub count_tests
{
  my ($t)= @_;
  my ($i, $nr_tests);

  $nr_tests= 0;
  for ($i= 0; defined(@$t[$i]); $i++) { $nr_tests++ if (@$t[$i]); }
  return $nr_tests;
}

sub unlink_log_files
{
  unlink "$full_tmpdir/aria_log_control", "$full_tmpdir/aria_log.00000001", "$full_tmpdir/aria_log.00000002";
}

sub unlink_all_possible_tmp_files()
{
  unlink_log_files();

  # Unlink tmp files that may have been created when testing the test programs
  unlink <$full_tmpdir/*.TMD $full_tmpdir/aria_read_log_test1.txt $full_tmpdir/test1*.MA? $full_tmpdir/ma_test_recovery.output aria_log_control aria_log.00000001 aria_log.00000002 aria_logtest1.MA? test1.MA? test2.MA? test3.MA? *.TMD>;
}

####
#### Run a bunch of tests
#### Arguments: $t:       an array of the tests
####            $verbose: to be passed to ok()
####            $clear:   clear log files if set
####

sub run_test_bunch
{
  my ($t, $verbose, $clear)= @_;
  my ($i);

  for ($i= 0; defined(@$t[$i]); $i++)
  {
    if ($clear && @$t[$i] eq $NEW_TEST)
    {
      unlink_log_files();
    }
    if (@$t[$i] ne $NEW_TEST)
    {
      ok(@$t[$i], $verbose, $i + 1);
    }
  }
}

####
#### usage
####

sub usage
{
  print <<EOF;
$my_progname version $VER

Description:

Run various Aria related tests. Typically used via make test as a unittest.

Options
--help             Show this help and exit.
--abort-on-error   Abort at once in case of error.
--number-of-tests  Print the total number of tests and exit.
--run-tests=...    Test range that should be run. You can give just
                   one number, to start tests from this test, or a range.
                   For example 45..89. To run a specific test alone,
                   for example test 215, use --run-tests=215..215
                   Use this option with caution, because some of the tests
                   might depend on previous ones.
--start-from=...   Alias for --run-tests
--silent=...       Silent option passed to ma_test* tests ('$opt_silent')
--tmpdir=...       Store tests data in this directory (works for most tests)
--valgrind=...     Options for valgrind.
                   ('$opt_valgrind')
--verbose          Be more verbose. Will print each unittest on a line
                   and result after. This mode cannot be used with unit.pl
                   when running in normal unit test mode.
--version          Show version number and exit.
EOF
  exit(0);
}