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_H__
17570af302Sopenharmony_ci#define __FUNCTIONALEXT_H__
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci#include <dlfcn.h>
20570af302Sopenharmony_ci#include <errno.h>
21570af302Sopenharmony_ci#include <math.h>
22570af302Sopenharmony_ci#include <stdio.h>
23570af302Sopenharmony_ci#include <string.h>
24570af302Sopenharmony_ci#include "test.h"
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci#define EPS (0.00001)
27570af302Sopenharmony_ci#define CMPFLAG 0
28570af302Sopenharmony_ci#define ERREXPECT (-1)
29570af302Sopenharmony_ci#define ONREXPECT 1
30570af302Sopenharmony_ci
31570af302Sopenharmony_ci#define EXPECT_TRUE(fun, c)                \
32570af302Sopenharmony_ci    do {                                   \
33570af302Sopenharmony_ci        if (!(c))                          \
34570af302Sopenharmony_ci            t_error("[%s] failed\n", fun); \
35570af302Sopenharmony_ci    } while (0)
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci#define EXPECT_FALSE(fun, c)                \
38570af302Sopenharmony_ci    do {                                    \
39570af302Sopenharmony_ci        if ((c))                            \
40570af302Sopenharmony_ci            t_error("[%s] failed \n", fun); \
41570af302Sopenharmony_ci    } while (0)
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci#define EXPECT_EQ(fun, a, b)                               \
44570af302Sopenharmony_ci    do {                                                   \
45570af302Sopenharmony_ci        if ((a) != (b))                                    \
46570af302Sopenharmony_ci            t_error("[%s] failed %d != %d \n", fun, a, b); \
47570af302Sopenharmony_ci    } while (0)
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci#define EXPECT_LT(fun, a, b)                                                              \
50570af302Sopenharmony_ci    do {                                                                                  \
51570af302Sopenharmony_ci        if ((a) >= (b))                                                                   \
52570af302Sopenharmony_ci            t_error("[%s] failed (errno: %s)  %d >= %d \n", #fun, strerror(errno), a, b); \
53570af302Sopenharmony_ci    } while (0)
54570af302Sopenharmony_ci
55570af302Sopenharmony_ci#define EXPECT_MT(fun, a, b)                                                              \
56570af302Sopenharmony_ci    do {                                                                                  \
57570af302Sopenharmony_ci        if ((a) <= (b))                                                                   \
58570af302Sopenharmony_ci            t_error("[%s] failed (errno: %s)  %d >= %d \n", #fun, strerror(errno), a, b); \
59570af302Sopenharmony_ci    } while (0)
60570af302Sopenharmony_ci
61570af302Sopenharmony_ci#define EXPECT_NE(fun, a, b)                                   \
62570af302Sopenharmony_ci    do {                                                       \
63570af302Sopenharmony_ci        if ((int)(a) == (int)(b))                              \
64570af302Sopenharmony_ci            t_error("[%s] failed %d == %d \n", fun, (a), (b)); \
65570af302Sopenharmony_ci    } while (0)
66570af302Sopenharmony_ci
67570af302Sopenharmony_ci/* char*, char[] comparison */
68570af302Sopenharmony_ci#define EXPECT_STREQ(fun, a, b)                                \
69570af302Sopenharmony_ci    do {                                                       \
70570af302Sopenharmony_ci        if (strlen(a) != strlen(b) || strcmp((a), (b)) != 0)   \
71570af302Sopenharmony_ci            t_error("[%s] failed %s != %s \n", fun, (a), (b)); \
72570af302Sopenharmony_ci    } while (0)
73570af302Sopenharmony_ci
74570af302Sopenharmony_ci#define EXPECT_STRLT(fun, a, b)                                                           \
75570af302Sopenharmony_ci    do {                                                                                  \
76570af302Sopenharmony_ci        if ((a) >= (b))                                                                   \
77570af302Sopenharmony_ci            t_error("[%s] failed (errno: %s)  %d >= %d \n", #fun, strerror(errno), a, b); \
78570af302Sopenharmony_ci    } while (0)
79570af302Sopenharmony_ci
80570af302Sopenharmony_ci#define EXPECT_STRMT(fun, a, b)                                                           \
81570af302Sopenharmony_ci    do {                                                                                  \
82570af302Sopenharmony_ci        if ((a) <= (b))                                                                   \
83570af302Sopenharmony_ci            t_error("[%s] failed (errno: %s)  %d >= %d \n", #fun, strerror(errno), a, b); \
84570af302Sopenharmony_ci    } while (0)
85570af302Sopenharmony_ci
86570af302Sopenharmony_ci/* floating point comparison */
87570af302Sopenharmony_ci#define FLOAT_EQUAL(fun, a, b)                                   \
88570af302Sopenharmony_ci    do {                                                         \
89570af302Sopenharmony_ci        if (!(fabs((a) - (b)) <= EPS))                           \
90570af302Sopenharmony_ci            t_error("[%s] failed %fd != %fd \n", fun, (a), (b)); \
91570af302Sopenharmony_ci    } while (0)
92570af302Sopenharmony_ci
93570af302Sopenharmony_ci#define EXPECT_PTREQ(fun, a, b)                            \
94570af302Sopenharmony_ci    do {                                                   \
95570af302Sopenharmony_ci        if ((a) != (b)) {                                  \
96570af302Sopenharmony_ci            t_error("[%s] failed %p != %p \n", fun, a, b); \
97570af302Sopenharmony_ci        }                                                  \
98570af302Sopenharmony_ci    } while (0)
99570af302Sopenharmony_ci
100570af302Sopenharmony_ci#define EXPECT_PTRNE(fun, a, b)                            \
101570af302Sopenharmony_ci    do {                                                   \
102570af302Sopenharmony_ci        if ((a) == (b)) {                                  \
103570af302Sopenharmony_ci            t_error("[%s] failed %p == %p \n", fun, a, b); \
104570af302Sopenharmony_ci        }                                                  \
105570af302Sopenharmony_ci    } while (0)
106570af302Sopenharmony_ci
107570af302Sopenharmony_ci#define EXPECT_STRNE(fun, a, b)                                \
108570af302Sopenharmony_ci    do {                                                       \
109570af302Sopenharmony_ci        if (strcmp((a), (b)) == 0)                             \
110570af302Sopenharmony_ci            t_error("[%s] failed %s == %s \n", fun, (a), (b)); \
111570af302Sopenharmony_ci    } while (0)
112570af302Sopenharmony_ci
113570af302Sopenharmony_ci#define EXPECT_LONGEQ(fun, a, b)                             \
114570af302Sopenharmony_ci    do {                                                     \
115570af302Sopenharmony_ci        if ((long)(a) != (long)(b))                          \
116570af302Sopenharmony_ci            t_error("[%s] failed %ld != %ld \n", fun, a, b); \
117570af302Sopenharmony_ci    } while (0)
118570af302Sopenharmony_ci
119570af302Sopenharmony_ci#define EXPECT_LONGLONGEQ(fun, a, b)                           \
120570af302Sopenharmony_ci    do {                                                       \
121570af302Sopenharmony_ci        if ((long)(a) != (long)(b))                            \
122570af302Sopenharmony_ci            t_error("[%s] failed %lld != %lld \n", fun, a, b); \
123570af302Sopenharmony_ci    } while (0)
124570af302Sopenharmony_ci
125570af302Sopenharmony_ci#define EXPECT_GT(fun, a, b)                               \
126570af302Sopenharmony_ci    do {                                                   \
127570af302Sopenharmony_ci        if ((a) <= (b)) {                                  \
128570af302Sopenharmony_ci            t_error("[%s] failed %d <= %d \n", fun, a, b); \
129570af302Sopenharmony_ci        }                                                  \
130570af302Sopenharmony_ci    } while (0)
131570af302Sopenharmony_ci
132570af302Sopenharmony_ci#define EXPECT_GTE(fun, a, b)                             \
133570af302Sopenharmony_ci    do {                                                  \
134570af302Sopenharmony_ci        if ((a) < (b)) {                                  \
135570af302Sopenharmony_ci            t_error("[%s] failed %d < %d \n", fun, a, b); \
136570af302Sopenharmony_ci        }                                                 \
137570af302Sopenharmony_ci    } while (0)
138570af302Sopenharmony_ci
139570af302Sopenharmony_ci#endif
140