Skip to content

Instantly share code, notes, and snippets.

@AWolf81
Created February 23, 2020 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AWolf81/dccbf95b203b24f4a958d70568173d7e to your computer and use it in GitHub Desktop.
Save AWolf81/dccbf95b203b24f4a958d70568173d7e to your computer and use it in GitHub Desktop.
ucunit example with trace point call counter feature (see Test_CheckTracepointsDemo function)
/*
* File: example.test.h
* Author: Alexander Wolf
* License MIT
* Created on 23. Februar 2020, 11:54
*/
#include "testsuite.h"
#ifndef EXAMPLE_TEST_H
#define EXAMPLE_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
static void Test_BasicChecksDemo(void)
{
char * s = NULL;
int a = 0;
int b = 0;
int c = 0;
UCUNIT_TestcaseBegin("DEMO:Simple Checks");
UCUNIT_CheckIsEqual( 0, a ); /* Pass*/
UCUNIT_CheckIsEqual( 0, b ); /* Pass */
UCUNIT_CheckIsEqual( 0, c ); /* Pass */
UCUNIT_CheckIsEqual( 0, (1-1) ); /* Pass */
UCUNIT_CheckIsEqual( 0, (-1+1) ); /* Pass */
UCUNIT_CheckIsEqual( -1, (-2+1) ); /* Pass */
UCUNIT_CheckIsNull(s); /* Pass */
UCUNIT_TestcaseEnd();
}
static void Test_PointersDemo(void)
{
char * s = NULL;
UCUNIT_TestcaseBegin("DEMO:Checking pointers");
UCUNIT_CheckIsNull(s); /* Pass */
/* Empty string has a 0 byte/word as end of string */
s = "";
UCUNIT_CheckIsNotNull(s); /* Pass */
UCUNIT_Check( (*s)=='\00' , "No end-of-string found","s" ); /* Pass */
/* Check if first character of string "Hello World!\n" is a 'H' */
s = "Hello World!\n";
UCUNIT_CheckIsNotNull(s); /* Pass */
UCUNIT_Check( (*s)=='H' , "String starts not with 'H'","s" ); /* Pass */
UCUNIT_TestcaseEnd(); /* Pass */
}
static void Test_ChecklistDemo(void)
{
int a = 0;
int b = 0;
int c = 0;
UCUNIT_TestcaseBegin("DEMO:Checklists");
/* Check if calculation works */
a = 10;
b = -20;
c = a + b;
UCUNIT_ChecklistBegin(UCUNIT_ACTION_WARNING);
UCUNIT_CheckIsEqual( -10, c ); /* Pass */
UCUNIT_CheckIsInRange(a,0,10); /* Pass */
UCUNIT_CheckIsInRange(a,10,20); /* Fail */ // changed from 11 to 10
UCUNIT_CheckIsInRange(a,0,10); /* Fail */ // change from 9 to 10
UCUNIT_ChecklistEnd();
UCUNIT_TestcaseEnd(); /* Fail */
}
static void Test_BitChecksDemo(void)
{
UCUNIT_TestcaseBegin("DEMO:Checking Bits");
UCUNIT_CheckIsBitSet(0x0001, 0); /* Pass */
UCUNIT_CheckIsBitClear(0x0000, 0); /* Fail */ // changed from IsBitSet to Clear
UCUNIT_CheckIsBitSet(0x0002, 1); /* Pass */
UCUNIT_CheckIsBitClear(0x0000, 6); /* Fail */ // changed from IsBitSet to Clear
UCUNIT_CheckIsBitSet(0xFFFF, 6); /* Pass */
UCUNIT_CheckIsBitSet(0x0001, 0); /* Fail */ // changed from IsBitClear to Set
UCUNIT_CheckIsBitClear(0x0001, 1); /* Pass */
UCUNIT_TestcaseEnd(); /* Fail */
}
static void Test_CheckTracepointsDemo(void)
{
int found = FALSE;
int i = 0;
unsigned short a = 0;
UCUNIT_TestcaseBegin("DEMO:Tracepoints");
a = 0x1234;
UCUNIT_ResetTracepointCoverage();
UCUNIT_Tracepoint(0); /* Pass */
UCUNIT_CheckIs16Bit(a); /* Fail */ // changed from is8bit to is 16bit
UCUNIT_CheckIs16Bit(a); /* Pass */
UCUNIT_CheckIs32Bit(a); /* Pass */
UCUNIT_Tracepoint(1); /* Pass */
UCUNIT_CheckTracepointCoverage(0); /* Pass */
UCUNIT_CheckTracepointCoverage(1); /* Pass */
UCUNIT_ResetTracepointCoverage();
for (i = 0; (i < 50) && (!found); i++)
{
UCUNIT_Tracepoint(3); // called 11 times
if (i == 25)
{
UCUNIT_Tracepoint(0); /* Never executed */
break;
}
if (i == 10)
{
UCUNIT_Tracepoint(1); /* Executed */
found = TRUE;
}
if (i == 15)
{
UCUNIT_Tracepoint(2); /* Never executed */
found = TRUE;
}
}
for (i=0; i < 65535; i++) {
UCUNIT_Tracepoint(4); // called 2^16 times
}
UCUNIT_CheckTracepointCoverage(0); /* Fail */
UCUNIT_CheckTracepointCoverage(1); /* Pass */
UCUNIT_CheckTracepointCoverage(2); /* Fail */
UCUNIT_TracepointCalled(3); /* Pass */
UCUNIT_TracepointNotCalled(0); /* Pass */
UCUNIT_TracepointCalledOnce(1); /* Pass */
UCUNIT_TracepointCalledOnce(3); /* Failed */ // called 11 times
UCUNIT_TracepointCalledNTimes(3, 10); /* Fail */
UCUNIT_TracepointCalledNTimes(3, 11); /* Pass */
UCUNIT_TracepointCalledNTimes(3, 1); /* Fail */
UCUNIT_TracepointCalledNTimes(3, 12); /* Fail */
UCUNIT_TracepointCalledNTimes(4, 65534); /* Pass */ // not exceeded 2^16-1
UCUNIT_TracepointCalledNTimes(4, 65535); /* Fail */ // on limit 2^16
UCUNIT_TestcaseEnd(); /* Fail */
}
#ifdef __cplusplus
}
#endif
#endif /* EXAMPLE_TEST_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment