MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_timebinmgr.c
Go to the documentation of this file.
1 /*Tests for the timebinmgr module, to ensure we got the bitwise arithmetic right.*/
2 
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7 #include <math.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include "stub.h"
11 //So All.OutputList is defined
12 #include <libgadget/timebinmgr.h>
13 
14 #define TIMEMAX 1.0
15 #define TIMEIC 0.1
16 double outs[4] = {TIMEIC, 0.2, 0.8, TIMEMAX};
17 double logouts[4];
18 
19 /*timebinmgr has no state*/
20 /*First test conversions between float and integer timelines*/
21 static void test_conversions(void ** state) {
22 
24 
25  /*Convert an integer to and from loga*/
26  /* double loga_from_ti(unsigned int ti); */
27  assert_true(fabs(loga_from_ti(0) - logouts[0]) < 1e-6);
28  assert_true(fabs(loga_from_ti(TIMEBASE) - logouts[1]) < 1e-6);
29  assert_true(fabs(loga_from_ti(TIMEBASE-1) - (logouts[0] + (logouts[1]-logouts[0])*(TIMEBASE-1)/TIMEBASE)) < 1e-6);
30  assert_true(fabs(loga_from_ti(TIMEBASE+1) - (logouts[1] + (logouts[2]-logouts[1])/TIMEBASE)) < 1e-6);
31  assert_true(fabs(loga_from_ti(2*TIMEBASE) - logouts[2]) < 1e-6);
32  /* unsigned int ti_from_loga(double loga); */
33  assert_true(ti_from_loga(logouts[0]) == 0);
34  assert_true(ti_from_loga(logouts[1]) == TIMEBASE);
35  assert_true(ti_from_loga(logouts[2]) == 2*TIMEBASE);
36  double midpt = (logouts[2] + logouts[1])/2;
37  assert_true(ti_from_loga(midpt) == TIMEBASE+TIMEBASE/2);
38  assert_true(fabs(loga_from_ti(TIMEBASE+TIMEBASE/2)-midpt)< 1e-6);
39 
40  /*Check behaviour past end*/
41  assert_true(ti_from_loga(0) == 3*TIMEBASE);
42  assert_true(fabs(loga_from_ti(ti_from_loga(log(0.1))) - log(0.1)) < 1e-6);
43 
45  assert_int_equal(find_next_sync_point(0)->ti , TIMEBASE);
46  assert_int_equal(find_next_sync_point(TIMEBASE)->ti , 2 * TIMEBASE);
47  assert_int_equal(find_next_sync_point(TIMEBASE-1)->ti , TIMEBASE);
48  assert_int_equal(find_next_sync_point(TIMEBASE+1)->ti , 2*TIMEBASE);
49  assert_int_equal(find_next_sync_point(4 * TIMEBASE) , NULL);
50 
51  assert_int_equal(find_current_sync_point(0)->ti , 0);
52  assert_int_equal(find_current_sync_point(TIMEBASE)->ti , TIMEBASE);
53  assert_int_equal(find_current_sync_point(-1) , NULL);
54  assert_int_equal(find_current_sync_point(TIMEBASE-1) , NULL);
55 
56  assert_int_equal(find_current_sync_point(0)->write_snapshot, 1);
57  assert_int_equal(find_current_sync_point(TIMEBASE)->write_snapshot, 1);
58  assert_int_equal(find_current_sync_point(2 * TIMEBASE)->write_snapshot, 1);
59  assert_int_equal(find_current_sync_point(3 * TIMEBASE)->write_snapshot, 1);
60 }
61 
62 static void test_skip_first(void ** state) {
63 
65  assert_int_equal(find_current_sync_point(0)->write_snapshot, 0);
66 
68  assert_int_equal(find_current_sync_point(0)->write_snapshot, 1);
69 }
70 
71 static void test_dloga(void ** state) {
72 
74 
75  inttime_t Ti_Current = ti_from_loga(log(0.55));
76 
77  /* unsigned int dti_from_dloga(double loga); */
78  /* double dloga_from_dti(unsigned int ti); */
79 
80  /*Get dloga from a timebin*/
81  /* double get_dloga_for_bin(int timebin); */
82  assert_true(fabs(get_dloga_for_bin(0, Ti_Current ))<1e-6);
83  assert_true(fabs(get_dloga_for_bin(TIMEBINS, Ti_Current )-(logouts[2]-logouts[1]))<1e-6);
84  assert_true(fabs(get_dloga_for_bin(TIMEBINS-2, Ti_Current)-(logouts[2]-logouts[1])/4)<1e-6);
85 
86  /*Enforce that an integer time is a power of two*/
87  /* unsigned int round_down_power_of_two(unsigned int ti); */
89  assert_true(round_down_power_of_two(TIMEBASE+1)==TIMEBASE);
90  assert_true(round_down_power_of_two(TIMEBASE-1)==TIMEBASE/2);
91 }
92 
93 #define OutputListLength 4
94 
95 static int
96 setup(void * p1, void * p2)
97 {
98  int i;
99  double OutputListTimes[OutputListLength];
100  for(i = 0; i < OutputListLength; i ++) {
101  OutputListTimes[i] = outs[i];
102  logouts[i] = log(outs[i]);
103  }
104 
105  set_sync_params(OutputListLength, OutputListTimes);
106  return 0;
107 }
108 static int
109 teardown(void * p1, void * p2)
110 {
111 
112  return 0;
113 }
114 
115 int main(void) {
116  const struct CMUnitTest tests[] = {
117  cmocka_unit_test(test_conversions),
118  cmocka_unit_test(test_dloga),
119  cmocka_unit_test(test_skip_first),
120  };
121  return cmocka_run_group_tests_mpi(tests, setup, teardown);
122 }
double outs[4]
static void test_conversions(void **state)
double logouts[4]
#define TIMEIC
int main(void)
#define OutputListLength
static int teardown(void *p1, void *p2)
static void test_skip_first(void **state)
static void test_dloga(void **state)
#define TIMEMAX
static int setup(void *p1, void *p2)
double loga_from_ti(int ti)
Definition: test_timefac.c:51
inttime_t ti_from_loga(double loga)
Definition: timebinmgr.c:236
SyncPoint * find_next_sync_point(inttime_t ti)
Definition: timebinmgr.c:174
inttime_t round_down_power_of_two(inttime_t dti)
Definition: timebinmgr.c:286
double get_dloga_for_bin(int timebin, const inttime_t Ti_Current)
Definition: timebinmgr.c:279
SyncPoint * find_current_sync_point(inttime_t ti)
Definition: timebinmgr.c:188
void setup_sync_points(double TimeIC, double TimeMax, double no_snapshot_until_time, int SnapshotWithFOF)
Definition: timebinmgr.c:97
void set_sync_params(int OutputListLength, double *OutputListTimes)
Definition: timebinmgr.c:76
#define TIMEBINS
Definition: timebinmgr.h:13
#define TIMEBASE
Definition: timebinmgr.h:14
int32_t inttime_t
Definition: types.h:8