Skip to content

Commit fa09383

Browse files
committed
Tests for signature shape matching syntax.
Check the declared shape against that required.
1 parent 75f702c commit fa09383

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

S06-signature/shape.t

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use v6;
2+
use Test;
3+
4+
plan 34;
5+
6+
sub single-dim(@a[3]) { }
7+
lives-ok { single-dim(Array.new(:shape(3))) }, '[3] shape constraint accepts matcing array';
8+
dies-ok { single-dim(Array.new()) }, '[3] shape constraint denies unshaped array';
9+
dies-ok { single-dim(Array.new(:shape(4))) }, '[3] shape constraint denies oversied array';
10+
dies-ok { single-dim(Array.new(:shape(2))) }, '[3] shape constraint denies undersized array';
11+
dies-ok { single-dim(Array.new(1, 2, 3)) }, 'Shape constraints are about declared shape';
12+
dies-ok { single-dim(Array.new(:shape(2,2))) }, '[3] shape constraint denies over-dimensioned array...';
13+
dies-ok { single-dim(Array.new(:shape(3,2))) }, '...even if first dimension matches';
14+
15+
sub multi-dim(@a[4,4]) { }
16+
lives-ok { multi-dim(Array.new(:shape(4,4))) }, '[4,4] shape constraint accepts matcing array';
17+
dies-ok { multi-dim(Array.new()) }, '[4,4] shape constraint denies unshaped array';
18+
dies-ok { multi-dim(Array.new(:shape(4, 5))) }, '[4,4] shape constraint denies oversied array (1)';
19+
dies-ok { multi-dim(Array.new(:shape(5, 4))) }, '[4,4] shape constraint denies oversied array (2)';
20+
dies-ok { multi-dim(Array.new(:shape(3,4))) }, '[4,4] shape constraint denies undersized array (1)';
21+
dies-ok { multi-dim(Array.new(:shape(4,3))) }, '[4,4] shape constraint denies undersized array (2)';
22+
dies-ok { multi-dim(Array.new([1..4],[1..4])) }, 'Shape constraints are about declared shape';
23+
dies-ok { multi-dim(Array.new(:shape(3,3,3))) }, '[4,4] shape constraint denies over-dimensioned array...';
24+
dies-ok { multi-dim(Array.new(:shape(4,4,3))) }, '...even if first dimensions match';
25+
dies-ok { multi-dim(Array.new(:shape(4))) }, '[4,4] shape constraint denies under-dimensioned array';
26+
27+
sub whatever-dim(@a[*]) { }
28+
lives-ok { whatever-dim(Array.new()) }, '[*] shape constraint accepts undimensioned array';
29+
lives-ok { whatever-dim(Array.new(:shape(3))) }, '[*] shape constraint accepts fixed single dimension array';
30+
dies-ok { whatever-dim(Array.new(:shape(3,3))) }, '[*] shape constraint rejects 2-dimensioned array';
31+
32+
sub whatever-multidim(@a[*,*]) { }
33+
lives-ok { whatever-multidim(Array.new(:shape(3,3))) }, '[*,*] shape constraint accepts 2-dim shaped array';
34+
dies-ok { whatever-multidim(Array.new(:shape(3))) }, '[*,*] shape constraint rejects 1-dim array';
35+
dies-ok { whatever-multidim(Array.new(:shape(3,3,3))) }, '[*,*] shape constraint rejects 3-dim array';
36+
dies-ok { whatever-multidim(Array.new()) }, '[*,*] shape constraint rejects undimensioned array';
37+
38+
sub whatever-partialdim(@[4,*]) { }
39+
lives-ok { whatever-partialdim(Array.new(:shape(4,3))) }, '[4,*] shape constraint accepts 2-dim shaped array with 4 first dims';
40+
dies-ok { whatever-partialdim(Array.new(:shape(3,3))) }, '[4,*] shape constraint rejects 2-dim shaped array with 3 first dims';
41+
dies-ok { whatever-partialdim(Array.new(:shape(4))) }, '[4,*] shape constraint rejects 1-dim array';
42+
dies-ok { whatever-partialdim(Array.new(:shape(4,4,4))) }, '[4,*] shape constraint rejects 3-dim array';
43+
dies-ok { whatever-partialdim(Array.new()) }, '[4,*] shape constraint rejects undimensioned array';
44+
45+
sub dependent($n, @a[$n]) { }
46+
lives-ok { dependent(3, Array.new(:shape(3))) }, 'can use earlier parameters in shape specification (1)';
47+
lives-ok { dependent(*, Array.new(:shape(3))) }, 'can use earlier parameters in shape specification (2)';
48+
dies-ok { dependent(4, Array.new(:shape(3))) }, 'can use earlier parameters in shape specification (3)';
49+
dies-ok { dependent(4, Array.new()) }, 'can use earlier parameters in shape specification (4)';
50+
dies-ok { dependent(4, Array.new(:shape(4,3))) }, 'can use earlier parameters in shape specification (5)';
51+

0 commit comments

Comments
 (0)