Motr  M0
clk_src_ut.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_DTM
23 #include "lib/trace.h"
24 
25 #include "ut/ut.h"
26 #include "dtm0/clk_src.h"
27 #include "lib/errno.h" /* EINVAL */
28 #include "lib/string.h" /* m0_asprintf, m0_streq */
29 #include "lib/memory.h" /* m0_free */
30 
31 /* test if a timestamp can be converted into a string */
32 static void ts_format(void)
33 {
34  char *str;
35  struct m0_dtm0_ts ts = M0_DTM0_TS_MIN;
36  const char *expected = "'@1'";
37 
38  m0_asprintf(&str, "'" DTS0_F "'", DTS0_P(&ts));
39  M0_UT_ASSERT(str);
40 
42  m0_free(str);
43 }
44 
45 static void init_fini(void)
46 {
47  struct m0_dtm0_clk_src dcs;
48 
51 }
52 
53 /* test if it is possible to get now() value */
54 static void get_now(void)
55 {
56  struct m0_dtm0_clk_src dcs;
57  struct m0_dtm0_ts now;
58 
60  m0_dtm0_clk_src_now(&dcs, &now);
62 }
63 
64 /* test relation between now() and the limits */
65 static void now_min_max(void)
66 {
67  struct m0_dtm0_clk_src dcs;
68  struct m0_dtm0_ts now;
69  struct m0_dtm0_ts past = M0_DTM0_TS_MIN;
70  struct m0_dtm0_ts future = M0_DTM0_TS_MAX;
71  int rc;
72 
74  m0_dtm0_clk_src_now(&dcs, &now);
75 
76  rc = m0_dtm0_ts_cmp(&dcs, &past, &now);
78  rc = m0_dtm0_ts_cmp(&dcs, &now, &past);
80  rc = m0_dtm0_ts_cmp(&dcs, &now, &now);
82 
83  rc = m0_dtm0_ts_cmp(&dcs, &now, &future);
85  rc = m0_dtm0_ts_cmp(&dcs, &future, &now);
87 
88  rc = m0_dtm0_ts_cmp(&dcs, &past, &future);
90 
91  rc = m0_dtm0_ts_cmp(&dcs, &future, &past);
93 
95 }
96 
97 /* test if clock is always advancing forward */
98 static void now_and_then(void)
99 {
100  struct m0_dtm0_clk_src dcs;
101  struct m0_dtm0_ts first;
102  struct m0_dtm0_ts second;
103  struct m0_dtm0_ts third;
104  int rc;
105 
107 
108  m0_dtm0_clk_src_now(&dcs, &first);
109  m0_dtm0_clk_src_now(&dcs, &second);
110  m0_dtm0_clk_src_now(&dcs, &third);
111 
112  rc = m0_dtm0_ts_cmp(&dcs, &first, &second);
114  rc = m0_dtm0_ts_cmp(&dcs, &second, &third);
116  rc = m0_dtm0_ts_cmp(&dcs, &first, &third);
118 
119  m0_dtm0_clk_src_fini(&dcs);
120 }
121 
123  .ts_name = "dtm0-clk-src-ut",
124  .ts_init = NULL,
125  .ts_fini = NULL,
126  .ts_tests = {
127  { "ts-format", ts_format },
128  { "phys-init-fini", init_fini },
129  { "phys-now", get_now },
130  { "phys-now-min-max", now_min_max },
131  { "phys-now-and-then", now_and_then },
132  { NULL, NULL }
133  }
134 };
135 
136 #undef M0_TRACE_SUBSYSTEM
137 /*
138  * Local variables:
139  * c-indentation-style: "K&R"
140  * c-basic-offset: 8
141  * tab-width: 8
142  * fill-column: 80
143  * scroll-step: 1
144  * End:
145  */
146 /*
147  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
148  */
M0_INTERNAL enum m0_dtm0_ts_ord m0_dtm0_ts_cmp(const struct m0_dtm0_clk_src *cs, const struct m0_dtm0_ts *left, const struct m0_dtm0_ts *right)
Definition: clk_src.c:61
#define NULL
Definition: misc.h:38
#define DTS0_P(_ts)
Definition: clk_src.h:105
static void get_now(void)
Definition: clk_src_ut.c:54
#define M0_DTM0_TS_MAX
Definition: clk_src.h:99
Definition: ut.h:77
static int expected
Definition: locality.c:102
struct m0_ut_suite dtm0_clk_src_ut
Definition: clk_src_ut.c:122
#define DTS0_F
Definition: clk_src.h:106
static void ts_format(void)
Definition: clk_src_ut.c:32
M0_INTERNAL void m0_dtm0_clk_src_now(struct m0_dtm0_clk_src *cs, struct m0_dtm0_ts *now)
Definition: clk_src.c:71
#define m0_streq(a, b)
Definition: string.h:34
const char * ts_name
Definition: ut.h:99
static void init_fini(void)
Definition: clk_src_ut.c:45
M0_INTERNAL void m0_dtm0_clk_src_fini(struct m0_dtm0_clk_src *cs)
Definition: clk_src.c:49
#define m0_asprintf(s, fmt,...)
Definition: string.h:44
#define M0_DTM0_TS_MIN
Definition: clk_src.h:97
void m0_free(void *data)
Definition: memory.c:146
static void now_min_max(void)
Definition: clk_src_ut.c:65
M0_INTERNAL void m0_dtm0_clk_src_init(struct m0_dtm0_clk_src *cs, enum m0_dtm0_cs_types type)
Definition: clk_src.c:38
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void now_and_then(void)
Definition: clk_src_ut.c:98