Exercise Reliability - JawaharT/Best-Practices-On-Azure-Sphere GitHub Wiki
In general, these exercises use a variety of the topics discussed as part of the 'How: Reliability' section such as an example of writing simple test cases where appropriate and creating an enum for exit codes.
In exercise one, we will write Test Cases using Assert for the Fibonacci program developed in a previous section. It is important to be able to write simple unit test cases that test the behaviour of programs (if it is possible, as mentioned previously), this will provide a glimpse into how well these algorithms can be trusted to be reliable for the long term. Make simple changes to the Fibonacci program to easily be able to write simple test cases.
For a quick reminder here is the Fibonacci program written earlier.
#include <stdio.h>
int end_integer_input(){
static int number;
// Use this for testing purposes
// printf("Enter a Number for Fibonacci Sequence: ");
// scanf("%d", number);
// For embedded systems, equivalent to keypad entry here
number = 10;
return (int)number;
}
void generate_sequence(int end_pos){
int fib_sequence[end_pos];
fib_sequence[0] = 0;
printf("%d ", 0);
if (end_pos >= 2){
fib_sequence[1] = 1;
printf("%d ", 1);
for(int index = 0; index < end_pos-2; index++){
int next_number = fib_sequence[index] + fib_sequence[index+1];
printf("%d ", next_number);
fib_sequence[index+2] = next_number;
}
}
}
int main(){
generate_sequence(end_integer_input());
return 0;
}
As part of exercise two, develop an enum for exit codes and use them inside a brief section of an Azure Sphere project as shown below:
static volatile sig_atomic_t terminationRequired = false;
static volatile sig_atomic_t _exitCode = 0;
// Create your exit code enum here
/// <summary>
/// Main entry point for this application.
/// </summary>
int main(int argc, char* argv[]) {
Log_Debug("Application Started.\n");
dx_registerTerminationHandler();
if (!dx_configParseCmdLineArguments(argc, argv, &dx_config)) {
// Add an potential exitcode here
}
// Create and open peripherals for button poll timer, button GPIO and epoll
if (InitPeripheralsAndHandlers() != 0) {
// Add an potential exitcode here
}
// Use epoll to wait for events and trigger handlers, until an error
while (!terminationRequired) {
int result = EventLoop_Run(dx_timerGetEventLoop(), -1, true);
if ((WaitForEventAndCallHandler(epollFd) != 0) || (result == -1)) {
// Add an potential exitcode here
ClosePeripheralsAndHandlers();
}
}
return 0;
}
For a version of the correct solutions to both exercises and explanations click here.