When the value of the global variable determines whether a function is called, it is tested using [Before/Atfer call code].

// Before call code
if( globalVar == 1 ) {
// Function call code.
func()
// After call code
}

In this way, add an if statement in the code before and after the function call to invoke the function when specific conditions are met. I’ll explain in detail using examples of traffic lights and sound signals.

Source Code and Requirements

The source code and requirements to be used as an example are as follows.

#include <stdio.h>

typedef enum {
    RED,
    GREEN
} TrafficLightState;

typedef enum {
	ON,
	OFF
} SoundSystemState;

TrafficLightState trafficLight;
SoundSystemState soundSystem;

void init() {
	trafficLight = GREEN;
	soundSystem = OFF;
}

void setSoundSystem (SoundSystemState state) {
	soundSystem = state;
}

void alarmForBlind() {
	if( trafficLight == GREEN ) {
		printf("beep for blind\n");
	} else if( trafficLight == RED ) {
		printf("warning for blind\n");
	}
}

void tick() {
    static int timer = 50;

	if(timer > 0) {
		timer--;
	}

	if( timer == 0 ) {
		timer = 50;
		if( trafficLight == GREEN ) {
			trafficLight = RED;
		} else if( trafficLight == RED ) {
			trafficLight = GREEN;
		}
	}
}

Requirement: The red and green lights are each on for 5 minutes.
If the sound signal is on and the traffic light is green, a signal for visually impaired individuals is output once per second.
If the sound signal is on and the traffic light is red, a warning for visually impaired individuals is output once per second.

Test Design

We will design a test that meets these requirements.

h3 Design of Periodic Function and Cycle Unit

In the tick() function, the timer of the traffic light is set to 50, and as per the requirement, each light stays on for 5 seconds. Therefore, the cycle unit is ms, and the cycle time for tick() is 100ms.
Since the sound signal outputs a signal every 1 second, the cycle time for alarmForBlind() is 1000ms (1 second).

h3 Design of Initial Function

Call the initial() function as an initial function to assign initial values to each sensor and state. At this point, the initial value of trafficLight is GREEN and the initial value of soundSystem is OFF.

h3 Design of Test Cases and Variables

To verify the requirements, we’ll turn on the sound signal and check signals when the light is blue or red. Accordingly, we’ll design the test cases as follows:

TC Time Sound Signal
1 5000 OFF
2 5000 OFF
3 5000 ON
4 5000 ON
5 5000 OFF
6 5000 OFF

To change the sound signal value, input the following into the user code. When the test case is 3, turn on the sound signal, and when the test case is 5, turn it off.

if (CS_TESTCASENO()==3) {
	setSoundSystem(ON);
} else if (CS_TESTCASENO()==5) {
	setSoundSystem(OFF);
}

Select trafficLight from the global variables and check [Output] to confirm the status of the traffic light.

Write Test

Based on the above design, writing tests will look like the following:

  • [Test Info Tab] > [User code]
  • [Test Info Tab] > [Test target function]
  • [Test Info Tab] > [Before/After call code]

Checking Test Results

Run the test and verify the results in [Test Case Tab] and [Source Code Editor].

  • In [Test Case Tab], confirm that trafficLight changes every 5 seconds.
  • Use the coverage displayed in [Source Code Editor] to confirm that the signal rang in test case #3 and the warning sounded in test case #4.

Need more help with this?
Don’t hesitate to contact us here.

Thanks for your feedback.