cse 15l winter 2015 · test-driven development junit is a tool used for tdd in tdd, tests are...

46
CSE 15L Winter 2015 Midterm :) Review

Upload: others

Post on 07-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

CSE 15L Winter 2015Midterm :) Review

Page 2: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Makefiles

Page 3: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Makefiles - The OverviewQuestions you should be able to answer● What is the point of a Makefile

○ Why don’t we just compile it again?○ Why don’t we just use a shell script?

● What is the format of a Makefile?● How do we define variables in Makefiles?● How do we call make in subdirectories?

Page 4: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Makefiles - The FormatDependencies can be files or targets

Basic Structure:

target: dependenciesaction

Note: Each action must be tab-indented

Page 5: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Makfile - The Example

Page 6: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Suffix Directives

Page 7: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Makefile Macros

Page 8: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Calling another Makefile

Page 9: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Unix Shell Scripting

Page 10: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Piping/FilteringWhat is a pipe used for?

A pipe is used to redirect the output of one command to the input of another.

What does a pipe look like?

A pipe is a vertical bar ‘|’Note: This is the same symbol used in ‘or’ ‘||’

Show me an Example.ls -l | grep Aprls | wcman ksh | grep “history” | wc -l

Page 11: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Shell Scripting● Lines starting with # are comments, but the first line #! is not a comment; it indicates the location

of the shell that will be run

● Quote characters○ “ double quote: if a string is enclosed in “ “ the references to variables will be replaced with

their values○ ‘ single quote: taken literally○ ` back quote: treated as command

■ echo “Date is:” `date`● chmod is used to change the permissions so we can run our script● Wildcards: http://ieng6.ucsd.edu/~cs15w/lab1_kjasdf/page4.html

Page 12: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

DeclarationsAre these two different?1) y=52) y = 5

Page 13: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Loops● Instead of using braces {} to control logic flow and statement blocks, shell

uses terminating words:○ if, then/ fi○ case / esac○ for, do, done○ while, do, done

for i in {1..10}do

if [ $[i % 2] = 1 ]; then

echo $i “is odd”else

echo $i “is even”fi

done

Page 14: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Shell ScriptingWhich one of these is the correct way to start a bash script?

A. !# /bin/bashB. # bin/bashC. ! /bin/bashD. #! /bin/bash

Page 15: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Shell ScriptingWhich one of these is the correct way to start a bash script?

A. !# /bin/bashB. # bin/bashC. ! /bin/bashD. #! /bin/bash

Page 16: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Shell Scripting (declarations)

● Array declarations:○ y=(Hello hi hey)○ To access declared variables, use $ and braces

■ echo ${y[0]} → Hello is printed

● Declarations must not include spaces around equals sign

Page 17: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Examplesfor (( c=1; c<=5; c++ ))do echo "Welcome $c times"done

__________________________

echo “Enter a fruit:”read fruitcase $fruit inapple)

echo “Apple”;;

banana)echo “Banana”;;

*)echo “Something else”;;

esac

c=0while [ $c -lt 10 ]doecho The counter is $clet c=c+1 done

Page 18: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php

Page 19: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Examples (more)ls | while read line

do

echo “Hello ${line}!”

done

_______________________

filename=$1cat $filename | while read linedo

echo “Hello ${line}!”done

filename=$1while read linedo

echo “Hello ${line}!”done < “$filename”

Page 20: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Standard Streams

http://upload.wikimedia.org/wikipedia/commons/7/70/Stdstreams-notitle.svg

Page 21: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Redirecting Streamshttp://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

1: stdout 2 file ls -l > ls-l.txt

2: stderr 2 file man grep | grep -n man 2> errors.txt

3: stdout 2 stderr man grep | grep -n man 1>&2

4: stderr 2 stdout man grep | grep -n man 2>&1

5: stderr and stdout 2 file man grep | grep -n man &> /dev/null

Page 22: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Redirection CommandsCommand Description

pgm > file Output of pgm is redirected to file

pgm < file Program pgm reads its input from file.

pgm >> file Output of pgm is appended to file.

n > file Output from stream with descriptor n redirected to file.

n >> file Output from stream with descriptor n appended to file.

n >& m Merge output from stream n with stream m.

n <& m Merge input from stream n with stream m.

| Takes output from one program, or process, and sends it to another.

http://www.tutorialspoint.com/unix/unix-io-redirections.htm

Page 23: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

What you should know● Know the difference between echo and cat● Variable declarations● How to call variables● How strings “”, ``, ‘’ work in bash● How to use simple constructs like if statements, for loops, etc● Commands like cd, rm, mv, cp, etc● If you don’t know what a command does type man <command>

Site to test:http://www.tutorialspoint.com/execute_bash_online.php

Page 24: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git

Page 25: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

What is Git?Git is a distributed version control system.

Page 26: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Local git project layout

Page 27: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git file lifecycle

Page 28: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git commandsgit init

Initialize a new git repogit add <file>

Add/Stage a new file to your repogit commit -m “message”

Commit staged changes to your repo

Page 29: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git commandsgit status

Show the status of files in the directorygit log

Log of all the commits made to the repogit diff

File differences for unstaged, modified files

Page 30: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git commandsgit pull

pull changes from a remote servergit push

push changes to a remote servergit remote add

adding a remote server

Page 31: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Git commandsgit branch <branchname>

Create a new branchgit checkout <branchname>

Checkout new branchgit merge <branchname>

Merge branch with current branch

Page 32: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

TDD & JUnit

Page 33: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Test-Driven Development

Page 34: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Test-Driven Development● JUnit is a tool used for TDD● In TDD, tests are written before software.● You must understand requirements first!● Regression testing

○ Everytime you change code, run original tests!○ Make sure old features work after adding new ones.

Page 35: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Test-Driven Development● What is a unit?

○ A single method.● Why use unit testing?

○ Find problems early.○ Documentation, shows how to use method.

● Why not to use…○ Doesn’t test full software.○ Takes a long time to write all those tests.

Page 36: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

JUnit● JUnit is a widely used framework for unit

testing in Java.● Makes testing standardized and easy

(relatively easier) to implement.● Testing whole suites at once.

Page 37: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

JUnit Terminology● Test fixture - sets up the data needed to

run tests.● Unit Test - A piece of code written by

developer that executes a particular part of the code being tested.

● Test case - Tests the response of a single method to a particular set of inputs.

Page 38: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

JUnit Terminology● Test suite - Collection of test cases.● Test runner - Software that runs your tests.● Integration test - how well classes work

together. Not good in JUnit.

http://www.vogella.com/tutorials/JUnit/article.html <- Useful source!http://ieng6.ucsd.edu/~cs15f/lab3_fdw/index.html <- More info here!

Page 39: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

JUnit● Tests pass when they return without failing

or throwing exceptions.● Failure happens when JUnit assertion fails.● Want to pass all the tests!

Page 40: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Other Frameworks● JUnit - JAVA● cppUnit - C++● PHPUnit - PHP● NUnit - .NETAll have: Test Runner, Test Cases, Test fixtures, Test suites, Test Execution

Page 41: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

GDB & Valgrind

Page 42: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

GDB CheatsheetWhen compiling, use -g, so debugging information can be in your executable file.e.g. $gcc -g -o myprogram myprogram.cTo start gdb: $gdb ./myprogram (e.g. “$gdb ./Driver”)

In the GDB console:

• run (to run your program)

• break x (where x is the name of your function in your program, line number)

• next (executes one more line, without stepping into the function if called)

• continue (when the program has stopped, it resumes execution)

• step (executes one more line, stepping into a function if called)

• print x (where x is an expression that can involve constants and variables)

• quit (to quit out of gdb)

Page 43: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Valgrind CheatsheetCompile using debug information option (-g) to get more info in valgrind.$gcc -g -o myprogram myprogram.c

To run valgrind:$valgrind ./myprogram (e.g. $valgrind ./Driver)

Page 44: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Valgrind output==15640==

==15640== HEAP SUMMARY:

==15640== in use at exit: 10 bytes in 5 blocks

==15640== total heap usage: 5 allocs, 0 frees, 10 bytes allocated

==15640==

==15640== LEAK SUMMARY:==15640== definitely lost: 10 bytes in 5 blocks

==15640== indirectly lost: 0 bytes in 0 blocks

==15640== possibly lost: 0 bytes in 0 blocks

==15640== still reachable: 0 bytes in 0 blocks

==15640== suppressed: 0 bytes in 0 blocks

==15640== Rerun with --leak-check=full to see details of leaked memory

Page 45: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Valgrind output (no leaks)==18957==

==18957== HEAP SUMMARY:

==18957== in use at exit: 0 bytes in 0 blocks

==18957== total heap usage: 5 allocs, 5 frees, 10 bytes allocated

==18957==

==18957== All heap blocks were freed -- no leaks are possible==18957==

==18957== For counts of detected and suppressed errors, rerun with: -v

==18957== ERROR SUMMARY: 28 errors from 15 contexts (suppressed: 12 from 8

Page 46: CSE 15L Winter 2015 · Test-Driven Development JUnit is a tool used for TDD In TDD, tests are written before software. You must understand requirements first! Regression testing Everytime

Thank you !