Skip to content

Commit

Permalink
Improve errors messages for various set operations
Browse files Browse the repository at this point in the history
AddSet, RemoveSet, UniteSet, IntersectSet, SubtractSet now print a
helpful error if the first argument is not mutable, or not a set.
  • Loading branch information
fingolfin committed Oct 16, 2020
1 parent e62d395 commit 5f4fdd4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/set.gi
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,22 @@ InstallMethod( SubtractSet,
RemoveSet( set, obj );
od;
end );


#############################################################################
##
## Fallback methods to give better user feedback if the first argument is
## not mutable or not a set
##
BindGlobal("_REQUIRE_MUTABLE_SET",
function( set, obj )
if not IsMutable(set) or not IsSSortedList( set ) then
Error( "<set> must be a mutable proper set" );
fi;
TryNextMethod();
end );
InstallOtherMethod( AddSet, "for two objects", [ IsObject, IsObject ], _REQUIRE_MUTABLE_SET);
InstallOtherMethod( RemoveSet, "for two objects", [ IsObject, IsObject ], _REQUIRE_MUTABLE_SET);
InstallOtherMethod( UniteSet, "for two objects", [ IsObject, IsObject ], _REQUIRE_MUTABLE_SET);
InstallOtherMethod( IntersectSet, "for two objects", [ IsObject, IsObject ], _REQUIRE_MUTABLE_SET);
InstallOtherMethod( SubtractSet, "for two objects", [ IsObject, IsObject ], _REQUIRE_MUTABLE_SET);
42 changes: 42 additions & 0 deletions tst/testinstall/set.tst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,48 @@ gap> HasIsSSortedList(c) and IsSSortedList(c);
true
gap> AddSet(a,(5,6));

#
gap> AddSet(Immutable([]), 1);
Error, <set> must be a mutable proper set
gap> AddSet(fail, 1);
Error, <set> must be a mutable proper set
gap> AddSet([2,1], 1);
Error, ADD_SET: <set> must be a mutable proper set (not a non-strictly-sorted \
plain list of cyclotomics)

#
gap> RemoveSet(Immutable([]), 1);
Error, <set> must be a mutable proper set
gap> RemoveSet(fail, 1);
Error, <set> must be a mutable proper set
gap> RemoveSet([2,1], 1);
Error, REM_SET: <set> must be a mutable proper set (not a non-strictly-sorted \
plain list of cyclotomics)

#
gap> UniteSet(Immutable([]), 1);
Error, <set> must be a mutable proper set
gap> UniteSet(fail, 1);
Error, <set> must be a mutable proper set
gap> UniteSet([2,1], 1);
Error, <set> must be a mutable proper set

#
gap> IntersectSet(Immutable([]), 1);
Error, <set> must be a mutable proper set
gap> IntersectSet(fail, 1);
Error, <set> must be a mutable proper set
gap> IntersectSet([2,1], 1);
Error, <set> must be a mutable proper set

#
gap> SubtractSet(Immutable([]), 1);
Error, <set> must be a mutable proper set
gap> SubtractSet(fail, 1);
Error, <set> must be a mutable proper set
gap> SubtractSet([2,1], 1);
Error, <set> must be a mutable proper set

#gap> HasIsSSortedList(a) and IsSSortedList(a);
#true
gap> c:=Union(a,[(1,2),(1,2,3)]);
Expand Down

0 comments on commit 5f4fdd4

Please sign in to comment.