SlideShare a Scribd company logo
1 of 74
1
Test-Driven Development for
Embedded C - Workshop
By
James Grenning
!
Nov 15, 2013
Talk to me on Twitter
@jwgrenning!
!
Find my book at!
http://www.pragprog.com/titles/jgade
!
Find me on linkedin.com
http://www.linkedin.com/in/jwgrenning
Please remind me how we met.
!
http://www.wingman-sw.com
http://www.wingman-sw.com/blog
http:// www.jamesgrenning.com
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Write Three Post-it Notes
2
What do you
like about
development?
I like...
I don’t like...
What is your
least favorite
development
activity?
What percentage of
your development
time is spent testing
and removing bugs
from your code?
coding %
+ testing %
+ debugging %
= 100%
There is a Lot to TDD
3
We’ll Look at a Few Parts of TDD
4
There’s more.
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Questions I’ll Answer
• Why should I care about TDD?
• What is TDD?
• How do I test code with dependencies?
• How is TDD adapted to embedded development?
5
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Saving Testing Until the End
Requirements
Code
Design
Test
Time
NovemberSeptemberJulyMayMarch
Test
6
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
devconfu
Riga, Latvia
www.renaissancesoftware.net
james@renaissancesoftware.net
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Agile without TDD
with Hardening Sprints
7
Sprint 1 Sprint 2 Sprint 3 Sprint 4 Hardening Sprint
Test and Fix
Test and Fix Test and Fix
Test and Fix
Test and FixTest and Fix
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
This Work Flow is Designed to
Produce Defects
Development
Test
Defects
8
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Your program will have
bugs. And they will
surprise you when you
find them.
9
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Edsger Dijkstra
Those who want really reliable
software will discover that they must
find means of avoiding the majority
of bugs to start with, and as a result,
the programming process will
become cheaper. If you want more
effective programmers, you will
discover that they should not waste
their time debugging, they should
not introduce the bugs to start with.
10
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
11
Can we Realize Dijkstra’s
Dream and
Prevent Defects with
Test Driven Development?
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
The Physics of Debug Later
Programming (DLP)
• As Td increases, Tfind increases dramatically
• Tfix is usually short, but can increase with Td
Bug discoveryMistake made
(bug injection)
Bug found Bug fixed
Td Tfind T fix
Time
12
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
A Bug’s Life
From http://www.softwaretestinghelp.com/bug-life-cycle/
13
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
What is TDD?
14
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
• Write a test
• Watch it not build
• Make it build, but fail
• Make it pass
• Refactor (clean up any mess)
• Repeat until done
T
D
D
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved. Test Driven Development
www.renaissancesoftware.net
james@renaissancesoftware.net
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved. For use by training attendees.
If test moved upstream, could we get
working features to flow?
16
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Development and Test are a Continuum
preventing defects
17
Development
Test
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
The Physics of Test Driven
Development
• When Td approaches zero, Tfind approaches zero
• In many cases, bugs are not around long enough to be considered
bugs.
• See: http://www.renaissancesoftware.net/blog/archives/16
Mistake
discovery
Mistake
made
Root cause
found
Mistake fixed
T d Tfind T fix
Time
18
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Let’s Test-Drive a CircularBuffer that
Holds Integers
+ Put
+ Get
+ IsEmpty
+ IsFull
//etc
CircularBuffer
19
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Create a Test-List
(avoid analysis paralysis)
Circular Buffer Tests
Initially Empty
Transition to empty
Transition to Full
Transition from Full
Put-Get FIFO
Put to full
Get from empty
Filled but not wrapped around
Wrap around
20
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Have an Idea of How it Will Work
Out-Index In-Index
23 7 66 12 99 16 90
Out-Index In-Index
42 -6 23 7 66 12 99 16 90
Out-Index In-Index
99
21
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Setup a Test Fixture
TEST_GROUP(CircularBuffer)	
{	
CircularBuffer* buffer;	
!
void setup()	
{	
buffer = CircularBuffer_Create();	
}	
!
void teardown()	
{	
CircularBuffer_Destroy(buffer);	
}	
};	
!
TEST(CircularBuffer, TestName)	
{	
}
22
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Make a Bare Skeleton of the
Production Code Header File
#ifndef D_CircularBuffer_H	
#define D_CircularBuffer_H	
!
typedef struct CircularBuffer CircularBuffer;	
!
CircularBuffer * CircularBuffer_Create();	
void CircularBuffer_Destroy(CircularBuffer *);	
!
#endif // D_CircularBuffer_H	
23
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Make a Bare Skeleton of the
Production Code Source File
#include "CircularBuffer.h"	
#include <stdlib.h>	
!
struct CircularBuffer	
{	
int dummy;	
} ;	
!
CircularBuffer* CircularBuffer_Create()	
{	
CircularBuffer* self = calloc(1, sizeof(CircularBuffer));	
return self;	
}	
!
void CircularBuffer_Destroy(CircularBuffer* self)	
{	
free(self);	
}	
!
24
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Choose a Test
Write the test
Make the test
link
Make the test
compile
Compilation error
Link error
New test failsMake the test pass
Refactor
(Make it right)
All tests pass
All tests pass
No more tests
Choose a test
Start
Compilation error
Link error
DONE!
Programming error
Compiles Clean
25
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Which Test Should We Start With?
Out-Index In-Index
41 59 14 33 7 31
In-Index Out-Index
26
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Exercise
27
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Write Three Post-it Notes
What concerns
you about TDD?
28
Liked
Concern
What did you
like about TDD?
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
A Complex system that
works is invariably
found to have evolved
from a simple system
that worked.
29
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
30
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Sustainability
31
Traditional
development
The cost of
doing business
debug-later-
programming
Test-driven
development
Speculate Code Test/Debug Debug
Specu
late
Test and Code Debug
Side-effect
Defect
Debug
Debu
Debug
Side-effect
Defect
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Managing Cognitive Load
32
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Debug Later Programming is Like
Juggling
33
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
How Many Knives Do
You Like to Juggle?
• TDD is solving one
problem at a time
Photo by Andrew Bossi (Own work) [CC-BY-SA-2.5
(www.creativecommons.org/licenses/by-sa/2.5)], via
Wikimedia Commons
34
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
!
!
• Get good at being careful and you can go fast.
Careful, the fast way is.
35
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Adaptation for Embedded
Software
36
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
What is special about embedded
• Concurrent HW development
• HW bottleneck
• Cross compilation
• Limited memory and IO
• Target debug
• Architecture
37
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
38
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Hardware is
Scarce!
• It does not exist.
• It is being used by
someone else.
• It has bugs of its
own.
39
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
40
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
41
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Minimize
Debug
on
Hardware
DoH!
Copyright Fox Broadcasting. Used under fair use.
42
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Write a Test
Make it Pass
Refactor
Stage 1
Compile
for Target
Processor
Stage 2
Run Tests
in the Eval
Hardware
Stage 3
Run Tests
in Target
Hardware
Stage 4
Acceptance
Tests
Stage 5
More Frequent Less Frequent
TDD Adaptation for Embedded
Development
43
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
TDD Adaptation for Embedded
Development
See : http://renaissancesoftware.net/files/articles/ProgressBeforeHardware.pdf
Write a Test
Make it Pass
Refactor
Stage 1
Compile
for Target
Processor
Stage 2
Run Tests
in the Eval
Hardware
Stage 3
Run Tests
in Target
Hardware
Stage 4
Acceptance
Tests
Stage 5
More Frequent Less Frequent
44
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
35
Continuous Integration - Embedded
3. CIS notices changes, refreshes its
local copy, builds and runs host
based tests.
2. Developer
checks in work
regularly.
1. Developer has
all unit tests
passing and
checks in work.
4. After successful
host build/test, CIS
builds and hands
images to TMS to
deploy to
simulator, eval/
reference boards
or target system.
The SCR, CIS and TMS are not necessarily
separate hardware systems.
Developer
Workstation
Source Code
Repository
Continuous Integration
Server
Target System
Eval/Reference
System
Simulator
Test Management
System
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Problems Found at Appropriate Stage
Stage Problems Likely to Find in Stage
1 Logic, design, modularity, interface, boundary conditions
2 Compiler compatibility (language features)!
Library compatibility (header files, declarations)
3 Processor executions problems (compiler and library bugs)!
Portability problems (word size, alignment, endian)
4 Ditto stage 3!
Hardware integration problems!
Misunderstood hardware specifications
5 Ditto stage 4!
Misunderstood feature specification
46
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
How do I test Code with
Dependencies?
47
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Spaghetti Slide
48
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
A Module with Collaborators
• Every minute, the
RTOS wakes up
the Light
Scheduler.
• If it is time for
one of the lights to
be controlled, the
LightController is
told to turn on/off
the light.
Time Service
+ GetTime()
+ SetPeriodicAlarm()
Light
Scheduler
+ScheduleTurnOn()
+RemoveSchedule()
+WakeUp()
Light Controller
+ On(id)
+ Off(id)
Hardware RTOS
<<anonymous callback>>
Admin
Console
49
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Program to Interfaces
• Separate interface
and implementation
as separate entities.
• This design has
good separation of
responsibilities
<<interface>>
Time Service
+ GetTime()
+ SetPeriodicAlarm()
Light
Scheduler
+ ScheduleTurnOn()
+ RemoveSchedule()
+WakeUp()
<<interface>>
Light Controller
+ On(id)
+ Off(id)
Model 42 Hardware RTOS
<<anonymous callback>>
Model 42
Light Controller
RTOS
Time Service
<<implements>> <<implements>>
Admin
Console
50
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Testing a Module with Collaborators
• Use the real
collaborators if
you can.
• Use fakes when
you must. <<interface>>
Time Service
+ GetTime()
+ SetPeriodicAlarm()
Light
Scheduler
Test
Light
Scheduler
+ ScheduleTurnOn()
+ RemoveSchedule()
+wakeUp()
<<interface>>
Light Controller
+ On(id)
+ Off(id)
Light Controller
Spy
Fake
Time Service
<<implements>> <<implements>>
51
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Testing the Scheduler
TEST(LightScheduler, ScheduleOnTodayNotTimeYet)	
{	
LightScheduler_ScheduleTurnOn(3, EVERYDAY, 1000);	
FakeTimeSource_SetMinute(999);	
!
LightScheduler_Wakeup();	
!
LONGS_EQUAL(LIGHT_NA, LightControllerSpy_GetState(3));	
}	
!
TEST(LightScheduler, ScheduleOnTodayItsTime)	
{	
LightScheduler_ScheduleTurnOn(3, EVERYDAY, 1000);	
FakeTimeSource_SetMinute(1000);	
!
LightScheduler_Wakeup();	
!
LONGS_EQUAL(LIGHT_ON, LightControllerSpy_GetState(3));	
}	
52
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Partial Skeleton Let’s You Try the
Design and Test Ideas Early
53
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Test-Doubles
54
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Test Double
• A test double stands in for some part of the
production code during testing
• Test doubles are part of the test fixture that allows a
module to be unit tested
55
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Test Dependency Octopus
56
Test
Code
Under Test
Depended
On
Component
DOC DOC
Transitively
DOC
TDOC TDOCTDOCTDOC
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Managing the Octopus
57
Test
Code
Under Test
Test Double Test Double Test Double
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Use Production Code when you Can
Test Doubles when you Must
58
Test
Code
Under Test
Depended
On
Component
Test Double Test Double
Transitively
DOC
TDOC
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Kinds of Test Doubles
[XUNIT]
• Other names
– Fakes
– Mock object
– Spy
– Null object
– Exploding fakes
– there are others
59
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
What are the Test Double
Options in C?
• Link-time substitution.
– Substitute whole object files or libraries
• Run time substitution.
– Function pointers.
– Function pointer tables.
• Preprocessor substitution
– My least favorite, but sometimes necessary
• Use the simplest option that will work.
60
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
What are the Test Doubles
Options in C++?
• Link-time substitution.
– Substitute whole object files or libraries
• Run time substitution.
– To substitute one class for another
–Virtual functions.
–Interface classes.
– To substitute one function for another
–C function pointers.
• Preprocessor substitution
– my least favorite, but sometimes necessary
• Use the simplest option that will work.
61
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Getting Around Tool
Dependencies
62
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Example Problems
• Compiler vendors extend C
• Embedded ‘asm’
• Header files lock you to the vendor
63
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Compiler vendors extend C
64
//snip...!
/* Address Mode Register */!
extern cregister volatile unsigned int AMR; !
//snip...!
!
//snip...!
interrupt void one_ms_tic(void)!
{!
...!
}!
//snip...
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Hide Them with a
Preprocessor Forced Include
65
//hide_stuff.h	
!
#define interrupt	
#define cregister	
!
Force Include hidestuff.h, to make them go away
read the blog: http://www.renaissancesoftware.net/blog/archives/
249
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Developers embed ‘asm’
66
void foo(void)	
{	
	 do	
	 {	
	 asm("NOP");	
	 asm("PLOP");	
	 asm("READ 0");	
	 status = IORead(0);	
	 } while ((status &amp; ReadyBit) == 0);	
}
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Hide asm Forced Include
67
//nullasm.h	
!
#define asm	
!
Force Include nullasm.h, to make asm go away
read the blog: http://www.renaissancesoftware.net/blog/archives/
136
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
AsmSpy Test Double
68
#define asm AsmSpy	
!
void AsmSpy_Create(int size);	
void AsmSpy_Destroy(void);	
void AsmSpy(const char *);	
const char * AsmSpy_Debrief(void);	
Force Include AsmSpy.h All asm() directives are
converted to AsmSpy() calls, allowing capture.
read the blog: http://www.renaissancesoftware.net/blog/archives/
136
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
AsmSpy Tests Show How to Monitor
the asm
69
TEST(AsmSpy, CaptureAsmInstructions)	
{	
	 AsmSpy("NOP");	
	 AsmSpy("FLOP");	
	 AsmSpy("POP");	
	 STRCMP_EQUAL("NOP;FLOP;POP;", AsmSpy_Debrief());	
}	
!
Force Include AsmSpy.h, to capture asm instructions
read the blog: http://www.renaissancesoftware.net/blog/archives/
136
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Header Files that Lock You to the
Vendor’s Compiler
70
#if defined(_ACME_X42)	
typedef unsigned int Uint_32;	
typedef unsigned short Uint_16;	
typedef unsigned char Uint_8;	
	
typedef int Int_32;	
typedef short Int_16;	
typedef char Int_8;	
#elif defined(_ACME_A12)	
typedef unsigned long Uint_32;	
typedef unsigned int Uint_16;	
typedef unsigned char Uint_8;	
	
typedef long Int_32;	
typedef int Int_16;	
typedef char Int_8;	
#else	
#error <acmetypes.h> is not supported for this environment	
#endif
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Create a #include Test Double
71
#include <stdint.h>	
!
typedef uint32_t	 	 Uint_32;	
typedef uint16_t	 	 Uint_16;	
typedef uint8_t	 	 Uint_8;	
!
typedef int32_t	 	 Int_32;	
typedef int16_t	 	 Int_16;	
typedef int8_t	 	 Int_8;	
!
#endif /* ACMETYPES_H_ */	
Also, limit dependencies on vendor specific includes
There is a Lot More to TDD
72
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Talk to me on Twitter
@jwgrenning!
!
Find my book at!
http://www.pragprog.com/titles/jgade
!
Find me on linkedin.com
http://www.linkedin.com/in/jwgrenning
Please remind me how we met.
!
http://www.wingman-sw.com
http://www.wingman-sw.com/blog
http:// www.jamesgrenning.com
73
devconfu, Nov 2013
Riga, Latvia
www.wingman-sw.com
james@wingman-sw.com
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
devconfu
Riga, Latvia
www.renaissancesoftware.net
james@renaissancesoftware.net
Copyright © 2008-2013 James W. Grenning	

All Rights Reserved.
Partners:

More Related Content

What's hot

Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)Suman Guha
 
BDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationBDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationJohn Ferguson Smart Limited
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung Nguyen Thanh
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Lars Thorup
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go langAmal Mohan N
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
 
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...Edureka!
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationhugo lu
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTsuhasreddy1
 

What's hot (20)

Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Testing in go
Testing in goTesting in go
Testing in go
 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin language
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)
 
BDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationBDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world application
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
Unit Testing in Android
Unit Testing in AndroidUnit Testing in Android
Unit Testing in Android
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Bdd Introduction
Bdd IntroductionBdd Introduction
Bdd Introduction
 
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPT
 

Viewers also liked

Agile Embedded Software
Agile Embedded SoftwareAgile Embedded Software
Agile Embedded SoftwareJames Grenning
 
Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!Jessica Mauerhan
 
TDD with BDD in PHP and Symfony
TDD with BDD in PHP and SymfonyTDD with BDD in PHP and Symfony
TDD with BDD in PHP and SymfonyKamil Adryjanek
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringAndy Maleh
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven developmentStephen Fuqua
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
Test driven development in meteor
Test driven development in meteorTest driven development in meteor
Test driven development in meteorMichael Lazarski
 
BDD presentation
BDD presentationBDD presentation
BDD presentationtemebele
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 

Viewers also liked (14)

Agile Embedded Software
Agile Embedded SoftwareAgile Embedded Software
Agile Embedded Software
 
Python SOLID
Python SOLIDPython SOLID
Python SOLID
 
TDD, BDD, RSpec
TDD, BDD, RSpecTDD, BDD, RSpec
TDD, BDD, RSpec
 
Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!
 
TDD with BDD in PHP and Symfony
TDD with BDD in PHP and SymfonyTDD with BDD in PHP and Symfony
TDD with BDD in PHP and Symfony
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Tdd in unity
Tdd in unityTdd in unity
Tdd in unity
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Test driven development in meteor
Test driven development in meteorTest driven development in meteor
Test driven development in meteor
 
BDD presentation
BDD presentationBDD presentation
BDD presentation
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 

Similar to Test Driven Development for Embedded C

Grails Worst Practices
Grails Worst PracticesGrails Worst Practices
Grails Worst PracticesBurt Beckwith
 
Patterns of Automation: Simplify Your Test Code
Patterns of Automation: Simplify Your Test CodePatterns of Automation: Simplify Your Test Code
Patterns of Automation: Simplify Your Test CodeTechWell
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integrationhaochenglee
 
Better Functional Design through TDD
Better Functional Design through TDDBetter Functional Design through TDD
Better Functional Design through TDDPhil Calçado
 
Demand Technical Excellence James Grenning keynote at AgileJapan 2013
Demand Technical Excellence James Grenning keynote at AgileJapan 2013Demand Technical Excellence James Grenning keynote at AgileJapan 2013
Demand Technical Excellence James Grenning keynote at AgileJapan 2013Kenji Hiranabe
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
通往測試最高殿堂的旅程 - GTAC 2016
通往測試最高殿堂的旅程 - GTAC 2016通往測試最高殿堂的旅程 - GTAC 2016
通往測試最高殿堂的旅程 - GTAC 2016Chloe Chen
 
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...Demi Ben-Ari
 
Feelin' Groovy: A Groovy Developer in the Java World
Feelin' Groovy: A Groovy Developer in the Java WorldFeelin' Groovy: A Groovy Developer in the Java World
Feelin' Groovy: A Groovy Developer in the Java WorldKen Kousen
 
Jdojo@Gbg Introduction
Jdojo@Gbg IntroductionJdojo@Gbg Introduction
Jdojo@Gbg IntroductionFredrik Wendt
 
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)Sebastian Hensiek
 
Cultural learnings of testing for make benefit glorious nation of startup
Cultural learnings of testing for make benefit glorious nation of startupCultural learnings of testing for make benefit glorious nation of startup
Cultural learnings of testing for make benefit glorious nation of startupGil Tayar
 
Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Peter Gfader
 
Big feature - small sprint
Big feature - small sprint Big feature - small sprint
Big feature - small sprint Igor Goldshmidt
 
Continuous Security Testing
Continuous Security TestingContinuous Security Testing
Continuous Security TestingSteven Mak
 
[2020 git lab commit] continuous infrastructure
[2020 git lab commit] continuous infrastructure[2020 git lab commit] continuous infrastructure
[2020 git lab commit] continuous infrastructureRodrigo Stefani Domingues
 
A minimal Django testing styleguide
A minimal Django testing styleguideA minimal Django testing styleguide
A minimal Django testing styleguideJosephZammit17
 
Agile Testing: It’s a Team Sport
Agile Testing: It’s a Team SportAgile Testing: It’s a Team Sport
Agile Testing: It’s a Team SportTechWell
 

Similar to Test Driven Development for Embedded C (20)

Grails Worst Practices
Grails Worst PracticesGrails Worst Practices
Grails Worst Practices
 
Patterns of Automation: Simplify Your Test Code
Patterns of Automation: Simplify Your Test CodePatterns of Automation: Simplify Your Test Code
Patterns of Automation: Simplify Your Test Code
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integration
 
Better Functional Design through TDD
Better Functional Design through TDDBetter Functional Design through TDD
Better Functional Design through TDD
 
Demand Technical Excellence James Grenning keynote at AgileJapan 2013
Demand Technical Excellence James Grenning keynote at AgileJapan 2013Demand Technical Excellence James Grenning keynote at AgileJapan 2013
Demand Technical Excellence James Grenning keynote at AgileJapan 2013
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Year Zero
Year ZeroYear Zero
Year Zero
 
通往測試最高殿堂的旅程 - GTAC 2016
通往測試最高殿堂的旅程 - GTAC 2016通往測試最高殿堂的旅程 - GTAC 2016
通往測試最高殿堂的旅程 - GTAC 2016
 
Testing in production
Testing in productionTesting in production
Testing in production
 
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...
Hacker vs company, Cloud Cyber Security Automated with Kubernetes - Demi Ben-...
 
Feelin' Groovy: A Groovy Developer in the Java World
Feelin' Groovy: A Groovy Developer in the Java WorldFeelin' Groovy: A Groovy Developer in the Java World
Feelin' Groovy: A Groovy Developer in the Java World
 
Jdojo@Gbg Introduction
Jdojo@Gbg IntroductionJdojo@Gbg Introduction
Jdojo@Gbg Introduction
 
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)
Webinar: Taking your JMeter Test Monitoring To The Next Level (Ft. PerfAcademy)
 
Cultural learnings of testing for make benefit glorious nation of startup
Cultural learnings of testing for make benefit glorious nation of startupCultural learnings of testing for make benefit glorious nation of startup
Cultural learnings of testing for make benefit glorious nation of startup
 
Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity
 
Big feature - small sprint
Big feature - small sprint Big feature - small sprint
Big feature - small sprint
 
Continuous Security Testing
Continuous Security TestingContinuous Security Testing
Continuous Security Testing
 
[2020 git lab commit] continuous infrastructure
[2020 git lab commit] continuous infrastructure[2020 git lab commit] continuous infrastructure
[2020 git lab commit] continuous infrastructure
 
A minimal Django testing styleguide
A minimal Django testing styleguideA minimal Django testing styleguide
A minimal Django testing styleguide
 
Agile Testing: It’s a Team Sport
Agile Testing: It’s a Team SportAgile Testing: It’s a Team Sport
Agile Testing: It’s a Team Sport
 

Recently uploaded

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 

Recently uploaded (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 

Test Driven Development for Embedded C

  • 1. 1 Test-Driven Development for Embedded C - Workshop By James Grenning ! Nov 15, 2013 Talk to me on Twitter @jwgrenning! ! Find my book at! http://www.pragprog.com/titles/jgade ! Find me on linkedin.com http://www.linkedin.com/in/jwgrenning Please remind me how we met. ! http://www.wingman-sw.com http://www.wingman-sw.com/blog http:// www.jamesgrenning.com
  • 2. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Write Three Post-it Notes 2 What do you like about development? I like... I don’t like... What is your least favorite development activity? What percentage of your development time is spent testing and removing bugs from your code? coding % + testing % + debugging % = 100%
  • 3. There is a Lot to TDD 3
  • 4. We’ll Look at a Few Parts of TDD 4 There’s more.
  • 5. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Questions I’ll Answer • Why should I care about TDD? • What is TDD? • How do I test code with dependencies? • How is TDD adapted to embedded development? 5
  • 6. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Saving Testing Until the End Requirements Code Design Test Time NovemberSeptemberJulyMayMarch Test 6
  • 7. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. devconfu Riga, Latvia www.renaissancesoftware.net james@renaissancesoftware.net Copyright © 2008-2013 James W. Grenning All Rights Reserved. Agile without TDD with Hardening Sprints 7 Sprint 1 Sprint 2 Sprint 3 Sprint 4 Hardening Sprint Test and Fix Test and Fix Test and Fix Test and Fix Test and FixTest and Fix
  • 8. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. This Work Flow is Designed to Produce Defects Development Test Defects 8
  • 9. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Your program will have bugs. And they will surprise you when you find them. 9
  • 10. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Edsger Dijkstra Those who want really reliable software will discover that they must find means of avoiding the majority of bugs to start with, and as a result, the programming process will become cheaper. If you want more effective programmers, you will discover that they should not waste their time debugging, they should not introduce the bugs to start with. 10
  • 11. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 11 Can we Realize Dijkstra’s Dream and Prevent Defects with Test Driven Development?
  • 12. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. The Physics of Debug Later Programming (DLP) • As Td increases, Tfind increases dramatically • Tfix is usually short, but can increase with Td Bug discoveryMistake made (bug injection) Bug found Bug fixed Td Tfind T fix Time 12
  • 13. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. A Bug’s Life From http://www.softwaretestinghelp.com/bug-life-cycle/ 13
  • 14. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. What is TDD? 14
  • 15. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. • Write a test • Watch it not build • Make it build, but fail • Make it pass • Refactor (clean up any mess) • Repeat until done T D D
  • 16. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Test Driven Development www.renaissancesoftware.net james@renaissancesoftware.net Copyright © 2008-2013 James W. Grenning All Rights Reserved. For use by training attendees. If test moved upstream, could we get working features to flow? 16
  • 17. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Development and Test are a Continuum preventing defects 17 Development Test
  • 18. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. The Physics of Test Driven Development • When Td approaches zero, Tfind approaches zero • In many cases, bugs are not around long enough to be considered bugs. • See: http://www.renaissancesoftware.net/blog/archives/16 Mistake discovery Mistake made Root cause found Mistake fixed T d Tfind T fix Time 18
  • 19. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Let’s Test-Drive a CircularBuffer that Holds Integers + Put + Get + IsEmpty + IsFull //etc CircularBuffer 19
  • 20. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Create a Test-List (avoid analysis paralysis) Circular Buffer Tests Initially Empty Transition to empty Transition to Full Transition from Full Put-Get FIFO Put to full Get from empty Filled but not wrapped around Wrap around 20
  • 21. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Have an Idea of How it Will Work Out-Index In-Index 23 7 66 12 99 16 90 Out-Index In-Index 42 -6 23 7 66 12 99 16 90 Out-Index In-Index 99 21
  • 22. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Setup a Test Fixture TEST_GROUP(CircularBuffer) { CircularBuffer* buffer; ! void setup() { buffer = CircularBuffer_Create(); } ! void teardown() { CircularBuffer_Destroy(buffer); } }; ! TEST(CircularBuffer, TestName) { } 22
  • 23. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Make a Bare Skeleton of the Production Code Header File #ifndef D_CircularBuffer_H #define D_CircularBuffer_H ! typedef struct CircularBuffer CircularBuffer; ! CircularBuffer * CircularBuffer_Create(); void CircularBuffer_Destroy(CircularBuffer *); ! #endif // D_CircularBuffer_H 23
  • 24. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Make a Bare Skeleton of the Production Code Source File #include "CircularBuffer.h" #include <stdlib.h> ! struct CircularBuffer { int dummy; } ; ! CircularBuffer* CircularBuffer_Create() { CircularBuffer* self = calloc(1, sizeof(CircularBuffer)); return self; } ! void CircularBuffer_Destroy(CircularBuffer* self) { free(self); } ! 24
  • 25. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Choose a Test Write the test Make the test link Make the test compile Compilation error Link error New test failsMake the test pass Refactor (Make it right) All tests pass All tests pass No more tests Choose a test Start Compilation error Link error DONE! Programming error Compiles Clean 25
  • 26. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Which Test Should We Start With? Out-Index In-Index 41 59 14 33 7 31 In-Index Out-Index 26
  • 27. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Exercise 27
  • 28. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Write Three Post-it Notes What concerns you about TDD? 28 Liked Concern What did you like about TDD?
  • 29. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. A Complex system that works is invariably found to have evolved from a simple system that worked. 29
  • 30. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 30
  • 31. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Sustainability 31 Traditional development The cost of doing business debug-later- programming Test-driven development Speculate Code Test/Debug Debug Specu late Test and Code Debug Side-effect Defect Debug Debu Debug Side-effect Defect
  • 32. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Managing Cognitive Load 32
  • 33. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Debug Later Programming is Like Juggling 33
  • 34. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. How Many Knives Do You Like to Juggle? • TDD is solving one problem at a time Photo by Andrew Bossi (Own work) [CC-BY-SA-2.5 (www.creativecommons.org/licenses/by-sa/2.5)], via Wikimedia Commons 34
  • 35. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. ! ! • Get good at being careful and you can go fast. Careful, the fast way is. 35
  • 36. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Adaptation for Embedded Software 36
  • 37. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. What is special about embedded • Concurrent HW development • HW bottleneck • Cross compilation • Limited memory and IO • Target debug • Architecture 37
  • 38. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 38
  • 39. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Hardware is Scarce! • It does not exist. • It is being used by someone else. • It has bugs of its own. 39
  • 40. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 40
  • 41. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 41
  • 42. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Minimize Debug on Hardware DoH! Copyright Fox Broadcasting. Used under fair use. 42
  • 43. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Write a Test Make it Pass Refactor Stage 1 Compile for Target Processor Stage 2 Run Tests in the Eval Hardware Stage 3 Run Tests in Target Hardware Stage 4 Acceptance Tests Stage 5 More Frequent Less Frequent TDD Adaptation for Embedded Development 43
  • 44. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. TDD Adaptation for Embedded Development See : http://renaissancesoftware.net/files/articles/ProgressBeforeHardware.pdf Write a Test Make it Pass Refactor Stage 1 Compile for Target Processor Stage 2 Run Tests in the Eval Hardware Stage 3 Run Tests in Target Hardware Stage 4 Acceptance Tests Stage 5 More Frequent Less Frequent 44
  • 45. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. 35 Continuous Integration - Embedded 3. CIS notices changes, refreshes its local copy, builds and runs host based tests. 2. Developer checks in work regularly. 1. Developer has all unit tests passing and checks in work. 4. After successful host build/test, CIS builds and hands images to TMS to deploy to simulator, eval/ reference boards or target system. The SCR, CIS and TMS are not necessarily separate hardware systems. Developer Workstation Source Code Repository Continuous Integration Server Target System Eval/Reference System Simulator Test Management System
  • 46. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Problems Found at Appropriate Stage Stage Problems Likely to Find in Stage 1 Logic, design, modularity, interface, boundary conditions 2 Compiler compatibility (language features)! Library compatibility (header files, declarations) 3 Processor executions problems (compiler and library bugs)! Portability problems (word size, alignment, endian) 4 Ditto stage 3! Hardware integration problems! Misunderstood hardware specifications 5 Ditto stage 4! Misunderstood feature specification 46
  • 47. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. How do I test Code with Dependencies? 47
  • 48. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Spaghetti Slide 48
  • 49. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. A Module with Collaborators • Every minute, the RTOS wakes up the Light Scheduler. • If it is time for one of the lights to be controlled, the LightController is told to turn on/off the light. Time Service + GetTime() + SetPeriodicAlarm() Light Scheduler +ScheduleTurnOn() +RemoveSchedule() +WakeUp() Light Controller + On(id) + Off(id) Hardware RTOS <<anonymous callback>> Admin Console 49
  • 50. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Program to Interfaces • Separate interface and implementation as separate entities. • This design has good separation of responsibilities <<interface>> Time Service + GetTime() + SetPeriodicAlarm() Light Scheduler + ScheduleTurnOn() + RemoveSchedule() +WakeUp() <<interface>> Light Controller + On(id) + Off(id) Model 42 Hardware RTOS <<anonymous callback>> Model 42 Light Controller RTOS Time Service <<implements>> <<implements>> Admin Console 50
  • 51. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Testing a Module with Collaborators • Use the real collaborators if you can. • Use fakes when you must. <<interface>> Time Service + GetTime() + SetPeriodicAlarm() Light Scheduler Test Light Scheduler + ScheduleTurnOn() + RemoveSchedule() +wakeUp() <<interface>> Light Controller + On(id) + Off(id) Light Controller Spy Fake Time Service <<implements>> <<implements>> 51
  • 52. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Testing the Scheduler TEST(LightScheduler, ScheduleOnTodayNotTimeYet) { LightScheduler_ScheduleTurnOn(3, EVERYDAY, 1000); FakeTimeSource_SetMinute(999); ! LightScheduler_Wakeup(); ! LONGS_EQUAL(LIGHT_NA, LightControllerSpy_GetState(3)); } ! TEST(LightScheduler, ScheduleOnTodayItsTime) { LightScheduler_ScheduleTurnOn(3, EVERYDAY, 1000); FakeTimeSource_SetMinute(1000); ! LightScheduler_Wakeup(); ! LONGS_EQUAL(LIGHT_ON, LightControllerSpy_GetState(3)); } 52
  • 53. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Partial Skeleton Let’s You Try the Design and Test Ideas Early 53
  • 54. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Test-Doubles 54
  • 55. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Test Double • A test double stands in for some part of the production code during testing • Test doubles are part of the test fixture that allows a module to be unit tested 55
  • 56. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Test Dependency Octopus 56 Test Code Under Test Depended On Component DOC DOC Transitively DOC TDOC TDOCTDOCTDOC
  • 57. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Managing the Octopus 57 Test Code Under Test Test Double Test Double Test Double
  • 58. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Use Production Code when you Can Test Doubles when you Must 58 Test Code Under Test Depended On Component Test Double Test Double Transitively DOC TDOC
  • 59. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Kinds of Test Doubles [XUNIT] • Other names – Fakes – Mock object – Spy – Null object – Exploding fakes – there are others 59
  • 60. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. What are the Test Double Options in C? • Link-time substitution. – Substitute whole object files or libraries • Run time substitution. – Function pointers. – Function pointer tables. • Preprocessor substitution – My least favorite, but sometimes necessary • Use the simplest option that will work. 60
  • 61. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. What are the Test Doubles Options in C++? • Link-time substitution. – Substitute whole object files or libraries • Run time substitution. – To substitute one class for another –Virtual functions. –Interface classes. – To substitute one function for another –C function pointers. • Preprocessor substitution – my least favorite, but sometimes necessary • Use the simplest option that will work. 61
  • 62. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Getting Around Tool Dependencies 62
  • 63. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Example Problems • Compiler vendors extend C • Embedded ‘asm’ • Header files lock you to the vendor 63
  • 64. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Compiler vendors extend C 64 //snip...! /* Address Mode Register */! extern cregister volatile unsigned int AMR; ! //snip...! ! //snip...! interrupt void one_ms_tic(void)! {! ...! }! //snip...
  • 65. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Hide Them with a Preprocessor Forced Include 65 //hide_stuff.h ! #define interrupt #define cregister ! Force Include hidestuff.h, to make them go away read the blog: http://www.renaissancesoftware.net/blog/archives/ 249
  • 66. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Developers embed ‘asm’ 66 void foo(void) { do { asm("NOP"); asm("PLOP"); asm("READ 0"); status = IORead(0); } while ((status &amp; ReadyBit) == 0); }
  • 67. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Hide asm Forced Include 67 //nullasm.h ! #define asm ! Force Include nullasm.h, to make asm go away read the blog: http://www.renaissancesoftware.net/blog/archives/ 136
  • 68. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. AsmSpy Test Double 68 #define asm AsmSpy ! void AsmSpy_Create(int size); void AsmSpy_Destroy(void); void AsmSpy(const char *); const char * AsmSpy_Debrief(void); Force Include AsmSpy.h All asm() directives are converted to AsmSpy() calls, allowing capture. read the blog: http://www.renaissancesoftware.net/blog/archives/ 136
  • 69. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. AsmSpy Tests Show How to Monitor the asm 69 TEST(AsmSpy, CaptureAsmInstructions) { AsmSpy("NOP"); AsmSpy("FLOP"); AsmSpy("POP"); STRCMP_EQUAL("NOP;FLOP;POP;", AsmSpy_Debrief()); } ! Force Include AsmSpy.h, to capture asm instructions read the blog: http://www.renaissancesoftware.net/blog/archives/ 136
  • 70. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Header Files that Lock You to the Vendor’s Compiler 70 #if defined(_ACME_X42) typedef unsigned int Uint_32; typedef unsigned short Uint_16; typedef unsigned char Uint_8; typedef int Int_32; typedef short Int_16; typedef char Int_8; #elif defined(_ACME_A12) typedef unsigned long Uint_32; typedef unsigned int Uint_16; typedef unsigned char Uint_8; typedef long Int_32; typedef int Int_16; typedef char Int_8; #else #error <acmetypes.h> is not supported for this environment #endif
  • 71. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Create a #include Test Double 71 #include <stdint.h> ! typedef uint32_t Uint_32; typedef uint16_t Uint_16; typedef uint8_t Uint_8; ! typedef int32_t Int_32; typedef int16_t Int_16; typedef int8_t Int_8; ! #endif /* ACMETYPES_H_ */ Also, limit dependencies on vendor specific includes
  • 72. There is a Lot More to TDD 72
  • 73. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. Talk to me on Twitter @jwgrenning! ! Find my book at! http://www.pragprog.com/titles/jgade ! Find me on linkedin.com http://www.linkedin.com/in/jwgrenning Please remind me how we met. ! http://www.wingman-sw.com http://www.wingman-sw.com/blog http:// www.jamesgrenning.com 73
  • 74. devconfu, Nov 2013 Riga, Latvia www.wingman-sw.com james@wingman-sw.com Copyright © 2008-2013 James W. Grenning All Rights Reserved. devconfu Riga, Latvia www.renaissancesoftware.net james@renaissancesoftware.net Copyright © 2008-2013 James W. Grenning All Rights Reserved. Partners: