testLogger.c 7.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2017 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

lhausamm's avatar
lhausamm committed
23
#if defined(HAVE_POSIX_FALLOCATE) && defined(WITH_LOGGER) /* Are we on a sensible platform? */
24

25
26
/* Some standard headers. */
#include <stdio.h>
27
#include <stdlib.h>
28
29
30
31
#include <string.h>
#include <unistd.h>

/* Local headers. */
32
#include "swift.h"
33

34
void test_log_parts(struct dump *d) {
35
36
37
38

  /* Write several copies of a part to the dump. */
  struct part p;
  bzero(&p, sizeof(struct part));
39
40
41
42
43
  p.x[0] = 1.0;
  p.v[0] = 0.1;

  /* Start with an offset at the end of the dump. */
  size_t offset = d->count;
44
45

  /* Write the full part. */
46
47
48
49
  logger_log_part(&p,
                  logger_mask_x | logger_mask_v | logger_mask_a |
                      logger_mask_u | logger_mask_h | logger_mask_rho |
                      logger_mask_consts,
50
                  &offset, d);
51
52
53
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Write only the position. */
54
55
  p.x[0] = 2.0;
  logger_log_part(&p, logger_mask_x, &offset, d);
56
57
58
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Write the position and velocity. */
59
60
61
  p.x[0] = 3.0;
  p.v[0] = 0.3;
  logger_log_part(&p, logger_mask_x | logger_mask_v, &offset, d);
62
63
64
65
66
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Recover the last part from the dump. */
  bzero(&p, sizeof(struct part));
  size_t offset_old = offset;
67
  int mask = logger_read_part(&p, &offset, (const char *)d->data);
68
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
69
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
70
71
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
72
73
  if (p.x[0] != 3.0 || p.v[0] != 0.3f) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
74
75
76
    abort();
  }

77
  /* Recover the second part from the dump (only position). */
78
79
  bzero(&p, sizeof(struct part));
  offset_old = offset;
80
  mask = logger_read_part(&p, &offset, (const char *)d->data);
81
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
82
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
83
84
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
85
86
  if (p.x[0] != 2.0 || p.v[0] != 0.0) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
87
88
89
90
91
92
    abort();
  }

  /* Recover the first part from the dump. */
  bzero(&p, sizeof(struct part));
  offset_old = offset;
93
  mask = logger_read_part(&p, &offset, (const char *)d->data);
94
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
95
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
96
97
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  if (p.x[0] != 1.0 || p.v[0] != 0.1f) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
    abort();
  }
}

void test_log_gparts(struct dump *d) {

  /* Write several copies of a part to the dump. */
  struct gpart p;
  bzero(&p, sizeof(struct gpart));
  p.x[0] = 1.0;
  p.v_full[0] = 0.1;

  /* Start with an offset at the end of the dump. */
  size_t offset = d->count;

  /* Write the full part. */
116
117
118
  logger_log_gpart(&p,
                   logger_mask_x | logger_mask_v | logger_mask_a |
                       logger_mask_h | logger_mask_consts,
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
                   &offset, d);
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Write only the position. */
  p.x[0] = 2.0;
  logger_log_gpart(&p, logger_mask_x, &offset, d);
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Write the position and velocity. */
  p.x[0] = 3.0;
  p.v_full[0] = 0.3;
  logger_log_gpart(&p, logger_mask_x | logger_mask_v, &offset, d);
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Recover the last part from the dump. */
  bzero(&p, sizeof(struct gpart));
  size_t offset_old = offset;
136
  int mask = logger_read_gpart(&p, &offset, (const char *)d->data);
137
138
139
140
141
142
143
144
145
146
147
148
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 3.0 || p.v_full[0] != 0.3f) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
    abort();
  }

  /* Recover the second part from the dump. */
  bzero(&p, sizeof(struct gpart));
  offset_old = offset;
149
  mask = logger_read_gpart(&p, &offset, (const char *)d->data);
150
151
152
153
154
155
156
157
158
159
160
161
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 2.0 || p.v_full[0] != 0.0) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
    abort();
  }

  /* Recover the first part from the dump. */
  bzero(&p, sizeof(struct gpart));
  offset_old = offset;
162
  mask = logger_read_gpart(&p, &offset, (const char *)d->data);
163
164
165
166
167
168
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 1.0 || p.v_full[0] != 0.1f) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
169
170
    abort();
  }
171
172
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
void test_log_timestamps(struct dump *d) {

  /* The timestamp to log. */
  unsigned long long int t = 10;

  /* Start with an offset at the end of the dump. */
  size_t offset = d->count;

  /* Log three consecutive timestamps. */
  logger_log_timestamp(t, &offset, d);
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
  logger_log_timestamp(t, &offset, d);
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
  logger_log_timestamp(t, &offset, d);
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);

  /* Recover the three timestamps. */
  size_t offset_old = offset;
  t = 0;
194
  int mask = logger_read_timestamp(&t, &offset, (const char *)d->data);
195
196
197
198
199
200
201
202
203
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 30) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }

  offset_old = offset;
  t = 0;
204
  mask = logger_read_timestamp(&t, &offset, (const char *)d->data);
205
206
207
208
209
210
211
212
213
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 20) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }

  offset_old = offset;
  t = 0;
214
  mask = logger_read_timestamp(&t, &offset, (const char *)d->data);
215
216
217
218
219
220
221
222
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 10) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
}

223
224
225
int main(int argc, char *argv[]) {

  /* Some constants. */
226
227
228
  char filename[256];
  const int now = time(NULL);
  sprintf(filename, "/tmp/SWIFT_logger_test_%d.out", now);
229
230
231
232
233
234
235
236

  /* Prepare a dump. */
  struct dump d;
  dump_init(&d, filename, 1024 * 1024);

  /* Test writing/reading parts. */
  test_log_parts(&d);

237
  /* Test writing/reading gparts. */
238
  test_log_gparts(&d);
239

240
241
242
  /* Test writing/reading timestamps. */
  test_log_timestamps(&d);

243
244
245
  /* Finalize the dump. */
  dump_close(&d);

246
247
248
  /* Be clean */
  remove(filename);

249
250
251
  /* Return a happy number. */
  return 0;
}
252
253
254

#else

255
int main(int argc, char *argv[]) { return 0; }
256
257

#endif /* HAVE_POSIX_FALLOCATE */