-
Notifications
You must be signed in to change notification settings - Fork 117
Add deterministic random class #1255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Fixture Monkey | ||
| * | ||
| * Copyright (c) 2021-present NAVER Corp. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.navercorp.fixturemonkey.api.random; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Random; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.apiguardian.api.API; | ||
| import org.apiguardian.api.API.Status; | ||
|
|
||
| @API(since = "1.1.16", status = Status.MAINTAINED) | ||
| public class DeterministicRandom extends Random { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to create a new interface for obtaining the It is to avoid using the |
||
| private static final Map<Long, Random> SEED_CACHE = new ConcurrentHashMap<>(); | ||
| private static final int MAX_CACHE_SIZE = 32; | ||
|
|
||
| private long currentSeed; | ||
|
|
||
| public DeterministicRandom(long seed) { | ||
| super(seed); | ||
| this.currentSeed = seed; | ||
| } | ||
|
|
||
| @Override | ||
| public long nextLong() { | ||
| long value = super.nextLong(); | ||
| this.currentSeed = value; | ||
|
|
||
| if (!SEED_CACHE.containsKey(value)) { | ||
| manageCacheSize(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's necessary to extract the method; it would be better to inline it. |
||
| SEED_CACHE.put(value, new Random(value)); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| public Random getRandomInstance(long seed) { | ||
| return SEED_CACHE.get(seed); | ||
| } | ||
|
|
||
| public Random getCurrentSeedRandom() { | ||
| return SEED_CACHE.get(this.currentSeed); | ||
| } | ||
|
|
||
| private static void manageCacheSize() { | ||
| if (SEED_CACHE.size() >= MAX_CACHE_SIZE) { | ||
| SEED_CACHE.clear(); | ||
| } | ||
| } | ||
|
|
||
| public static Map<Long, Random> getSeedCache() { | ||
| return SEED_CACHE; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package com.navercorp.fixturemonkey.api.random; | ||
|
|
||
| import static org.assertj.core.api.BDDAssertions.then; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| import net.jqwik.api.ForAll; | ||
| import net.jqwik.api.Property; | ||
| import net.jqwik.api.constraints.LongRange; | ||
|
|
||
| class DeterministicRandomTest { | ||
|
|
||
| @Property | ||
| void nextLongCreatesRandomInstanceInCache(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
|
|
||
| // when | ||
| long seed = deterministicRandom.nextLong(); | ||
|
|
||
| // then | ||
| then(DeterministicRandom.getSeedCache()).containsKey(seed); | ||
| then(DeterministicRandom.getSeedCache().get(seed)).isNotNull(); | ||
| } | ||
|
|
||
| @Property | ||
| void getRandomInstanceRetrievesCachedInstance(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
| long seed = deterministicRandom.nextLong(); | ||
|
|
||
| // when | ||
| Random cachedRandom = deterministicRandom.getRandomInstance(seed); | ||
|
|
||
| // then | ||
| then(cachedRandom).isNotNull(); | ||
| then(cachedRandom).isSameAs(DeterministicRandom.getSeedCache().get(seed)); | ||
| } | ||
|
|
||
| @Property | ||
| void getCurrentSeedRandomReturnsCurrentSeedRandom(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
|
|
||
| // when | ||
| long firstSeed = deterministicRandom.nextLong(); | ||
| Random firstRandom = deterministicRandom.getCurrentSeedRandom(); | ||
|
|
||
| long secondSeed = deterministicRandom.nextLong(); | ||
| Random secondRandom = deterministicRandom.getCurrentSeedRandom(); | ||
|
|
||
| // then | ||
| then(firstRandom).isSameAs(deterministicRandom.getRandomInstance(firstSeed)); | ||
| then(secondRandom).isSameAs(deterministicRandom.getRandomInstance(secondSeed)); | ||
| then(firstRandom).isNotSameAs(secondRandom); | ||
| } | ||
|
|
||
| @Property | ||
| void cacheSizeIsLimitedTo32(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
|
|
||
| // when | ||
| for (int i = 0; i < 35; i++) { | ||
| deterministicRandom.nextLong(); | ||
| } | ||
|
|
||
| // then | ||
| then(DeterministicRandom.getSeedCache().size()).isLessThanOrEqualTo(32); | ||
| } | ||
|
|
||
| @Property | ||
| void sameInitialSeedProducesSameLongSequence(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom random1 = new DeterministicRandom(initialSeed); | ||
| DeterministicRandom random2 = new DeterministicRandom(initialSeed); | ||
|
|
||
| // when | ||
| long value1 = random1.nextLong(); | ||
| long value2 = random2.nextLong(); | ||
|
|
||
| // then | ||
| then(value1).isEqualTo(value2); | ||
| } | ||
|
|
||
| @Property | ||
| void cachedRandomInstanceProducesDeterministicValues(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
| long seed = deterministicRandom.nextLong(); | ||
|
|
||
| // when | ||
| Random cached1 = deterministicRandom.getRandomInstance(seed); | ||
| Random cached2 = deterministicRandom.getRandomInstance(seed); | ||
|
|
||
| // then | ||
| then(cached1).isSameAs(cached2); | ||
| int value1 = cached1.nextInt(); | ||
| int value2 = cached2.nextInt(); | ||
| then(value1).isNotEqualTo(value2); // Same instance, so state progresses | ||
| } | ||
|
|
||
| @Property | ||
| void multipleSeedsAreStoredIndependently(@ForAll @LongRange(min = 1L) long initialSeed) { | ||
| // given | ||
| DeterministicRandom.getSeedCache().clear(); | ||
| DeterministicRandom deterministicRandom = new DeterministicRandom(initialSeed); | ||
|
|
||
| // when | ||
| long seed1 = deterministicRandom.nextLong(); | ||
| long seed2 = deterministicRandom.nextLong(); | ||
| long seed3 = deterministicRandom.nextLong(); | ||
|
|
||
| // then | ||
| then(DeterministicRandom.getSeedCache()).containsKeys(seed1, seed2, seed3); | ||
| then(deterministicRandom.getRandomInstance(seed1)) | ||
| .isNotSameAs(deterministicRandom.getRandomInstance(seed2)); | ||
| then(deterministicRandom.getRandomInstance(seed2)) | ||
| .isNotSameAs(deterministicRandom.getRandomInstance(seed3)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be
EXPERIMENTAL