Lab 7 Unit testing and debugging

Similar documents
Lab 6 Vectors and functions

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Project 1 Balanced binary

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

Programming assignment A

Lab 9: Pointers and arrays

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS354 gdb Tutorial Written by Chris Feilbach

Upgrading Your Geant4 Release

Lab 12: Lijnenspel revisited

Lab 12 Lijnenspel revisited

Reliable programming

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

The compiler is spewing error messages.

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Pragmatic Guide to Git

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

T H E I N T E R A C T I V E S H E L L

OrbBasic Lesson 1 Goto and Variables: Student Guide

OrbBasic 1: Student Guide

Testing and Debugging

Adding content to your Blackboard 9.1 class

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Table of Laplace Transforms

Software Engineering /48

IT 220 Course Notes. Don Colton Brigham Young University Hawaii

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Lecture 1: Overview

Debugging Your Python Code: For Dummies

Getting Started With Linux and Fortran Part 2

1.7 Limit of a Function

CS 103 Lab The Files are *In* the Computer

Intro. Scheme Basics. scm> 5 5. scm>

Lesson 10: Exercise: Tip Calculator as a Universal App

1. User-Defined Functions & Subroutines Part 2 Outline

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

Navigating and Managing Files and Folders in Windows XP

Shorthand for values: variables

Splitting a Procedure File

Why C? Because we can t in good conscience espouse Fortran.

Here we will look at some methods for checking data simply using JOSM. Some of the questions we are asking about our data are:

Programming Assignment III

Using the Zoo Workstations

12 Follow-up Templates

Depending on the computer you find yourself in front of, here s what you ll need to do to open SPSS.

Programming Robots with ROS, Morgan Quigley, Brian Gerkey & William D. Smart

Close Your File Template

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Java/RealJ Troubleshooting Guide

Here we will look at some methods for checking data simply using JOSM. Some of the questions we are asking about our data are:

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

We first learn one useful option of gcc. Copy the following C source file to your

Blackfin Online Learning & Development

Learn Linux in a Month of Lunches by Steven Ovadia

Using GitHub to Share with SparkFun a

Setting up a ColdFusion Workstation

The Dynamic Typing Interlude

RIS shading Series #2 Meet The Plugins

Excel Basics: Working with Spreadsheets

How to set up your wireless network

SECTION 2: Loop Reasoning & HW3 Setup

APPENDIX B. Fortran Hints

One of the fundamental kinds of websites that SharePoint 2010 allows

6.001 Notes: Section 6.1

SECTION 2: Loop Reasoning & HW3 Setup

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

XP: Backup Your Important Files for Safety

Getting Started With Linux and Fortran Part 3

the NXT-G programming environment

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

The first program: Little Crab

Term Definition Introduced in: This option, located within the View tab, provides a variety of options to choose when sorting and grouping Arrangement

CMSC 201 Spring 2017 Lab 12 Recursion

Activity Guide - Will it Crash?

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

LeakDAS Version 4 The Complete Guide

Sending an to Your Chapter Through Neon

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Assignment III: Graphing Calculator

Lecture 2: SML Basics

If Statements, For Loops, Functions

Chapter 2 The SAS Environment

Finding and Fixing Bugs

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

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.

Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data.

Laboratory 1: Eclipse and Karel the Robot

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

COMP Lecture Notes The Compiler

[Video] and so on... Problems that require a function definition can be phrased as a word problem such as the following:

Excel programmers develop two basic types of spreadsheets: spreadsheets

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

Starting to Program in C++ (Basics & I/O)

TMG Clerk. User Guide

Transcription:

CMSC160 Intro to Algorithmic Design Blaheta Lab 7 Unit testing and debugging 13 March 2018 Below are the instructions for the drill. Pull out your hand traces, and in a few minutes we ll go over what you did with them; for now skip ahead and read the FOTD about compiler options. Your drill task this week involves reading a function I have written and seeded with bugs, and doing some preliminary analysis on it. 1. First, copy this drill version into your working directory for this lab. In your working directory, type cp /home/shared/160/countdivisors-drill.cpp. (don t forget the lonely dot at the end to put it in the current directory). 2. It has several errors! Read through the file, but don t fix them yet. 3. You ll be tracing the code using a value of 12 for numtocheck. Before you do so, write down what you think the correct return value should be (and why). 4. By hand, trace the code using a value of 12 for numtocheck. Anytime you encounter a bug: If it is an error that would generate a compiler error with a clear fix (e.g. misspelled identifier), fix the eror in your head and keep going with the trace. If it is an error that would generate a compiler error with multiple syntactically valid fixes (e.g. unattached else), pick one such fix, make a note of what you chose, and keep going with the trace. If it is an error that would crash the program at that point, stop the trace and read the next drill step before continuing. If it is an error that you happen to spot, that you know would generate a wrong final answer but would let the program continue from that point, don t fix it yet, treat it as-is, and keep going with the trace.

5. Anytime you get to a point in your trace where either the function finishes or the program would crash: make a note of it; make a guess at how to fix it (and write down the guess); and then start a fresh trace with that fix in place. Don t just restart the trace in the middle with the changed lines! 6. If you get through two or three traces and still have not fixed everything, that s sufficient for now; there will be more on this to come later in the lab. Command line FOTD: compile options Actually, just two quick things: a fact about compile, and two things you can do with compile that will make our lives easier. First, compile is happy to accept any number of input files at once: compile sourcefile1.cpp otherstuff.cpp stillmore.cpp Functions in any of those files can then refer to functions defined in the other files. There are just two ground rules: 1. Across all the files, there needs to be exactly one main (or a main replacement, as we ll see later). 2. Functions used from other files need to have what the book calls a function declaration. (We ll see those in a little while too.) Related to this fact, one convenient thing that we can do is tell the compile command to only compile, and not also link which means we can check for compiler errors in a single.cpp file even if it doesn t have a main function in it, or in a single.u file (we ll see those in a minute), even if it refers to functions that aren t defined yet. We do this with the -c option: compile -c partialprogram.cpp This will not generate an executable even on success, but will give you any compiler errors lurking in that file. The second convenient thing is that even when linking an executable, compile doesn t always have to generate a file named a.out. You can specify the name of the executable by preceding it with -o: 2

compile sourcefile1.cpp otherstuff.cpp stillmore.cpp -o programname (Note, however, that this will overwrite whatever is in programname don t accidentally type -o sourcefile1.cpp and overwrite your own code!) You could then call the program as./programname We ll see examples of that, too. This is a good reason to include to compile and to run instructions in your readme the compile instructions may include multiple files or additional options, and depending on the compiler options, the command to run may vary. When you use the -o option, let me strongly encourage you to copy the compile line and paste it into the window, or (once you ve done that once) use the up-arrow to re-execute the previous command; this reduces opportunities to accidentally overwrite something. As you go through this lab, take note of how I ve written the instructions in the provided readme you can use them as a model for your own versions in future labs. Setup for the rest of the lab In your working directory for this lab, type the following to copy several files you ll need for the lab: cp /home/shared/160/lab7/*. (again, don t forget the lonely dot at the end to put it in the current directory). There are four files here. Consider them in this order: 1. The readme. Read it. 2. The.h file. This is a place to put the function declarations that we talked about in class yesterday. Read it; one of the functions is the one from the drill, but you ll be working with all three. 3

3. The.cpp file. This is where the functions are defined. At the top is the (broken, buggy) function you saw in the drill; there is a stub for the second function and a (buggy) implementation I wrote for the third. For now, just quickly skim this file (you ll come back to it later). 4. The.u file. This is a kind of file you ve not seen before. Open it in vim and then continue to the next section. Unci files and unit testing Unit testing is the idea that we can (and should!) write test cases not just for the entire program overall, but also for individual pieces (units) of it. I ve written a framework called Unci that supports unit testing with a clean interface; this is not universal-standard C++ but is similar in flavour to what happens in other unit-testing systems. 1 In this lab, you won t be building a.u file from scratch, just reading (and later, adding to) the one I ve provided, so rather than rigorously define the Unci syntax here, I want you to read the file (and its comments) to get a sense of its layout; and as you get the different functions working, and run the provided compile command and run the test cases, refer back to the.u file and ask lots of questions. (We ll also be talking about this in class, of course). Once you ve spent some time reading through the.u file don t worry about deep understanding yet continue on to the next section. Fixing countdivisors As you saw in the drill, countdivisors has a few bugs. Fix them and each time you make a change, leave a comment in the code OR write a line in the readme to describe the error and how you fixed it. After you think you ve fixed each one, recompile the tests and run them. 1 Many unit testing frameworks exist, but the ones for C++ are often clunky, awkward, and highly unforgiving to the beginner. Unci is better. :) 4

Writing countwords I ve provided a declaration for countwords, and a stub definition, and a bunch of test cases to help define its behaviour. Write the function. Focus first on getting a version that works with the basic test cases; move on to the zero and challenge parts only once the basic block passes. Testing and fixing secondhighest The third function compiles and runs and seems to work, but the provided test cases are inadequate and the function is subtly buggy. Add test cases that more thoroughly test the function; don t change the function until you ve added a test case that the existing definition fails and that thus demonstrates the bug(s). Once you have such a test case, then work on fixing the function. Handing in and rubric Hand in as lab7. Rubric TBA. 5