Skip to content

Commit

Permalink
lib: improve error handling for Image, ...
Browse files Browse the repository at this point in the history
..., `Images`, `PreImage`, and `PreImages`.

If one of these functions figures out that the second argument is not an
element or subset of the Range or the Source, it now tells the user.

Improves the structuring of the `mapping.tst` file. Adds tests for the
new error handling.

Also adds some general tests for `Image`, `Images`, `PreImage`,
`PreImages`.
  • Loading branch information
ssiccha committed May 22, 2019
1 parent 87794e8 commit 8064400
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 37 deletions.
111 changes: 76 additions & 35 deletions lib/mapping.gi
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ InstallGlobalFunction( Image, function ( arg )
local map, # mapping <map>, first argument
elm; # element <elm>, second argument

if Length(arg) > 0 and not IsGeneralMapping( arg[1] ) then
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# image of the source under <map>, which may be multi valued in this case
if Length( arg ) = 1 and IsGeneralMapping(arg[1]) then
if Length( arg ) = 1 then

return ImagesSource( arg[1] );

Expand All @@ -165,17 +169,20 @@ InstallGlobalFunction( Image, function ( arg )
map := arg[1];
elm := arg[2];

# image of a single element <elm> under the mapping <map>
if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) ) then

if not IsMapping( map ) then
Error( "<map> must be a mapping" );
elif elm in Source( map ) then
return ImageElm( map, elm );
ErrorNoReturn( "<map> must be single-valued and total" );
elif not elm in Source( map ) then
ErrorNoReturn( "<elm> must be an element of Source(<map>)" );
fi;
return ImageElm( map, elm );

# image of a set or list of elments <elm> under the mapping <map>
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) )
and IsSubset( Source( map ), elm ) then
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) ) then
if not IsSubset( Source( map ), elm ) then
ErrorNoReturn( "<coll> must be a subset of Source(<map>)" );
fi;

if IsDomain( elm ) or IsSSortedList( elm ) then
if HasSource(map) and IsIdenticalObj(Source(map),elm) then
Expand All @@ -192,9 +199,15 @@ InstallGlobalFunction( Image, function ( arg )

return [];

else
ErrorNoReturn( "couldn't match families of <elm> or <coll> and ",
"Source(<map>),\n",
"maybe <elm> or <coll> are not contained in ",
"Source(<map>) or\naren't homogeneous lists," );
fi;
fi;
Error( "usage: Image(<map>), Image(<map>,<elm>), Image(<map>,<coll>)" );
ErrorNoReturn( "usage: Image(<map>), Image(<map>,<elm>), ",
"Image(<map>,<coll>)" );
end );


Expand All @@ -209,6 +222,10 @@ InstallGlobalFunction( Images, function ( arg )
local map, # mapping <map>, first argument
elm; # element <elm>, second argument

if Length(arg) > 0 and not IsGeneralMapping( arg[1] ) then
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# image of the source under <map>
if Length( arg ) = 1 then

Expand All @@ -219,19 +236,18 @@ InstallGlobalFunction( Images, function ( arg )
map := arg[1];
elm := arg[2];

if not IsGeneralMapping( map ) then
Error( "<map> must be a general mapping" );
fi;

# image of a single element <elm> under the mapping <map>
if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) )
and elm in Source( map ) then

if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) ) then
if not elm in Source( map ) then
ErrorNoReturn( "<elm> must be an element of Source(<map>)" );
fi;
return ImagesElm( map, elm );

# image of a set or list of elments <elm> under the mapping <map>
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) )
and IsSubset( Source( map ), elm ) then
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) ) then
if not IsSubset( Source( map ), elm ) then
ErrorNoReturn( "<coll> must be a subset of Source(<map>)" );
fi;

if IsDomain( elm ) or IsSSortedList( elm ) then
return ImagesSet( map, elm );
Expand All @@ -244,9 +260,15 @@ InstallGlobalFunction( Images, function ( arg )

return [];

else
ErrorNoReturn( "couldn't match families of <elm> or <coll> and ",
"Source(<map>),\n",
"maybe <elm> or <coll> are not contained in ",
"Source(<map>) or\naren't homogeneous lists," );
fi;
fi;
Error("usage: Images(<map>), Images(<map>,<elm>), Images(<map>,<coll>)");
ErrorNoReturn( "usage: Images(<map>), Images(<map>,<elm>), ",
"Images(<map>,<coll>)" );
end );


Expand All @@ -261,6 +283,10 @@ InstallGlobalFunction( PreImage, function ( arg )
local map, # gen. mapping <map>, first argument
img; # element <img>, second argument

if Length( arg ) > 0 and not IsGeneralMapping( arg[1] ) then
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# preimage of the range under <map>, which may be a general mapping
if Length( arg ) = 1 then

Expand All @@ -273,16 +299,18 @@ InstallGlobalFunction( PreImage, function ( arg )

# preimage of a single element <img> under <map>
if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) ) then
if not ( IsGeneralMapping( map ) and IsInjective( map )
and IsSurjective( map ) ) then
Error( "<map> must be an inj. and surj. mapping" );
elif img in Range( map ) then
return PreImageElm( map, img );
if not ( IsInjective( map ) and IsSurjective( map ) ) then
ErrorNoReturn( "<map> must be an inj. and surj. mapping" );
elif not img in Range( map ) then
ErrorNoReturn( "<elm> must be an element of Range(<map>)" );
fi;
return PreImageElm( map, img );

# preimage of a set or list of elments <img> under <map>
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) )
and IsSubset( Range( map ), img ) then
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) ) then
if not IsSubset( Range( map ), img ) then
ErrorNoReturn( "<coll> must be a subset of Range(<map>)" );
fi;

if IsDomain( img ) or IsSSortedList( img ) then
return PreImagesSet( map, img );
Expand All @@ -295,10 +323,15 @@ InstallGlobalFunction( PreImage, function ( arg )

return [];

else
ErrorNoReturn( "couldn't match families of <elm> or <coll> and ",
"Range(<map>),\n",
"maybe <elm> or <coll> are not contained in ",
"Source(<map>) or\naren't homogeneous lists," );
fi;
fi;
Error( "usage: PreImage(<map>), PreImage(<map>,<img>), ",
"PreImage(<map>,<coll>)" );
ErrorNoReturn( "usage: PreImage(<map>), PreImage(<map>,<img>), ",
"PreImage(<map>,<coll>)" );
end );


Expand All @@ -324,18 +357,21 @@ InstallGlobalFunction( PreImages, function ( arg )
img := arg[2];

if not IsGeneralMapping( map ) then
Error( "<map> must be a general mapping" );
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# preimage of a single element <img> under <map>
if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) )
and img in Range( map ) then

if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) ) then
if not img in Range( map ) then
ErrorNoReturn( "<elm> must be an element of Range(<map>)" );
fi;
return PreImagesElm( map, img );

# preimage of a set or list of elements <img> under <map>
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) )
and IsSubset( Range( map ), img ) then
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) ) then
if not IsSubset( Range( map ), img ) then
ErrorNoReturn( "<coll> must be a subset of Range(<map>)" );
fi;

if IsDomain( img ) or IsSSortedList( img ) then
return PreImagesSet( map, img );
Expand All @@ -348,10 +384,15 @@ InstallGlobalFunction( PreImages, function ( arg )

return [];

else
ErrorNoReturn( "couldn't match families of <elm> or <coll> and ",
"Range(<map>),\n",
"maybe <elm> or <coll> are not contained in ",
"Source(<map>) or\naren't homogeneous lists," );
fi;
fi;
Error( "usage: PreImages(<map>), PreImages(<map>,<img>), ",
"PreImages(<map>,<coll>)" );
ErrorNoReturn( "usage: PreImages(<map>), PreImages(<map>,<img>), ",
"PreImages(<map>,<coll>)" );
end );


Expand Down
Loading

0 comments on commit 8064400

Please sign in to comment.