Testing and Debugging
Status Codes
Named status values make success and failure paths easier to read.
named codes
Constants give status numbers readable names.
branch result
The final branch can translate the status into the value the caller needs.
Status Codes
status_codes.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = 2;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = 0;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = 5;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
STATUS_OK ← 0, STATUS_EMPTY ← 1, count ← 2, status ← 0, average ← 0
3int main(void) {4 const int STATUS_OK→ 0 = 0;5 const int STATUS_EMPTY→ 1 = 1;6 int count→ 2 = 2; //@count=0, 57 int status→ 0 = STATUS_OK0;8 int average→ 0 = 0;average ← 15
11 status = STATUS_EMPTY;12} else {13 average→ 15 = 30 / count2;14}printf("status=%d average=%d ", status, average);
16 printf("status=%d average=%d\n", status0, average15);17 return 0;18}outputstatus=0 average=15
STATUS_OK ← 0, STATUS_EMPTY ← 1, count ← 0, status ← 0, average ← 0
3int main(void) {4 const int STATUS_OK→ 0 = 0;5 const int STATUS_EMPTY→ 1 = 1;6 int count→ 0 = 0;7 int status→ 0 = STATUS_OK0;8 int average→ 0 = 0;status ← 1
10if (count0 == 0) {11 status→ 1 = STATUS_EMPTY1;12} else {printf("status=%d average=%d ", status, average);
16 printf("status=%d average=%d\n", status1, average0);17 return 0;18}outputstatus=1 average=0
STATUS_OK ← 0, STATUS_EMPTY ← 1, count ← 5, status ← 0, average ← 0
3int main(void) {4 const int STATUS_OK→ 0 = 0;5 const int STATUS_EMPTY→ 1 = 1;6 int count→ 5 = 5;7 int status→ 0 = STATUS_OK0;8 int average→ 0 = 0;average ← 6
11 status = STATUS_EMPTY;12} else {13 average→ 6 = 30 / count5;14}printf("status=%d average=%d ", status, average);
16 printf("status=%d average=%d\n", status0, average6);17 return 0;18}outputstatus=0 average=6