Skip to content

Commit f9b363c

Browse files
committed
Add multivarinit test cases
7 tests covering global vars, local vars, typed constants, inline vars (explicit type and type inference), single-evaluation of function calls, and a compile-fail test without the modeswitch.
1 parent 485a2c2 commit f9b363c

7 files changed

Lines changed: 137 additions & 0 deletions

File tree

tests/test/tmultivarinit1.pp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{ test multi-var init: global static vars }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
5+
var
6+
a, b, c: integer = 42;
7+
x, y: boolean = true;
8+
s1, s2: string = 'hello';
9+
10+
begin
11+
if (a <> 42) or (b <> 42) or (c <> 42) then
12+
halt(1);
13+
if not x or not y then
14+
halt(2);
15+
if (s1 <> 'hello') or (s2 <> 'hello') then
16+
halt(3);
17+
{ values are independent copies }
18+
a := 0;
19+
if (b <> 42) or (c <> 42) then
20+
halt(4);
21+
s1 := 'changed';
22+
if s2 <> 'hello' then
23+
halt(5);
24+
end.

tests/test/tmultivarinit2.pp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{ test multi-var init: local vars }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
5+
procedure test;
6+
var
7+
a, b, c: integer = 10;
8+
begin
9+
if (a <> 10) or (b <> 10) or (c <> 10) then
10+
halt(1);
11+
a := 0;
12+
if (b <> 10) or (c <> 10) then
13+
halt(2);
14+
end;
15+
16+
begin
17+
test;
18+
end.

tests/test/tmultivarinit3.pp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ test multi-var init: typed constants }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
5+
type
6+
TRec = record x, y: integer; end;
7+
8+
const
9+
a, b, c: integer = 99;
10+
r1, r2: TRec = (x: 1; y: 2);
11+
12+
begin
13+
if (a <> 99) or (b <> 99) or (c <> 99) then
14+
halt(1);
15+
if (r1.x <> 1) or (r1.y <> 2) then
16+
halt(2);
17+
if (r2.x <> 1) or (r2.y <> 2) then
18+
halt(3);
19+
end.

tests/test/tmultivarinit4.pp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{ test multi-var init: inline var with explicit type }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
{$modeswitch inlinevars}
5+
6+
procedure test;
7+
begin
8+
var a, b: integer := 55;
9+
if (a <> 55) or (b <> 55) then
10+
halt(1);
11+
a := 0;
12+
if b <> 55 then
13+
halt(2);
14+
end;
15+
16+
begin
17+
test;
18+
end.

tests/test/tmultivarinit5.pp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{ test multi-var init: inline var with type inference }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
{$modeswitch inlinevars}
5+
6+
procedure test;
7+
begin
8+
var a, b := 42;
9+
if (a <> 42) or (b <> 42) then
10+
halt(1);
11+
var s1, s2 := 'hello';
12+
if (s1 <> 'hello') or (s2 <> 'hello') then
13+
halt(2);
14+
{ verify type promotion: result should be LongInt, not byte }
15+
var x, y := 10;
16+
if SizeOf(x) <> SizeOf(LongInt) then
17+
halt(3);
18+
end;
19+
20+
begin
21+
test;
22+
end.

tests/test/tmultivarinit6.pp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{ test multi-var init: function call evaluated once }
2+
{$mode objfpc}
3+
{$modeswitch multivarinit}
4+
{$modeswitch inlinevars}
5+
6+
var
7+
callcount: integer = 0;
8+
9+
function GetValue: integer;
10+
begin
11+
inc(callcount);
12+
result := 77;
13+
end;
14+
15+
procedure test;
16+
begin
17+
var a, b: integer := GetValue;
18+
if (a <> 77) or (b <> 77) then
19+
halt(1);
20+
if callcount <> 1 then
21+
halt(2);
22+
end;
23+
24+
begin
25+
test;
26+
end.

tests/test/tmultivarinit7.pp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{%FAIL}
2+
{ test multi-var init: must fail without modeswitch }
3+
{$mode objfpc}
4+
5+
var
6+
a, b: integer = 42;
7+
8+
begin
9+
halt(1);
10+
end.

0 commit comments

Comments
 (0)