File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match statement: basic first-match with ordinal and string subjects }
3+ program tmatch1;
4+
5+ procedure TestOrdinal ;
6+ var
7+ x: Integer;
8+ r: Integer;
9+ begin
10+ x := 2 ;
11+ r := 0 ;
12+ match x of
13+ 1 : r := 10 ;
14+ 2 : r := 20 ;
15+ 3 : r := 30 ;
16+ end ;
17+ if r <> 20 then Halt(1 );
18+ end ;
19+
20+ procedure TestString ;
21+ var
22+ s: String;
23+ r: Integer;
24+ begin
25+ s := ' hello' ;
26+ r := 0 ;
27+ match s of
28+ ' world' : r := 1 ;
29+ ' hello' : r := 2 ;
30+ ' foo' : r := 3 ;
31+ end ;
32+ if r <> 2 then Halt(2 );
33+ end ;
34+
35+ procedure TestFirstMatch ;
36+ var
37+ x: Integer;
38+ r: Integer;
39+ begin
40+ { first-match: only first matching branch executes }
41+ x := 5 ;
42+ r := 0 ;
43+ match x of
44+ 5 : r := r + 1 ;
45+ 5 : r := r + 10 ;
46+ end ;
47+ if r <> 1 then Halt(3 );
48+ end ;
49+
50+ begin
51+ TestOrdinal;
52+ TestString;
53+ TestFirstMatch;
54+ WriteLn(' OK' );
55+ end .
Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match statement: else and _ catch-all }
3+ program tmatch2;
4+
5+ procedure TestElse ;
6+ var
7+ x, r: Integer;
8+ begin
9+ x := 99 ;
10+ r := 0 ;
11+ match x of
12+ 1 : r := 10 ;
13+ 2 : r := 20 ;
14+ else
15+ r := 999 ;
16+ end ;
17+ if r <> 999 then Halt(1 );
18+ end ;
19+
20+ procedure TestUnderscore ;
21+ var
22+ x, r: Integer;
23+ begin
24+ x := 42 ;
25+ r := 0 ;
26+ match x of
27+ 1 : r := 10 ;
28+ _: r := 100 ;
29+ end ;
30+ if r <> 100 then Halt(2 );
31+ end ;
32+
33+ procedure TestUnderscoreSkipsOnMatch ;
34+ var
35+ x, r: Integer;
36+ begin
37+ x := 1 ;
38+ r := 0 ;
39+ match x of
40+ 1 : r := 10 ;
41+ _: r := 100 ;
42+ end ;
43+ if r <> 10 then Halt(3 );
44+ end ;
45+
46+ begin
47+ TestElse;
48+ TestUnderscore;
49+ TestUnderscoreSkipsOnMatch;
50+ WriteLn(' OK' );
51+ end .
Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match all: fallthrough mode and leave }
3+ program tmatch3;
4+
5+ procedure TestFallthrough ;
6+ var
7+ x, r: Integer;
8+ begin
9+ x := 5 ;
10+ r := 0 ;
11+ match all x of
12+ 5 : r := r + 1 ;
13+ 5 : r := r + 10 ;
14+ 3 : r := r + 100 ;
15+ end ;
16+ { both x=5 branches fire, x=3 does not }
17+ if r <> 11 then Halt(1 );
18+ end ;
19+
20+ procedure TestFallthroughCatchAll ;
21+ var
22+ x, r: Integer;
23+ begin
24+ x := 1 ;
25+ r := 0 ;
26+ match all x of
27+ 1 : r := r + 1 ;
28+ 2 : r := r + 10 ;
29+ _: r := r + 100 ;
30+ end ;
31+ { x=1 matches, x=2 does not, _ always matches }
32+ if r <> 101 then Halt(2 );
33+ end ;
34+
35+ procedure TestLeave ;
36+ var
37+ x, r: Integer;
38+ begin
39+ x := 5 ;
40+ r := 0 ;
41+ match all x of
42+ 5 : begin r := r + 1 ; leave; end ;
43+ 5 : r := r + 10 ;
44+ _: r := r + 100 ;
45+ end ;
46+ { leave exits after first branch }
47+ if r <> 1 then Halt(3 );
48+ end ;
49+
50+ begin
51+ TestFallthrough;
52+ TestFallthroughCatchAll;
53+ TestLeave;
54+ WriteLn(' OK' );
55+ end .
Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match without of: condition-based matching }
3+ program tmatch4;
4+
5+ procedure TestFirstMatch ;
6+ var
7+ x, r: Integer;
8+ begin
9+ x := 50 ;
10+ r := 0 ;
11+ match
12+ x > 100 : r := 1 ;
13+ x > 10 : r := 2 ;
14+ x > 0 : r := 3 ;
15+ _: r := 4 ;
16+ end ;
17+ { first-match: x=50 matches x>10 first }
18+ if r <> 2 then Halt(1 );
19+ end ;
20+
21+ procedure TestFallthrough ;
22+ var
23+ x, r: Integer;
24+ begin
25+ x := 50 ;
26+ r := 0 ;
27+ match all
28+ x > 100 : r := r + 1 ;
29+ x > 10 : r := r + 10 ;
30+ x > 0 : r := r + 100 ;
31+ end ;
32+ { x=50: x>10 and x>0 match }
33+ if r <> 110 then Halt(2 );
34+ end ;
35+
36+ procedure TestCatchAll ;
37+ var
38+ x, r: Integer;
39+ begin
40+ x := -5 ;
41+ r := 0 ;
42+ match
43+ x > 0 : r := 1 ;
44+ _: r := 99 ;
45+ end ;
46+ if r <> 99 then Halt(3 );
47+ end ;
48+
49+ begin
50+ TestFirstMatch;
51+ TestFallthrough;
52+ TestCatchAll;
53+ WriteLn(' OK' );
54+ end .
Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match: comma-separated patterns (OR) }
3+ program tmatch5;
4+
5+ procedure TestComma ;
6+ var
7+ x, r: Integer;
8+ begin
9+ x := 3 ;
10+ r := 0 ;
11+ match x of
12+ 1 , 2 , 3 : r := 10 ;
13+ 4 , 5 , 6 : r := 20 ;
14+ _: r := 99 ;
15+ end ;
16+ if r <> 10 then Halt(1 );
17+ end ;
18+
19+ procedure TestCommaSecondGroup ;
20+ var
21+ x, r: Integer;
22+ begin
23+ x := 5 ;
24+ r := 0 ;
25+ match x of
26+ 1 , 2 , 3 : r := 10 ;
27+ 4 , 5 , 6 : r := 20 ;
28+ _: r := 99 ;
29+ end ;
30+ if r <> 20 then Halt(2 );
31+ end ;
32+
33+ procedure TestCommaNoMatch ;
34+ var
35+ x, r: Integer;
36+ begin
37+ x := 100 ;
38+ r := 0 ;
39+ match x of
40+ 1 , 2 , 3 : r := 10 ;
41+ 4 , 5 , 6 : r := 20 ;
42+ _: r := 99 ;
43+ end ;
44+ if r <> 99 then Halt(3 );
45+ end ;
46+
47+ begin
48+ TestComma;
49+ TestCommaSecondGroup;
50+ TestCommaNoMatch;
51+ WriteLn(' OK' );
52+ end .
Original file line number Diff line number Diff line change 1+ { $mode unleashed}
2+ { match as expression }
3+ program tmatch6;
4+
5+ procedure TestBasic ;
6+ var
7+ x: Integer;
8+ s: String;
9+ begin
10+ x := 2 ;
11+ s := match x of
12+ 1 : ' one' ;
13+ 2 : ' two' ;
14+ 3 : ' three' ;
15+ _: ' other' ;
16+ end ;
17+ if s <> ' two' then Halt(1 );
18+ end ;
19+
20+ procedure TestWithElse ;
21+ var
22+ x: Integer;
23+ s: String;
24+ begin
25+ x := 99 ;
26+ s := match x of
27+ 1 : ' one' ;
28+ 2 : ' two' ;
29+ else
30+ ' unknown'
31+ end ;
32+ if s <> ' unknown' then Halt(2 );
33+ end ;
34+
35+ procedure TestConditionBased ;
36+ var
37+ x: Integer;
38+ s: String;
39+ begin
40+ x := 50 ;
41+ s := match
42+ x > 100 : ' big' ;
43+ x > 10 : ' medium' ;
44+ _: ' small' ;
45+ end ;
46+ if s <> ' medium' then Halt(3 );
47+ end ;
48+
49+ begin
50+ TestBasic;
51+ TestWithElse;
52+ TestConditionBased;
53+ WriteLn(' OK' );
54+ end .
You can’t perform that action at this time.
0 commit comments