private MutableAttributeSplitRequest createRequest(final Object value, final int childrenCount) { final IAttributeStorage parentAttributeStorage = NullAttributeStorage.instance; final List<IAttributeStorage> attributeStorages = new ArrayList<IAttributeStorage>(childrenCount); for (int i = 0; i < childrenCount; i++) { attributeStorages.add(NullAttributeStorage.instance); } final MutableAttributeSplitRequest request = new MutableAttributeSplitRequest(parentAttributeStorage, attributeStorages, attribute); request.setValueInitial(value); request.setValueToSplit(value); return request; }
private void test( final BigDecimal valueToSplit, final int childrenCount, final MathContext mathContext) { final String info = "ValueToSplit=" + valueToSplit + ", childrenCount=" + childrenCount + ", mathContext=" + mathContext; final int precision = mathContext.getPrecision(); final BigDecimal splitValueExpected = valueToSplit.divide(BigDecimal.valueOf(childrenCount), mathContext); // Delta: tolerated difference between calculated split value and actual split value // i.e. 1^(-precision) = 1/(1^precision) ... because BigDecimal does not support negative powers final BigDecimal splitValueExpectedDelta = BigDecimal.ONE.divide(BigDecimal.ONE.pow(precision)); // Example: For ValueToSplit=100, childrenCount=33, precision=2 // splitValueExpected = 0.03 // splitValueExpectedDelta = 1^(-2) = 0.01 final MutableAttributeSplitRequest request = createRequest(valueToSplit, childrenCount); BigDecimal splitValueSUM = BigDecimal.ZERO; for (int index = 0; index < childrenCount; index++) { // // Update request current index request.setAttributeStorageCurrent(request.getAttributeStorages().get(index)); request.setAttributeStorageCurrentIndex(index); // // Execute splitting final IAttributeSplitResult result = splitter.split(request); // // Get and check split value final BigDecimal splitValue = (BigDecimal) result.getSplitValue(); assertEquals( "Invalid split value on index=" + index + "\n" + info, splitValueExpected, // expected splitValue, // actual splitValueExpectedDelta // delta ); // // Update SUM of split values splitValueSUM = splitValueSUM.add(splitValue); // // Update: request's ValueToSplit = last split remaining value final BigDecimal remainingValue = (BigDecimal) result.getRemainingValue(); final BigDecimal remainingValueExpected = valueToSplit.subtract(splitValueSUM); assertEquals( "Invalid remaining value on index=" + index + "\n" + info, remainingValueExpected, // expected remainingValue, // actual BigDecimal.ZERO // delta=exact ); request.setValueToSplit(remainingValue); } // // Assume: sum of all split values shall be the same as initial value to split Assert.assertThat( "Invalid splitValues SUM" + "\n" + info, splitValueSUM, // actual Matchers.comparesEqualTo(valueToSplit) // expected ); }