Simple Pseudocode: Test Cases for X & Y

  • Thread starter Thread starter Hooke's Law
  • Start date Start date
AI Thread Summary
The discussion revolves around determining the minimum number of test cases needed to evaluate a pseudocode that returns "hello" or "bye" based on the values of x and y. The participants clarify that to fully test the code, one must consider different ranges of x relative to y. It is established that if x is greater than y, "hello" is returned, while "bye" is returned if x is less than y + 3. The key takeaway is that the least number of test cases corresponds to the smallest set of values for x and y that can cover all possible outcomes of the code. Ultimately, understanding the relationship between x and y is crucial for identifying effective test cases.
Hooke's Law
Messages
30
Reaction score
0

Homework Statement



Pseudocode:

if ( x is greater than y )
then return "hello"
else if ( x is less than y + 3 )
then return "bye"
What is the least number of different test cases that I would use to test this coude(eg. What would you use as values for x and y to fully test this piece of code?

The Attempt at a Solution


I'm thinking [ x = 0 ; y = 0 ] & [ x = 4 ; y = 0 ] , . But I'm confused with what's the "least number of different test cases".

thanks
 
Physics news on Phys.org
Hooke's Law said:

Homework Statement



Pseudocode:

if ( x is greater than y )
then return "hello"
else if ( x is less than y + 3 )
then return "bye"
What is the least number of different test cases that I would use to test this coude(eg. What would you use as values for x and y to fully test this piece of code?

The Attempt at a Solution


I'm thinking [ x = 0 ; y = 0 ] & [ x = 4 ; y = 0 ] , . But I'm confused with what's the "least number of different test cases".

Let's simplify things a bit, and suppose that y = 0.
Code:
if (x > 0)
  return "hello"
else if (x < 3)
  return "bye"

Under what conditions would "hello" be returned? Under what conditions would "bye" be returned? Are there any conditions in which nothing would be returned?
 
Mark44 said:
Under what conditions would "hello" be returned? Under what conditions would "bye" be returned? Are there any conditions in which nothing would be returned?

"hello" would be returned if x = 1,2,3,4...
"bye" would be returned if x = ...-2,-3,-1,0,1, & 2

There are no conditions when nothing is returned.
 
If x is any positive number, not just integers, "hello" is returned.
If x is any negative number or zero, "bye" is returned.

"bye" is NOT returned if x is any number between 0 and 3.
 
EDIT: Ok. I get why bye is not returned with any number between 0 and 3. So what's next?
 
To find out the least number of test cases you can construct a Venn Diagram of each number range associated with each case. Then just pull a random number from each.
 
Hooke's Law said:
EDIT: Ok. I get why bye is not returned with any number between 0 and 3. So what's next?
If you recall I simplified the problem by setting y to 0. Now that you understand the code with y == 0, can you understand the original code?
 
viscousflow said:
To find out the least number of test cases you can construct a Venn Diagram of each number range associated with each case. Then just pull a random number from each.

What do you mean by "each number range associated with each case"?
 
1) Pick a number, say y = 10

case 1 (explicitly)
x>y
if x = 11, 12,13 or 14...
return 'hello'

case 2 (explicitly)
else if x<(10+3)
i.e. x = 12, 11, 10, 9, 8, 7..
return 'bye'

Now combine cases to get a range(sorta like a Venn Diagram - this is my mental picture)
'hello' is returned for all x == 11, 12,13 or 14...
'bye' is only returned for all x == 10, 9, 8, 7..

As you can see the statement 'elseif x<(10+3)' is only active for all numbers less than 11 since case 1 (x>10) is satisfied first, from 11 onwards no matter what.
 
  • #10
viscousflow said:
Now combine cases to get a range(sorta like a Venn Diagram - this is my mental picture)
'hello' is returned for all x == 11, 12,13 or 14...
'bye' is only returned for all x == 10, 9, 8, 7..

Is that what Mark44 meant by combining cases? But I still can't see how to get the least number of cases.
 
  • #11
Do I need to pick other numbers for x and y? Could someone please explain to me what ""least number of different test cases" means? Is it having various numbers for x and y and finding the similar number in them?
 
  • #12
"Least number of test cases" means the smallest number of values of x and y that are needed to exercise each branch in the code.

Going back to the simplified example I gave, where I set y to 0 -
Code:
if (x > 0)
  return "hello"
else if (x < 3)
  return "bye"
If x is in the interval (0, infinity), the code returns the string "hello". Otherwise (if x is in the interval (-infinity, 0], the code returns the string "bye". There are two test cases here. Two values that can be used are x = .5 and x = -1.
Note that the comparison x < 3 in the else clause is a red herring. If x <= 0 it will automatically be less than 3. If x is positive and less than 3, the first branch is chosen (resulting in "hello" being returned) because x is positive. Being less than 3 or greater than 3 doesn't enter into things.

Now, can you apply this reasoning to your original problem?
 

Similar threads

Replies
2
Views
3K
Replies
2
Views
2K
Replies
3
Views
1K
Replies
7
Views
3K
Replies
7
Views
2K
Replies
7
Views
1K
Back
Top