diff --git a/common/src/main/java/com/scottlogic/deg/common/profile/DateTimeGranularity.java b/common/src/main/java/com/scottlogic/deg/common/profile/DateTimeGranularity.java index 913b2fa6d..70d4134c8 100644 --- a/common/src/main/java/com/scottlogic/deg/common/profile/DateTimeGranularity.java +++ b/common/src/main/java/com/scottlogic/deg/common/profile/DateTimeGranularity.java @@ -2,7 +2,9 @@ import com.scottlogic.deg.common.ValidationException; import com.scottlogic.deg.common.date.TemporalAdjusterGenerator; +import com.scottlogic.deg.generator.utils.RandomNumberGenerator; +import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; @@ -78,6 +80,19 @@ public OffsetDateTime getPrevious(OffsetDateTime value) { return trimToGranularity(value); } + @Override + public OffsetDateTime getRandom(OffsetDateTime min, OffsetDateTime max, RandomNumberGenerator randomNumberGenerator) { + long generatedLong = (long) randomNumberGenerator.nextDouble(getMilli(min), getMilli(max)); + + OffsetDateTime generatedDate = Instant.ofEpochMilli(generatedLong).atZone(ZoneOffset.UTC).toOffsetDateTime(); + + return trimToGranularity(generatedDate); + } + + private long getMilli(OffsetDateTime date) { + return date.toInstant().toEpochMilli(); + } + private static int nanoToMilli(int nano) { int factor = NANOS_IN_MILLIS; return (nano / factor) * factor; diff --git a/common/src/main/java/com/scottlogic/deg/common/profile/Granularity.java b/common/src/main/java/com/scottlogic/deg/common/profile/Granularity.java index 9bd487792..f70893507 100644 --- a/common/src/main/java/com/scottlogic/deg/common/profile/Granularity.java +++ b/common/src/main/java/com/scottlogic/deg/common/profile/Granularity.java @@ -16,6 +16,8 @@ package com.scottlogic.deg.common.profile; +import com.scottlogic.deg.generator.utils.RandomNumberGenerator; + public interface Granularity { boolean isCorrectScale(T value); @@ -31,4 +33,6 @@ public interface Granularity { default T getNext(T value){ return getNext(value, 1); }; + + T getRandom(T min, T max, RandomNumberGenerator randomNumberGenerator); } diff --git a/common/src/main/java/com/scottlogic/deg/common/profile/NumericGranularity.java b/common/src/main/java/com/scottlogic/deg/common/profile/NumericGranularity.java index 4f82a21e9..f01d8ff6a 100644 --- a/common/src/main/java/com/scottlogic/deg/common/profile/NumericGranularity.java +++ b/common/src/main/java/com/scottlogic/deg/common/profile/NumericGranularity.java @@ -19,6 +19,8 @@ import com.scottlogic.deg.common.ValidationException; import com.scottlogic.deg.common.util.NumberUtils; +import com.scottlogic.deg.generator.utils.RandomNumberGenerator; + import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Objects; @@ -72,6 +74,12 @@ public BigDecimal getNext(BigDecimal value) { return value.add(BigDecimal.ONE.scaleByPowerOfTen(decimalPlaces * -1)); } + @Override + public BigDecimal getRandom(BigDecimal min, BigDecimal max, RandomNumberGenerator randomNumberGenerator) { + BigDecimal value = randomNumberGenerator.nextBigDecimal(max, max); + return trimToGranularity(value); + } + @Override public BigDecimal getPrevious(BigDecimal value) { if (!isCorrectScale(value)){ diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluator.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluator.java index 496cbaa08..1b95d6f20 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluator.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluator.java @@ -19,7 +19,7 @@ import com.scottlogic.deg.common.profile.FieldType; import com.scottlogic.deg.generator.fieldspecs.FieldSpec; import com.scottlogic.deg.generator.generation.fieldvaluesources.*; -import com.scottlogic.deg.generator.generation.fieldvaluesources.datetime.DateTimeFieldValueSource; +import com.scottlogic.deg.generator.generation.fieldvaluesources.LinearFieldValueSource; import com.scottlogic.deg.generator.generation.string.generators.RegexStringGenerator; import com.scottlogic.deg.generator.generation.string.generators.StringGenerator; import com.scottlogic.deg.generator.restrictions.*; @@ -28,6 +28,7 @@ import java.math.BigDecimal; import java.time.OffsetDateTime; import java.util.*; +import java.util.stream.Collectors; import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createDateTimeRestrictions; import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createNumericRestrictions; @@ -65,21 +66,20 @@ private Optional getSource(FieldType type, FieldSpec fieldSpec private FieldValueSource getRestrictionSource(FieldType type, FieldSpec fieldSpec) { switch (type) { - case DATETIME: - return getDateTimeSource(fieldSpec); case STRING: return getStringSource(fieldSpec); + case DATETIME: case NUMERIC: - return getNumericSource(fieldSpec); + return getLinearSource(fieldSpec); default: throw new UnsupportedOperationException("unexpected type"); } } - private FieldValueSource getNumericSource(FieldSpec fieldSpec) { - LinearRestrictions restrictions = (LinearRestrictions) fieldSpec.getRestrictions(); - - return new RealNumberFieldValueSource(restrictions, fieldSpec.getBlacklist()); + private > FieldValueSource getLinearSource(FieldSpec fieldSpec) { + LinearRestrictions restrictions = (LinearRestrictions) fieldSpec.getRestrictions(); + Set blacklist = fieldSpec.getBlacklist().stream().map(d -> (T) d).collect(Collectors.toSet()); + return new LinearFieldValueSource(restrictions, blacklist); } private FieldValueSource getStringSource(FieldSpec fieldSpec) { @@ -93,10 +93,4 @@ private FieldValueSource getStringSource(FieldSpec fieldSpec) { return generator; } - - private FieldValueSource getDateTimeSource(FieldSpec fieldSpec) { - LinearRestrictions restrictions = (LinearRestrictions) fieldSpec.getRestrictions(); - - return new DateTimeFieldValueSource(restrictions, fieldSpec.getBlacklist()); - } } diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/LinearFieldValueSource.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/LinearFieldValueSource.java new file mode 100644 index 000000000..252d2b0be --- /dev/null +++ b/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/LinearFieldValueSource.java @@ -0,0 +1,80 @@ +/* + * Copyright 2019 Scott Logic Ltd + * + * 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.scottlogic.deg.generator.generation.fieldvaluesources; + +import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions; +import com.scottlogic.deg.generator.utils.RandomNumberGenerator; + +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.scottlogic.deg.generator.utils.SetUtils.stream; + +public class LinearFieldValueSource> implements FieldValueSource { + + private final LinearRestrictions restrictions; + private final Set blacklist; + + public LinearFieldValueSource(LinearRestrictions restrictions, Set blacklist) { + this.restrictions = restrictions; + this.blacklist = blacklist.stream() + .map(i -> restrictions.getGranularity().trimToGranularity(i)) + .collect(Collectors.toSet()); + } + + @Override + public Stream generateAllValues() { + return stream(new LinearIterator<>(restrictions)) + .filter(this::notInBlacklist); + } + + @Override + public Stream generateInterestingValues() { + return Stream.of(restrictions.getMin(), restrictions.getMax()) + .distinct() + .filter(this::notInBlacklist); + } + + @Override + public Stream generateRandomValues(RandomNumberGenerator randomNumberGenerator) { + return Stream.generate(() -> restrictions.getGranularity() + .getRandom(restrictions.getMin(), restrictions.getMax(), randomNumberGenerator)) + .filter(this::notInBlacklist); + } + + // we call this instead of .contains because .contains doesn't treat 2 and 2.0 as equal + private boolean notInBlacklist(T t) { + return blacklist.stream().noneMatch(x->x.compareTo(t)==0); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + LinearFieldValueSource otherSource = (LinearFieldValueSource) obj; + return restrictions.equals(otherSource.restrictions) && + blacklist.equals(otherSource.blacklist); + } + + @Override + public int hashCode() { + return Objects.hash(restrictions, blacklist); + } +} diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSource.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSource.java deleted file mode 100644 index b0584a5c3..000000000 --- a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSource.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2019 Scott Logic Ltd - * - * 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.scottlogic.deg.generator.generation.fieldvaluesources; - -import com.scottlogic.deg.common.util.Defaults; -import com.scottlogic.deg.common.util.NumberUtils; -import com.scottlogic.deg.generator.restrictions.linear.Limit; -import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions; -import com.scottlogic.deg.generator.utils.*; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -import static com.scottlogic.deg.generator.utils.SetUtils.stream; - -public class RealNumberFieldValueSource implements FieldValueSource { - private final Set blacklist; - private final LinearRestrictions restrictions; - - public RealNumberFieldValueSource(LinearRestrictions restrictions, Set blacklist) { - this.restrictions = restrictions; - - this.blacklist = blacklist.stream() - .map(NumberUtils::coerceToBigDecimal) - .filter(Objects::nonNull) - .map(i -> restrictions.getGranularity().trimToGranularity(i)) - .filter(i -> restrictions.getMin().compareTo(i) <= 0 && i.compareTo(restrictions.getMax()) <= 0) - .collect(Collectors.toSet()); - } - - @Override - public Stream generateInterestingValues() { - return Stream.of(restrictions.getMin(), BigDecimal.ZERO, restrictions.getMax()) - .distinct() - .filter(restrictions::match) - .filter(this::notInBlacklist); - } - - @Override - public Stream generateAllValues() { - return stream(new LinearIterator<>(restrictions)) - .filter(this::notInBlacklist); - } - - @Override - public Stream generateRandomValues(RandomNumberGenerator randomNumberGenerator) { - return Stream.generate(() -> - randomNumberGenerator.nextBigDecimal( - restrictions.getMin(), - restrictions.getMax())) - .map(val -> restrictions.getGranularity().trimToGranularity(val)) - .filter(this::notInBlacklist); - } - - private boolean notInBlacklist(BigDecimal i) { - return blacklist.stream().noneMatch(x->x.compareTo(i)==0); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) return false; - - RealNumberFieldValueSource otherSource = (RealNumberFieldValueSource) obj; - return Objects.equals(restrictions, otherSource.restrictions) && Objects.equals(blacklist, otherSource.blacklist); - } - - @Override - public int hashCode() { - return Objects.hash(restrictions, blacklist); - } - - private Stream streamOf(Iterable iterable){ - return StreamSupport.stream(iterable.spliterator(), false); - } -} \ No newline at end of file diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSource.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSource.java deleted file mode 100644 index 73e782869..000000000 --- a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSource.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2019 Scott Logic Ltd - * - * 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.scottlogic.deg.generator.generation.fieldvaluesources.datetime; - -import com.scottlogic.deg.common.util.Defaults; -import com.scottlogic.deg.generator.generation.fieldvaluesources.FieldValueSource; -import com.scottlogic.deg.generator.generation.fieldvaluesources.LinearIterator; -import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions; -import com.scottlogic.deg.generator.utils.RandomNumberGenerator; - -import java.math.BigDecimal; -import java.time.*; -import java.util.ArrayList; -import java.util.Objects; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Stream; - -import static com.scottlogic.deg.generator.utils.Defaults.DATETIME_MAX_LIMIT; -import static com.scottlogic.deg.generator.utils.Defaults.DATETIME_MIN_LIMIT; -import static com.scottlogic.deg.generator.utils.SetUtils.stream; - -public class DateTimeFieldValueSource implements FieldValueSource { - - private final LinearRestrictions restrictions; - private final Set blacklist; - - private final RandomDateGenerator randomDateGenerator; - - public DateTimeFieldValueSource( - LinearRestrictions restrictions, - Set blacklist) { - this.restrictions = restrictions; - this.blacklist = blacklist; - - this.randomDateGenerator = new RandomDateGenerator(restrictions); - } - - @Override - public Stream generateAllValues() { - return stream(new LinearIterator<>(restrictions)) - .filter(i -> !blacklist.contains(i)); - } - - @Override - public Stream generateInterestingValues() { - return Stream.of(restrictions.getMin(), restrictions.getMax()) - .distinct() - .filter(i -> !blacklist.contains(i)); - } - - @Override - public Stream generateRandomValues(RandomNumberGenerator randomNumberGenerator) { - return Stream.generate(() -> randomDateGenerator.next(randomNumberGenerator)) - .filter(i -> !blacklist.contains(i)); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) return false; - - DateTimeFieldValueSource otherSource = (DateTimeFieldValueSource) obj; - return restrictions.equals(otherSource.restrictions) && - blacklist.equals(otherSource.blacklist); - } - - private static boolean equals(OffsetDateTime x, OffsetDateTime y) { - if (x == null && y == null) { - return true; - } - - if (x == null || y == null) { - return false; //either x OR y is null, but not both (XOR) - } - - return x.equals(y); - } - - @Override - public int hashCode() { - return Objects.hash(restrictions, blacklist); - } -} diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/RandomDateGenerator.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/RandomDateGenerator.java deleted file mode 100644 index fd83ccb6a..000000000 --- a/generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/RandomDateGenerator.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2019 Scott Logic Ltd - * - * 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.scottlogic.deg.generator.generation.fieldvaluesources.datetime; -import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions; -import com.scottlogic.deg.generator.utils.RandomNumberGenerator; - -import java.math.BigDecimal; -import java.time.*; -import java.util.Iterator; - -class RandomDateGenerator { - - private final LinearRestrictions restrictions; - - RandomDateGenerator(LinearRestrictions restrictions) { - this.restrictions = restrictions; - } - - public OffsetDateTime next(RandomNumberGenerator random) { - long min = getMilli(restrictions.getMin()); - long max = getMilli(restrictions.getMax()); - - long generatedLong = (long) random.nextDouble(min, max); - - OffsetDateTime generatedDate = Instant.ofEpochMilli(generatedLong).atZone(ZoneOffset.UTC).toOffsetDateTime(); - - return restrictions.getGranularity().trimToGranularity(generatedDate); - } - - private long getMilli(OffsetDateTime date) { - return date.toInstant().toEpochMilli(); - } -} diff --git a/generator/src/main/java/com/scottlogic/deg/generator/utils/JavaUtilRandomNumberGenerator.java b/generator/src/main/java/com/scottlogic/deg/generator/utils/JavaUtilRandomNumberGenerator.java index 3bad7d0d4..1ecff86c5 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/utils/JavaUtilRandomNumberGenerator.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/utils/JavaUtilRandomNumberGenerator.java @@ -64,7 +64,9 @@ public int nextInt(int lowerInclusive, int upperExclusive) { @Override public double nextDouble(double lowerInclusive, double upperExclusive) { - return random.nextDouble() * (upperExclusive - lowerInclusive) + lowerInclusive; + return random.nextDouble() + * (upperExclusive - lowerInclusive) + + lowerInclusive; } @Override diff --git a/generator/src/test/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluatorTests.java b/generator/src/test/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluatorTests.java index 51419e154..80398075f 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluatorTests.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/generation/FieldValueSourceEvaluatorTests.java @@ -203,7 +203,6 @@ void getFieldValueSources_fieldSpecContainsNumericRestrictionsWithMinAndMaxNull_ final List expectedValues = Arrays.asList( new BigDecimal("-1E+20"), - new BigDecimal("0"), new BigDecimal("1E+20") ); Assert.assertEquals(expectedValues, valuesFromResult); diff --git a/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSourceTests.java b/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSourceTests.java index 10746189b..64b6584e1 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSourceTests.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSourceTests.java @@ -103,16 +103,6 @@ void whenBlacklistHasNoValuesInRange() { expectAllValues(3, 4, 5); } - @Test - void whenBlacklistContainsNonIntegralValues() { - givenLowerBound(3, true); - givenUpperBound(6, true); - - givenBlacklist("hello", 4, new BigDecimal(5)); - - expectAllValues(3, 6); - } - @Test void whenBlacklistContainsAllValuesInRange() { givenLowerBound(3, true); @@ -151,7 +141,7 @@ void shouldSupplyInterestingValues() { givenUpperBound(10, true); givenScale(1); - expectInterestingValues("-10", "0", "10"); + expectInterestingValues("-10", "10"); } @Test @@ -244,15 +234,13 @@ void shouldSupplyToLowerBoundary() { givenUpperBound(4, true); expectInterestingValues( - Defaults.NUMERIC_MIN, - 0, 4); + Defaults.NUMERIC_MIN, 4); } @Test void shouldSupplyToBoundary() { expectInterestingValues( Defaults.NUMERIC_MIN, - 0, Defaults.NUMERIC_MAX ); } @@ -283,12 +271,12 @@ void shouldNotEmitInterestingValueTwiceWhenBoundsPermitOnlyOneValueIncluding0(){ @Test public void shouldBeEqualWhenAllPropertiesMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); + toBlacklist(Arrays.asList(1, 2))); Assert.assertThat(a, equalTo(b)); Assert.assertThat(a.hashCode(), equalTo(b.hashCode())); @@ -296,12 +284,12 @@ public void shouldBeEqualWhenAllPropertiesMatch(){ @Test public void shouldBeEqualWhenAllPropertiesMatchBlacklistInDifferentOrder(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(2, 1))); + toBlacklist(Arrays.asList(2, 1))); Assert.assertThat(a, equalTo(b)); Assert.assertThat(a.hashCode(), equalTo(b.hashCode())); @@ -309,10 +297,10 @@ public void shouldBeEqualWhenAllPropertiesMatchBlacklistInDifferentOrder(){ @Test public void shouldBeEqualWhenAllPropertiesMatchBlacklistEmpty(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 10, 1), Collections.emptySet()); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), Collections.emptySet()); @@ -322,72 +310,76 @@ public void shouldBeEqualWhenAllPropertiesMatchBlacklistEmpty(){ @Test public void shouldBeEqualWhenAllPropertiesExceptMinMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(5, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); + toBlacklist(Arrays.asList(1, 2))); Assert.assertThat(a, not(equalTo(b))); } + private Set toBlacklist(Collection asList) { + return asList.stream().map(NumberUtils::coerceToBigDecimal).collect(Collectors.toSet()); + } + @Test public void shouldBeEqualWhenAllPropertiesExceptMaxMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 20, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); + toBlacklist(Arrays.asList(1, 2))); Assert.assertThat(a, not(equalTo(b))); } @Test public void shouldBeEqualWhenAllPropertiesExceptBlacklistMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(3, 4))); + toBlacklist(Arrays.asList(3, 4))); Assert.assertThat(a, not(equalTo(b))); } @Test public void shouldBeEqualWhenAllPropertiesExceptScaleMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 2), - new HashSet<>(Arrays.asList(1, 2))); + toBlacklist(Arrays.asList(1, 2))); Assert.assertThat(a, not(equalTo(b))); } @Test public void shouldBeEqualWhenAllPropertiesExceptMinAndMaxMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(5, 20, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 1), - new HashSet<>(Arrays.asList(1, 2))); + toBlacklist(Arrays.asList(1, 2))); Assert.assertThat(a, not(equalTo(b))); } @Test public void shouldBeEqualWhenAllPropertiesDontMatch(){ - RealNumberFieldValueSource a = new RealNumberFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( numericRestrictions(5, 20, 1), - new HashSet<>(Arrays.asList(1, 2))); - RealNumberFieldValueSource b = new RealNumberFieldValueSource( + toBlacklist(Arrays.asList(1, 2))); + LinearFieldValueSource b = new LinearFieldValueSource( numericRestrictions(1, 10, 2), - new HashSet<>(Arrays.asList(3, 4))); + toBlacklist(Arrays.asList(3, 4))); Assert.assertThat(a, not(equalTo(b))); } @@ -398,7 +390,7 @@ public void interestingValuesInclusively_UpperLimitLargerThanConfig_IncludesConf givenUpperBound(1e30, true); givenScale(0); - expectInterestingValues(new BigDecimal("1e20"), "0", "-10"); + expectInterestingValues(new BigDecimal("1e20"), "-10"); } @Test @@ -417,7 +409,7 @@ public void interestingValuesExclusively_UpperLimitLargerThanConfig_IncludesConf givenUpperBound(1e30, false); givenScale(0); - expectInterestingValues("100000000000000000000", "0", "-9"); + expectInterestingValues("100000000000000000000", "-9"); } @Test @@ -436,7 +428,7 @@ public void interestingValuesInclusively_LowerLimitSmallerThanConfig_IncludesCon givenUpperBound(10, true); givenScale(0); - expectInterestingValues(new BigDecimal("-1e20"), "0", "10"); + expectInterestingValues(new BigDecimal("-1e20"), "10"); } @Test @@ -470,7 +462,7 @@ private LinearRestrictions numericRestrictions(Integer min, Integer private Limit lowerLimit = NUMERIC_MIN_LIMIT; private int scale; private Set blacklist = new HashSet<>(); - private RealNumberFieldValueSource objectUnderTest; + private LinearFieldValueSource objectUnderTest; private void givenLowerBound(Object limit, boolean isInclusive) { givenLowerBound(NumberUtils.coerceToBigDecimal(limit), isInclusive); @@ -521,7 +513,7 @@ private FieldValueSource getObjectUnderTest() { if (objectUnderTest == null) { LinearRestrictions restrictions = createNumericRestrictions(lowerLimit, upperLimit, new NumericGranularity(scale)); restrictions = (LinearRestrictions) new LinearRestrictionsMerger().merge(restrictions, createNumericRestrictions(NUMERIC_MIN_LIMIT, NUMERIC_MAX_LIMIT)).get(); - objectUnderTest = new RealNumberFieldValueSource(restrictions, blacklist); + objectUnderTest = new LinearFieldValueSource(restrictions, toBlacklist(blacklist)); } return objectUnderTest; diff --git a/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSourceTests.java b/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSourceTests.java index 588eed427..ea41c9b4a 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSourceTests.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/generation/fieldvaluesources/datetime/DateTimeFieldValueSourceTests.java @@ -17,6 +17,7 @@ package com.scottlogic.deg.generator.generation.fieldvaluesources.datetime; import com.scottlogic.deg.common.util.Defaults; +import com.scottlogic.deg.generator.generation.fieldvaluesources.LinearFieldValueSource; import com.scottlogic.deg.generator.restrictions.linear.Limit; import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions; import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory; @@ -42,7 +43,7 @@ public class DateTimeFieldValueSourceTests { private Limit lowerLimit = DATETIME_MIN_LIMIT; private Limit upperLimit = DATETIME_MAX_LIMIT; private Set blackList = new HashSet<>(); - private DateTimeFieldValueSource fieldSource; + private LinearFieldValueSource fieldSource; @Test public void whenGeneratingUnboundSet() { @@ -93,7 +94,7 @@ public void getRandomValues_withExclusiveUpperBound_shouldGenerateCorrectValues( givenUpperBound(OffsetDateTime.of(date.atTime(LocalTime.of(18, 0, 0)), ZoneOffset.UTC), false); LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); TestRandomNumberGenerator rng = new TestRandomNumberGenerator(); rng.setNextDouble(0); @@ -130,7 +131,7 @@ public void getRandomValues_withInclusiveUpperBound_shouldGenerateCorrectValues( LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); TestRandomNumberGenerator rng = new TestRandomNumberGenerator(); rng.setNextDouble(0); @@ -165,7 +166,7 @@ public void getRandomValues_withExclusiveLowerBound_shouldGenerateCorrectValues( LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); TestRandomNumberGenerator rng = new TestRandomNumberGenerator(); rng.setNextDouble(0); @@ -198,7 +199,7 @@ public void getRandomValues_withSmallPermittedRangeAtEndOfLegalRange_shouldGener LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); TestRandomNumberGenerator rng = new TestRandomNumberGenerator(); rng.setNextDouble(0.99); @@ -211,10 +212,10 @@ public void getRandomValues_withSmallPermittedRangeAtEndOfLegalRange_shouldGener @Test public void shouldBeEqualWhenAllPropertiesMatch(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); @@ -224,10 +225,10 @@ public void shouldBeEqualWhenAllPropertiesMatch(){ @Test public void shouldBeEqualWhenAllPropertiesMatchWhenBlacklistInDifferentOrder(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MAX, OffsetDateTime.MIN))); @@ -237,10 +238,10 @@ public void shouldBeEqualWhenAllPropertiesMatchWhenBlacklistInDifferentOrder(){ @Test public void shouldBeEqualWhenAllPropertiesMatchWhenBlacklistEmpty(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), Collections.emptySet()); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), Collections.emptySet()); @@ -250,10 +251,10 @@ public void shouldBeEqualWhenAllPropertiesMatchWhenBlacklistEmpty(){ @Test public void shouldBeUnequalWhenAllPropertiesMatchExceptMin(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-28", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); @@ -262,10 +263,10 @@ public void shouldBeUnequalWhenAllPropertiesMatchExceptMin(){ @Test public void shouldBeUnequalWhenAllPropertiesMatchExceptMax(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-30"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); @@ -274,10 +275,10 @@ public void shouldBeUnequalWhenAllPropertiesMatchExceptMax(){ @Test public void shouldBeUnequalWhenAllPropertiesMatchExceptBlacklist(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Collections.singletonList(OffsetDateTime.MIN))); @@ -286,10 +287,10 @@ public void shouldBeUnequalWhenAllPropertiesMatchExceptBlacklist(){ @Test public void shouldBeUnequalWhenOnlyBlacklistMatches(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-28", "2010-11-30"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); @@ -298,10 +299,10 @@ public void shouldBeUnequalWhenOnlyBlacklistMatches(){ @Test public void shouldBeUnequalWhenAllPropertiesDontMatch(){ - DateTimeFieldValueSource a = new DateTimeFieldValueSource( + LinearFieldValueSource a = new LinearFieldValueSource( restrictions("2001-02-28", "2010-11-30"), new HashSet<>(Arrays.asList(OffsetDateTime.MIN, OffsetDateTime.MAX))); - DateTimeFieldValueSource b = new DateTimeFieldValueSource( + LinearFieldValueSource b = new LinearFieldValueSource( restrictions("2001-02-03", "2010-11-12"), new HashSet<>(Collections.singletonList(OffsetDateTime.MIN))); @@ -316,7 +317,7 @@ public void datetimeGenerateAllValues_withMinSetToMaxDate_emitsNoValues(){ DATETIME_MAX_LIMIT ); - DateTimeFieldValueSource datesAfterLastPermittedDate = new DateTimeFieldValueSource(min, Collections.emptySet()); + LinearFieldValueSource datesAfterLastPermittedDate = new LinearFieldValueSource(min, Collections.emptySet()); //Act Iterator iterator = datesAfterLastPermittedDate.generateAllValues().iterator(); @@ -331,7 +332,7 @@ public void datetimeGenerateAllValues_withMaxSetToMinDate_emitsNoValues(){ DATETIME_MIN_LIMIT, new Limit<>(ISO_MIN_DATE, false) ); - DateTimeFieldValueSource datesBeforeFirstPermittedDate = new DateTimeFieldValueSource(max, Collections.emptySet()); + LinearFieldValueSource datesBeforeFirstPermittedDate = new LinearFieldValueSource(max, Collections.emptySet()); //Act Iterator iterator = datesBeforeFirstPermittedDate.generateAllValues().iterator(); @@ -372,7 +373,7 @@ private void expectAllValues(Object... expectedValuesArray) { List actualValues = new ArrayList<>(); LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); fieldSource.generateAllValues().forEach(actualValues::add); @@ -385,7 +386,7 @@ private void expectInterestingValues(Object... expectedValuesArray) { LinearRestrictions restrictions = LinearRestrictionsFactory.createDateTimeRestrictions(lowerLimit, upperLimit); - fieldSource = new DateTimeFieldValueSource(restrictions, blackList); + fieldSource = new LinearFieldValueSource(restrictions, blackList); fieldSource.generateInterestingValues().forEach(actualValues::add);