Simple Pseudocode: Test Cases for X & Y

  • Thread starter Hooke's Law
  • Start date
In summary: Yes. Let's suppose that y = 0.if (x > 0) return "hello"else if (x < 3) return "bye"elsereturn "nothing"In summary, if x is greater than y, "hello" is returned. If x is less than y + 3, "bye" is returned. If x is equal to y, "nothing" is returned.
  • #1
Hooke's Law
30
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
  • #2
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?
 
  • #3
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.
 
  • #4
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.
 
  • #5
EDIT: Ok. I get why bye is not returned with any number between 0 and 3. So what's next?
 
  • #6
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.
 
  • #7
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?
 
  • #8
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"?
 
  • #9
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?
 

FAQ: Simple Pseudocode: Test Cases for X & Y

1. What is pseudocode?

Pseudocode is a simplified, high-level programming language used to plan and design algorithms before writing actual code. It uses natural language and basic programming constructs to describe the logic of a program without getting into the specifics of a particular programming language.

2. Why is pseudocode useful for writing test cases?

Pseudocode is useful for writing test cases because it allows you to focus on the logic and functionality of the program without getting bogged down by the syntax and details of a specific programming language. This makes it easier to identify potential errors and edge cases in the logic of the program.

3. What are test cases for X and Y in pseudocode?

Test cases for X and Y in pseudocode are specific scenarios or input values that are used to test the functionality of a program. These test cases should cover a range of possible inputs and edge cases to ensure that the program is functioning correctly.

4. How do you write effective test cases in pseudocode?

To write effective test cases in pseudocode, you should start by identifying the different paths and outcomes of your program. Then, create test cases that cover these paths and outcomes, including both valid and invalid inputs. It is also important to test edge cases and boundary values to ensure that the program can handle unexpected inputs.

5. Can pseudocode be used for any programming language?

Yes, pseudocode can be used for any programming language as it is a universal language that focuses on the logic and functionality of a program rather than the syntax of a specific language. However, it is important to keep in mind the limitations and specific features of the programming language you will be using when writing pseudocode for test cases.

Similar threads

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