diff options
-rw-r--r-- | testing_and_debugging/script.rst | 35 | ||||
-rw-r--r-- | testing_and_debugging/slides.org | 13 | ||||
-rw-r--r-- | testing_and_debugging/slides.tex | 52 |
3 files changed, 64 insertions, 36 deletions
diff --git a/testing_and_debugging/script.rst b/testing_and_debugging/script.rst index 607aee5..7a18df9 100644 --- a/testing_and_debugging/script.rst +++ b/testing_and_debugging/script.rst @@ -63,7 +63,7 @@ and determining that it meets its required results. ... R5 Lets first write a simple function to calculate gcd of two numbers. -Open an editor and type the code shown on the slide. +Open an editor and type the code shown on the slide and save it as gcd.py .. L5 @@ -78,6 +78,14 @@ def gcd(a, b): .. R6 +Save the file as gcd.py in /home/fossee/ path + +.. L6 + +{{{ Save the file as gcd.py }}} + +.. R7 + Now we need to evaluate this function. That is, we have to check whether this function successfully gives us the gcd of two whole numbers. Thus we need a set of inputs and the exact outputs that are expected for @@ -86,10 +94,10 @@ those input test cases. Let our test case be 48 and 64 as *a* and *b* respectively. For this test case we know that the GCD is 16. So that is the expected output. -.. L6 - .. L7 +.. L8 + {{{ Show slide,Test for gcd.py }}} def gcd(a, b): @@ -105,19 +113,11 @@ test case we know that the GCD is 16. So that is the expected output. {{{ Pause for some time and then continue }}} -.. R7 - -Let us include code for testing in our gcd.py file -Add the remaining lines of code to the file. - .. R8 -Save the file as gcd.py in /home/fossee/ path - -.. L8 - -{{{ Save the file as gcd.py }}} - +Let us include code for testing in our file gcd.py and +add the remaining lines of code to the file. + .. R9 Let us now run the script and test our code @@ -128,7 +128,7 @@ We run the code by providing the entire path where the file is located. {{{ Open a terminal }}} :: - python /home/fossee/gcd_py + python /home/fossee/gcd.py .. R10 @@ -231,6 +231,7 @@ We add this code piece to automate the test. Let us now test this code. Open the file gcd.py which we had created before and add this piece of code +accordingly. .. R17 @@ -245,7 +246,7 @@ Now, we run it as, {{{ Switch to terminal }}} :: - python /home/fossee/gcd_py + python /home/fossee/gcd.py .. R19 @@ -266,7 +267,7 @@ For the same inputs as gcd write automated tests for LCM. .. R21 We shall make use of the same automated test code which we had used for GCD -with minor changes. +with minor changes. Use the data from the file lcmtestcases.txt . The solution is on your screen. .. L21 diff --git a/testing_and_debugging/slides.org b/testing_and_debugging/slides.org index cd0e32b..cb5f7ff 100644 --- a/testing_and_debugging/slides.org +++ b/testing_and_debugging/slides.org @@ -93,6 +93,19 @@ if \_\_name\_\_ == '\_\_main\_\_': | 12 | 28 | 4 | | 18 | 36 | 18 | | 4678 | 39763 | 2339 | +* Code piece +#+begin_src python +if __name__ == '__main__': + for line in open('testcases.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = int(numbers[2]) + if gcd(x, y) != result: + print "Failed gcd test for", x, y + else: + print "Test passed", result +#+end_src * Exercise 2 - For the same inputs as gcd write automated tests for LCM. * Solution 2 diff --git a/testing_and_debugging/slides.tex b/testing_and_debugging/slides.tex index 7599107..baa0811 100644 --- a/testing_and_debugging/slides.tex +++ b/testing_and_debugging/slides.tex @@ -1,4 +1,4 @@ -% Created 2011-07-19 Tue 16:14 +% Created 2011-08-10 Wed 14:46 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} @@ -143,20 +143,34 @@ if \_\_name\_\_ == `\_\_main\_\_': \begin{center} -\begin{tabular}{|l|c|c|c} -\hline - 12 & 28 & 4 \\ -\hline - 18 & 36 & 18 \\ -\hline - 4678 & 39763 & 2339 \\ -\hline +\begin{tabular}{rrr} + 12 & 28 & 4 \\ + 18 & 36 & 18 \\ + 4678 & 39763 & 2339 \\ \end{tabular} \end{center} \end{frame} +\begin{frame}[fragile] +\frametitle{Code piece} +\label{sec-9} + +\lstset{language=Python} +\begin{lstlisting} +if __name__ == '__main__': + for line in open('testcases.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = int(numbers[2]) + if gcd(x, y) != result: + print "Failed gcd test for", x, y + else: + print "Test passed", result +\end{lstlisting} +\end{frame} \begin{frame} \frametitle{Exercise 2} -\label{sec-9} +\label{sec-10} \begin{itemize} @@ -165,7 +179,7 @@ if \_\_name\_\_ == `\_\_main\_\_': \end{frame} \begin{frame}[fragile] \frametitle{Solution 2} -\label{sec-10} +\label{sec-11} \lstset{language=Python} \begin{lstlisting} @@ -189,7 +203,7 @@ if __name__ == '__main__': \end{frame} \begin{frame}[fragile] \frametitle{Meaning full names} -\label{sec-11} +\label{sec-12} \lstset{language=Python} \begin{lstlisting} @@ -202,7 +216,7 @@ rAmount = nCoins * denom \end{frame} \begin{frame} \frametitle{Code style} -\label{sec-12} +\label{sec-13} \begin{itemize} @@ -219,7 +233,7 @@ rAmount = nCoins * denom \end{frame} \begin{frame} \frametitle{Exercise 3} -\label{sec-13} +\label{sec-14} \begin{itemize} @@ -232,7 +246,7 @@ rAmount = nCoins * denom \end{frame} \begin{frame}[fragile] \frametitle{Solution 3} -\label{sec-14} +\label{sec-15} \lstset{language=Python} \begin{lstlisting} @@ -242,7 +256,7 @@ quotient = dividend / divisor \end{frame} \begin{frame}[fragile] \frametitle{Using idb} -\label{sec-15} +\label{sec-16} \small \begin{lstlisting} @@ -266,7 +280,7 @@ ipdb> total \end{frame} \begin{frame} \frametitle{Summary} -\label{sec-16} +\label{sec-17} In this tutorial, we have learnt to, @@ -282,7 +296,7 @@ ipdb> total \end{frame} \begin{frame} \frametitle{Evaluation} -\label{sec-17} +\label{sec-18} \begin{enumerate} @@ -307,7 +321,7 @@ ipdb> total \end{frame} \begin{frame} \frametitle{Solutions} -\label{sec-18} +\label{sec-19} \begin{enumerate} |