Skip to content
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

Big decimal range #69

Merged
merged 3 commits into from Dec 25, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -106,7 +106,7 @@ To do so, implement the Processor interface:

And use it:

Fixture.from(SomeClass.class).uses(new MyCustomProcessor()).gimme("someTempalte");
Fixture.from(SomeClass.class).uses(new MyCustomProcessor()).gimme("someTemplate");

The #execute method will be called for each object that Fixture-Factory generates. For instance, if a Client has an Address, the framework will generate the Address, call #execute with the generated Address as argument, set the Address into the Client, call #execute with the generated Client as argument and then return it.

Expand Down
Expand Up @@ -85,17 +85,19 @@ public <T> T generateValue() {
result = new BigDecimal(random.nextDouble(), this.mathContext);

} else {
result = getRandomBigDecimal((BigDecimal) this.range.getStart(), (BigDecimal) this.range.getEnd());

result = getRandomBigDecimalByRange();

}

} else if (this.type.isAssignableFrom(BigInteger.class)) {
if (this.range == null) {
result = new BigInteger(64, random);

} else {
BigDecimal start = new BigDecimal((BigInteger) range.getStart());
BigDecimal end = new BigDecimal((BigInteger) range.getEnd());
result = getRandomBigDecimal(start, end).toBigInteger();

result = getRandomBigDecimalByRange().toBigInteger();

}
}

Expand All @@ -114,4 +116,10 @@ private BigDecimal getRandomBigDecimal(BigDecimal start, BigDecimal end) {
int scale = start.scale() > end.scale() ? start.scale() : end.scale();
return start.add(new BigDecimal(Math.random()).multiply(end.subtract(start))).setScale(scale, RoundingMode.HALF_EVEN);
}

private BigDecimal getRandomBigDecimalByRange() {
final BigDecimal start = new BigDecimal(this.range.getStart().toString());
final BigDecimal end = new BigDecimal(this.range.getEnd().toString());
return getRandomBigDecimal(start, end);
}
}
Expand Up @@ -77,13 +77,35 @@ public void randomBigDecimalWithMathContextTest() {
assertTrue("Generated value should have a precision of 3", ((BigDecimal) value).precision() == 3);
}

@Test
public void randomBigDecimalUsingRange() {
RandomFunction random = new RandomFunction(BigDecimal.class, new Range(1, 1000));
BigDecimal bigDecimal = random.generateValue();

assertNotNull("Generated BigDecimal must not be null", bigDecimal);
assertTrue("Generated value must be a BigDecimal", bigDecimal instanceof BigDecimal);
assertTrue(new BigDecimal("1").compareTo(bigDecimal) != 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe here should be better to check if the generated value is in the range instead of checking if it's not the limit values. Something like:

assertTrue(new BigDecimal("1").compareTo(bigDecimal) >= 1);
assertTrue(new BigDecimal("1000").compareTo(bigDecimal) <= 1000);

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

Yeah I agree, reading BigDecimal comparison is really annoying. But the problem is that the compareTo method will only return (-1, 0, 1) for less than, equals to, greater than the other value.

But I'll come up with some clever idea to make it a little more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csrcordeiro actually is possible to compare exactly the same as with operators, check the Javadoc here: http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#compareTo(java.math.BigDecimal)

You could change your asserts to be like this:

assertTrue(bigDecimal.compareTo(new BigDecimal("1")) >= 0);
assertTrue(bigDecimal.compareTo(new BigDecimal("1000")) <= 0);

The above code is the same as:

assertTrue(number >= 1);
assertTrue(number <= 1000);

assertTrue(new BigDecimal("1000").compareTo(bigDecimal) != -1);
}

@Test
public void randomBigIntegerTest() {
Object value = new RandomFunction(BigInteger.class).generateValue();
assertNotNull("Generated BigInteger must not be null", value);
assertTrue("Generated value is not a BigInteger", value instanceof BigInteger);
}

@Test
public void randomBigIntegerTestUsingRange() {
RandomFunction random = new RandomFunction(BigInteger.class, new Range(1, 1000));
BigInteger bigInteger = random.generateValue();

assertNotNull("Generated BigInteger must not be null", bigInteger);
assertTrue("Generated value is not a BigInteger", bigInteger instanceof BigInteger);
assertTrue(new BigInteger("1").compareTo(bigInteger) != 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about the range check here.

assertTrue(new BigInteger("1000").compareTo(bigInteger) != -1);
}

@Test
public void randomBooleanTest() {
Object value = new RandomFunction(Boolean.class).generateValue();
Expand Down