omdl  v0.6.1
OpenSCAD Mechanical Design Library
datatypes_operate_scalar.scad
Go to the documentation of this file.
1 //! Scalar data type operations.
2 /***************************************************************************//**
3  \file datatypes_operate_scalar.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 *******************************************************************************/
29 
30 //----------------------------------------------------------------------------//
31 /***************************************************************************//**
32  \page tv_datatypes_operate_scalar Scalar
33  \li \subpage tv_datatypes_operate_scalar_s
34  \li \subpage tv_datatypes_operate_scalar_r
35 
36  \page tv_datatypes_operate_scalar_s Script
37  \dontinclude datatypes_operate_scalar_validate.scad
38  \skip include
39  \until end-of-tests
40  \page tv_datatypes_operate_scalar_r Results
41  \include datatypes_operate_scalar_validate.log
42 *******************************************************************************/
43 //----------------------------------------------------------------------------//
44 
45 //----------------------------------------------------------------------------//
46 /***************************************************************************//**
47  \addtogroup datatypes_operate
48  @{
49 
50  \defgroup datatypes_operate_scalar Scalars
51  \brief Scalar data type operations.
52 
53  \details
54 
55  See validation \ref tv_datatypes_operate_scalar_r "results".
56  @{
57 *******************************************************************************/
58 //----------------------------------------------------------------------------//
59 
60 //! Return a value when it is defined or a default value when it is not.
61 /***************************************************************************//**
62  \param v <value> A value.
63  \param d <value> A default value.
64 
65  \returns <value> \p v when it is defined and \p d otherwise.
66 *******************************************************************************/
67 function defined_or
68 (
69  v,
70  d
71 ) = is_defined(v) ? v : d;
72 
73 //! Map an index position into a circularly indexed list.
74 /***************************************************************************//**
75  \param i <integer> Any index, in or out of bounds.
76  \param l <integer> The circular list length.
77  \param f <integer> The starting index number.
78 
79  \returns <integer> A index position in the circular list within the
80  range <tt>[f : l+f-1]</tt>.
81 *******************************************************************************/
82 function circular_index
83 (
84  i,
85  l,
86  f = 0
87 ) = f + (((i % l) + l) % l);
88 
89 //! @}
90 //! @}
91 
92 //----------------------------------------------------------------------------//
93 // openscad-amu auxiliary scripts
94 //----------------------------------------------------------------------------//
95 
96 /*
97 BEGIN_SCOPE validate;
98  BEGIN_OPENSCAD;
99  include <console.scad>;
100  include <datatypes/datatypes-base.scad>;
101  include <datatypes/datatypes_table.scad>;
102  include <validation.scad>;
103 
104  show_passing = true; // show passing tests
105  show_skipped = true; // show skipped tests
106 
107  echo( str("OpenSCAD Version ", version()) );
108 
109  // test-values columns
110  test_c =
111  [
112  ["id", "identifier"],
113  ["td", "description"],
114  ["tv", "test value"]
115  ];
116 
117  // test-values rows
118  test_r =
119  [
120  ["t01", "The undefined value", undef],
121  ["t02", "The empty list", empty_lst],
122  ["t03", "A range", [0:0.5:9]],
123  ["t04", "A string", "A string"],
124  ["t05", "Test list 01", ["orange","apple","grape","banana"]],
125  ["t06", "Test list 02", ["b","a","n","a","n","a","s"]],
126  ["t07", "Test list 03", [undef]],
127  ["t08", "Test list 04", [[1,2],[2,3]]],
128  ["t09", "Test list 05", ["ab",[1,2],[2,3],[4,5]]],
129  ["t10", "Test list 06", [[1,2,3],[4,5,6],[7,8,9],["a","b","c"]]],
130  ["t11", "Vector of integers 0 to 15", [for (i=[0:15]) i]]
131  ];
132 
133  test_ids = get_table_ridl( test_r );
134 
135  // expected columns: ("id" + one column for each test)
136  good_c = pmerge([concat("id", test_ids), concat("identifier", test_ids)]);
137 
138  // expected rows: ("golden" test results), use 's' to skip test
139  skip = -1; // skip test
140 
141  good_r =
142  [ // function
143  ["defined_or_D",
144  "default", // t01
145  empty_lst, // t02
146  [0:0.5:9], // t03
147  "A string", // 04
148  ["orange","apple","grape","banana"], // t05
149  ["b","a","n","a","n","a","s"], // t06
150  [undef], // t07
151  [[1,2],[2,3]], // t08
152  ["ab",[1,2],[2,3],[4,5]], // t09
153  [[1,2,3],[4,5,6],[7,8,9],["a","b","c"]], // t10
154  [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] // t11
155  ]
156  ];
157 
158  // sanity-test tables
159  table_check( test_r, test_c, false );
160  table_check( good_r, good_c, false );
161 
162  // validate helper function and module
163  function get_value( vid ) = get_table_v(test_r, test_c, vid, "tv");
164  module run_test( fname, fresult, vid )
165  {
166  value_text = get_table_v(test_r, test_c, vid, "td");
167  pass_value = get_table_v(good_r, good_c, fname, vid);
168 
169  test_pass = validate( cv=fresult, t="equals", ev=pass_value, pf=true );
170  test_text = validate( str(fname, "(", get_value(vid), ")=", pass_value), fresult, "equals", pass_value );
171 
172  if ( pass_value != skip )
173  {
174  if ( !test_pass )
175  log_warn( str(vid, "(", value_text, ") ", test_text) );
176  else if ( show_passing )
177  log_info( str(vid, " ", test_text) );
178  }
179  else if ( show_skipped )
180  log_info( str(vid, " *skip*: '", fname, "(", value_text, ")'") );
181  }
182 
183  // Indirect function calls would be very useful here!!!
184  for (vid=test_ids) run_test( "defined_or_D", defined_or(get_value(vid),"default"), vid );
185  // circular_index() not tested
186 
187  // end-of-tests
188  END_OPENSCAD;
189 
190  BEGIN_MFSCRIPT;
191  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
192  include --path "${INCLUDE_PATH}" script_std.mfs;
193  END_MFSCRIPT;
194 END_SCOPE;
195 */
196 
197 //----------------------------------------------------------------------------//
198 // end of file
199 //----------------------------------------------------------------------------//
function defined_or(v, d)
Return a value when it is defined or a default value when it is not.
function is_defined(v)
Test if a value is defined.
function circular_index(i, l, f=0)
Map an index position into a circularly indexed list.