1
0
mirror of https://github.com/meekrosoft/fff synced 2026-01-26 18:01:36 +01:00

Add example for the weak linking

This commit is contained in:
Pauli Salmenrinne
2019-01-22 15:11:10 +02:00
parent 647737304d
commit 0a7fbeceec
25 changed files with 414 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
#include "bus.fake.h"
DEFINE_FAKE_VALUE_FUNC( bool, bus_read_write, uint8_t, uint8_t, uint8_t*, int, bool );
DEFINE_FAKE_VALUE_FUNC( bool, bus_write, uint8_t, uint8_t, const uint8_t*, int, bool );

View File

@@ -0,0 +1,11 @@
#ifndef _AUTOMOCK_BUS_H
#define _AUTOMOCK_BUS_H
#include "fff.h"
#include "bus.h"
DECLARE_FAKE_VALUE_FUNC( bool, bus_read_write, uint8_t, uint8_t, uint8_t*, int, bool );
DECLARE_FAKE_VALUE_FUNC( bool, bus_write, uint8_t, uint8_t, const uint8_t*, int, bool );
#endif // _AUTOMOCK_BUS_H

View File

@@ -0,0 +1,4 @@
#include "display.fake.h"
DEFINE_FAKE_VALUE_FUNC( bool, display_init );
DEFINE_FAKE_VOID_FUNC( display_update, const char* );

View File

@@ -0,0 +1,11 @@
#ifndef _AUTOMOCK_DISPLAY_H
#define _AUTOMOCK_DISPLAY_H
#include "fff.h"
#include "display.h"
DECLARE_FAKE_VALUE_FUNC( bool, display_init );
DECLARE_FAKE_VOID_FUNC( display_update, const char* );
#endif // _AUTOMOCK_DISPLAY_H

View File

@@ -0,0 +1,27 @@
#include "test_common.h"
#include "display.h"
DEFINE_FFF_GLOBALS;
int main(void)
{
init_tests(); // will reset common and hook errors to asserts
GLOBAL_TEST_bus_read_ret[2] = 0x10;
assert( display_init() == true );
assert( bus_read_write_fake.call_count == 1);
display_update( "TEST INFO" );
assert( bus_read_write_fake.call_count == 1 );
assert( bus_write_fake.call_count == 1 );
GLOBAL_TEST_bus_read_ret[2] = 0x00;
assert( display_init() == false );
printf("Test " __FILE__ " ok\n");
return 0;
}

View File

@@ -0,0 +1,4 @@
#include "error.fake.h"
DEFINE_FAKE_VOID_FUNC( runtime_error, const char* );
DEFINE_FAKE_VALUE_FUNC( char*, runtime_error_nice_print, const char* );

View File

@@ -0,0 +1,11 @@
#ifndef _AUTOMOCK_ERROR_H
#define _AUTOMOCK_ERROR_H
#include "fff.h"
#include "error.h"
DECLARE_FAKE_VOID_FUNC( runtime_error, const char* );
DECLARE_FAKE_VALUE_FUNC( char*, runtime_error_nice_print, const char* );
#endif // _AUTOMOCK_ERROR_H

View File

@@ -0,0 +1,28 @@
#include "display.fake.h"
#include "sensor.fake.h"
#include "test_common.h"
DEFINE_FFF_GLOBALS;
int update_main( void );
int main(void)
{
init_tests(); // will reset common and hook errors to asserts
sensor_init_fake.return_val = true;
display_init_fake.return_val = true;
update_main();
assert( sensor_init_fake.call_count == 1 );
assert( display_init_fake.call_count == 1 );
assert( display_update_fake.call_count == 1 );
printf("Test " __FILE__ " ok\n");
return 0;
}

View File

@@ -0,0 +1,12 @@
#include "sensor.fake.h"
DEFINE_FAKE_VALUE_FUNC( bool, sensor_init );
DEFINE_FAKE_VALUE_FUNC( float, sensor_read );
void TEST_sensor_generate_data( char* buffer, float value )
{
buffer[0] = 0x10;
buffer[1] = (int)(value);
buffer[2] = (value - buffer[1])*255;
}

View File

@@ -0,0 +1,13 @@
#ifndef _AUTOMOCK_SENSOR_H
#define _AUTOMOCK_SENSOR_H
#include "fff.h"
#include "sensor.h"
DECLARE_FAKE_VALUE_FUNC( bool, sensor_init );
DECLARE_FAKE_VALUE_FUNC( float, sensor_read );
void TEST_sensor_generate_data( char* buffer, float value );
#endif // _AUTOMOCK_SENSOR_H

View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include "test_common.h"
#include "sensor.h"
#include "sensor.fake.h"
DEFINE_FFF_GLOBALS;
int main(void)
{
init_tests();
assert( sensor_init() == true );
TEST_sensor_generate_data( GLOBAL_TEST_bus_read_ret, 1.5f );
float value = sensor_read();
float value_error = value - 1.5f;
assert( value_error < 0.1f && value_error > -0.1f );
printf("Test " __FILE__ " ok\n");
return 0;
}

View File

@@ -0,0 +1,33 @@
#include "test_common.h"
#include <assert.h>
#include <stdio.h>
char GLOBAL_TEST_bus_read_ret[32];
void spoof_runtime_error( const char* info )
{
fprintf(stderr, "Runtime error: %s\n", info );
assert(0);
}
bool spoof_bus_read_write( uint8_t dev, uint8_t registry, uint8_t* buffer, int len, bool assume_echo )
{
memcpy( buffer, GLOBAL_TEST_bus_read_ret, len );
fprintf(stderr, "bus spoof %d %d\n", (int)dev, (int)registry );
return true;
}
void init_tests()
{
memset( GLOBAL_TEST_bus_read_ret, 0x00, sizeof(GLOBAL_TEST_bus_read_ret));
FFF_RESET_HISTORY();
RESET_FAKE(bus_read_write);
RESET_FAKE(bus_write);
RESET_FAKE(runtime_error);
runtime_error_fake.custom_fake = spoof_runtime_error;
bus_read_write_fake.custom_fake = spoof_bus_read_write;
bus_write_fake.return_val = true;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include <assert.h>
#include <stdio.h>
#include "fff.h"
#include "bus.fake.h"
#include "error.fake.h"
void init_tests();
extern char GLOBAL_TEST_bus_read_ret[32];