Tuesday 15 July 2014

testing - Combining three variables in java -



testing - Combining three variables in java -

i have question deal in java.

my code has 3 variables: a,b,c respectively, doubles. sum of variables must equal 1.

i need test possible combinations in these variables, values varying 0.10

example:

combination 1:

a = 0.20 b = 0.20 c = 0.60

combination 2:

a = 0.10 b = 0.10 c = 0.80

and go on.

is there framework or lib automatize kind of test?

while commenter pointed out can accomplished 3-deep nested for loop, there might more elegant solution, perhaps using library combinatoricslib. this:

icombinatoricsvector<bigdecimal> originalvector = factory.createvector(new bigdecimal[] { new bigdecimal("0.0"), new bigdecimal("0.1"), new bigdecimal("0.2"), new bigdecimal("0.3"), new bigdecimal("0.4"), new bigdecimal("0.5"), new bigdecimal("0.6"), new bigdecimal("0.7"), new bigdecimal("0.8"), new bigdecimal("0.9") }); generator<bigdecimal> gen = factory.createpermutationwithrepetitiongenerator(originalvector, 3); (icombinatoricsvector<bigdecimal> perm : gen) { // values in perm.getvalue(0), perm.getvalue(1), , perm.getvalue(2) }

afterward, iterate through each permutation , check sum for loop of depth 1.

note have replaced float/double bigdecimal in order address floating point precision/representation issues. farther note have used string constructor of bigdecimal avoid an insipid form of same precision/representation issues.

java testing combinations

No comments:

Post a Comment