omdl  v0.6.1
OpenSCAD Mechanical Design Library
validation.scad
Go to the documentation of this file.
1 //! Function validation methods.
2 /***************************************************************************//**
3  \file validation.scad
4  \author Roy Allen Sutton
5  \date 2015-2017
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \ingroup utilities utilities_validate
31 *******************************************************************************/
32 
33 include <datatypes/datatypes-base.scad>;
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \addtogroup utilities
38  @{
39 
40  \defgroup utilities_validate Validation
41  \brief Function validation methods.
42 
43  \details
44 
45  \b Example
46 
47  \dontinclude validation_example.scad
48  \skip use
49  \until tvae2, 4) );
50 
51  \b Result \include validation_example.log
52 
53  @{
54 *******************************************************************************/
55 //----------------------------------------------------------------------------//
56 
57 //! Compare a computed test value with an known good result.
58 /***************************************************************************//**
59  \param d <string> A description.
60  \param cv <value> A computed value to validate.
61  \param t <string|boolean> The validation type.
62  \param ev <value> The expected good value.
63 
64  \param p <number> A numerical precision for approximate comparisons.
65 
66  \param pf <boolean> Report result as pass or fail boolean value.
67 
68  \returns <string|boolean> Validation result indicating if the test
69  passed or failed.
70 
71  \details
72 
73  validation types | pass if (else fail)
74  :--------------------:|:----------------------------:
75  "almost" | \p cv almost equals \p ev
76  "equals" | \p cv equals \p ev
77  "not" | \p cv not equal to \p ev
78  "true" \| \b true | \p cv is \b true
79  "false" \| \b false | \p cv is \b false
80 
81  \note When performing an \b "almost" equal validation, the
82  comparison precision is controlled by \p p. This specifies
83  the number of digits of precision for each numerical
84  comparison. A passing result indicates that \p cv equals
85  \p ev to the number of decimal digits specified by \p p. The
86  comparison is performed by the function almost_equal().
87 *******************************************************************************/
88 function validate
89 (
90  d,
91  cv,
92  t,
93  ev,
94  p = 4,
95  pf = false
96 )
97  = (t == "equals") ?
98  (
99  (cv == ev)
100  ? (pf?true : str("passed: '", d, "'"))
101  : (pf?false : str
102  (
103  "FAILED: '", d, "'. Got '", cv,
104  "'. Expected to equal '", ev, "'"
105  )
106  )
107  )
108  : (t == "not") ?
109  (
110  (cv != ev)
111  ? (pf?true : str("passed: '", d, "'"))
112  : (pf?false : str
113  (
114  "FAILED: '", d, "'. Got '", cv,
115  "'. Expected to not equal '", ev, "'"
116  )
117  )
118  )
119  : ( (t == true) || (t == "true") ) ? validate(d, cv, "equals", true, p, pf)
120  : ( (t == false) || (t == "false") ) ? validate(d, cv, "equals", false, p, pf)
121  : (t == "almost") ?
122  (
123  almost_equal(cv, ev, p)
124  ? (pf?true : str("passed: '", d, "'"))
125  : (pf?false : str
126  (
127  "FAILED: '", d, "'. Got '", cv,
128  "'. Expected to almost equal '", ev, "'",
129  " to ", p, " digits"
130  )
131  )
132  )
133  : (pf?false : str("FAILED: '", d, "'. Unknown test '", t, "'"));
134 
135 //! @}
136 //! @}
137 
138 //----------------------------------------------------------------------------//
139 // openscad-amu auxiliary scripts
140 //----------------------------------------------------------------------------//
141 
142 /*
143 BEGIN_SCOPE example;
144  BEGIN_OPENSCAD;
145  include <console.scad>;
146  include <validation.scad>;
147 
148  //
149  // function to validate
150  //
151  function f1( x ) = (x == undef) ? 1 : 2;
152 
153  farg = undef; // function test argument
154  erv1 = 1; // correct expected function result
155  erv2 = 3; // incorrect expected function result
156 
157  //
158  // pass test example
159  //
160  pass_result = validate("test-a f1(farg)", f1(farg), "equals", erv1);
161 
162  if ( !validate(cv=f1(farg), t="equals", ev=erv1, pf=true) )
163  log_warn( pass_result );
164  else
165  log_info( pass_result );
166 
167  //
168  // fail test example
169  //
170  fail_result = validate("test-b f1(farg)", f1(farg), "equals", erv2);
171 
172  if ( !validate(cv=f1(farg), t="equals", ev=erv2, pf=true) )
173  log_warn( fail_result );
174  else
175  log_info( fail_result );
176 
177  //
178  // almost equal test example
179  //
180  tvae1 = [[90.001], [[45.009], true]];
181  tvae2 = [[90.002], [[45.010], true]];
182 
183  log_info( validate("test-c", tvae1, "almost", tvae2, 3) );
184  log_warn( validate("test-d", tvae1, "almost", tvae2, 4) );
185  END_OPENSCAD;
186 
187  BEGIN_MFSCRIPT;
188  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
189  include --path "${INCLUDE_PATH}" script_std.mfs;
190  END_MFSCRIPT;
191 END_SCOPE;
192 */
193 
194 //----------------------------------------------------------------------------//
195 // end of file
196 //----------------------------------------------------------------------------//
function validate(d, cv, t, ev, p=4, pf=false)
Compare a computed test value with an known good result.
function almost_equal(v1, v2, p=6)
Test if all numerical elements of two lists of values are sufficiently equal.