Skip to content

Commit

Permalink
Add type checks to Concatenation
Browse files Browse the repository at this point in the history
In particular, this lets the undefined expression `Concatenation( [ 1 ] )`
throw an error instead of returning `1`.
  • Loading branch information
zickgraf committed Apr 2, 2023
1 parent a78e3d2 commit 6220a99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/list.gi
Original file line number Diff line number Diff line change
Expand Up @@ -2084,8 +2084,14 @@ InstallGlobalFunction( Concatenation, function ( arg )
if Length( arg ) = 0 then
return [ ];
fi;
if not IsList( arg[1] ) then
Error( "Concatenation: arguments must be lists" );
fi;
res := ShallowCopy( arg[1] );
for i in [ 2 .. Length( arg ) ] do
if not IsList( arg[i] ) then
Error( "Concatenation: arguments must be lists" );
fi;
Append( res, arg[i] );
od;
return res;
Expand Down
18 changes: 18 additions & 0 deletions tst/testinstall/opers/Concatenation.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
gap> START_TEST("Concatenation.tst");

#
gap> Concatenation( );
[ ]
gap> Concatenation( [ ] );
[ ]
gap> Concatenation( [ 1 ] );
Error, Concatenation: arguments must be lists
gap> Concatenation( [ [ 1, 2 ] ] );
[ 1, 2 ]
gap> Concatenation( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] );
[ 1, 2, 3, 4, 5, 6 ]
gap> Concatenation( [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] );
[ 1, 2, 3, 4, 5, 6 ]

#
gap> STOP_TEST("Concatenation.tst", 1);

0 comments on commit 6220a99

Please sign in to comment.