\n",
"\n",
"
Exercise One: TDD
\n",
"\n",
"\n",
"Try and write your own attempt at TTD for the following problems:\n",
"\n",
"
\n",
" - Write an implementation of a fizz buzz function. Fizz Buzz is a children's game, where players count up from one around a circle. \n",
"
\n",
" - However, any number divisible by 3 is replaced by the word \"fizz\".
\n",
" - Any number divisible by 5 is replaced by the word \"buzz\".
\n",
" - Numbers divisible by both become \"fizz buzz\".
\n",
"
\n",
" Players who say the worng thing lose the game. Writing a program to generate the first n integers in fizz buzz encoding has sometimes been used as an interview question for programmers.\n",
"\n",
" Your function should accept an integer, $x$, and return either $x$, 'fizz'
, 'buzz'
or 'fizz buzz'
according to the rules above.
\n",
" \n",
"
\n",
" \n",
" Remember, in TTD first you write a test for an aspect of the function, then you write the code to solve it.\n",
" \n",
"- Write a program to put the elements of an $n$ dimensional
numpy
array, $X$, into order of size.\n",
" \n",
" - The code should have output $Y$ with
Y[i,...]<[j,...]
for all i<j
. \n",
" - The code should have output $Y$ with
Y[k,i..]<[k,j,..]
for all i<j
and fixed k \n",
" - And so on.
\n",
" - Note that this means when written in the form\n",
"
[[..[x_1, .., x_n], [x_n+1, .., x_2n], .., x_N]]
\n",
"we have x_n< x_n+i
for all i>0
. \n",
"
\n",
"\n",
"- Write a function to accept or reject a string as a candidate for a password based on the following criteria:\n",
"
\n",
" - The string is between 8 and 1024 characters long.
\n",
" - The string contains a number
\n",
" - The string contains a capital letter
\n",
" - The string contains none of the following characters:
@<>!
\n",
"
\n",
"
\n",
"\n",
"Write only enough new code to satisfy each test you write, and to fix your previous tests, before moving on to another test stage. Once the test passes, remember to have a look at your code to see if anything can be refactored.\n",
"\n",
"The goal here is to concentrate on the TDD process, not on the code itself, but for completeness, [model answers](https://msc-acse.github/ACSE-1/lectures/lecture10-solutions.html) are available.\n",
" \n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At this point you can hopefully see that \n",
"1. TDD is a fine ideal for correct software engineering\n",
"2. No sane person would write all their tests at that level of detail and in that order all the time.\n",
"\n",
"However, there are still some very important ideas to pull out of the paradigm:\n",
"\n",
"1. Make sure your tests can fail if things go wrong.\n",
"2. Consider both the usual and the (obvious) corner cases.\n",
"3. Each bug you fix is a missing test you might need to add."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ways to implement and run tests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets have a slighly more concrete example. We'll look for solutions to the quadratic equation\n",
"$$ ax^2 + bx + c =0,$$\n",
"where $x$ is a real number. The formula for solutions is\n",
"$$ x=\\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}.$$\n",
"Now to write some code in a module file"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"import numpy as np # for the sqrt function\n",
"\n",
"def solve_quadratic(a,b,c):\n",
" \"\"\"Solve a quadratic equation ax^2+bx+c=0 in the reals\"\"\"\n",
" if 0