Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Similar documents
Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 45

Comp 11 Lectures. Mike Shah. June 20, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 20, / 38

Comp 11 Lectures. Dr. Mike Shah. August 9, Tufts University. Dr. Mike Shah (Tufts University) Comp 11 Lectures August 9, / 34

An analogy from Calculus: limits

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

What Every Programmer Should Know About Floating-Point Arithmetic DRAFT. Last updated: November 3, Abstract

Theory versus Experiment: Analysis and Measurements of Allocation Costs in Sorting (With Hints for Applying Similar Techniques to Garbage Collection)

Solving with Absolute Value

The Inductive Proof Template

Hypothesis testing I. - In particular, we are talking about statistical hypotheses. [get everyone s finger length!] n =

Expansion of Terms. f (x) = x 2 6x + 9 = (x 3) 2 = 0. x 3 = 0

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Negative Bit Representation Outline

1 Maintaining a Dictionary

Sequences and Series

LECTURE 15: SIMPLE LINEAR REGRESSION I

Quadratic Equations Part I

Introduction to Python and its unit testing framework

Modern Algebra Prof. Manindra Agrawal Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

1 Closest Pair of Points on the Plane

Deterministic Finite Automata

CS1800: Hex & Logic. Professor Kevin Gold

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Expected Value II. 1 The Expected Number of Events that Happen

Non-Convex Optimization. CS6787 Lecture 7 Fall 2017

Software Testing Lecture 7 Property Based Testing. Justin Pearson

(Refer Slide Time: 0:21)

Water Resources Systems Prof. P. P. Mujumdar Department of Civil Engineering Indian Institute of Science, Bangalore

Memory Elements I. CS31 Pascal Van Hentenryck. CS031 Lecture 6 Page 1

Introduction to Axiomatic Semantics

Guide to Proofs on Sets

(Refer Slide Time: 2:11)

CS 361: Probability & Statistics

Announcements. Problem Set 6 due next Monday, February 25, at 12:50PM. Midterm graded, will be returned at end of lecture.

At the start of the term, we saw the following formula for computing the sum of the first n integers:

Regression, part II. I. What does it all mean? A) Notice that so far all we ve done is math.

CS 124 Math Review Section January 29, 2018

Complexity Theory Part I

#29: Logarithm review May 16, 2009

How to write maths (well)

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Implicit Differentiation Applying Implicit Differentiation Applying Implicit Differentiation Page [1 of 5]

CS 170 Algorithms Spring 2009 David Wagner Final

Unit 8: Sequ. ential Circuits

Intermediate Logic. Natural Deduction for TFL

CSE 105 THEORY OF COMPUTATION

What is proof? Lesson 1

A Brief Introduction to Proofs

Ordinary Differential Equations Prof. A. K. Nandakumaran Department of Mathematics Indian Institute of Science Bangalore

Horizontal asymptotes

Warm-Up Problem. Please fill out your Teaching Evaluation Survey! Please comment on the warm-up problems if you haven t filled in your survey yet.

Thermodynamics (Classical) for Biological Systems. Prof. G. K. Suraishkumar. Department of Biotechnology. Indian Institute of Technology Madras

Symbolic Logic Outline

S C F F F T T F T T S C B F F F F F T F T F F T T T F F T F T T T F T T T

PHP-Einführung - Lesson 4 - Object Oriented Programming. Alexander Lichter June 27, 2017

Communication Engineering Prof. Surendra Prasad Department of Electrical Engineering Indian Institute of Technology, Delhi

Natural deduction for truth-functional logic

COMS 6100 Class Notes

ECS 120 Lesson 18 Decidable Problems, the Halting Problem

from Euclid to Einstein

Let s now begin to formalize our analysis of sequential machines Powerful methods for designing machines for System control Pattern recognition Etc.

1 Work, Power, and Machines

Formal Methods in Software Engineering

Intro To Digital Logic

Nonregular Languages

Structural Induction

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

Sequential Logic (3.1 and is a long difficult section you really should read!)

CS187 - Science Gateway Seminar for CS and Math

CHM 105 & 106 UNIT TWO, LECTURE EIGHT 1 IN OUR PREVIOUS LECTURE WE WERE LOOKING AT CONCENTRATION UNITS FOR SOLUTIONS

Turing Machine Recap

Robust Programs with Filtered Iterators

Lab 2: Static Response, Cantilevered Beam

Matrix Assembly in FEA

A Short Introduction to Hoare Logic

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Park School Mathematics Curriculum Book 9, Lesson 2: Introduction to Logarithms

Lecture 08 Born Oppenheimer Approximation

MITOCW MITRES_18-007_Part1_lec3_300k.mp4

P vs. NP. Data Structures and Algorithms CSE AU 1

Surveying Prof. Bharat Lohani Department of Civil Engineering Indian Institute of Technology, Kanpur. Module - 3 Lecture - 4 Linear Measurements

Building a Computer Adder

MAT Mathematics in Today's World

Series, Parallel, and other Resistance

Select/ Special Topics in Atomic Physics Prof. P. C. Deshmukh Department of Physics Indian Institute of Technology, Madras

MITOCW ocw nov2005-pt1-220k_512kb.mp4

Amount of Substance and Its Unit Mole- Connecting the Invisible Micro World to the Observable Macro World Part 2 (English, mp4)

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Biostatistics and Design of Experiments Prof. Mukesh Doble Department of Biotechnology Indian Institute of Technology, Madras

New Minimal Weight Representations for Left-to-Right Window Methods

Handouts. CS701 Theory of Computation

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

MITOCW MITRES_18-007_Part3_lec5_300k.mp4

Chemical Applications of Symmetry and Group Theory Prof. Manabendra Chandra Department of Chemistry Indian Institute of Technology, Kanpur

The Gram-Schmidt Process

Introduction to Algebra: The First Week

Transcription:

Comp 11 Lectures Mike Shah Tufts University July 26, 2017 Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 1 / 40

Please do not distribute or host these slides without prior permission. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 2 / 40

Testing Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 3 / 40

Comp 11 - Pre-Class warm up Mike Shah (Tufts University) Figure 1: The Compfirst 11 Lectures computer bug July 26, 2017 4 / 40

Lecture Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 5 / 40

Grace Hopper Figure 2: Grace Hopper was one of the first famous programmers, working on the Harvard Mark 1 machine (Just across the river). She coined the term computer bug when she found a machine not running because a moth got stuck in it. Grace also led development on the COBOL programming language, and made many contributions to the field of compilers. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 6 / 40

Lecture Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 7 / 40

Testing Software Software Correctness Testing can show the presence of errors, but not their absence. E. W. Dijkstra With this in mind, it is hard to prove that our software is correct. For small programs we may be okay, but we need some techniques that will help us as we work on larger software projects. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 8 / 40

Famous Software Bugs 1 - Case 1 http://listverse.com/2012/12/24/ 10-seriously-epic-computer-software-bugs/ Therac-25 Medical Accelerator It had two types of radiation therapy. One setting would apply a strong dosage, one a lower dosage. If the operator changed modes of the devices two quickly, the wrong one would be applied! 1 The term coming from the moth we previously saw Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 9 / 40

Famous Software Bugs - Case 2 North American blackout was due to a generator going out. The software bug however, was that the system failed to sound the alarms (it failed quietly) and the situation could have been mitigated much quicker. Figure 3: August 14, 2003 Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 10 / 40

Figure 4: Y2K was a seemingly simple bug conceptually that required lots of maintenance! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 11 / 40 Famous Software Bugs - Case 3 Y2K Bug 2 digits were used to represent the year instead of 4, which would break some calculations. This was attributed to short-sightedness on programmers. Had they taken this course, they would know data representation matters! :) (We are going to have this same problem in Unix systems in 2038 actually based on how 32-bit Unix systems store time)

Famous Software Bugs - Case 4 Kerberos Random Number Generator from 1988-1996 Unfortunately, this did not generate truly random numbers based on the seed. The seed was always the same, so if a computer uses randomness for authentication, the system is compromised! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 12 / 40

Famous Software Bugs - Case 5 Intel Pentium floating point divide Dividing numbers within a specific range yielded a small error. Critical software systems could be compromised! (especially if you divide a lot!) This bug is estimated to cost Intel $475 million over the bugs lifetime. Not a small chunk of change! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 13 / 40

Tools for testing So for us, we need some tools that can help prevent bugs! We have actually been exposed to one through some of the labs and examples. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 14 / 40

Unit Test Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 15 / 40

Unit Testing Unit Test A self-contained test where a part of the program is executed and matched against a known correct result Unit tests can be as big or as small as you like. The key is though, know what you are testing Your tests must obviously be correct! (Sometimes easier said than done!) Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 16 / 40

Unit Test - Example 1 1 #i n c l u d e <i o s t r e a m > 2 // Some f u n c t i o n 3 i n t s q u a r e ( i n t x ) { 4 r e t u r n x x ; 5 } 6 // Simple u n i t t e s t 7 b o o l u n i t t e s t ( i n t expected, i n t answer ) { 8 r e t u r n ( e x p e c t e d==answer ) ; 9 } 0 1 i n t main ( ) { 2 // Run some t e s t s (1 means p a s s e d or t r u e ) 3 f o r ( i n t i =0; i <10; ++i ) { 4 s t d : : cout << Test# << i << = << u n i t t e s t ( i i, s q u a r e ( i ) ) << \n ; 5 } 6 r e t u r n 0 ; 7 } Listing 1: Test our square function Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 17 / 40

Unit Test - Example 2 1 #i n c l u d e <i o s t r e a m > 2 u s i n g namespace s t d ; 3 4 // Unit t e s t to s e e i f two f i x e d s i z e a r r a y s a r e s o r t e d 5 b o o l c o r r e c t l y S o r t e d ( i n t c o r r e c t, i n t myarray, i n t s i z e ) { 6 f o r ( i n t i =0; i < s i z e ; ++i ) { 7 i f ( c o r r e c t [ i ]!= myarray [ i ] ) { 8 r e t u r n f a l s e ; 9 } 0 } 1 r e t u r n t r u e ; 2 } 3 i n t main ( ) { 4 i n t s o r t e d [ ] = { 1, 2, 3, 4, 5 } ; 5 i n t u n s o r t e d [ ] = { 2, 4, 3, 5, 1 } ; 6 s t d : : cout << I s i t s o r t e d : << c o r r e c t l y S o r t e d ( s o r t e d, u n s o r t e d, 5 ) << \n ; 7 r e t u r n 0 ; 8 } Listing 2: Check if an array is sorted Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 18 / 40

Unit Tests in Practice If you like breaking things, this can be your job. Many software engineers work as test engineers and do this exact thing. It is not uncommon to have thousands of tests (imagine how many there might be for the clang++ compiler!) And what is nice, is that as you update functionality to your program, you just run your test suite to see if it breaks any previous tests. The more code coverage you have(i.e. what % of functionality do you have a test for), the more confidence you can have in your software. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 19 / 40

Assertions Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 20 / 40

Assertion Sometimes we want to make strong claims in our programs. If the claim is false for any reason, then the safest thing to do in the system is terminate execution. Assertion A statement that is always expected to be true. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 21 / 40

Assertion Assertions are created with the assert function Assertions are checked while the program is running. We can use the C-preprocessor to turn on and off asserts if we do not want to test functionality. (In C++11 we can have static assert, which is checked at compile-time) Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 22 / 40

Assert - Example 1 1 #i n c l u d e <i o s t r e a m > 2 #i n c l u d e <c a s s e r t > // Adds i n a MACRO a s s e r t f u n c t i o n 3 4 i n t main ( ) { 5 6 a s s e r t (1==1) ; // Expected to p a s s 7 a s s e r t (1==2) ; // Something i s t e r r i b l y wrong!! 8 s t d : : cout << H e l l o World \n ; 9 0 r e t u r n 0 ; 1 } Listing 3: Check if the program runs Figure 5: Assertion is caught, and even tells us where! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 23 / 40

Assert - Example 2 1 #i n c l u d e <i o s t r e a m > 2 // F l i p s a s w i t c h i n the c o m p i l e r to b a s i c a l l y i g n o r e 3 // a l l a s s e r t s t a t e m e n t s. This goes b e f o r e we i n c l u d e 4 // the c a s s e r t h e a d e r f i l e. 5 #d e f i n e NDEBUG 6 #i n c l u d e <c a s s e r t > // Adds i n a MACRO a s s e r t f u n c t i o n 7 8 i n t main ( ) { 9 a s s e r t (1==1) ; // Expected to p a s s 0 a s s e r t (1==2) ; // Something i s t e r r i b l y wrong!! 1 s t d : : cout << H e l l o World \n ; 2 3 r e t u r n 0 ; 4 } Listing 4: assert statements ignored and Hello World is printed with a newline. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 24 / 40

Assert effectiveness It is argued whether terminating a program right away is the best thing to do in the case of an error. I think asserts might be quite confusing to the user when running the software and it just shuts down. However, as a software developer, they are very helpful for testing your assumptions of the system. Let s say we want to do one step better, and actually handle errors as they occur. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 25 / 40

Exceptions Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 26 / 40

Exception In some domains (say during our bank transactions), we do not want the default behavior to be to terminate the entire program if an error occurs. We may want to handle the error, log it, and then continue on with our the rest of our program. Exception A way to react to runtime errors in programs by transferring control to special functions called handlers. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 27 / 40

Types of Exceptions A list is enumerated here: http://en.cppreference.com/w/cpp/error/exception These boil down to: logic errors runtime errors bad data types bad memory allocations And bad function calls to name a few Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 28 / 40

Exception - Example 1 #i n c l u d e <i o s t r e a m > 2 3 i n t main ( ) { 4 5 i n t age = 5; 6 7 t r y { 8 i f ( age < 0) { 9 throw age ; 0 } 1 } c a t c h ( i n t e ) { 2 // I f we throw, then p r i n t an e r r o r message. 3 s t d : : cout << E x c e p t i o n : throw age= << e << \n ; 4 } 5 6 s t d : : cout << Let s c o n t i n u e... \ n ; 7 8 r e t u r n 0 ; 9 } Listing 5: Throw an integer exception Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 29 / 40

Testing Matters So as you have seen, testing matters! Lots of smart folks have put effort into building tools to automate some of this for us. A variety of Debuggers/IDEs/C++ Tools/Programmer Tools exist. Let s take a look at a few Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 30 / 40

Valgrind - Helps find memory errors 1 #i n c l u d e <i o s t r e a m > 2 3 i n t main ( ) { 4 5 i n t myarray = new i n t [ 5 0 0 ] ; 6 myarray [ 0 ] = 0 ; 7 s t d : : cout << myarray [ 0 ] << \n ; 8 9 // Oops, we f o r g o t to f r e e! 0 r e t u r n 0 ; 1 } Listing 6: The compiler will let us run this program Figure 6: But we know something is going on! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 31 / 40

Valgrind - Example Valgrind (available on CS machines or download) will hint where there are memory errors for us! Figure 7: Valgrind alerts us we failed to free blocks of memory! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 32 / 40

GDB GDB is a debugger tool that allows you to step through a program. We can help GDB by adding a -g argument, which inserts special debugging symbols into our code for us, to allow us to control execution. example: clang++ -g main.cpp -o myprogram We will actually follow a tutorial for GDB for our in-class activity to get some hands on experience. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 33 / 40

Integrated Development Environment (IDE) An Integrated Development Environment often has several of these tools built in. Some of the popular ones at this time for C++ are: Visual Studio(Windows), XCode(Mac), CodeBlocks(Unix), QtCreator(All platforms), Eclipse(All Platforms). Other features like intellisense and code completion are useful for productivity (these tools can be activated in VIM as well). Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 34 / 40

In-Class Activity Part one of this tutorial! https: //aaronbloomfield.github.io/pdr/tutorials/02-gdb/index.html http: //www.mshah.io/comp/11/activities/activity15/activity.pdf Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 35 / 40

Activity Discussion Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 36 / 40

Review of what we learned (At least) Two students Tell me each 1 thing you learned or found interesting in lecture. Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 37 / 40

5-10 minute break Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 38 / 40

To the lab! Lab: http://www.mshah.io/comp/11/labs/lab15/lab.pdf 2 2 You should have gotten an e-mail and hopefully setup an account at https://www.eecs.tufts.edu/~accounts prior to today. If not no worries, we ll take care of it during lab! Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 39 / 40

Glossary Assertion A statement that is always expected to be true.. 21 Exception. 27 Unit Test A self-contained test where a part of the program is executed and matched against a known correct result. 16 Mike Shah (Tufts University) Comp 11 Lectures July 26, 2017 40 / 40