HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test_pbank.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include <hipc/pbank.h>
6 
7 struct page {
8  int a;
9  char b;
10 };
11 
12 static void test_hipcPbank(void)
13 {
14  struct HIPC_pbank bank;
15  struct page buf[2];
16  struct page *pageRW;
17  const struct page *pageRO;
18 
19  buf[0].a = 2;
20  buf[0].b = 3;
21  hipcPbankInit(&bank, buf, sizeof(buf[0]));
22  assert(HIPC_PBANK_STAT_GROUND == hipcPbankGetStat(&bank));
23  assert(sizeof(buf[0]) == hipcPbankGetPageSize(&bank));
24 
25  pageRW = hipcPbankGetWritablePage(&bank);
26  assert(NULL != pageRW);
27  assert(2 == pageRW->a);
28  assert(3 == pageRW->b);
29 
30  pageRW->a = 5;
31 
32  assert(0 == hipcPbankSwitchAccept(&bank));
33  pageRO = hipcPbankGetReadOnlyPage(&bank);
34  assert(NULL != pageRO);
35  assert(2 == pageRO->a);
36  assert(3 == pageRO->b);
37 
38  assert(HIPC_SUCCESS == hipcPbankSwitchRequest(&bank));
40 
41  assert(1 == hipcPbankSwitchAccept(&bank));
42  pageRO = hipcPbankGetReadOnlyPage(&bank);
43  assert(NULL != pageRO);
44  assert(5 == pageRO->a);
45  assert(3 == pageRO->b);
47 
48  pageRW = hipcPbankGetWritablePage(&bank);
49  assert(NULL != pageRW);
50  assert(2 == pageRW->a);
51  assert(3 == pageRW->b);
52 
53  assert(HIPC_SUCCESS == hipcPbankSwitchComplete(&bank));
54  assert(HIPC_PBANK_STAT_GROUND == hipcPbankGetStat(&bank));
55  assert(5 == pageRW->a);
56  assert(3 == pageRW->b);
57 
58  return;
59 }
60 
61 static void test_hipcPbank_err(void)
62 {
63  struct HIPC_pbank bank;
64  struct page buf[2];
65 
66  hipcPbankInit(&bank, buf, sizeof(buf[0]));
67  assert(HIPC_PBANK_STAT_GROUND == hipcPbankGetStat(&bank));
68 
71 
72  assert(HIPC_SUCCESS == hipcPbankSwitchRequest(&bank));
74 
76  hipcPbankSwitchRequest(&bank));
79 
80  assert(1 == hipcPbankSwitchAccept(&bank));
82 
84  hipcPbankSwitchRequest(&bank));
85 
86  assert(HIPC_SUCCESS == hipcPbankSwitchComplete(&bank));
87  assert(HIPC_PBANK_STAT_GROUND == hipcPbankGetStat(&bank));
88 
89  return;
90 }
91 
92 int main(int argc, char *argv[])
93 {
94  if (2 != argc) {
95  fprintf(stderr, "Usage: %s test_id\n", argv[0]);
96  exit(EXIT_FAILURE);
97  }
98 
99  switch (argv[1][0]) {
100  case 'a':
101  test_hipcPbank();
102  break;
103  case 'b':
105  break;
106  default:
107  fprintf(stderr, "Invalid test_id: %s \n", argv[1]);
108  exit(EXIT_FAILURE);
109  }
110 
111  return 0;
112 }
void * hipcPbankGetWritablePage(struct HIPC_pbank *const bank_p)
Gets a current writable page.
Definition: pbank.c:55
int main(int argc, char *argv[])
Definition: test_pbank.c:92
enum HIPC_errno hipcPbankSwitchRequest(struct HIPC_pbank *const bank_p)
Requests to switch pages.
Definition: pbank.c:77
int a
Definition: test_pbank.c:8
const void * hipcPbankGetReadOnlyPage(struct HIPC_pbank *const bank_p)
Gets a current read only page.
Definition: pbank.c:44
Definition: test_pbank.c:7
enum HIPC_pbank_stat hipcPbankGetStat(struct HIPC_pbank *const bank_p)
Gets current status of a bank.
Definition: pbank.c:66
static void test_hipcPbank_err(void)
Definition: test_pbank.c:61
static void test_hipcPbank(void)
Definition: test_pbank.c:12
char b
Definition: test_pbank.c:9
void hipcPbankInit(struct HIPC_pbank *const bank_p, void *const buf_p, const size_t page_size)
Initializes a parameter bank.
Definition: pbank.c:14
size_t hipcPbankGetPageSize(struct HIPC_pbank *const bank_p)
Gets page size.
Definition: pbank.c:33
enum HIPC_errno hipcPbankSwitchComplete(struct HIPC_pbank *const bank_p)
Makes both pages have the same latest contents and completes the switching.
Definition: pbank.c:121
int hipcPbankSwitchAccept(struct HIPC_pbank *const bank_p)
Accepts a switching request and switches pages if switching is requested.
Definition: pbank.c:96