summaryrefslogtreecommitdiff
path: root/deps/fff/examples/weak_linking/test
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-24 22:47:46 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-24 22:47:46 +1100
commit7b3afcaf77f96e7d62f6cd1623ead7f17512d79f (patch)
treeb5f82c64e9c06a84e4d095ab4ac48712e860b673 /deps/fff/examples/weak_linking/test
parentb047be5252aeb981faea077409c1768fda0301d9 (diff)
repo init. partial port of existing code
Diffstat (limited to 'deps/fff/examples/weak_linking/test')
-rw-r--r--deps/fff/examples/weak_linking/test/include/bus.fake.h10
-rw-r--r--deps/fff/examples/weak_linking/test/include/display.fake.h10
-rw-r--r--deps/fff/examples/weak_linking/test/include/error.fake.h10
-rw-r--r--deps/fff/examples/weak_linking/test/include/sensor.fake.h13
-rw-r--r--deps/fff/examples/weak_linking/test/include/test_common.h13
-rw-r--r--deps/fff/examples/weak_linking/test/src/bus.fake.c4
-rw-r--r--deps/fff/examples/weak_linking/test/src/display.fake.c4
-rw-r--r--deps/fff/examples/weak_linking/test/src/display.test.c24
-rw-r--r--deps/fff/examples/weak_linking/test/src/error.fake.c4
-rw-r--r--deps/fff/examples/weak_linking/test/src/main.test.c26
-rw-r--r--deps/fff/examples/weak_linking/test/src/sensor.fake.c11
-rw-r--r--deps/fff/examples/weak_linking/test/src/sensor.test.c23
-rw-r--r--deps/fff/examples/weak_linking/test/src/test_common.c33
13 files changed, 185 insertions, 0 deletions
diff --git a/deps/fff/examples/weak_linking/test/include/bus.fake.h b/deps/fff/examples/weak_linking/test/include/bus.fake.h
new file mode 100644
index 0000000..170e45a
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/include/bus.fake.h
@@ -0,0 +1,10 @@
+#ifndef _AUTOFAKE_BUS_H
+#define _AUTOFAKE_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 // _AUTOFAKE_BUS_H
diff --git a/deps/fff/examples/weak_linking/test/include/display.fake.h b/deps/fff/examples/weak_linking/test/include/display.fake.h
new file mode 100644
index 0000000..ef2adae
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/include/display.fake.h
@@ -0,0 +1,10 @@
+#ifndef _AUTOFAKE_DISPLAY_H
+#define _AUTOFAKE_DISPLAY_H
+
+#include "fff.h"
+#include "display.h"
+
+DECLARE_FAKE_VALUE_FUNC( bool, display_init );
+DECLARE_FAKE_VOID_FUNC( display_update, const char* );
+
+#endif // _AUTOFAKE_DISPLAY_H
diff --git a/deps/fff/examples/weak_linking/test/include/error.fake.h b/deps/fff/examples/weak_linking/test/include/error.fake.h
new file mode 100644
index 0000000..ee127ec
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/include/error.fake.h
@@ -0,0 +1,10 @@
+#ifndef _AUTOFAKE_ERROR_H
+#define _AUTOFAKE_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 // _AUTOFAKE_ERROR_H
diff --git a/deps/fff/examples/weak_linking/test/include/sensor.fake.h b/deps/fff/examples/weak_linking/test/include/sensor.fake.h
new file mode 100644
index 0000000..ae363f8
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/include/sensor.fake.h
@@ -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
diff --git a/deps/fff/examples/weak_linking/test/include/test_common.h b/deps/fff/examples/weak_linking/test/include/test_common.h
new file mode 100644
index 0000000..a282dd1
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/include/test_common.h
@@ -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];
diff --git a/deps/fff/examples/weak_linking/test/src/bus.fake.c b/deps/fff/examples/weak_linking/test/src/bus.fake.c
new file mode 100644
index 0000000..e3a9f76
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/bus.fake.c
@@ -0,0 +1,4 @@
+#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 );
diff --git a/deps/fff/examples/weak_linking/test/src/display.fake.c b/deps/fff/examples/weak_linking/test/src/display.fake.c
new file mode 100644
index 0000000..f8dec66
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/display.fake.c
@@ -0,0 +1,4 @@
+#include "display.fake.h"
+
+DEFINE_FAKE_VALUE_FUNC( bool, display_init );
+DEFINE_FAKE_VOID_FUNC( display_update, const char* );
diff --git a/deps/fff/examples/weak_linking/test/src/display.test.c b/deps/fff/examples/weak_linking/test/src/display.test.c
new file mode 100644
index 0000000..269ae32
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/display.test.c
@@ -0,0 +1,24 @@
+#include "test_common.h"
+#include "display.h"
+
+DEFINE_FFF_GLOBALS;
+
+
+int main(void)
+{
+ init_tests(); // Resets 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;
+}
diff --git a/deps/fff/examples/weak_linking/test/src/error.fake.c b/deps/fff/examples/weak_linking/test/src/error.fake.c
new file mode 100644
index 0000000..0a39478
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/error.fake.c
@@ -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* );
diff --git a/deps/fff/examples/weak_linking/test/src/main.test.c b/deps/fff/examples/weak_linking/test/src/main.test.c
new file mode 100644
index 0000000..6ac023c
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/main.test.c
@@ -0,0 +1,26 @@
+
+#include "display.fake.h"
+#include "sensor.fake.h"
+#include "test_common.h"
+
+DEFINE_FFF_GLOBALS;
+
+
+int update_main( void );
+
+int main(void)
+{
+ init_tests(); // Resets 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;
+}
diff --git a/deps/fff/examples/weak_linking/test/src/sensor.fake.c b/deps/fff/examples/weak_linking/test/src/sensor.fake.c
new file mode 100644
index 0000000..4fdaa66
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/sensor.fake.c
@@ -0,0 +1,11 @@
+#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;
+}
diff --git a/deps/fff/examples/weak_linking/test/src/sensor.test.c b/deps/fff/examples/weak_linking/test/src/sensor.test.c
new file mode 100644
index 0000000..8126cb6
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/sensor.test.c
@@ -0,0 +1,23 @@
+#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;
+}
diff --git a/deps/fff/examples/weak_linking/test/src/test_common.c b/deps/fff/examples/weak_linking/test/src/test_common.c
new file mode 100644
index 0000000..58c3b44
--- /dev/null
+++ b/deps/fff/examples/weak_linking/test/src/test_common.c
@@ -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;
+} \ No newline at end of file