1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#ifndef __FUNCTIONALEXT_PTHREAD_UTIL_H__
17570af302Sopenharmony_ci#define __FUNCTIONALEXT_PTHREAD_UTIL_H__
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci#include <time.h>
20570af302Sopenharmony_ci#include <sys/shm.h>
21570af302Sopenharmony_ci#include "test.h"
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci#define EXPECT_EQ(a, b) do { \
24570af302Sopenharmony_ci    if ((a) != (b)) \
25570af302Sopenharmony_ci        t_error("failed %d != %d \n", a, b); \
26570af302Sopenharmony_ci} while (0)
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci#define EXPECT_GE(a, b) do { \
29570af302Sopenharmony_ci    if ((a) < (b)) \
30570af302Sopenharmony_ci        t_error("failed %d < %d \n", a, b); \
31570af302Sopenharmony_ci} while (0)
32570af302Sopenharmony_ci
33570af302Sopenharmony_ci#define EXPECT_LE(a, b) do { \
34570af302Sopenharmony_ci    if ((a) > (b)) \
35570af302Sopenharmony_ci        t_error("failed %d < %d \n", a, b); \
36570af302Sopenharmony_ci} while (0)
37570af302Sopenharmony_ci
38570af302Sopenharmony_ci#define TEST(c, ...) ((c) || (t_error(#c " failed: " __VA_ARGS__), 0))
39570af302Sopenharmony_ci#define NSEC_PER_SEC 1000000000
40570af302Sopenharmony_ci#define NSEC_PER_100MS 100000000
41570af302Sopenharmony_ci#define NSEC_PER_MSEC 1000000
42570af302Sopenharmony_ci#define US_PER_S 1000000
43570af302Sopenharmony_ci#define MS_PER_S 1000
44570af302Sopenharmony_ci#define SLEEP_200_MS 200
45570af302Sopenharmony_ci#define SLEEP_100_MS 100
46570af302Sopenharmony_ci#define DELAY_TIME_100_MS 100
47570af302Sopenharmony_ci#define SLEEP_50_MS 50
48570af302Sopenharmony_ci#define SLEEP_20_MS 20
49570af302Sopenharmony_ci#define SLEEP_10_MS 10
50570af302Sopenharmony_ci#define CHECK_STEP_FIVE 5
51570af302Sopenharmony_ci#define CHECK_STEP_FOUR 4
52570af302Sopenharmony_ci#define CHECK_STEP_THREE 3
53570af302Sopenharmony_ci#define CHECK_STEP_TWO 2
54570af302Sopenharmony_ci#define CHECK_STEP_ONE 1
55570af302Sopenharmony_ci
56570af302Sopenharmony_cistatic int CARRY = 4;
57570af302Sopenharmony_cistatic int SHMID_CHECK_STEP = 0;
58570af302Sopenharmony_cistatic long unsigned int ONE_KB = 1024;
59570af302Sopenharmony_cistatic int FLAG = 0666;
60570af302Sopenharmony_ci
61570af302Sopenharmony_citypedef void(*TEST_FUN)(void);
62570af302Sopenharmony_ci
63570af302Sopenharmony_ci// get cur-time plus ms
64570af302Sopenharmony_cistatic inline void GetDelayedTimeByClockid(struct timespec *ts, unsigned int ms, clockid_t clockid)
65570af302Sopenharmony_ci{
66570af302Sopenharmony_ci    const unsigned int nsecPerSec = NSEC_PER_SEC;
67570af302Sopenharmony_ci    unsigned int setTimeNs = ms * US_PER_S;
68570af302Sopenharmony_ci    struct timespec tsNow = {0};
69570af302Sopenharmony_ci    clock_gettime(clockid, &tsNow);
70570af302Sopenharmony_ci    ts->tv_sec = tsNow.tv_sec + (tsNow.tv_nsec + setTimeNs) / nsecPerSec;
71570af302Sopenharmony_ci    ts->tv_nsec = (tsNow.tv_nsec + setTimeNs) % nsecPerSec;
72570af302Sopenharmony_ci}
73570af302Sopenharmony_ci
74570af302Sopenharmony_ci// calculate time difference, in ms
75570af302Sopenharmony_cistatic inline int GetTimeDiff(struct timespec ts1, struct timespec ts2)
76570af302Sopenharmony_ci{
77570af302Sopenharmony_ci    const unsigned int nsecPerSec = NSEC_PER_SEC;
78570af302Sopenharmony_ci    int ms = (ts1.tv_sec - ts2.tv_sec) * nsecPerSec + (ts1.tv_nsec - ts2.tv_nsec);
79570af302Sopenharmony_ci    ms = ms / NSEC_PER_MSEC;
80570af302Sopenharmony_ci    return ms;
81570af302Sopenharmony_ci}
82570af302Sopenharmony_ci
83570af302Sopenharmony_cistatic inline void Msleep(int msec)
84570af302Sopenharmony_ci{
85570af302Sopenharmony_ci    usleep(msec * MS_PER_S);
86570af302Sopenharmony_ci}
87570af302Sopenharmony_ci
88570af302Sopenharmony_ci#endif // __FUNCTIONALEXT_PTHREAD_UTIL_H__