EECS 183 Labs
EECS 183 Lab 4: Testing and Functions
Lab due on your scheduled lab day (September 17 - September 23)
Lab accepted for full credit until Tuesday, September 24, 2024, 11:59 pm Eastern
You must complete both part 1 and part 2 to be able to recevie full credit for the lab.
Code autograder direct submission link
Canvas RME quiz direct submission link
In this lab, you are writing code and solving practice exam questions to master the use of conditionals and prepare for the first exam. The main focus of the lab exercise is how to use (call) and test functions. You will have a lot of practice writing the definitions for functions in Project 2. While completing this lab, you will learn the testing tools necessary to master Project 2.
By completing this lab assignment, you will learn:
- To understand how conditionals work in C++.
- To understand how to call and test functions.
- To learn how to write testing code for the functions you will implement when completing projects.
- To promote test-driven development, the good practice of writing tests before code.
- Understand how to read RMEs and utilize them in defining and calling functions.
For all labs in EECS 183, to receive a grade, every student must individually submit the lab assignment, even if you are working in a group.
Starter Files
You can download the starter files using this link.
The IDE setup tutorials for Visual Studio and XCode include a video about how to set up a project using the starter files. You can access the tutorials here:
Lab Assignment Part 1 - lab4.cpp
- Create a new project in Xcode or in Visual Studio. See the links above for creating a new project.
- Add
lab4.cpp
to your project. - Don’t forget to delete
main.cpp
(if you are using Xcode). Otherwise, you will get a build error. - For this portion of the Lab, you are to write the test suites for
isMoveGood()
andannounceRoundWinner()
(only tests, you will not implement these functions). You write:test_isMoveGood()
, andtest_announceRoundWinner()
.
NOTE: The test cases you create in this lab can and should be used for your Project 2 tests. Specifically, the tests you create in this lab should be added to your
test.cpp
. While Project 2 is an individual assignment, the test cases you create with your lab team can be submitted for credit.
Function to test: isMoveGood()
- This function determines whether or not a player’s move is valid. A valid move consists of an ‘r’, ‘p’, or ‘s’ character, corresponding to “rock”, “paper”, and “scissors”, respectively. The uppercase versions of these characters are also considered valid. Any other characters a user enters will be considered invalid.
IMPORTANT: Note that for this project you can assume user input will always be of the data type expected (which is
char
for moves). In other words, as the programmer, you only need to check that values entered are valid moves - you do not need to worry about users enteringintegers
,doubles
,strings
,bools
, or any combination of these. Those are bad inputs and are prohibited by the REQUIRES clause.
-
This function utilizes no other functions.
-
RME and function declaration:
/** * Requires: nothing * Modifies: nothing * Effects: Returns true if and only if move represents a valid move character: * one of 'R', 'r', 'P', 'p', 'S', 's'. Returns false otherwise. */ bool isMoveGood(char move);
- We have provided a stub (empty implementation) for this function that will always return
false
, however (so that the program compiles). You don’t need to implementisMoveGood
in the lab.
Function to write: test_isMoveGood()
-
We have started the function
test_isMoveGood()
and called it withinint main()
. - What moves should you check that are good?
-
What moves should you check that are not good?
- The simplest way to test is to just print out the return values to standard output (
cout
). Below is the beginning of the testing forisMoveGood()
. Note: the followingtest_isMoveGood()
is NOT a complete test suite forisMoveGood()
.
IMPORTANT: Since
isMoveGood()
returns a value of typebool
, this will print a0
forfalse
and a1
fortrue
.
void test_isMoveGood() { cout << "Now testing function isMoveGood()\n"; cout << "'r': Expected: 1, Actual: " << isMoveGood('r') << endl; // TODO: implement more test cases here return; }
- A testing method I prefer is slightly longer to type but much easier to interpret. Basically, just print out tests that fail. This method gives you information on the tests you really need to investigate further.
void test_isMoveGood() { cout << "Now testing function isMoveGood()\n"; if (!isMoveGood('r')) { cout << "FAILED: isMoveGood('r') should have returned 'true'" << endl; } // etc. return; }
- The expression
!isMoveGood('r')
is the equivalent ofisMoveGood('r') != true
. The expressionisMoveGood('r')
is the equivalent ofisMoveGood('r') == true
. However, you should write the first, as comparing thebool
value returned by the function to eithertrue
orfalse
is redundant. See the EECS 183 Style Guide linked in the project specifications for more detail on conditions.
Function to test: annouceRoundWinner()
-
This function announces the name of the round winner. If there is no round winner, it outputs that the round resulted in a draw.
-
This function utilizes no other functions.
-
RME and function declaration:
/** * Requires: winnerName is the name of the player who won the round. * Modifies: cout * Effects: If winnerName is empty, print a message indicating the round * is a draw. Otherwise, print a congratulatory message to the * winner. * Prompt: This round is a draw! * ------------- OR ------------- * [winner_name] wins the round! */ void announceRoundWinner(string winnerName);
- We have provided a stub (empty implementation) for this function (so that the program compiles). You don’t need to implement
announceRoundWinner
in the lab.
Function to write: test_annouceRoundWinner()
-
We have started the function
test_annouceRoundWinner()
do not forget to call it withinint main()
. -
What inputs should you test that display This round is a draw!?
-
What inputs should you test that display [winner_name] wins the round!?
IMPORTANT: Since
announceRoundWinner()
has a return type ofvoid
, you cannot print the non-existent return value from calling the function!.
Bugs To Expose
There are a total of 4 unique bugs to find in our implementations of isMoveGood()
and announceRoundWinner()
. Your tests will need to expose all of the bugs to receive full points for the lab. The autograder will tell you the names of the bugs that you have exposed, from the following set:
- CHECK_ISMOVEGOOD_1
- CHECK_ISMOVEGOOD_2
- CHECK_ANNOUNCEROUNDWINNER_1
- CHECK_ANNOUNCEROUNDWINNER_2
How to Submit
-
When ready to submit to the autograder, visit https://autograder.io/web/project/2675. You will submit your
lab4.cpp
file which should contain your solution for this lab as one program. -
Drag and drop your completed lab4.cpp file to the box labeled Drop files here or select Choose file and navigate to your file. The file you submit to the autograder MUST be called
lab4.cpp
.
Note: If you’re using Xcode and don’t know how where exactly lab4.cpp
is located on your disk, right-click (or click while holding down the Control key) on the file in the Navigator area on the left side of the Xcode window and choose Show in Finder.
If you’re using Visual Studio and would like to know where lab4.cpp
is on your disk, right-click on lab4.cpp
in the tab above the Code pane and choose Open Containing Folder.
- If confident that you’ve selected the correct file, click Submit to submit to the autograder. The autograder will tell you if you did not select a file with the correct name. We strongly urge you NOT to hit submit, but choose the correct file instead.
IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the lab. Late submissions for labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.
- Once you receive a grade of 5 of 5 points from the autograder you will have received full credit for this part of lab.
Lab Assignment Part 2 - RME Canvas Quiz
RMEs
RMEs (Requires, Modifies, Effects) are the comments that appear above function declarations for labs and projects. They document what a programmer needs to know to be able to call a function. These comments also contain essential information needed to write the function definitions for labs and projects.
- To complete this portion of the lab, you need to do the following steps:
- Take a few minutes to review the slides on RME’s here.
- Complete the Canvas quiz Lab 4 - RME Quiz.. You have an unlimited number of attempts for the quiz but must complete it by the date at the top of this document.