HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test_mockio.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include <mockio.h>
6 
7 #define S HIPC_SUCCESS
8 #define R 0
9 #define W 1
10 
11 static void test_mockio(void)
12 {
13  unsigned char buf[HIPC_IMG_ALLOCSIZE];
14  struct MockIO mio;
15  int i;
16 
17  struct MockIOOp ops[] = {
18  {R, S, 0},
19  {W, S, 0},
20  {R, S, 4, {2, 3, 5, 7}},
21  {W, S, 3, {11, 13, 17}},
22  {R, HIPC_ERR_IO_READ, 1, {6}},
23  {W, HIPC_ERR_IO_WRITE, 1, {30}},
24  {W, S, 1, {23}},
25  };
26  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
27  assert(0 == mockIOCheckCompletion(&mio));
28 
29  for (i = 0; i < sizeof(buf); ++i) {
30  buf[i] = 19;
31  }
32 
33  assert(S == mockIORead(&mio, buf, 0));
34  for (i = 0; i < sizeof(buf); ++i) {
35  assert(19 == buf[i]);
36  }
37  assert(0 == mockIOCheckCompletion(&mio));
38 
39  assert(S == mockIOWrite(&mio, buf, 0));
40  for (i = 0; i < sizeof(buf); ++i) {
41  assert(19 == buf[i]);
42  }
43  assert(0 == mockIOCheckCompletion(&mio));
44 
45  assert(S == mockIORead(&mio, buf, 4));
46  assert(2 == buf[0]);
47  assert(3 == buf[1]);
48  assert(5 == buf[2]);
49  assert(7 == buf[3]);
50  for (i = 4; i < sizeof(buf); ++i) {
51  assert(19 == buf[i]);
52  }
53  assert(0 == mockIOCheckCompletion(&mio));
54 
55  buf[0] = 11;
56  buf[1] = 13;
57  buf[2] = 17;
58  assert(S == mockIOWrite(&mio, buf, 3));
59  assert(0 == mockIOCheckCompletion(&mio));
60 
61  assert(HIPC_ERR_IO_READ == mockIORead(&mio, buf, 1));
62  assert(6 == buf[0]);
63  assert(0 == mockIOCheckCompletion(&mio));
64 
65  buf[0] = 30;
66  assert(HIPC_ERR_IO_WRITE == mockIOWrite(&mio, buf, 1));
67  assert(0 == mockIOCheckCompletion(&mio));
68 
69  buf[0] = 23;
70  assert(S == mockIOWrite(&mio, buf, 1));
71  assert(1 == mockIOCheckCompletion(&mio));
72 
73  return;
74 }
75 
76 static void test_mockIORead_err(void)
77 {
78  unsigned char buf[HIPC_IMG_ALLOCSIZE];
79  struct MockIO mio;
80 
81  {
82  mockIOInit(&mio, NULL, 0);
83  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIORead(&mio, buf, 0));
84  assert(MOCK_ERR_OVERRUN == mio.mock_errno);
85  assert(-1 == mockIOCheckCompletion(&mio));
86  }
87  {
88  struct MockIOOp ops[] = {
89  {W, S, 0},
90  };
91  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
92  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIORead(&mio, buf, 0));
93  assert(MOCK_ERR_OPCODE == mio.mock_errno);
94  assert(-1 == mockIOCheckCompletion(&mio));
95  }
96  {
97  struct MockIOOp ops[] = {
98  {R, S, 0},
99  {R, S, 0},
100  };
101  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
102  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIORead(&mio, buf, 1));
103  assert(MOCK_ERR_SIZE == mio.mock_errno);
104  assert(-1 == mockIOCheckCompletion(&mio));
105  }
106 
107  return;
108 }
109 
110 static void test_mockIOWrite_err(void)
111 {
112  unsigned char buf[HIPC_IMG_ALLOCSIZE];
113  struct MockIO mio;
114 
115  {
116  mockIOInit(&mio, NULL, 0);
117  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIOWrite(&mio, buf, 0));
118  assert(MOCK_ERR_OVERRUN == mio.mock_errno);
119  assert(-1 == mockIOCheckCompletion(&mio));
120  }
121  {
122  struct MockIOOp ops[] = {
123  {R, S, 0},
124  };
125  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
126  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIOWrite(&mio, buf, 0));
127  assert(MOCK_ERR_OPCODE == mio.mock_errno);
128  assert(-1 == mockIOCheckCompletion(&mio));
129  }
130  {
131  struct MockIOOp ops[] = {
132  {W, S, 0},
133  {W, S, 0},
134  };
135  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
136  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIOWrite(&mio, buf, 1));
137  assert(MOCK_ERR_SIZE == mio.mock_errno);
138  assert(-1 == mockIOCheckCompletion(&mio));
139  }
140  {
141  struct MockIOOp ops[] = {
142  {W, S, 1, {2}},
143  };
144  mockIOInit(&mio, ops, sizeof(ops) / sizeof(ops[0]));
145  buf[0] = 3;
146  assert(HIPC_ERR_TEST_LIB_INTERNAL == mockIOWrite(&mio, buf, 1));
147  assert(MOCK_ERR_DATA == mio.mock_errno);
148  assert(-1 == mockIOCheckCompletion(&mio));
149  }
150 
151  return;
152 }
153 
154 int main(int argc, char *argv[])
155 {
156  if (2 != argc) {
157  fprintf(stderr, "Usage: %s test_id\n", argv[0]);
158  exit(EXIT_FAILURE);
159  }
160 
161  switch (argv[1][0]) {
162  case 'a':
163  test_mockio();
164  break;
165  case 'b':
167  break;
168  case 'c':
170  break;
171  default:
172  fprintf(stderr, "Invalid test_id: %s \n", argv[1]);
173  exit(EXIT_FAILURE);
174  }
175 
176  return 0;
177 }
#define HIPC_IMG_ALLOCSIZE
Definition: hipc.h:46
static void test_mockio(void)
Definition: test_mockio.c:11
#define W
Definition: test_mockio.c:9
int mockIOCheckCompletion(struct MockIO *mio_p)
Definition: mockio.c:19
static void test_mockIORead_err(void)
Definition: test_mockio.c:76
#define R
Definition: test_mockio.c:8
void mockIOInit(struct MockIO *mio_p, const struct MockIOOp *ops, const unsigned int nops)
Definition: mockio.c:8
unsigned int i
Definition: mockio.h:21
enum MOCK_errno mock_errno
Definition: mockio.h:20
static void test_mockIOWrite_err(void)
Definition: test_mockio.c:110
enum HIPC_errno mockIOWrite(struct MockIO *mio_p, const void *buf, const size_t size)
Definition: mockio.c:63
Definition: mockio.h:19
#define S
Definition: test_mockio.c:7
enum HIPC_errno mockIORead(struct MockIO *mio_p, void *buf, const size_t size)
Definition: mockio.c:30
int main(int argc, char *argv[])
Definition: test_mockio.c:154