MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_hci.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <setjmp.h>
5 #include <cmocka.h>
6 #include <math.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include "stub.h"
10 
11 #include <libgadget/hci.h>
12 
13 char prefix[1024] = "XXXXXXXX";
14 
15 static void
16 touch(char * prefix, char * b)
17 {
18  char * fn = fastpm_strdup_printf("%s/%s", prefix, b);
19  FILE * fp = fopen(fn, "w");
20  myfree(fn);
21  fclose(fp);
22 }
23 
24 static int
25 exists(char * prefix, char * b)
26 {
27  char * fn = fastpm_strdup_printf("%s/%s", prefix, b);
28  FILE * fp = fopen(fn, "r");
29  myfree(fn);
30  if(fp) {
31  fclose(fp);
32  return 1;
33  }
34  return 0;
35 }
36 
38  {.OVERRIDE_NOW = 1, ._now = 0.0}};
39 
40 
41 static void
42 test_hci_no_action(void ** state)
43 {
44  HCIAction action[1];
45 
47  hci_init(manager, prefix, 10.0, 1.0, 0);
48 
49  hci_query(manager, action);
50  assert_int_equal(action->type, HCI_NO_ACTION);
51  assert_int_equal(action->write_snapshot, 0);
52 
53 }
54 
55 static void
57 {
58  HCIAction action[1];
59 
61  hci_init(manager, prefix, 10.0, 1.0, 1);
62 
64  hci_query(manager, action);
65 
67  hci_query(manager, action);
68 
69  assert_int_equal(action->type, HCI_AUTO_CHECKPOINT);
70  assert_int_equal(action->write_snapshot, 1);
71  assert_int_equal(action->write_fof, 1);
72  assert_true(manager->LongestTimeBetweenQueries == 1.0);
73 }
74 
75 static void
77 {
78 
79  HCIAction action[1];
81  hci_init(manager, prefix, 10.0, 1.0, 0);
82 
84  hci_query(manager, action);
85 
87  hci_query(manager, action);
88 
89  assert_true(manager->LongestTimeBetweenQueries == 2.0);
90  assert_int_equal(action->type, HCI_AUTO_CHECKPOINT);
91  assert_int_equal(action->write_snapshot, 1);
92  assert_int_equal(action->write_fof, 0);
93 }
94 
95 static void
96 test_hci_timeout(void ** state)
97 {
98  HCIAction action[1];
100  hci_init(manager, prefix, 10.0, 1.0, 1);
101 
103  hci_query(manager, action);
104 
105  assert_true(manager->LongestTimeBetweenQueries == 4.0);
106 
108  hci_query(manager, action);
109  assert_int_equal(action->type, HCI_TIMEOUT);
110  assert_int_equal(action->write_snapshot, 1);
111 }
112 
113 static void
114 test_hci_stop(void ** state)
115 {
116  HCIAction action[1];
118  hci_init(manager, prefix, 10.0, 1.0, 1);
119 
120  touch(prefix, "stop");
122  hci_query(manager, action);
123  assert_false(exists(prefix, "stop"));
124 
125  assert_int_equal(action->type, HCI_STOP);
126  assert_int_equal(action->write_snapshot, 1);
127 }
128 
129 static void
130 test_hci_checkpoint(void ** state)
131 {
132  HCIAction action[1];
134  hci_init(manager, prefix, 10.0, 1.0, 1);
135 
136  touch(prefix, "checkpoint");
138  hci_query(manager, action);
139  assert_false(exists(prefix, "checkpoint"));
140 
141  assert_int_equal(action->type, HCI_CHECKPOINT);
142  assert_int_equal(action->write_snapshot, 1);
143 }
144 
145 static void
146 test_hci_terminate(void ** state)
147 {
148  HCIAction action[1];
150  hci_init(manager, prefix, 10.0, 1.0, 1);
151 
152  touch(prefix, "terminate");
154  hci_query(manager, action);
155  assert_false(exists(prefix, "terminate"));
156 
157  assert_int_equal(action->type, HCI_TERMINATE);
158  assert_int_equal(action->write_snapshot, 0);
159 }
160 
161 static int setup(void ** state)
162 {
163  char * ret = mkdtemp(prefix);
164  message(0, "UsingPrefix : '%s'\n", prefix);
165  return !ret;
166 }
167 
168 static int teardown(void ** state)
169 {
170  remove(prefix);
171  return 0;
172 }
173 
174 int main(void) {
175  const struct CMUnitTest tests[] = {
176  cmocka_unit_test(test_hci_no_action),
177  cmocka_unit_test(test_hci_auto_checkpoint),
178  cmocka_unit_test(test_hci_auto_checkpoint2),
179  cmocka_unit_test(test_hci_timeout),
180  cmocka_unit_test(test_hci_stop),
181  cmocka_unit_test(test_hci_checkpoint),
182  cmocka_unit_test(test_hci_terminate),
183  };
184  return cmocka_run_group_tests_mpi(tests, setup, teardown);
185 }
void message(int where, const char *fmt,...)
Definition: endrun.c:175
void hci_init(HCIManager *manager, char *prefix, double WallClockTimeLimit, double AutoCheckPointTime, int FOFEnabled)
Definition: hci.c:25
int hci_query(HCIManager *manager, HCIAction *action)
Definition: hci.c:152
void hci_override_now(HCIManager *manager, double now)
Definition: hci.c:48
@ HCI_TERMINATE
Definition: hci.h:26
@ HCI_AUTO_CHECKPOINT
Definition: hci.h:24
@ HCI_CHECKPOINT
Definition: hci.h:25
@ HCI_TIMEOUT
Definition: hci.h:23
@ HCI_NO_ACTION
Definition: hci.h:21
@ HCI_STOP
Definition: hci.h:22
#define myfree(x)
Definition: mymalloc.h:19
char * fastpm_strdup_printf(const char *fmt,...)
Definition: string.c:41
Definition: hci.h:31
enum HCIActionType type
Definition: hci.h:32
int write_snapshot
Definition: hci.h:33
int write_fof
Definition: hci.h:34
Definition: hci.h:4
double LongestTimeBetweenQueries
Definition: hci.h:10
int OVERRIDE_NOW
Definition: hci.h:16
static int teardown(void **state)
Definition: test_hci.c:168
static void test_hci_auto_checkpoint(void **state)
Definition: test_hci.c:56
static void touch(char *prefix, char *b)
Definition: test_hci.c:16
static void test_hci_auto_checkpoint2(void **state)
Definition: test_hci.c:76
char prefix[1024]
Definition: test_hci.c:13
static void test_hci_terminate(void **state)
Definition: test_hci.c:146
int main(void)
Definition: test_hci.c:174
static int exists(char *prefix, char *b)
Definition: test_hci.c:25
HCIManager manager[1]
Definition: test_hci.c:37
static void test_hci_stop(void **state)
Definition: test_hci.c:114
static void test_hci_timeout(void **state)
Definition: test_hci.c:96
static int setup(void **state)
Definition: test_hci.c:161
static void test_hci_checkpoint(void **state)
Definition: test_hci.c:130
static void test_hci_no_action(void **state)
Definition: test_hci.c:42