test ranges distribution
[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 => 7;
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 subtest 'single-char alphabet' => sub {
13         plan tests => 4;
14         my @uniform = 'a'..'z';
15         my $index = List::Index->new(\@uniform) or return;
16         is_deeply(\@uniform, ['a'..'z'], 'original data unaltered');
17         is_deeply($index->ranges, ['-'], 'single page');
18         is_deeply($index->ranges({pages => 3}), [qw(-i j-q r-)], 'given pages');
19         is_deeply($index->ranges({pagesize => @uniform / 2.1}), [qw(
20                 -i j-q r-
21         )], 'equivalent pagesize');
22 };
23
24 subtest 'uniform alphanumeric' => sub {
25         plan tests => 2;
26         my $index = List::Index->new(['aa'..'zz', 1..202]) or return;
27         is_deeply($index->ranges, [qw(
28                 -.
29                 .-bp bq-dm dn-fi fj-hf hg-i j-k l-m n-os ot-qp qq-sm sn-uj uk-wf wg-x y-
30
31         )], 'default ranges');
32         is_deeply($index->ranges({pagesize => 300}), [qw(-c d-n o-)], 'large pagesize');
33 };
34
35 subtest 'context' => sub {
36         plan tests => 4;
37         my $index = List::Index->new([qw(
38                 baa1 baa2  baa3 baaa  bbc cbc  daaa ea  eaaa zed
39         )]) or return;
40         is_deeply($index->ranges({pagesize => 2, context => 0}), [
41                 qw(-baa. baa.-bbb bbc-daa. daaa-eaa. eaaa-)
42         ], 'no context');
43         is_deeply($index->ranges({pagesize => 2}), [
44                 qw(-a b c d e-)
45         ], 'default context');  # context should be 1
46         is_deeply($index->ranges({pagesize => 2, context => 2}), [
47                 qw(-a b-c d e-)
48         ], 'overlap');  # first item equals second due to large context
49         is_deeply($index->ranges({pagesize => 2, context => 0, length => 1}), [
50                 qw(-a b-c d e-)
51         ], 'single char');
52
53         #pp($index->ranges({pagesize => 2, context => 2, length => 1}));
54 };
55
56 subtest 'distribution' => sub {
57         plan tests => 2;
58         my $index = List::Index->new([qw(
59                 kkeg kl km kmlu knsy   koxb kpeo kqbt krzu ktyp
60                 kuap kuy kvbc kyy kzb  lc lg lgaa lgbv lgbw
61                 lgu lij ljr ljs lka    lkq lks lln llq llx
62         )]) or return;
63 TODO: {
64         local $TODO = 'under development';
65         is_deeply(
66                 $index->ranges({ pagesize=>10, context=>5 }),
67                 # after 'kuap' forwards to 'kzb', 'lgu' shouldn't go back to 'lc'
68                 # otherwise we get qw[-k l-]
69                 [qw(-k l-lg lgu-)],
70                 'lookbehind after full lookahead'
71         );
72 }
73         is_deeply(
74                 $index->ranges({ pagesize=>10, context=>4 }),
75                 [qw(-kt ku-lf lg-)],
76                 'maximal lookahead'
77         );
78 }
79