SlideShare a Scribd company logo
VIA University College
ICT-Engineering
Bringideastolife
VIAUniversityCollege
ObjectOrientering,TestDriven
DevelopmentogC
InfinIT, 21. November 2018
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 1
Ib Havn
B.Sc.E.E.
M.Sc.ITSoftware Construction
VIA University College
ICT-Engineering
Whatistheproblems withhardwarenear
programming?
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 2
– The nearer the hardware the testing difficulties grows
– There are not much help from different tools
– Errors/bugs are difficult to find
– The software are target dependent
– Design for Test is the solution
– Debug Later Programming (DLP) vs Test Driven Development
VIA University College
ICT-Engineering
Whydoweforgeteverythingwehave
learnedwhenprogramminglow-levelC?
Typical excuses:
1. Hardware near programming is for Electronics Engineers
2. The hardware is not available yet
3. It is not possible to test due to limited target resources
– Test Driven Development (TDD) is not possible on the small target
4. Debugging facilities in the target toolchain is limited
– Debugging is difficult and is done with oscilloscopes, leds, simple printouts
etc.
5. UML is not useful for C-programming
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 3
VIA University College
ICT-Engineering
ObjectOrientedDesignandProgramming
inC
According to Robert C. Martin an OOP language has the following:
1. Encapsulation
2. Inheritance
3. Polymorphism
The first two are easy to obtain in C, the third is a little harder
1. Abstract Data Types (ADT)
2. Nested struct’s
3. Pointer tables
For a complete description how to do use three read
Axel Schreiner’s book: Object-oriented Programming in ANSI-C
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 4
VIA University College
ICT-Engineering
Exampleofcomplete Encapsulation inC
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 5
VIA University College
ICT-Engineering
Exampleofcomplete Encapsulation inC
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 6
// Abstract Data Type (ADT)
typedef struct serial_struct *serial_p;
serial_p serial_new_instance(e_com_port_t com_port, uint32_t baud, e_data_bit_t data_bit,
e_stop_bit_t stop_bit, e_parity_t parity, uint8_t rx_fifo_size,
uint8_t tx_fifo_size,
void(*handler_call_back )(serial_p, uint8_t));
e_serial_return_code_t serial_send_bytes(serial_p handle, uint8_t *buf, uint8_t len);
e_serial_return_code_t serial_send_byte(serial_p handle, uint8_t byte);
e_serial_return_code_t serial_get_byte(serial_p handle, uint8_t *byte);
void serial_flush_rx_fifo(serial_p handle);
void serial_flush_tx_fifo(serial_p handle);
This is all the clients using the driver can see:
VIA University College
ICT-Engineering
ObjectOrientedDesignandProgramming
inC
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 7
Why not use UML – A World wide Industry Standard
VIA University College
ICT-Engineering
SOLIDDesignPrinciples
Robert C. Martin has assembled these five more or less well-known principles together
Single Responsibility Principle (SRP)
– A class should have one, and only one, reason to change
Open Close Principle (OCP)
– You should be able to extend a class’s behaviour, without modifying it
Liskov Substitution Principle (LSP)
– Derived classes must be substitutables for their base classes
Interface Segregation Principle (ISP)
– Make fine grained interfaces that are client specific
Dependency Inversion Principle (DIP)
– Depend on abstractions, not on concretions
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 8
VIA University College
ICT-Engineering
TestDrivenDevelopment(TDD)
TDDState-machine
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 9
source: Test Driven Development for Embedded C, James W. Grenning.
Is it possible on small targets?
VIA University College
ICT-Engineering
SimplifiedDevelopmentProcess
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 10
Production
code
(.h,.c)
Cross
Compiler
Target Platform
Hardware
(registers etc.)
Hardware
API(.h)
Cross
Linker
11101
00011
00110
00111
Hardware
libsandothertargetlibs(.so)
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 11
Host
Compiler
Host Platform
Production
code
(.h,.c)
Hardware
API(.h)
Host
Linker
11101
00011
00110
00111
Host
Libs(.so)
ForcedIncludefiles
(.h)
Mock’sandTestCases
(.h,.c,.cpp)
Hardware
(registers etc.)
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Forced include files:
– The pre-processor includes specified files before anything else is included
– We can add macros and definitions that
– Disable target hardware specific things
– Enable hardware fakes
Compiler/pre-processor options
– gcc option (gnu): –include <file to be included>
– cl option (Microsoft): /FI<file to be included>
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 12
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
All what’s needed to cheat the toolchain for the ATMEGA2560 MCU:
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 13
#include <stdint.h>
#define __AVR_LIBC_DEPRECATED_ENABLE__
#define __AVR_ATmega2560__
#define _AVR_SFR_DEFS_H_ 1
// 0x136 is highest address of registers in ATMEGA2560
#define _HIGHEST_REGISTER_ADD0x136
// These global variables (fake hardware registers) needs to be accessible from both C and C++
// Therefore they must be declared in C scope if it is the C++ compiler that access
#ifdef __cplusplus
extern "C" {
#endif
extern uint8_t __avr_reg[_HIGHEST_REGISTER_ADD];
#ifdef __cplusplus
}
#endif
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
All what’s needed to cheat the toolchain for an ATMEGA2560 MCU:
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 14
// Macros to access the fake hardware registers
#define _SFR_MEM8(mem_addr) (*(uint8_t *)(&__avr_reg[mem_addr]))
#define _SFR_IO8(io_addr) (*(uint8_t *)(&__avr_reg[io_addr]))
// Byte value from bit_no
#define _BV(bit) (1 << (bit))
// Interrupt
#define _AVR_INTERRUPT_H_
#define ISR(vector, ...) void ISR_##vector(void)
#define sei() SREG |= _BV(SREG_I)
#define cli() SREG &= ~_BV(SREG_I)
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Test cases can now check contents of hardware registers
using a test macro: CHECK_EQUAL_C_BITS(expected, actual, mask)
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 15
TEST(TEST_HAL, ADPS2AndADPS1isSetAndADPS0isClearedInADCSRAAfterCreate)
{
CHECK_EQUAL_C_BITS(_BV(ADPS2)|_BV(ADPS1), ADCSRA, _BV(ADPS2) | _BV(ADPS1)|_BV(ADPS0));
}
TEST(TEST_HAL, get_voltageCalledWithCh15SetsMuxTo100111)
{
hal_get_voltage(15);
CHECK_EQUAL_C_BITS(_BV(MUX5), ADCSRB, _BV(MUX5));
CHECK_EQUAL_C_BITS(_BV(MUX2) | _BV(MUX1) | _BV(MUX0), ADMUX,
_BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0));
}
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Test cases can check that bits in registers are set and read in right order
and return values can be controlled
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 16
TEST(TEST_HAL, get_voltageCalledSetsAndWaitsForADSCtoClear)
{
mock().expectOneCall("mock_set_bit").withPointerParameter("reg",
&ADCSRA).withIntParameter("bit_no", ADSC);
mock().expectNCalls(5,"mock_read_bit").withPointerParameter("reg",
&ADCSRA).withIntParameter("bit_no", ADSC).andReturnValue(_BV(ADSC));
mock().expectOneCall("mock_read_bit")
.withPointerParameter("reg"&ADCSRA).withIntParameter("bit_no", ADSC).andReturnValue(0);
hal_get_voltage(0);
}
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
The Mocks are injected via dependency injection:
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 17
// ---------------------------------------------------------------------------------------
uint8_t mock_read_bit(volatile uint8_t * reg, uint8_t bit_no)
{
mock().actualCall("mock_read_bit").withPointerParameter("reg", (void *)reg)
.withIntParameter("bit_no", bit_no);
return mock().returnIntValueOrDefault(0);
}
// --------------------------------------------------------------------------------------
void mock_set_bit(volatile uint8_t * reg, uint8_t bit_no)
{
// Production code function called
set_bit(reg, bit_no);
mock().actualCall("mock_set_bit").withPointerParameter("reg", (void *)reg)
.withIntParameter("bit_no", bit_no);
}
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Test that calculations are correct in driver, faking hardware registers
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 18
TEST(TEST_HAL, get_voltageReturnsFullVRef)
{
// Given
ADCL = 0b11111111;
ADCH = 0b00000011;
// When
float result = hal_get_voltage(0);
// Then
float expected = ((ADCH << 8) + ADCL) * _V_REF / 1024;
CHECK_EQUAL_C_REAL(expected, result, 0.001);
}
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Faking Interrupts
The Interrupt Service Routine (ISR ) in the production code:
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 19
ISR(INT0_vect)
{
PORTA = 0x55;
}
The macro that fakes it:
#define ISR(vector, ...) void ISR_##vector(void)
In the test cases the ISR is now turned into a normal function with the following
signature:
void ISR_INT0_vect(void)
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Faking Interrupts
The Test that check that the ISR is doing what it is supposed to do
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 20
TEST(TEST_HAL, INT0ISRIsSettingPORTAto0x55)
{
ISR_INT0_vect(); // Fake INT0 Interrupt
CHECK_EQUAL_C_INT(0x55, PORTA);
}
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
What Test-cases to write:
Take a look at James W. Grenning’s ZOMBIES:
– Z – Zero
– O – One
– M – Many (or More complex)
– B – Boundary Behaviours
– I – Interface definition
– E – Exercise Exceptional behaviour
– S – Simple Scenarios, Simple Solutions
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 21
VIA University College
ICT-Engineering
TestDrivenDevelopmentinC
Pros:
– Production code can be developed at Host-computer
– Use all Host-computer tools and Debugger facilities
– Less errors and failures when moving to target computer
– Develop without the target hardware
Cons:
– It takes time to figure out how to get rid of the hardware dependencies in the
target tool-chain (One time only investment)
– Real-time aspects can’t be tested on the Host-computer
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 22
VIA University College
ICT-Engineering
Conclusion
– It is hard to develop low-level and hardware near software
– Invest time in setting up your tools and environment
(one time investment per platform)
– Use all the principles that we know from high-level development
(OOD/P)
– Document using UML and/or SysML
– TTD is possible and not very hard when tools are setup
– Software is easy and safe to maintain if it is well designed and the test
suite is up-to-date
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 23
VIA University College
ICT-Engineering
References
– CppUTest (open source): http://cpputest.github.io/index.html
– James W. Grenning, 2011: Test Driven Development for Embedded C, 1st Edition,
ISBN-13: 978-1934356623, ISBN-10: 9781934356623
– Axel Schreiner : Object-oriented Programming in ANSI-C
– Beck, K., 2002 Test Driven Development: By Example, ISBN-10: 9780321146533
ISBN-13: 978-0321146533
– Robert C. Martin, 2002: Agile Software Development, Principles, Patterns, and
Practices, ISBN10 0135974445 ISBN13 9780135974445
– James W. Grenning’s ZOMBIES: http://blog.wingman-sw.com/archives/677
2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 24

More Related Content

What's hot

Symbexecsearch
SymbexecsearchSymbexecsearch
Symbexecsearch
Abhik Roychoudhury
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
Samuel Narcisse
 
Hardware Description Language
Hardware Description Language Hardware Description Language
Hardware Description Language
Prachi Pandey
 
VHDL Programs
VHDL ProgramsVHDL Programs
VHDL Programs
A B Shinde
 
Maheswara reddy 10+years_avionics
Maheswara reddy 10+years_avionicsMaheswara reddy 10+years_avionics
Maheswara reddy 10+years_avionics
maheswarareddy pr
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
Abhiraj Bohra
 

What's hot (6)

Symbexecsearch
SymbexecsearchSymbexecsearch
Symbexecsearch
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
Hardware Description Language
Hardware Description Language Hardware Description Language
Hardware Description Language
 
VHDL Programs
VHDL ProgramsVHDL Programs
VHDL Programs
 
Maheswara reddy 10+years_avionics
Maheswara reddy 10+years_avionicsMaheswara reddy 10+years_avionics
Maheswara reddy 10+years_avionics
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 

Similar to Object orientering, test driven development og c

PARTH DESAI RESUME
PARTH DESAI RESUMEPARTH DESAI RESUME
PARTH DESAI RESUMEParth Desai
 
Performance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL ModelsPerformance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL Models
Space Codesign
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineerTodd Nguyen
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineerTodd Nguyen
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineerTodd Nguyen
 
Dipak_Desai_Resume
Dipak_Desai_ResumeDipak_Desai_Resume
Dipak_Desai_Resumenotoha
 
Prayat hegde resume_firmware_embedded
Prayat hegde resume_firmware_embeddedPrayat hegde resume_firmware_embedded
Prayat hegde resume_firmware_embedded
Prayat Hegde
 
AtifBhatti resume
AtifBhatti resumeAtifBhatti resume
AtifBhatti resumeAtif Bhatti
 
Blake Xu Resume
Blake Xu ResumeBlake Xu Resume
Blake Xu ResumeBlake Xu
 
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
Christopher Diamantopoulos
 
OTTAVIO C. D'ANGELIS DDD
OTTAVIO C. D'ANGELIS DDDOTTAVIO C. D'ANGELIS DDD
OTTAVIO C. D'ANGELIS DDDTavio D'Angelis
 
Kavita resume
Kavita resume Kavita resume
Kavita resume
Kavita Raghunathan
 
Kavita resume startup
Kavita resume startupKavita resume startup
Kavita resume startup
Kavita Raghunathan
 
Amruth_Kumar_Juturu_Resume
Amruth_Kumar_Juturu_ResumeAmruth_Kumar_Juturu_Resume
Amruth_Kumar_Juturu_ResumeAmruth Kumar
 

Similar to Object orientering, test driven development og c (20)

PARTH DESAI RESUME
PARTH DESAI RESUMEPARTH DESAI RESUME
PARTH DESAI RESUME
 
Performance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL ModelsPerformance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL Models
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineer
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineer
 
SoftwareEngineer
SoftwareEngineerSoftwareEngineer
SoftwareEngineer
 
apurva resume
apurva resumeapurva resume
apurva resume
 
Dipak_Desai_Resume
Dipak_Desai_ResumeDipak_Desai_Resume
Dipak_Desai_Resume
 
veera (updated)
veera (updated)veera (updated)
veera (updated)
 
Prayat hegde resume_firmware_embedded
Prayat hegde resume_firmware_embeddedPrayat hegde resume_firmware_embedded
Prayat hegde resume_firmware_embedded
 
AtifBhatti resume
AtifBhatti resumeAtifBhatti resume
AtifBhatti resume
 
Resume_Mohan Selvamoorthy_Sec
Resume_Mohan Selvamoorthy_SecResume_Mohan Selvamoorthy_Sec
Resume_Mohan Selvamoorthy_Sec
 
Redentor_Adolfo_CV
Redentor_Adolfo_CVRedentor_Adolfo_CV
Redentor_Adolfo_CV
 
Blake Xu Resume
Blake Xu ResumeBlake Xu Resume
Blake Xu Resume
 
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
IMAGE CAPTURE, PROCESSING AND TRANSFER VIA ETHERNET UNDER CONTROL OF MATLAB G...
 
OTTAVIO C. D'ANGELIS DDD
OTTAVIO C. D'ANGELIS DDDOTTAVIO C. D'ANGELIS DDD
OTTAVIO C. D'ANGELIS DDD
 
Kavita resume
Kavita resume Kavita resume
Kavita resume
 
Ankit sarin
Ankit sarinAnkit sarin
Ankit sarin
 
Kavita resume startup
Kavita resume startupKavita resume startup
Kavita resume startup
 
Amruth_Kumar_Juturu_Resume
Amruth_Kumar_Juturu_ResumeAmruth_Kumar_Juturu_Resume
Amruth_Kumar_Juturu_Resume
 
RamachandraParlapalli_RESUME
RamachandraParlapalli_RESUMERamachandraParlapalli_RESUME
RamachandraParlapalli_RESUME
 

More from InfinIT - Innovationsnetværket for it

Erfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermarkErfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermark
InfinIT - Innovationsnetværket for it
 
Embedded softwaredevelopment hcs
Embedded softwaredevelopment hcsEmbedded softwaredevelopment hcs
Embedded softwaredevelopment hcs
InfinIT - Innovationsnetværket for it
 
C og c++-jens lund jensen
C og c++-jens lund jensenC og c++-jens lund jensen
C og c++-jens lund jensen
InfinIT - Innovationsnetværket for it
 
201811xx foredrag c_cpp
201811xx foredrag c_cpp201811xx foredrag c_cpp
C som-programmeringssprog-bt
C som-programmeringssprog-btC som-programmeringssprog-bt
C som-programmeringssprog-bt
InfinIT - Innovationsnetværket for it
 
Infinit seminar 060918
Infinit seminar 060918Infinit seminar 060918
DCR solutions
DCR solutionsDCR solutions
Not your grandfathers BPM
Not your grandfathers BPMNot your grandfathers BPM
Not your grandfathers BPM
InfinIT - Innovationsnetværket for it
 
Kmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolutionKmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolution
InfinIT - Innovationsnetværket for it
 
EcoKnow - oplæg
EcoKnow - oplægEcoKnow - oplæg
Martin Wickins Chatbots i fronten
Martin Wickins Chatbots i frontenMartin Wickins Chatbots i fronten
Martin Wickins Chatbots i fronten
InfinIT - Innovationsnetværket for it
 
Marie Fenger ai kundeservice
Marie Fenger ai kundeserviceMarie Fenger ai kundeservice
Marie Fenger ai kundeservice
InfinIT - Innovationsnetværket for it
 
Mads Kaysen SupWiz
Mads Kaysen SupWizMads Kaysen SupWiz
Leif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support CenterLeif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support Center
InfinIT - Innovationsnetværket for it
 
Jan Neerbek NLP og Chatbots
Jan Neerbek NLP og ChatbotsJan Neerbek NLP og Chatbots
Jan Neerbek NLP og Chatbots
InfinIT - Innovationsnetværket for it
 
Anders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer SupportAnders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer Support
InfinIT - Innovationsnetværket for it
 
Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018
InfinIT - Innovationsnetværket for it
 
Innovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekterInnovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekter
InfinIT - Innovationsnetværket for it
 
Rokoko infin it presentation
Rokoko infin it presentation Rokoko infin it presentation
Rokoko infin it presentation
InfinIT - Innovationsnetværket for it
 
Kenny erleben infinit_workshop
Kenny erleben infinit_workshopKenny erleben infinit_workshop
Kenny erleben infinit_workshop
InfinIT - Innovationsnetværket for it
 

More from InfinIT - Innovationsnetværket for it (20)

Erfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermarkErfaringer med-c kurt-noermark
Erfaringer med-c kurt-noermark
 
Embedded softwaredevelopment hcs
Embedded softwaredevelopment hcsEmbedded softwaredevelopment hcs
Embedded softwaredevelopment hcs
 
C og c++-jens lund jensen
C og c++-jens lund jensenC og c++-jens lund jensen
C og c++-jens lund jensen
 
201811xx foredrag c_cpp
201811xx foredrag c_cpp201811xx foredrag c_cpp
201811xx foredrag c_cpp
 
C som-programmeringssprog-bt
C som-programmeringssprog-btC som-programmeringssprog-bt
C som-programmeringssprog-bt
 
Infinit seminar 060918
Infinit seminar 060918Infinit seminar 060918
Infinit seminar 060918
 
DCR solutions
DCR solutionsDCR solutions
DCR solutions
 
Not your grandfathers BPM
Not your grandfathers BPMNot your grandfathers BPM
Not your grandfathers BPM
 
Kmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolutionKmd workzone - an evolutionary approach to revolution
Kmd workzone - an evolutionary approach to revolution
 
EcoKnow - oplæg
EcoKnow - oplægEcoKnow - oplæg
EcoKnow - oplæg
 
Martin Wickins Chatbots i fronten
Martin Wickins Chatbots i frontenMartin Wickins Chatbots i fronten
Martin Wickins Chatbots i fronten
 
Marie Fenger ai kundeservice
Marie Fenger ai kundeserviceMarie Fenger ai kundeservice
Marie Fenger ai kundeservice
 
Mads Kaysen SupWiz
Mads Kaysen SupWizMads Kaysen SupWiz
Mads Kaysen SupWiz
 
Leif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support CenterLeif Howalt NNIT Service Support Center
Leif Howalt NNIT Service Support Center
 
Jan Neerbek NLP og Chatbots
Jan Neerbek NLP og ChatbotsJan Neerbek NLP og Chatbots
Jan Neerbek NLP og Chatbots
 
Anders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer SupportAnders Soegaard NLP for Customer Support
Anders Soegaard NLP for Customer Support
 
Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018Stephen Alstrup infinit august 2018
Stephen Alstrup infinit august 2018
 
Innovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekterInnovation og værdiskabelse i it-projekter
Innovation og værdiskabelse i it-projekter
 
Rokoko infin it presentation
Rokoko infin it presentation Rokoko infin it presentation
Rokoko infin it presentation
 
Kenny erleben infinit_workshop
Kenny erleben infinit_workshopKenny erleben infinit_workshop
Kenny erleben infinit_workshop
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 

Object orientering, test driven development og c

  • 1. VIA University College ICT-Engineering Bringideastolife VIAUniversityCollege ObjectOrientering,TestDriven DevelopmentogC InfinIT, 21. November 2018 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 1 Ib Havn B.Sc.E.E. M.Sc.ITSoftware Construction
  • 2. VIA University College ICT-Engineering Whatistheproblems withhardwarenear programming? 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 2 – The nearer the hardware the testing difficulties grows – There are not much help from different tools – Errors/bugs are difficult to find – The software are target dependent – Design for Test is the solution – Debug Later Programming (DLP) vs Test Driven Development
  • 3. VIA University College ICT-Engineering Whydoweforgeteverythingwehave learnedwhenprogramminglow-levelC? Typical excuses: 1. Hardware near programming is for Electronics Engineers 2. The hardware is not available yet 3. It is not possible to test due to limited target resources – Test Driven Development (TDD) is not possible on the small target 4. Debugging facilities in the target toolchain is limited – Debugging is difficult and is done with oscilloscopes, leds, simple printouts etc. 5. UML is not useful for C-programming 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 3
  • 4. VIA University College ICT-Engineering ObjectOrientedDesignandProgramming inC According to Robert C. Martin an OOP language has the following: 1. Encapsulation 2. Inheritance 3. Polymorphism The first two are easy to obtain in C, the third is a little harder 1. Abstract Data Types (ADT) 2. Nested struct’s 3. Pointer tables For a complete description how to do use three read Axel Schreiner’s book: Object-oriented Programming in ANSI-C 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 4
  • 5. VIA University College ICT-Engineering Exampleofcomplete Encapsulation inC 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 5
  • 6. VIA University College ICT-Engineering Exampleofcomplete Encapsulation inC 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 6 // Abstract Data Type (ADT) typedef struct serial_struct *serial_p; serial_p serial_new_instance(e_com_port_t com_port, uint32_t baud, e_data_bit_t data_bit, e_stop_bit_t stop_bit, e_parity_t parity, uint8_t rx_fifo_size, uint8_t tx_fifo_size, void(*handler_call_back )(serial_p, uint8_t)); e_serial_return_code_t serial_send_bytes(serial_p handle, uint8_t *buf, uint8_t len); e_serial_return_code_t serial_send_byte(serial_p handle, uint8_t byte); e_serial_return_code_t serial_get_byte(serial_p handle, uint8_t *byte); void serial_flush_rx_fifo(serial_p handle); void serial_flush_tx_fifo(serial_p handle); This is all the clients using the driver can see:
  • 7. VIA University College ICT-Engineering ObjectOrientedDesignandProgramming inC 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 7 Why not use UML – A World wide Industry Standard
  • 8. VIA University College ICT-Engineering SOLIDDesignPrinciples Robert C. Martin has assembled these five more or less well-known principles together Single Responsibility Principle (SRP) – A class should have one, and only one, reason to change Open Close Principle (OCP) – You should be able to extend a class’s behaviour, without modifying it Liskov Substitution Principle (LSP) – Derived classes must be substitutables for their base classes Interface Segregation Principle (ISP) – Make fine grained interfaces that are client specific Dependency Inversion Principle (DIP) – Depend on abstractions, not on concretions 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 8
  • 9. VIA University College ICT-Engineering TestDrivenDevelopment(TDD) TDDState-machine 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 9 source: Test Driven Development for Embedded C, James W. Grenning. Is it possible on small targets?
  • 10. VIA University College ICT-Engineering SimplifiedDevelopmentProcess 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 10 Production code (.h,.c) Cross Compiler Target Platform Hardware (registers etc.) Hardware API(.h) Cross Linker 11101 00011 00110 00111 Hardware libsandothertargetlibs(.so)
  • 11. VIA University College ICT-Engineering TestDrivenDevelopmentinC 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 11 Host Compiler Host Platform Production code (.h,.c) Hardware API(.h) Host Linker 11101 00011 00110 00111 Host Libs(.so) ForcedIncludefiles (.h) Mock’sandTestCases (.h,.c,.cpp) Hardware (registers etc.)
  • 12. VIA University College ICT-Engineering TestDrivenDevelopmentinC Forced include files: – The pre-processor includes specified files before anything else is included – We can add macros and definitions that – Disable target hardware specific things – Enable hardware fakes Compiler/pre-processor options – gcc option (gnu): –include <file to be included> – cl option (Microsoft): /FI<file to be included> 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 12
  • 13. VIA University College ICT-Engineering TestDrivenDevelopmentinC All what’s needed to cheat the toolchain for the ATMEGA2560 MCU: 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 13 #include <stdint.h> #define __AVR_LIBC_DEPRECATED_ENABLE__ #define __AVR_ATmega2560__ #define _AVR_SFR_DEFS_H_ 1 // 0x136 is highest address of registers in ATMEGA2560 #define _HIGHEST_REGISTER_ADD0x136 // These global variables (fake hardware registers) needs to be accessible from both C and C++ // Therefore they must be declared in C scope if it is the C++ compiler that access #ifdef __cplusplus extern "C" { #endif extern uint8_t __avr_reg[_HIGHEST_REGISTER_ADD]; #ifdef __cplusplus } #endif
  • 14. VIA University College ICT-Engineering TestDrivenDevelopmentinC All what’s needed to cheat the toolchain for an ATMEGA2560 MCU: 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 14 // Macros to access the fake hardware registers #define _SFR_MEM8(mem_addr) (*(uint8_t *)(&__avr_reg[mem_addr])) #define _SFR_IO8(io_addr) (*(uint8_t *)(&__avr_reg[io_addr])) // Byte value from bit_no #define _BV(bit) (1 << (bit)) // Interrupt #define _AVR_INTERRUPT_H_ #define ISR(vector, ...) void ISR_##vector(void) #define sei() SREG |= _BV(SREG_I) #define cli() SREG &= ~_BV(SREG_I)
  • 15. VIA University College ICT-Engineering TestDrivenDevelopmentinC Test cases can now check contents of hardware registers using a test macro: CHECK_EQUAL_C_BITS(expected, actual, mask) 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 15 TEST(TEST_HAL, ADPS2AndADPS1isSetAndADPS0isClearedInADCSRAAfterCreate) { CHECK_EQUAL_C_BITS(_BV(ADPS2)|_BV(ADPS1), ADCSRA, _BV(ADPS2) | _BV(ADPS1)|_BV(ADPS0)); } TEST(TEST_HAL, get_voltageCalledWithCh15SetsMuxTo100111) { hal_get_voltage(15); CHECK_EQUAL_C_BITS(_BV(MUX5), ADCSRB, _BV(MUX5)); CHECK_EQUAL_C_BITS(_BV(MUX2) | _BV(MUX1) | _BV(MUX0), ADMUX, _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0)); }
  • 16. VIA University College ICT-Engineering TestDrivenDevelopmentinC Test cases can check that bits in registers are set and read in right order and return values can be controlled 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 16 TEST(TEST_HAL, get_voltageCalledSetsAndWaitsForADSCtoClear) { mock().expectOneCall("mock_set_bit").withPointerParameter("reg", &ADCSRA).withIntParameter("bit_no", ADSC); mock().expectNCalls(5,"mock_read_bit").withPointerParameter("reg", &ADCSRA).withIntParameter("bit_no", ADSC).andReturnValue(_BV(ADSC)); mock().expectOneCall("mock_read_bit") .withPointerParameter("reg"&ADCSRA).withIntParameter("bit_no", ADSC).andReturnValue(0); hal_get_voltage(0); }
  • 17. VIA University College ICT-Engineering TestDrivenDevelopmentinC The Mocks are injected via dependency injection: 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 17 // --------------------------------------------------------------------------------------- uint8_t mock_read_bit(volatile uint8_t * reg, uint8_t bit_no) { mock().actualCall("mock_read_bit").withPointerParameter("reg", (void *)reg) .withIntParameter("bit_no", bit_no); return mock().returnIntValueOrDefault(0); } // -------------------------------------------------------------------------------------- void mock_set_bit(volatile uint8_t * reg, uint8_t bit_no) { // Production code function called set_bit(reg, bit_no); mock().actualCall("mock_set_bit").withPointerParameter("reg", (void *)reg) .withIntParameter("bit_no", bit_no); }
  • 18. VIA University College ICT-Engineering TestDrivenDevelopmentinC Test that calculations are correct in driver, faking hardware registers 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 18 TEST(TEST_HAL, get_voltageReturnsFullVRef) { // Given ADCL = 0b11111111; ADCH = 0b00000011; // When float result = hal_get_voltage(0); // Then float expected = ((ADCH << 8) + ADCL) * _V_REF / 1024; CHECK_EQUAL_C_REAL(expected, result, 0.001); }
  • 19. VIA University College ICT-Engineering TestDrivenDevelopmentinC Faking Interrupts The Interrupt Service Routine (ISR ) in the production code: 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 19 ISR(INT0_vect) { PORTA = 0x55; } The macro that fakes it: #define ISR(vector, ...) void ISR_##vector(void) In the test cases the ISR is now turned into a normal function with the following signature: void ISR_INT0_vect(void)
  • 20. VIA University College ICT-Engineering TestDrivenDevelopmentinC Faking Interrupts The Test that check that the ISR is doing what it is supposed to do 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 20 TEST(TEST_HAL, INT0ISRIsSettingPORTAto0x55) { ISR_INT0_vect(); // Fake INT0 Interrupt CHECK_EQUAL_C_INT(0x55, PORTA); }
  • 21. VIA University College ICT-Engineering TestDrivenDevelopmentinC What Test-cases to write: Take a look at James W. Grenning’s ZOMBIES: – Z – Zero – O – One – M – Many (or More complex) – B – Boundary Behaviours – I – Interface definition – E – Exercise Exceptional behaviour – S – Simple Scenarios, Simple Solutions 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 21
  • 22. VIA University College ICT-Engineering TestDrivenDevelopmentinC Pros: – Production code can be developed at Host-computer – Use all Host-computer tools and Debugger facilities – Less errors and failures when moving to target computer – Develop without the target hardware Cons: – It takes time to figure out how to get rid of the hardware dependencies in the target tool-chain (One time only investment) – Real-time aspects can’t be tested on the Host-computer 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 22
  • 23. VIA University College ICT-Engineering Conclusion – It is hard to develop low-level and hardware near software – Invest time in setting up your tools and environment (one time investment per platform) – Use all the principles that we know from high-level development (OOD/P) – Document using UML and/or SysML – TTD is possible and not very hard when tools are setup – Software is easy and safe to maintain if it is well designed and the test suite is up-to-date 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 23
  • 24. VIA University College ICT-Engineering References – CppUTest (open source): http://cpputest.github.io/index.html – James W. Grenning, 2011: Test Driven Development for Embedded C, 1st Edition, ISBN-13: 978-1934356623, ISBN-10: 9781934356623 – Axel Schreiner : Object-oriented Programming in ANSI-C – Beck, K., 2002 Test Driven Development: By Example, ISBN-10: 9780321146533 ISBN-13: 978-0321146533 – Robert C. Martin, 2002: Agile Software Development, Principles, Patterns, and Practices, ISBN10 0135974445 ISBN13 9780135974445 – James W. Grenning’s ZOMBIES: http://blog.wingman-sw.com/archives/677 2018-11-21Object Orientering, Test Driven Development og C - InfinIT - Ib Havn, iha@via.dk 24