Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: TDD can be useful even when it doesn't achieve what you want Date: Thu, 18 Feb 2016 21:24:14 +1300 Lines: 61 Message-ID: References: <56c34b5c$0$18569$e4fe514c@newszilla.xs4all.nl> <56c4b474$0$4233$e4fe514c@newszilla.xs4all.nl> <56c4ef61$0$4197$e4fe514c@newszilla.xs4all.nl> <56c573ab$0$4031$e4fe514c@newszilla.xs4all.nl> <56c5798b$0$4048$e4fe514c@newszilla.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Uk7R8s+orV1AUFhc6IeOMgsivV77Xxdwf64vAA6L40pW45kDWx Cancel-Lock: sha1:fCfbcvN1Fx/ZaqzDlK9V9VrltIk= User-Agent: Mozilla/5.0 (X11; SunOS i86pc; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Xref: csiph.com comp.lang.c++:41025 Ian Collins wrote: > Wouter van Ooijen wrote: >> Op 18-Feb-16 om 8:42 AM schreef Ian Collins: >>> Wouter van Ooijen wrote: >>>> Op 17-Feb-16 om 11:19 PM schreef Ian Collins: >>>>> >>>>> Let me ask you why you wouldn't obey the rules for say a device driver? >>>>> I certainly do and for such low level fiddly code I find TDD to be an >>>>> ideal tool. I like to step through, almost bit by bit, tasks such as >>>>> setting up device registers. The steps, both conceptually and in code, >>>>> are small and ideally suited to an incremental test/code/refactor cycle >>>>> such as TDD. Being close to a pure coding exercise, any objections to >>>>> TDD and design are irrelevant at this level. >>>> >>>> Let's say I write code for a UART to transmit data at a (run-time) >>>> specified baudrate. What would your (first) test be? >>> >>> It wouldn't be the first test (there would be more generic configuration >>> to test first), >> >> such as? > > That depends on the device, if the UART is stand alone, there might not > be any but if it is part of a processor, there will probably > configuration registers that have to be setup before the UART can be used. > >>> but the last test for the requirement would be that the >>> drive sits the correct bits in the the appropriate configuration >>> register(s). >> >> But how do you determine what is the correct setting (or: how do you >> make sure that the settings you have determined have the desired effect)? > > The device specification! > > I actually wrote tests for a UART driver on an ARM STM32 processor last > year. To verify the settings I had a block of mapped memory at the > appropriate address for the device registers and simply tested for the > correct values getting written. An example: void TestUART::testConstructDeviceOneAssignsCorrectBaudRateDivider() { const auto baudRate = 100; const auto clockFrequency = 1000; test::STM32F3xxuPClockDriver_GetClock::willReturn(clockFrequency); STM32F3xxUARTDriver( STM32F3xxUARTDriver::USARTPort1, nullptr, 0, baudRate ); TS_ASSERT_EQUALS( clockFrequency/baudRate, usart1->BRR ); } test::STM32F3xxuPClockDriver_GetClock is a mock for another interface, usart1 is an address defined in one of the headers. -- Ian Collins