forked from royasutton/omdl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.scad
More file actions
194 lines (159 loc) · 6.21 KB
/
validation.scad
File metadata and controls
194 lines (159 loc) · 6.21 KB
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
//! Function validation methods.
/***************************************************************************//**
\file
\author Roy Allen Sutton
\date 2015-2018
\copyright
This file is part of [omdl] (https://github.com/royasutton/omdl),
an OpenSCAD mechanical design library.
The \em omdl is free software; you can redistribute it and/or modify
it under the terms of the [GNU Lesser General Public License]
(http://www.gnu.org/licenses/lgpl.html) as published by the Free
Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
The \em omdl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the \em omdl; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA; or see <http://www.gnu.org/licenses/>.
\details
\amu_define group_name (Validation)
\amu_define group_brief (Function validation methods.)
\amu_include (include/amu/pgid_path_pstem_pg.amu)
*******************************************************************************/
//----------------------------------------------------------------------------//
// group.
//----------------------------------------------------------------------------//
/***************************************************************************//**
\amu_include (include/amu/group_start.amu)
\details
\b Example
\dontinclude \amu_scope(index=1).scad
\skip include
\until tvae2, 4) );
\b Result \include \amu_scope(index=1).log
*******************************************************************************/
//----------------------------------------------------------------------------//
//! Compare a computed test value with an known good result.
/***************************************************************************//**
\param d <string> A description.
\param cv \<value> A computed value to validate.
\param t <string|boolean> The validation type.
\param ev \<value> The expected good value.
\param p <number> A numerical precision for approximate comparisons.
\param pf <boolean> Report result as pass or fail boolean value.
\returns <string|boolean> Validation result indicating if the test
passed or failed.
\details
validation types | pass if (else fail)
:--------------------:|:----------------------------:
"almost" | \p cv almost equals \p ev
"equals" | \p cv equals \p ev
"not" | \p cv not equal to \p ev
"true" \| \b true | \p cv is \b true
"false" \| \b false | \p cv is \b false
\note When performing an \b "almost" equal validation, the
comparison precision is controlled by \p p. This specifies
the number of digits of precision for each numerical
comparison. A passing result indicates that \p cv equals
\p ev to the number of decimal digits specified by \p p. The
comparison is performed by the function almost_equal().
*******************************************************************************/
function validate
(
d,
cv,
t,
ev,
p = 4,
pf = false
)
= (t == "equals") ?
(
(cv == ev)
? (pf?true : str("passed: '", d, "'"))
: (pf?false : str
(
"failed: '", d, "'; got '", cv,
"', expected to equal '", ev, "'."
)
)
)
: (t == "not") ?
(
(cv != ev)
? (pf?true : str("passed: '", d, "'"))
: (pf?false : str
(
"failed: '", d, "'; got '", cv,
"', expected to not equal '", ev, "'."
)
)
)
: ( (t == true) || (t == "true") ) ? validate(d, cv, "equals", true, p, pf)
: ( (t == false) || (t == "false") ) ? validate(d, cv, "equals", false, p, pf)
: (t == "almost") ?
(
almost_equal(cv, ev, p)
? (pf?true : str("passed: '", d, "'"))
: (pf?false : str
(
"failed: '", d, "'; got '", cv,
"', expected to almost equal '", ev, "'.",
" to ", p, " digits"
)
)
)
: (pf?false : str("failed: '", d, "'; unknown test '", t, "'."));
//! @}
//----------------------------------------------------------------------------//
// openscad-amu auxiliary scripts
//----------------------------------------------------------------------------//
/*
BEGIN_SCOPE example;
BEGIN_OPENSCAD;
include <omdl-base.scad>;
include <validation.scad>;
//
// function to validate
//
function f1( x ) = (x == undef) ? 1 : 2;
farg = undef; // function test argument
erv1 = 1; // correct expected function result
erv2 = 3; // incorrect expected function result
//
// pass test example
//
pass_result = validate("test-a f1(farg)", f1(farg), "equals", erv1);
if ( !validate(cv=f1(farg), t="equals", ev=erv1, pf=true) )
log_warn( pass_result );
else
log_info( pass_result );
//
// fail test example
//
fail_result = validate("test-b f1(farg)", f1(farg), "equals", erv2);
if ( !validate(cv=f1(farg), t="equals", ev=erv2, pf=true) )
log_warn( fail_result );
else
log_info( fail_result );
//
// almost equal test example
//
tvae1 = [[90.001], [[45.009], true]];
tvae2 = [[90.002], [[45.010], true]];
log_info( validate("test-c", tvae1, "almost", tvae2, 3) );
log_warn( validate("test-d", tvae1, "almost", tvae2, 4) );
END_OPENSCAD;
BEGIN_MFSCRIPT;
include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
include --path "${INCLUDE_PATH}" script_std.mfs;
END_MFSCRIPT;
END_SCOPE;
*/
//----------------------------------------------------------------------------//
// end of file
//----------------------------------------------------------------------------//