Today I noticed this older post on stackoverflow and the first thing I thought, this would be great if done in a TDD manner. Very short too.

The question is

**Design a function f, such that:

f(f(n)) == -n

Where n is a 32 bit signed integer; you can’t use complex numbers arithmetic.

If you can’t design such a function for the whole range of numbers, design it for the largest range possible.

So you have to have a number *n*, run it through a method twice an get *-n* as the result. In fact negating the result.

1 becomes -1 and -1 becomes 1. usually remainder 0.

Hey I just got my test cases.

so first test case is to see if 1 becomes -1

```vbnet Imports NUnit.Framework

<TestFixture()> _ Public Class TestFunctionF

```
<Test()> _
Public Sub testfunctiongivesnegativewhengivinnegative()
Dim fclass As New Fclass
Assert.AreEqual(-1, fclass.f(fclass.f(1)))
End Sub
```

End Class``` use some resharper magic and I get this.

```vbnet Public Class Fclass Shared n As Integer

```
Public Function f(ByVal o As Integer) As Integer
Throw new NotImplementedException()
End Function
```

End Class``` Run the test and get a red light.

The simplest I could think of to solve the puzzle was this.

```vbnet Public Class Fclass Shared n As Integer

```
Public Function f(ByVal o As Integer) As Integer
n += 1
If n = 1 Then
Return -o
Else
n = 0
Return o
End If
End Function
```

End Class``` Sorry for all the bad naming of variables, but that made the test pass.

Now let’s see if test 2 passes also

```
vbnet
<Test()> _
Public Sub testfunctiongivespositivewhengivennegative()
Dim fclass As New Fclass
Assert.AreEqual(1, fclass.f(fclass.f(-1)))
End Sub
```

and it does.

Now for test number 3 to see if it gives 0 when needed.

```
vbnet
<Test()> _
Public Sub testfunctiongiveszerowhengivenzero()
Dim fclass As New Fclass
Assert.AreEqual(0, fclass.f(fclass.f(0)))
End Sub
```

and it does.

In the end, perhaps this wasn’t the best case for TDD since we only had one red, but that was because the simplest solution seemed to work from the word go for all solutions.