7hacker
7hacker

Reputation: 1988

What is the best way of accessing all combinations

As a job of a tester, one of my concerns is to always ensure complete test coverage. This can get hard since sometimes the number of possible combinations are really a lot. Lets take an example for instance. A good ol' example of making tea

As you can see, now if i want to test all the possible ways to make tea (assuming there is a hypothetical software which allows creation of a variety of teas), then i have to test: 3x2x3x2 combinations = 36, because there are indeed 36 unique ways to make tea

What algorithm is best in such a case. I can see a nested for loop being best. Am I right?

Upvotes: 5

Views: 340

Answers (4)

dfb
dfb

Reputation: 13289

Nested looping can get pretty messy. I've written test generators recursively to avoid this and also generalize to multiple dimensions.

fun build_test_args(possible_values, i, current_values):
     if i>len(possible_values):
         do_test(current_values)
     for value in possible_values[i]:
         cur_list = current_values + [value]
         build_test_args(possible_values, i+1, cur_list)

Upvotes: 1

poy
poy

Reputation: 10507

Basically you are forming a matrix... with each variable being another dimension. You would simply multiply this matrix by itself...

MEANING... nested loops would work well.

Create some arrays... and get cranking!

Upvotes: 0

Jimmy
Jimmy

Reputation: 91472

It can be a bit programming language-dependent... but you're basically looking for the cartesian product of the set of arguments.

for example, in Python

import itertools

for args in itertools.product(
      ['black tea','green tea','white tea'],
      ['milk','water'],
      ['sugar','honey','none'],
      ['iced','hot']
   ): 
   drink_tea(*args)

Upvotes: 2

corsiKa
corsiKa

Reputation: 82579

Yep. The nested for loop is best for doing all possible combinations.

It's worth noting that this isn't feasable for most production code, which often have dozens of significant inputs, each with multiple input families.

Upvotes: 1

Related Questions