-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Integers to access a random source
- Loading branch information
1 parent
67a4114
commit 0f92231
Showing
3 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
gap> START_TEST("random.tst"); | ||
gap> Read( Filename( DirectoriesLibrary( "tst" ), "testrandom.g" ) ); | ||
gap> randomTest(Integers, Random); | ||
gap> randomTest([1..10], Random); | ||
gap> randomTest([1,-6,"cheese", Group(())], Random); | ||
gap> STOP_TEST("random.tst", 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
randomTest := function(collection, method) | ||
local test1, test2, test3, test4, test5, test6, localgen; | ||
# Firstly, we will generate a base list | ||
Init(GlobalMersenneTwister, 6); | ||
test1 := List([1..1000], x -> method(collection)); | ||
# test2 should = test1 | ||
Init(GlobalMersenneTwister, 6); | ||
test2 := List([1..1000], x -> method(collection)); | ||
# test3 should also = test1 | ||
Init(GlobalMersenneTwister, 6); | ||
test3 := List([1..1000], x -> method(GlobalMersenneTwister, collection)); | ||
# test4 should be different (as it came from a different seed) | ||
Init(GlobalMersenneTwister, 8); | ||
test4 := List([1..1000], x -> method(collection)); | ||
# test5 should be the same as test4, as it is made from seed 8 | ||
# test6 should be the same as test1. Also, it checks that making test5 | ||
# did not touch the global source at all. | ||
Init(GlobalMersenneTwister, 8); | ||
localgen := RandomSource(IsMersenneTwister, 6); | ||
test5 := List([1..1000], x -> method(localgen, collection)); | ||
test6 := List([1..1000], x -> method(collection)); | ||
if ForAny(Concatenation(test1, test2, test3, test4, test5, test6), x -> not (x in collection) ) then | ||
Print("Random member outside collection: ", collection,"\n"); | ||
fi; | ||
if test1 <> test2 then | ||
Print("Random not repeatable: ", collection, "\n"); | ||
fi; | ||
if test2 <> test3 then | ||
Print("Random 2-arg vs 1-arg broken: ", collection, "\n"); | ||
fi; | ||
if test1 = test4 then | ||
Print("Random not changing with random seed: ", collection, "\n"); | ||
fi; | ||
if test1 <> test5 then | ||
Print("Alt gen broken: ", collection, "\n"); | ||
fi; | ||
if test4 <> test6 then | ||
Print("Random with a passed in seed effects the global generator: ", collection, "\n"); | ||
fi; | ||
end;; | ||
|
||
# A special test for collections of size 1 | ||
randomTestForSizeOneCollection := function(collection, method) | ||
local i, val, localgen, intlist1, intlist2; | ||
if Size(collection) <> 1 then | ||
Print("randomTestForSizeOneCollection is only for collections of size 1"); | ||
return; | ||
fi; | ||
|
||
val := Representative(collection); | ||
|
||
Init(GlobalMersenneTwister, 6); | ||
intlist1 := List([1..1000], x -> Random([1..10])); | ||
|
||
for i in [1..1000] do | ||
if method(collection) <> val then | ||
Print("Random returned something outside collection :", collection, ":", val); | ||
fi; | ||
od; | ||
|
||
for i in [1..1000] do | ||
if method(GlobalMersenneTwister, collection) <> val then | ||
Print("Random returned something outside collection :", collection, ":", val); | ||
fi; | ||
od; | ||
|
||
localgen := RandomSource(IsMersenneTwister, 6); | ||
|
||
Init(GlobalMersenneTwister, 6); | ||
for i in [1..1000] do | ||
if method(localgen, collection) <> val then | ||
Print("Random returned something outside collection :", collection, ":", val); | ||
fi; | ||
od; | ||
|
||
# The previous loop should not have effected GlobalMersenneTwister, | ||
# so this should be the same as intlist1 | ||
intlist2 := List([1..1000], x -> Random([1..10])); | ||
|
||
if intlist1 <> intlist2 then | ||
Print("Random read from local gen effected global gen: ", collection); | ||
fi; | ||
end;; |