How To Test Your Code A CS 1371 Homework Guide

Similar documents
CS3 Midterm 1 Fall 2006

WINTER 2017 ECE 102 ENGINEERING COMPUTATION STANDARD HOMEWORK #3 ECE DEPARTMENT PORTLAND STATE UNIVERSITY

VIDYA SAGAR COMPUTER ACADEMY. Excel Basics

ENED 1090: Engineering Models I Homework Assignment #2 Due: Week of September 16 th at the beginning of your Recitation Section

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Debugging Your Python Code: For Dummies

Expressions and Casting

MITOCW watch?v=kz7jjltq9r4

CS Homework 2 p. 1. CS Homework 2

There we are; that's got the 3D screen and mouse sorted out.

= 3 + (5*4) + (1/2)*(4/2)^2.

Why Use Graphs? Test Grade. Time Sleeping (Hrs) Time Sleeping (Hrs) Test Grade

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

15-110: Principles of Computing, Spring 2018

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

BBC Learning English Face up to Phrasals Mark's Mistake

Examples, examples: Outline


Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Testing. UW CSE 160 Winter 2016

Your Name: Section: INTRODUCTION TO STATISTICAL REASONING Computer Lab #4 Scatterplots and Regression

EGR 111 Functions and Relational Operators

The following content is provided under a Creative Commons license. Your support

6.001 Notes: Section 8.1

If Statements, For Loops, Functions

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Monday. A few notes on homework I want ONE spreadsheet with TWO tabs

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

Math 182. Assignment #4: Least Squares

CME 193: Introduction to Scientific Python Lecture 1: Introduction

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

PYTHON PROGRAMMING FOR BEGINNERS: AN INTRODUCTION TO THE PYTHON COMPUTER LANGUAGE AND COMPUTER PROGRAMMING BY JASON CANNON

MITOCW watch?v=flgjisf3l78

Introduction to Python Code Quality

AP Computer Science Unit 3. Programs

using cells to create dynamic formulas

Exercises: Instructions and Advice

EGR 111 Functions and Relational Operators

Unit II Graphing Functions and Data

1 Data Exploration: The 2016 Summer Olympics

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

An introduction to plotting data

Popcorn's ini Tutorial

Module 1: Introduction RStudio

ECS Baruch Lab 2 Fall 2019 Name

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

MITOCW watch?v=hverxup4cfg

AREA Judo Math Inc.

6.001 Notes: Section 15.1

Reference Guide. Adding a Generic File Store - Importing From a Local or Network ShipWorks Page 1 of 21

How to approach a computational problem

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Lecture 10: Lindenmayer Systems

Contents. Foreword. Examples of GeoGebra Applet Construction 1 A Straight Line Graph... 1 A Quadratic Graph... 6 The Scalar Product...

Area and Perimeter EXPERIMENT. How are the area and perimeter of a rectangle related? You probably know the formulas by heart:

Week - 01 Lecture - 04 Downloading and installing Python

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Exploration: Multiplying Fractions & Decimals Using an Area Model

EGR 111 Functions and Relational Operators

Introduction to MATLAB

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Excel for Algebra 1 Lesson 5: The Solver

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

(Refer Slide Time: 01:25)

Graded Project. Microsoft Excel

CS3 Midterm 1 Fall 2007 Standards and solutions

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Repetition Structures

Mr G s Java Jive. #11: Formatting Numbers

Session 4b: Review of Program Quality

SISG/SISMID Module 3

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Type Checking and Type Equality

Helping the Compiler Help You. Thomas Dy

CSE200 Lecture 6: RECURSION

Clustering Images. John Burkardt (ARC/ICAM) Virginia Tech... Math/CS 4414:

Animations involving numbers

Recognizing a Function

NetLogo Tutorial Series: Langton's Ant. Nicholas Bennett Grass Roots Consulting

The following content is provided under a Creative Commons license. Your support

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

MATLAB TUTORIAL WORKSHEET

% ALSO SEE % for other types of plots you can make

Wednesday. Wednesday, September 17, CS 1251 Page 1

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

ENED 1090: Engineering Models I Homework Assignment #4 Due: Week of October 7 th at the beginning of your Recitation Section

QUIZ Friends class Y;

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

Recursively Enumerable Languages, Turing Machines, and Decidability

MicroSurvey Users: How to Report a Bug

Chapter 2 Basic Elements of C++

Graphing and Equations

Friday. Last class Some of what we covered: for loop nested for loops toast. The place from which you call a function affects what it can do!

MOODLE MANUAL TABLE OF CONTENTS

2. The histogram. class limits class boundaries frequency cumulative frequency

Transcription:

Introduction After you have completed each drill problem, you should make it a habit to test your code. There are good ways of testing your code and there are bad ways of testing your code. This guide is written to teach you the good ways and to help you avoid the bad ways. What To Avoid The first thing many students try is to copy and paste each test case from the hw<##>.m file into the Command Window to execute the function and to compare the printout in the Command Window with the solution in the hw<##>.m file by inspection. Not only is this method slow and tedious, it is also prone to many errors, including the following: 1. Your function, as you have coded it, may simply be printing the values of the outputs to the Command Window instead of actually setting output variables. Please make sure your function is producing output variables that appear in the Workspace. 2. Your outputs may look correct when inspected by eye, but you cannot be sure that they are exactly identical to the correct outputs. This is especially true of output strings, where it is easy to miss a space or punctuation mark or even several English words entirely! For example, 'A C' vs. 'A C ' or 'I like turtles.' vs. 'I like turtles' or 'This item is worth $9.' vs. 'The item is worth $9.'. 3. You may also fall victim to misinterpreting the typed-out solution on the hw<##>.m file. The typed-out solution (which usually looks like out2 => [4, 5; 6, 7; 8, 9] or like str1 => 'Congratulations! You''re the big winner!') is meant to be interpreted as a block of code to be entered into the Command Window. For example, in the first case, a correctly-coded function would produce an output variable called out2 that is a 3x2 array of type double. If you were to compare this variable to the result of entering out2_soln = [4, 5; 6, 7; 8, 9]; into the Command Window, the two variables out2 and out2_soln should be identical. In the second case, a correctlycoded function would output a variable called str1 that is identical to the result of entering str1_soln = 'Congratulations! You''re the big winner!'; into the Command Window. Note that the two single quotation marks are necessary to create a single quotation mark character inside of a string in MATLAB. 4. Your function will ultimately be tested by the Homework Autograder, which compares the outputs of your function with the outputs of the solution file (the file that looks like functionname_soln.p), given inputs from several different test cases. This means that, ideally, you should be testing your code against the solution file instead of the typed-out solutions on the hw##.m file, which are not guaranteed to be correct.

What To Do Instead Write a test script! Test Scripts A test script is a script that you run in order to execute your functions with test cases, each of which is just an input or series of inputs with an associated expected output. A script is just a block of code that you can run without any inputs or outputs. The ABCs pretest file that comes with every homework assignment is a perfect example of a test script. You can't open it, because it's encrypted, but if you could look inside of it, you would see nothing more than a block of code that calls your ABCs function using one or two test cases and compares the outputs of your function to the expected outputs. It then prints out a series of statements to the Command Window which tell you which outputs were correct and which were incorrect. Our goal is to design a similar script to test the code that you write for the drill problems. Writing A Test Script Writing a test script is easy! The first thing you should start with is the built-in clear function. This function will delete all of the variables in your current Workspace so that you can test your code in a clean environment without the risk of mixing up variable names. Remember that you should never use clear in a function, only in a script. The next thing to do is to call your functions with a couple of test cases. For simplicity, we will only call one function in this simple example. Suppose your function is supposed to find the area and perimeter of a rectangle given two side lengths. The function should work as follows: [area, perim] = rectanglemath(width, height) Notice that the function is called rectanglemath and that it takes two inputs and produces two outputs. This means that we will have to execute or call this function in our test script using two inputs from a test case. Which test case should we use? Theoretically, you can use any test case you want. However, for your benefit, there are suggested test cases in the hw<##>.m file. They might look the following: width1 = 2; height1 = 5; and width2 = 0; height2 = 1; Each pair of inputs is a test case. Notice that the names of the inputs of the first test case end with the number 1 and the names of the inputs of the second test case end with the number 2. This makes sense and is good coding practice. After copying the code above into our

test script, we are ready to use them to call our function. Remember that we need to assign two outputs for each function call. We will assign them as follows: [area1, perim1] = rectanglemath(width1, height1); [area2, perim2] = rectanglemath(width2, height2); Again, notice the use of the numbers 1 and 2 at the end of the names of our outputs. Keep the naming of your variables consistent to avoid unnecessary confusion. At this point, you may think that we are done. We can run the script as it is and inspect the outputs, comparing them to what we know should be the correct outputs. Not so fast! Remember what we said about using the solution files. It may seem trivial in this simple example, but it is very important when testing more complicated code. In your homework folder, you should have been provided with encrypted solution files. In this example, the solution file will be a function called rectanglemath_soln. Let's call the solution function on the same test cases as follows: [area1_soln, perim1_soln] = rectanglemath_soln(width1, height1); [area2_soln, perim2_soln] = rectanglemath_soln(width2, height2); Notice that we are storing our outputs in new variables, with _soln appended at the end of each. This is to distinguish them from the output variables of our own implementation of the function. If we had not done this, we would have overwritten the previous values of area1, perim1, area2, and perim2. Now we can compare the value of area1 with the value of area1_soln and compare the value of perim1 with the value of perim1_soln and so on for the second test case, right? We're done, right? Wrong! Remember that you should refrain from comparing your outputs to the solution outputs by inspection. Instead, we will use a more precise, foolproof method of comparison, the built-in isequal() function. The isequal() function takes two inputs and tells you if they are equal or not by outputting a logical true or false. Let's call isequal() for each pair of output variables to see if our function behaves the same as the solution function. We will write the following lines of code: check_area1 = isequal(area1, area1_soln); check_perim1 = isequal(perim1, perim1_soln); check_area2 = isequal(area2, area2_soln); check_perim2 = isequal(perim2, perim2_soln); Note that the naming of the logical output variables is consistent with what they represent. For example, if after running out test script, check_area1 has the value true, then we know that the area computed by our function is exactly the same as the area computed by the solution file, at least for the first test case. If all of these check variables are true, then we know that our function is probably correct. This does not guarantee, however, that your code will receive full credit when you submit it. That will depend on which test cases the Autograder runs.

That is why it is important to test your code with many different test cases and why we provide you with several suggested test cases. If any of the check variables are false, then there is something wrong with your function and you should go back to your code and try to fix the problem. Congratulations! You just wrote a test script! Of course, on your actual homework, you will have more than one function to implement and test. The process of adding more functions to your test script is the same. Define some input variables from the suggested test cases. Call your function on those inputs. Call the solution function on those same inputs. Generate check variables by using isequal to compare the resulting outputs. Inspect the check variables in the Workspace to ensure that all of them are true. That's it! Just make sure that you do not reuse any variable names. This will cause unexpected and potentially frustrating results. Here is the complete test script: % Test Script clear % Test Case 1 width1 = 2; height1 = 5; % Test Case 2 width2 = 0; height2 = 1; % My Function Outputs [area1, perim1] = rectanglemath(width1, height1); [area2, perim2] = rectanglemath(width2, height2); % The Solution Function Outputs [area1_soln, perim1_soln] = rectanglemath_soln(width1, height1); [area2_soln, perim2_soln] = rectanglemath_soln(width2, height2); % Check Test Case 1 check_area1 = isequal(area1, area1_soln); check_perim1 = isequal(perim1, perim1_soln); % Check Test Case 2 check_area2 = isequal(area2, area2_soln); check_perim2 = isequal(perim2, perim2_soln);

Testing Outputs That Are Not Variables The example test script above will work for all types of output variables, including doubles, logicals, chars (strings), cells, and structures. However, some weeks, the homework will require you to write functions that output things other than variables, such as text files, Excel files, plots, or image files. How can you test these? Let's break it down. Testing Text Files The first thing to check is to see that your text file is named correctly, exactly as specified in the problem statement. You may choose to do this by inspection. Now comes the hard part. How can you compare the contents of your output text file with the contents of the solution text file? It turns out that MATLAB has a built-in tool that will take care of this for you. Suppose your output file is called 'textfile1.txt' and the solution text file provided in the homework is called 'textfile1_soln.txt'. From the Command Window, type and run the following command: visdiff('textfile1.txt','textfile1_soln.txt'); At this point, a new window should pop up. This is the MATLAB File Comparison Tool. It will not only tell you if the selected files match, but it will also tell you exactly what and where all of the differences are. Use this tool to your advantage. Testing Excel Files There is no file comparison tool for Excel files. Instead, you can simply read the Excel files you would like to compare into MATLAB using xlsread and compare the raw cell array outputs using isequal. For example, to compare 'excelfile1.xlsx' and 'excelfile1_soln.xlsx', you could type the following in your test script: [~,~,raw1] = xlsread('excelfile1.xlsx'); [~,~,raw1_soln] = xlsread('excelfile1_soln.xlsx'); check_excelfile1 = isequal(raw11,raw1_soln); Testing Plots Unfortunately, there is no easy way to test plots. By inspection, make sure that your plot has the same title and axis labels as the solution plot. Also make sure that you plotted all the correct points with the correct colors and styles. If your plot should contain more than one curve, it does not matter what order you plot the curves in. Therefore, the overlap of the lines or points on your plot will not affect the "correctness" of your plot. Testing Image Files Comparing two image files is very similar to comparing two Excel files. Instead of comparing the images outside of MATLAB, read the images into MATLAB using imread and compare the resulting image arrays as follows:

im1 = imread('imagefile1.png'); im1_soln = imread('imagefile1_soln.png'); check_imagefile1 = isequal(im1,im1_soln); Conclusion Test your code the smart way! Don't do more work than you have to! Happy coding!