28.02.2014 Views

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We've already seen how to make sure that it progresses to the right state, but now we also want to<br />

check that it correctly updates the speed.<br />

Obviously, the solution here is testing. Testing is the activity of giving input to the code to see how it<br />

behaves. The goal of testing is not to show that it is correct, but rather to show that there are bugs (or<br />

faults, as we say).<br />

Broadly speaking, there are two ways to create test cases for a piece of code:<br />

• Black-box testing: look at the specification and come up with inputs that exercise as much of<br />

the spec as possible, i.e. try all the different conditions that are described<br />

• White-box testing: look at the code and come up with inputs that exercise as many<br />

statements/paths/branches as possible, i.e. try all the code<br />

Let's start with black-box testing. Consider this part of the spec:<br />

“if the driver chooses to accelerate, the speed increases by 10, unless that would put the speed over<br />

100, in which case it doesn't change”<br />

There are two conditions here that we'd want to check:<br />

1. the driver chooses to accelerate, but that doesn't put the speed over 100<br />

2. the driver chooses to accelerate, and that does put the speed over 100<br />

For each of these, we make the assumption that if it works correctly for some input, it will also work<br />

correctly for “similar” inputs. This is known as equivalence partitioning. For instance, if we decide to<br />

use 20 as the speed for #1, and it works correctly, we probably don't need to also test 21, 22, 30, 40, 80,<br />

etc.<br />

So we can now create some test code like this:<br />

/* this tests accelerating but not going over 100 */<br />

int curr_speed = 20;<br />

enum events event = accel;<br />

enum states curr_state = moving;<br />

int new_speed = control(curr_speed, event, &curr_state);<br />

if (new_speed != 30)<br />

printf(“control returned wrong speed!\n”);<br />

if (curr_state != moving)<br />

printf(“control returned wrong state!\n”);<br />

/* this tests accelerating but trying to go over 100 */<br />

curr_speed = 95;<br />

event = accel;<br />

curr_state = moving;<br />

new_speed = control(curr_speed, event, &curr_state);<br />

if (new_speed != 95)<br />

printf(“control returned wrong speed!\n”);<br />

if (curr_state != moving)<br />

printf(“control returned wrong state!\n”);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!