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

Post on 07-Jul-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSE 15L Winter 2015Midterm :) Review

Makefiles

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?

Makefiles - The FormatDependencies can be files or targets

Basic Structure:

target: dependenciesaction

Note: Each action must be tab-indented

Makfile - The Example

Suffix Directives

Makefile Macros

Calling another Makefile

Unix Shell Scripting

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

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

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

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

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

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

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

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

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

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

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

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”

Standard Streams

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

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

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

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

Git

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

Local git project layout

Git file lifecycle

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

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

Git commandsgit pull

pull changes from a remote servergit push

push changes to a remote servergit remote add

adding a remote server

Git commandsgit branch <branchname>

Create a new branchgit checkout <branchname>

Checkout new branchgit merge <branchname>

Merge branch with current branch

TDD & JUnit

Test-Driven Development

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.

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.

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.

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.

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!

JUnit● Tests pass when they return without failing

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

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

GDB & Valgrind

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)

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)

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

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

Thank you !

top related