173ec32533a4ca04c8de8dcb17ae53f7fd9bd504
[perl/list-index.git] / t / 10-ranges.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 16;
6 use Test::NoWarnings;
7 use Data::Dump 'pp';
8
9 BEGIN { use_ok('List::Index'); }
10 ok(eval { List::Index->VERSION(1) }, 'version 1.00 compatibility');
11
12 {
13 my @uniform = 'a'..'z';
14 ok(my $index = List::Index->new(\@uniform), 'object (single-char values)');
15 is_deeply(\@uniform, ['a'..'z'], 'original data unaltered');
16 is_deeply($index->ranges, [['a','']], 'single page');
17 is_deeply($index->ranges({pages => 3}), [map { [split /-/, $_, 2] } qw(
18         a-h i-q r-
19 )], 'given pages');
20 is_deeply($index->ranges({pagesize => @uniform / 2.1}), [map { [split /-/, $_, 2] } qw(
21         a-h i-q r-
22 )], 'equivalent pagesize');
23 }
24
25 {
26 ok(my $index = List::Index->new(['aa'..'zz', 1..193]), 'non-alphabetic values (uniform)');
27 is_deeply($index->ranges, [map { [split /-/, $_, 2] } qw(
28         .-.z ..-.z ..-..z ...-
29         a-bv bw-dr ds-fn fo-hk hl-jg jh-k l-m n-ov ow-qr qs-sn so-uk ul-wg wh-x y-
30 )], 'default ranges');
31 is_deeply($index->ranges({pagesize => 300}), [map { [split /-/, $_, 2] } qw(
32         .-c d-ov ow-
33 )], 'large pagesize');
34 }
35
36 {
37 ok(my $index = List::Index->new([qw(
38         baa1 baa2  baa3 baaa  bbc cbc  daaa ea  eaaa zed
39 )]), 'variable length values');
40 is_deeply($index->ranges({pagesize => 2, context => 0}), [
41         map { [split /-/, $_, 2] } qw(baa.-baaz baa.-bbb bbc-daa daaa-eaa eaaa-)
42 ], 'no context');
43 is_deeply($index->ranges({pagesize => 2}), [
44         map { [split /-/, $_, 2] } qw(b-baaz baa.-ba bb-c d-ea eaa-)
45 ], 'default context');  # context should be 1
46 is_deeply($index->ranges({pagesize => 2, context => 2}), [
47         map { [split /-/, $_, 2] } qw(b-a b-ba bb-c d-d e-)
48 ], 'overlap');  # first item equals second due to large context
49 is_deeply($index->ranges({pagesize => 2, length => 1}), [
50         map { [split /-/, $_, 2] } qw(b-a b-a b-c d-d e-)
51 ], 'single char');
52
53 #pp($index->ranges({pagesize => 2, context => 2, length => 1}));
54 }
55