The correct version of the code we are testing is in a module named piggy. This module simulates a ceramic piggy bank. It works like this: initially it starts off containing no coins and having no value. As you add coins, the number of coins in the bank increases, and also the total value of the coins increases. Of course, it is limited in size, so there is a maximum number of coins it can hold (we are assuming that all coins take up the same space regardless of their value). Once it reaches that maximum, you can't add any more. Finally, it can be smashed to get the money out. But after it is smashed, it contains no coins and you can't add any more, until you re-initialize the module by calling start again.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

The correct version of the code we are testing is in a module named piggy. This module simulates a ceramic piggy bank. It works like this: initially it starts off containing no coins and having no value. As you add coins, the number of coins in the bank increases, and also the total value of the coins increases. Of course, it is limited in size, so there is a maximum number of coins it can hold (we are assuming that all coins take up the same space regardless of their value). Once it reaches that maximum, you can't add any more. Finally, it can be smashed to get the money out. But after it is smashed, it contains no coins and you can't add any more, until you re-initialize the module by calling start again.

The module has the following functions:

start(max) - initializes the piggy bank so it can hold the given maximum number of coins. Initially it contains no coins and has zero value and is not smashed.

add_coin(coin_value) - adds a coin with the given value to the piggy bank, unless it is full or smashed. The coin value can be anything

get_value() - returns the total value of all the coins in the piggy bank

get_num_coins() - returns the total number of coins in the piggy bank

smash() - smashes the piggy bank, after which it has no coins and no coins can be added

For example, we should be able to write code like this:

import piggy as p # initialize the piggy bank to have a capacity of 5 coins p.start(5) print(p.get_num_coins()) # expected 0 print(p.get_value()) # expected 0 # add a couple of coins p.add_coin(10) p.add_coin(25) # check what we've got print(p.get_num_coins()) # expected 2 print(p.get_value()) # expected 35

Bad piggies

There are also six implementations of the piggy bank specification that have errors in the code, named piggy1 through piggy6. Your task is to write a set of test cases, similar to the ones above, so that the original piggy module passes all your tests, but each of piggy1 through piggy6 fails at least one of your test cases.

To simplify the whole process of testing and checking, we are going to do two things

  1. Notice the form of the import statement above. Instead of

import piggy

we write

import piggy as p

Then, we can refer to the piggy module simply as p throughout our code. The advantage of this is that we can test a different module just by changing the import.

  1. Use a helper function to check whether we're getting correct values. Instead of simply printing the output as above, and then having to carefully read it to see if it's correct, we'll just pass the value we expect into the helper function to be checked, along with a description of what we're testing.

Take a look at the template in the code editor below, and try running it (in "Develop" mode). Then, modify the import statement:

import piggy1 as p

and run it again. Notice it is obvious that piggy1 has a bug, since the actual value returned by getValue doesn't match the expected. value 35.

Value after add_coin(25): expected 35, found 25

Please solve in python :)

11.py
iplate...
1 # Edit here to import piggy1, piggy2, etc
2 import piggy as p
3 #import piggy1 as p
4
5
6 # Call this method to simplify checking results.
7 def check(description, expected, actual):
print(description, end=': ')
if (expected
print("OK")
else:
8
9.
actual):
10
11
12
print("expected", expected, ", found", actual)
13
14 # A few sample tests
15 p.start(5)
16 check("Initial coins after start", 0, p.get_num_coins())
17 check("Initial value after start", 0, p.get_value())
18
19 p.add_coin(10)
20 check("Coins after add_coin(10)", 1, p.get_num_coins())
21 check("Value after add_coin(10)", 10, p.get_value())
22
23 p.add_coin(25)
24 check("Coins after add_coin(25)", 2, p.get_num_coins(O)
25 check("Value after add_coin(25)", 35, p.get_value())
26
27 p.smash()
28 check("Coins after smash", 0,
29 check("Value after smash", 0, p.get_value())
%3D
p.get_num_coins())
30
Transcribed Image Text:11.py iplate... 1 # Edit here to import piggy1, piggy2, etc 2 import piggy as p 3 #import piggy1 as p 4 5 6 # Call this method to simplify checking results. 7 def check(description, expected, actual): print(description, end=': ') if (expected print("OK") else: 8 9. actual): 10 11 12 print("expected", expected, ", found", actual) 13 14 # A few sample tests 15 p.start(5) 16 check("Initial coins after start", 0, p.get_num_coins()) 17 check("Initial value after start", 0, p.get_value()) 18 19 p.add_coin(10) 20 check("Coins after add_coin(10)", 1, p.get_num_coins()) 21 check("Value after add_coin(10)", 10, p.get_value()) 22 23 p.add_coin(25) 24 check("Coins after add_coin(25)", 2, p.get_num_coins(O) 25 check("Value after add_coin(25)", 35, p.get_value()) 26 27 p.smash() 28 check("Coins after smash", 0, 29 check("Value after smash", 0, p.get_value()) %3D p.get_num_coins()) 30
get_value() - returns the total value of all the coins in the piggy bank
get_num_coins () - returns the total number of coins in the piggy bank
smash()- smashes the piggy bank, after which it has no coins and no coins can be added
For example, we should be able to write code like this:
import piggy as p
# initialize the piggy bank to have a capacity of 5 coins
p.start(5)
print (p.get_num_coins () )
# expected 0
print (p.get_value ())
# expected 0
# add a couple of coins
p.add_coin(10)
p.add_coin (25)
# check what we've got
print (p.get_num_coins () )
# expected 2
print (p.get_value ())
# expected 35
Bad piggies
There are also six implementations of the piggy bank specification that have errors in the code, named piggy1 through piggy6. Your task is
to write a set of test cases, similar to the ones above, so that the original piggy module passes all your tests, but each of piggy1 through
piggy6 fails at least one of your test cases.
To simplify the whole process of testing and checking, we are going to do two things
1. Notice the form of the import statement above. Instead of
import piggY
Transcribed Image Text:get_value() - returns the total value of all the coins in the piggy bank get_num_coins () - returns the total number of coins in the piggy bank smash()- smashes the piggy bank, after which it has no coins and no coins can be added For example, we should be able to write code like this: import piggy as p # initialize the piggy bank to have a capacity of 5 coins p.start(5) print (p.get_num_coins () ) # expected 0 print (p.get_value ()) # expected 0 # add a couple of coins p.add_coin(10) p.add_coin (25) # check what we've got print (p.get_num_coins () ) # expected 2 print (p.get_value ()) # expected 35 Bad piggies There are also six implementations of the piggy bank specification that have errors in the code, named piggy1 through piggy6. Your task is to write a set of test cases, similar to the ones above, so that the original piggy module passes all your tests, but each of piggy1 through piggy6 fails at least one of your test cases. To simplify the whole process of testing and checking, we are going to do two things 1. Notice the form of the import statement above. Instead of import piggY
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY