From f1ca39807c0ce65ce34648286209f60c53c13998 Mon Sep 17 00:00:00 2001 From: Paul Daulby Date: Tue, 15 Oct 2019 10:35:33 +0100 Subject: [PATCH] refactor(#1430): replace RealNumberFieldValueSource with LinearFieldValueSource --- .../generation/FieldValueSourceEvaluator.java | 19 ++-- .../LinearFieldValueSource.java | 9 +- .../RealNumberFieldValueSource.java | 97 ------------------ .../FieldValueSourceEvaluatorTests.java | 1 - .../RealNumberFieldValueSourceTests.java | 98 +++++++++---------- 5 files changed, 56 insertions(+), 168 deletions(-) delete mode 100644 generator/src/main/java/com/scottlogic/deg/generator/generation/fieldvaluesources/RealNumberFieldValueSource.java 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 8877d1aae..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 @@ -66,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) { @@ -94,10 +93,4 @@ private FieldValueSource getStringSource(FieldSpec fieldSpec) { return generator; } - - private FieldValueSource getDateTimeSource(FieldSpec fieldSpec) { - LinearRestrictions restrictions = (LinearRestrictions) fieldSpec.getRestrictions(); - Set blacklist = fieldSpec.getBlacklist().stream().map(d -> (OffsetDateTime) d).collect(Collectors.toSet()); - return new LinearFieldValueSource(restrictions, blacklist); - } } 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 index aa0325ac8..3b4346ff6 100644 --- 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 @@ -21,6 +21,7 @@ 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; @@ -30,11 +31,11 @@ public class LinearFieldValueSource> implements FieldVal private final LinearRestrictions restrictions; private final Set blacklist; - public LinearFieldValueSource( - LinearRestrictions restrictions, - Set blacklist) { + public LinearFieldValueSource(LinearRestrictions restrictions, Set blacklist) { this.restrictions = restrictions; - this.blacklist = blacklist; + this.blacklist = blacklist.stream() + .map(i -> restrictions.getGranularity().trimToGranularity(i)) + .collect(Collectors.toSet()); } @Override 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/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;