5 use Test::More tests => 7;
9 BEGIN { use_ok('List::Index'); }
10 ok(eval { List::Index->VERSION(1) }, 'version 1.00 compatibility');
12 subtest 'single-char alphabet' => sub {
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(
21 )], 'equivalent pagesize');
24 subtest 'uniform alphanumeric' => sub {
26 my $index = List::Index->new(['aa'..'zz', 1..202]) or return;
27 is_deeply($index->ranges, [qw(
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-
31 )], 'default ranges');
32 is_deeply($index->ranges({pagesize => 300}), [qw(-c d-n o-)], 'large pagesize');
35 subtest 'context' => sub {
37 my $index = List::Index->new([qw(
38 kkeg kl km kmlu knsy koxb kpeo kuaa kuab kuac
39 kuapa kuq kur kux kzb lc lg lgu lgua lguc
40 lguq lgur lgus lgx lka lkq lks lln llq llx
43 $index->ranges({ pagesize=>10, context=>0, length=>5 }),
44 # ranges should match offsets exactly
45 [qw(-kuap. kuapa-lgup lguq-)],
49 $index->ranges({ pagesize=>10, context=>0 }),
50 # default length limits to 4 chars
51 [qw(-kuao kuap-lgup lguq-)],
55 $index->ranges({ pagesize=>10, context=>1 }),
56 # lookbehinds aren't shorter (kuac<kuap, lguc<lguq)
57 # 'kuap' can advance to 'kuq'
58 [qw(-kup kuq-lgup lguq-)],
62 local $TODO = 'backtrack';
64 $index->ranges({ pagesize=>10, context=>2 }),
65 # allowed to advance to 'kur', but provides no benefits over 'kuq'
66 [qw(-kup kuq-lgup lguq-)],
71 $index->ranges({ pagesize=>10, context=>3 }),
72 # shorten 'kuap' to 'ku' because lookbehind is 'kp...'
73 # 'lguq' matches 'lg', but may only backtrack to 'lgu'
74 [qw(-kt ku-lgt lgu-)],
78 $index->ranges({ pagesize=>10, context=>4 }),
83 $index->ranges({ pagesize=>10, context=>5 }),
84 # after forwarding 'kuap' to 'lc'
85 # disallow backtracking of 'lguq' to 'lc' to prevent qw[-k l-]
86 # so only lookahead (to 'lkq') remains
88 'no lookbehind after full lookahead'
91 $index->ranges({ pagesize=>10, context=>9 }),
92 # allow a single (10-9) entry (l-lf = lc) to remain
94 'lookbehind after full lookahead'
97 $index->ranges({ pagesize=>10, context=>10 }),
98 # allow the last page to go back upto 'lc', replacing the 2nd page
104 subtest 'context' => sub {
106 my $index = List::Index->new([qw(
107 baa1 baa2 baa3 baaa bbc cbc daaa ea eaaa zed
109 is_deeply($index->ranges({pagesize => 2, context => 0}), [
110 qw(-baa. baa.-bbb bbc-daa. daaa-eaa. eaaa-)
112 is_deeply($index->ranges({pagesize => 2}), [
114 ], 'default context'); # context should be 1
115 is_deeply($index->ranges({pagesize => 2, context => 2}), [
117 ], 'overlap'); # first item equals second due to large context
118 is_deeply($index->ranges({pagesize => 2, context => 0, length => 1}), [
122 #pp($index->ranges({pagesize => 2, context => 2, length => 1}));