omdl  v0.6.1
OpenSCAD Mechanical Design Library
Script
include <console.scad>;
include <datatypes/datatypes_table.scad>;
include <math/math_bitwise.scad>;
include <validation.scad>;
show_passing = true; // show passing tests
show_skipped = true; // show skipped tests
echo( str("OpenSCAD Version ", version()) );
// test-values columns
test_c =
[
["id", "identifier"],
["td", "description"],
["tv", "test value"]
];
// test-values rows
test_r =
[
["fac", "Function argument count", undef],
["t01", "All undefined", [undef,undef]],
["t02", "All empty lists", [empty_lst,empty_lst]],
["t03", "test value 1", [254, 0]],
["t04", "test value 2", [254, 1]],
["t05", "test value 3", [255, 0]],
["t06", "test value 4", [0, 255]],
["t07", "test value 5", [126, 63]],
["t08", "test value 6", [25, 10]],
["t09", "test value 7", [1024, 512]],
["t10", "test value 8", [4253, 315]],
["t11", "test value 9", [835, 769]],
["t12", "test value 10", [856, 625]]
];
test_ids = get_table_ridl( test_r );
// expected columns: ("id" + one column for each test)
good_c = pmerge([concat("id", test_ids), concat("identifier", test_ids)]);
// expected rows: ("golden" test results), use 'skip' to skip test
skip = -1; // skip test
good_r =
[ // function
["bitwise_is_equal_0", 2,
false, // t01
false, // t02
true, // t03
false, // t04
false, // t05
true, // t06
true, // t07
true, // t08
true, // t09
true, // t10
true, // t11
true // t12
],
["bitwise_is_equal_1", 2,
false, // t01
false, // t02
false, // t03
true, // t04
true, // t05
false, // t06
false, // t07
false, // t08
false, // t09
false, // t10
false, // t11
false // t12
],
["bitwise_i2v", 1,
undef, // t01
undef, // t02
[1,1,1,1,1,1,1,0], // t03
[1,1,1,1,1,1,1,0], // t04
[1,1,1,1,1,1,1,1], // t05
[0], // t06
[1,1,1,1,1,1,0], // t07
[1,1,0,0,1], // t08
[1,0,0,0,0,0,0,0,0,0,0], // t09
[1,0,0,0,0,1,0,0,1,1,1,0,1], // t10
[1,1,0,1,0,0,0,0,1,1], // t11
[1,1,0,1,0,1,1,0,0,0] // t12
],
["bitwise_i2v_v2i", 1,
undef, // t01
undef, // t02
254, // t03
254, // t04
255, // t05
0, // t06
126, // t07
25, // t08
1024, // t09
4253, // t10
835, // t11
856 // t12
],
["bitwise_i2s", 1,
undef, // t01
undef, // t02
"11111110", // t03
"11111110", // t04
"11111111", // t05
"0", // t06
"1111110", // t07
"11001", // t08
"10000000000", // t09
"1000010011101", // t10
"1101000011", // t11
"1101011000" // t12
],
["bitwise_i2s_s2i", 1,
undef, // t01
undef, // t02
254, // t03
254, // t04
255, // t05
0, // t06
126, // t07
25, // t08
1024, // t09
4253, // t10
835, // t11
856 // t12
],
["bitwise_imi_32", 1,
undef, // t01
undef, // t02
7, // t03
7, // t04
7, // t05
0, // t06
7, // t07
6, // t08
0, // t09
7, // t10
0, // t11
6 // t12
],
["bitwise_and", 2,
undef, // t01
undef, // t02
0, // t03
0, // t04
0, // t05
0, // t06
62, // t07
8, // t08
0, // t09
25, // t10
769, // t11
592 // t12
],
["bitwise_or", 2,
undef, // t01
undef, // t02
254, // t03
255, // t04
255, // t05
255, // t06
127, // t07
27, // t08
1536, // t09
4543, // t10
835, // t11
889 // t12
],
["bitwise_xor", 2,
undef, // t01
undef, // t02
254, // t03
255, // t04
255, // t05
255, // t06
65, // t07
19, // t08
1536, // t09
4518, // t10
66, // t11
297 // t12
],
["bitwise_not", 1,
undef, // t01
undef, // t02
1, // t03
1, // t04
0, // t05
1, // t06
1, // t07
6, // t08
1023, // t09
3938, // t10
188, // t11
167 // t12
],
["bitwise_lsh", 1,
undef, // t01
undef, // t02
508, // t03
508, // t04
510, // t05
0, // t06
252, // t07
50, // t08
2048, // t09
8506, // t10
1670, // t11
1712 // t12
],
["bitwise_rsh", 1,
undef, // t01
undef, // t02
127, // t03
127, // t04
127, // t05
0, // t06
63, // t07
12, // t08
512, // t09
2126, // t10
417, // t11
428 // t12
]
];
// sanity-test tables
table_check( test_r, test_c, false );
table_check( good_r, good_c, false );
// validate helper function and module
function get_value( vid ) = get_table_v(test_r, test_c, vid, "tv");
function gv( vid, e ) = get_value( vid )[e];
module run( fname, vid )
{
value_text = get_table_v(test_r, test_c, vid, "td");
if ( get_table_v(good_r, good_c, fname, vid) != skip )
children();
else if ( show_skipped )
log_info( str("*skip*: ", vid, " '", fname, "(", value_text, ")'") );
}
module test( fname, fresult, vid )
{
value_text = get_table_v(test_r, test_c, vid, "td");
fname_argc = get_table_v(good_r, good_c, fname, "fac");
pass_value = get_table_v(good_r, good_c, fname, vid);
test_pass = validate(cv=fresult, t="equals", ev=pass_value, pf=true);
farg_text = lstr(eappend(", ", rselect(get_value(vid), [0:fname_argc-1]), r=false, j=false, l=false));
test_text = validate(str(fname, "(", farg_text, ")=", pass_value), fresult, "equals", pass_value);
if ( pass_value != skip )
{
if ( !test_pass )
log_warn( str(vid, "(", value_text, ") ", test_text) );
else if ( show_passing )
log_info( str(vid, " ", test_text) );
}
else if ( show_skipped )
log_info( str(vid, " *skip*: '", fname, "(", value_text, ")'") );
}
// Indirect function calls would be very useful here!!!
run_ids = delete( test_ids, mv=["fac", "crp"] );
for (vid=run_ids) run("bitwise_is_equal_0",vid) test( "bitwise_is_equal_0", bitwise_is_equal(gv(vid,0),gv(vid,1),0), vid );
for (vid=run_ids) run("bitwise_is_equal_1",vid) test( "bitwise_is_equal_1", bitwise_is_equal(gv(vid,0),gv(vid,1),1), vid );
for (vid=run_ids) run("bitwise_i2v",vid) test( "bitwise_i2v", bitwise_i2v(gv(vid,0)), vid );
for (vid=run_ids) run("bitwise_i2v_v2i",vid) test( "bitwise_i2v_v2i", bitwise_v2i(bitwise_i2v(gv(vid,0))), vid );
for (vid=run_ids) run("bitwise_i2s",vid) test( "bitwise_i2s", bitwise_i2s(gv(vid,0)), vid );
for (vid=run_ids) run("bitwise_i2s_s2i",vid) test( "bitwise_i2s_s2i", bitwise_s2i(bitwise_i2s(gv(vid,0))), vid );
for (vid=run_ids) run("bitwise_imi_32",vid) test( "bitwise_imi_32", bitwise_imi(gv(vid,0),3,2), vid );
for (vid=run_ids) run("bitwise_and",vid) test( "bitwise_and", bitwise_and(gv(vid,0),gv(vid,1)), vid );
for (vid=run_ids) run("bitwise_or",vid) test( "bitwise_or", bitwise_or(gv(vid,0),gv(vid,1)), vid );
for (vid=run_ids) run("bitwise_xor",vid) test( "bitwise_xor", bitwise_xor(gv(vid,0),gv(vid,1)), vid );
for (vid=run_ids) run("bitwise_not",vid) test( "bitwise_not", bitwise_not(gv(vid,0)), vid );
for (vid=run_ids) run("bitwise_lsh",vid) test( "bitwise_lsh", bitwise_lsh(gv(vid,0)), vid );
for (vid=run_ids) run("bitwise_rsh",vid) test( "bitwise_rsh", bitwise_rsh(gv(vid,0)), vid );
// end-of-tests