-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcu_mocks.c
91 lines (80 loc) · 1.89 KB
/
cu_mocks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @file Mocking library
*
* Elements to allow unit tests primary focused on embedded-like environments,
* but in a full featured environment
*
* @author CieNTi
* @version 1.0.0
*/
#include "cu_mocks.h"
#include "cu_commons.h"
#include "cu_ui.h"
#include <stdio.h>
/* Position at source array */
static int fgetc_mock_idx = 0;
/* Integer with multiple 0x00 as "nothing received" and a backspace */
static char nonblock_int[] =
{
0x00, 0x00, '-', '1', '3', '\b', '2', 0x00, 0x00, 0x00, '\r', '\n'
};
/* Float with multiple 0x00 as "nothing received" and a backspace */
static char nonblock_float[] =
{
0x00, 0x00, '-', '1', '3', '\b', '2', 0x00, 0x00, 0x00, '3', '.', '4',
'5', '6', '\r', '\n'
};
/* String with multiple 0x00 as "nothing received" and a backspace */
static char nonblock_string[] =
{
0x00, 0x00, 'h', 'e', 'k', '\b', 'l', 0x00, 0x00, 0x00, 'l', 'o', 'l',
'o', 'l', 'o', 'l', 'o', 'l', 'o', 'l', 'o', 'l', 'o', 'l', 'o', 'l',
'o', 'l', 'o', 'l', 'o', 'l', 'a', 'i', '\r', '\n'
};
/* Data selector */
static char *source_data = NULL;
/**/
void set_fgetc_mock_type(int data_type)
{
/* Reset and set! */
fgetc_mock_idx = 0;
PRINTF("* FGETC will returns a ");
switch (data_type)
{
case CU_MOCK_TYPE_INT:
PRINTF("int");
source_data = nonblock_int;
break;
case CU_MOCK_TYPE_FLOAT:
PRINTF("float");
source_data = nonblock_float;
break;
case CU_MOCK_TYPE_STRING:
PRINTF("string");
source_data = nonblock_string;
break;
default:
source_data = NULL;
break;
}
PRINTF(" type\n");
}
/**/
char fgetc_nonblock_mock(void)
{
if (source_data != NULL)
{
if (source_data[fgetc_mock_idx] == '\n')
{
/* Start array again */
fgetc_mock_idx = 0;
return '\n';
}
else
{
/* Keep going */
return source_data[fgetc_mock_idx++];
}
}
return 0x00;
}