CSE 15L Winter 2015 Midterm :) Review
Makefiles
Makefiles - The Overview Questions 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 Format Dependencies can be files or targets Basic Structure: target: dependencies action Note: Each action must be tab-indented
Makfile - The Example
Suffix Directives
Makefile Macros
Calling another Makefile
Unix Shell Scripting
Piping/Filtering What 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 Apr ls wc man 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
Declarations Are these two different? 1) y=5 2) 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 for i in {1..10} do while, do, done if [ $[i % 2] = 1 ]; then echo $i is odd else echo $i is even fi done
Shell Scripting Which one of these is the correct way to start a bash script? A.!# /bin/bash B. # bin/bash C.! /bin/bash D. #! /bin/bash
Shell Scripting Which one of these is the correct way to start a bash script? A.!# /bin/bash B. # bin/bash C.! /bin/bash D. #! /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
Examples for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done c=0 while [ $c -lt 10 ] do echo The counter is $c let c=c+1 done echo Enter a fruit: read fruit case $fruit in apple) echo Apple ;; banana) echo Banana ;; *) echo Something else ;; esac
Examples (more) ls while read line do echo Hello ${line}! done filename=$1 cat $filename while read line do echo Hello ${line}! done filename=$1 while read line do echo Hello ${line}! done < $filename
Standard Streams http://upload.wikimedia.org/wikipedia/commons/7/70/stdstreams-notitle.svg
Redirecting Streams http://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 Commands Command pgm > file pgm < file pgm >> file n > file n >> file Description Output of pgm is redirected to file Program pgm reads its input from file. Output of pgm is appended to file. Output from stream with descriptor n redirected to 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 commands git init Initialize a new git repo git add <file> Add/Stage a new file to your repo git commit -m message Commit staged changes to your repo
Git commands git status Show the status of files in the directory git log Log of all the commits made to the repo git diff File differences for unstaged, modified files
Git commands git pull pull changes from a remote server git push push changes to a remote server git remote add adding a remote server
Git commands git branch <branchname> Create a new branch git checkout <branchname> Checkout new branch git 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 -.NET All have: Test Runner, Test Cases, Test fixtures, Test suites, Test Execution
GDB & Valgrind
GDB Cheatsheet When compiling, use -g, so debugging information can be in your executable file. e.g. $gcc -g -o myprogram myprogram.c To 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 Cheatsheet Compile 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!