-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCustomFunctionMath.cpp
194 lines (184 loc) · 6.26 KB
/
CustomFunctionMath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "CustomFunctionMath.h"
#include <algorithm>
#include <cerrno>
#include <cfloat>
#include <cmath>
#include <cstdlib>
namespace Aws
{
namespace IoTFleetWise
{
namespace CustomFunctionMath
{
CustomFunctionInvokeResult
absFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() != 1 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
if ( args[0].isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( !args[0].isBoolOrDouble() )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
return { ExpressionErrorCode::SUCCESSFUL, std::abs( args[0].asDouble() ) };
}
CustomFunctionInvokeResult
minFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() < 2 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
double minimum = DBL_MAX;
for ( const auto &arg : args )
{
if ( arg.isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( !arg.isBoolOrDouble() )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
minimum = std::min( minimum, arg.asDouble() );
}
return { ExpressionErrorCode::SUCCESSFUL, minimum };
}
CustomFunctionInvokeResult
maxFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() < 2 )
{
return { ExpressionErrorCode::TYPE_MISMATCH };
}
double maximum = DBL_MIN;
for ( const auto &arg : args )
{
if ( arg.isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( !arg.isBoolOrDouble() )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
maximum = std::max( maximum, arg.asDouble() );
}
return { ExpressionErrorCode::SUCCESSFUL, maximum };
}
CustomFunctionInvokeResult
powFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() != 2 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
if ( args[0].isUndefined() || args[1].isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( ( !args[0].isBoolOrDouble() ) || ( !args[1].isBoolOrDouble() ) )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `pow` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `pow` to indicate a domain error
errno = 0;
// coverity[autosar_cpp14_a0_4_4_violation] Range errors are detected via errno
auto powRes = std::pow( args[0].asDouble(), args[1].asDouble() );
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `pow` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `pow` to indicate a domain error
if ( errno != 0 )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
return { ExpressionErrorCode::SUCCESSFUL, powRes };
}
CustomFunctionInvokeResult
logFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() != 2 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
if ( args[0].isUndefined() || args[1].isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( ( !args[0].isBoolOrDouble() ) || ( !args[1].isBoolOrDouble() ) )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `log` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `log` to indicate a domain error
errno = 0;
auto logBase = std::log( args[0].asDouble() );
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `log` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `log` to indicate a domain error
if ( errno != 0 )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `log` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `log` to indicate a domain error
errno = 0;
auto logNum = std::log( args[1].asDouble() );
// coverity[misra_cpp_2008_rule_19_3_1_violation] errno is used by `log` to indicate a domain error
// coverity[autosar_cpp14_m19_3_1_violation] errno is used by `log` to indicate a domain error
if ( errno != 0 )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
return { ExpressionErrorCode::SUCCESSFUL, logNum / logBase };
}
CustomFunctionInvokeResult
ceilFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() != 1 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
if ( args[0].isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( !args[0].isBoolOrDouble() )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
return { ExpressionErrorCode::SUCCESSFUL, std::ceil( args[0].asDouble() ) };
}
CustomFunctionInvokeResult
floorFunc( CustomFunctionInvocationID invocationId, const std::vector<InspectionValue> &args )
{
static_cast<void>( invocationId );
if ( args.size() != 1 )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
if ( args[0].isUndefined() )
{
return ExpressionErrorCode::SUCCESSFUL; // Undefined result
}
if ( !args[0].isBoolOrDouble() )
{
return ExpressionErrorCode::TYPE_MISMATCH;
}
return { ExpressionErrorCode::SUCCESSFUL, std::floor( args[0].asDouble() ) };
}
} // namespace CustomFunctionMath
} // namespace IoTFleetWise
} // namespace Aws