# overloaded-vector-and-matrix-ops-unit-test.pkg
# #DO set_control "compiler::verbose_compile_log" "TRUE";
# Compiled by:
#
src/lib/test/unit-tests.lib# Run by:
#
src/lib/test/all-unit-tests.pkg# Unit tests for:
# Overloading/vector/matrix functionality.
stipulate
package fil = file__premicrothread; # file__premicrothread is from
src/lib/std/src/posix/file--premicrothread.pkg package m64 = rw_matrix_of_eight_byte_floats; # rw_matrix_of_eight_byte_floats is from
src/lib/std/src/rw-matrix-of-eight-byte-floats.pkg package m1b = rw_matrix_of_one_byte_unts; # rw_matrix_of_one_byte_unts is from
src/lib/std/src/rw-matrix-of-one-byte-unts.pkg package u1b = one_byte_unt; # one_byte_unt is from
src/lib/std/types-only/basis-structs.pkg package psx = posixlib; # posixlib is from
src/lib/std/src/psx/posixlib.pkg infix my 50 ==== !=;
(====) = eight_byte_float::(====);
herein
package overloaded_vector_and_matrix_ops_unit_test {
#
include package unit_test; # unit_test is from
src/lib/src/unit-test.pkg name = "src/lib/src/overloaded-vector-and-matrix-ops-unit-test.pkg unit tests";
fun exercise_vector_gets_and_sets ()
=
{
vector_of_chars = "abc";
rw_vector_of_chars = rw_vector_of_chars::from_list [ 'x', 'y', 'z' ] ;
chars_in_vector = vector::from_list [ 'A', 'B', 'C' ] ;
chars_in_rw_vector = rw_vector::from_list [ 'X', 'Y', 'Z' ] ;
vector_of_one_byte_unts = vector_of_one_byte_unts::from_list (map one_byte_unt::from_int [ 5, 6, 7 ]);
rw_vector_of_one_byte_unts = rw_vector_of_one_byte_unts::from_list (map one_byte_unt::from_int [ 15, 16, 17 ]);
rw_vector_of_eight_byte_floats = rw_vector_of_eight_byte_floats::from_list [ 1.0, 2.0, 3.0 ] ;
assert ( string::get_byte_as_char (vector_of_chars,1) == 'b');
assert ( vector::get (chars_in_vector,1) == 'B');
assert ( rw_vector::get (chars_in_rw_vector,1) == 'Y');
assert ( rw_vector_of_chars::get (rw_vector_of_chars,1) == 'y');
assert ( vector_of_one_byte_unts::get (vector_of_one_byte_unts,1) == one_byte_unt::from_int 6 );
assert ( rw_vector_of_one_byte_unts::get (rw_vector_of_one_byte_unts,1) == one_byte_unt::from_int 16 );
assert (rw_vector_of_eight_byte_floats::get (rw_vector_of_eight_byte_floats,1) ==== 2.0 );
# assert ( vector_of_chars[1] == 'b' ); # I spent 2-3 months getting this one to work. :-) -- 2013-11-20 CrT # Commented out 2015-05-27 CrT because with utf8 support the v[i] support is deceptive -- suggests all indices are valid.
assert ( chars_in_vector[1] == 'B' );
assert ( chars_in_rw_vector[1] == 'Y' );
assert ( rw_vector_of_chars[1] == 'y' );
assert ( vector_of_one_byte_unts[1] == one_byte_unt::from_int 6 );
assert ( rw_vector_of_one_byte_unts[1] == one_byte_unt::from_int 16 );
assert (rw_vector_of_eight_byte_floats[1] ==== 2.0 );
chars_in_rw_vector[1] := chars_in_rw_vector[2];
rw_vector_of_chars[1] := rw_vector_of_chars[2];
rw_vector_of_one_byte_unts[1] := rw_vector_of_one_byte_unts[2];
rw_vector_of_eight_byte_floats[1] := rw_vector_of_eight_byte_floats[2];
assert ( chars_in_rw_vector[1] == 'Z' );
assert ( rw_vector_of_chars[1] == 'z' );
assert ( rw_vector_of_one_byte_unts[1] == one_byte_unt::from_int 17 );
assert (rw_vector_of_eight_byte_floats[1] ==== 3.0 );
};
fun exercise_rw_matrix_gets_and_sets_of_chars ()
=
{
chars_in_rw_matrix = rw_matrix::from_lists [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ];
assert ( rw_matrix::get (chars_in_rw_matrix, (0,0)) == 'a' );
assert ( rw_matrix::get (chars_in_rw_matrix, (0,1)) == 'b' );
assert ( rw_matrix::get (chars_in_rw_matrix, (0,2)) == 'c' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,0)) == 'd' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,1)) == 'e' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,2)) == 'f' );
rw_matrix::set (chars_in_rw_matrix, (0,0), 'A');
rw_matrix::set (chars_in_rw_matrix, (0,1), 'B');
rw_matrix::set (chars_in_rw_matrix, (0,2), 'C');
rw_matrix::set (chars_in_rw_matrix, (1,0), 'D');
rw_matrix::set (chars_in_rw_matrix, (1,1), 'E');
rw_matrix::set (chars_in_rw_matrix, (1,2), 'F');
assert ( rw_matrix::get (chars_in_rw_matrix, (0,0)) == 'A' );
assert ( rw_matrix::get (chars_in_rw_matrix, (0,1)) == 'B' );
assert ( rw_matrix::get (chars_in_rw_matrix, (0,2)) == 'C' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,0)) == 'D' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,1)) == 'E' );
assert ( rw_matrix::get (chars_in_rw_matrix, (1,2)) == 'F' );
# (_[]) = rw_matrix::(_[]);
# (_[]:=) = rw_matrix::(_[]:=);
chars_in_rw_matrix = rw_matrix::from_list (2,3) [ 'a', 'b', 'c', 'd', 'e', 'f' ];
assert ( chars_in_rw_matrix[0,0] == 'a' );
assert ( chars_in_rw_matrix[0,1] == 'b' );
assert ( chars_in_rw_matrix[0,2] == 'c' );
assert ( chars_in_rw_matrix[1,0] == 'd' );
assert ( chars_in_rw_matrix[1,1] == 'e' );
assert ( chars_in_rw_matrix[1,2] == 'f' );
chars_in_rw_matrix[0,0] := 'A';
chars_in_rw_matrix[0,1] := 'B';
chars_in_rw_matrix[0,2] := 'C';
chars_in_rw_matrix[1,0] := 'D';
chars_in_rw_matrix[1,1] := 'E';
chars_in_rw_matrix[1,2] := 'F';
assert ( chars_in_rw_matrix[0,0] == 'A' );
assert ( chars_in_rw_matrix[0,1] == 'B' );
assert ( chars_in_rw_matrix[0,2] == 'C' );
assert ( chars_in_rw_matrix[1,0] == 'D' );
assert ( chars_in_rw_matrix[1,1] == 'E' );
assert ( chars_in_rw_matrix[1,2] == 'F' );
};
fun exercise_rw_matrix_gets_and_sets_of_strings ()
=
{
strings_in_rw_matrix = rw_matrix::from_lists [ [ "a", "b", "c" ], [ "d", "e", "f" ] ];
assert ( rw_matrix::get (strings_in_rw_matrix, (0,0)) == "a" );
assert ( rw_matrix::get (strings_in_rw_matrix, (0,1)) == "b" );
assert ( rw_matrix::get (strings_in_rw_matrix, (0,2)) == "c" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,0)) == "d" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,1)) == "e" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,2)) == "f" );
rw_matrix::set (strings_in_rw_matrix, (0,0), "A");
rw_matrix::set (strings_in_rw_matrix, (0,1), "B");
rw_matrix::set (strings_in_rw_matrix, (0,2), "C");
rw_matrix::set (strings_in_rw_matrix, (1,0), "D");
rw_matrix::set (strings_in_rw_matrix, (1,1), "E");
rw_matrix::set (strings_in_rw_matrix, (1,2), "F");
assert ( rw_matrix::get (strings_in_rw_matrix, (0,0)) == "A" );
assert ( rw_matrix::get (strings_in_rw_matrix, (0,1)) == "B" );
assert ( rw_matrix::get (strings_in_rw_matrix, (0,2)) == "C" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,0)) == "D" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,1)) == "E" );
assert ( rw_matrix::get (strings_in_rw_matrix, (1,2)) == "F" );
# (_[]) = rw_matrix::(_[]);
# (_[]:=) = rw_matrix::(_[]:=);
strings_in_rw_matrix = rw_matrix::from_list (2,3) [ "a", "b", "c", "d", "e", "f" ];
assert ( strings_in_rw_matrix[0,0] == "a" );
assert ( strings_in_rw_matrix[0,1] == "b" );
assert ( strings_in_rw_matrix[0,2] == "c" );
assert ( strings_in_rw_matrix[1,0] == "d" );
assert ( strings_in_rw_matrix[1,1] == "e" );
assert ( strings_in_rw_matrix[1,2] == "f" );
strings_in_rw_matrix[0,0] := "A";
strings_in_rw_matrix[0,1] := "B";
strings_in_rw_matrix[0,2] := "C";
strings_in_rw_matrix[1,0] := "D";
strings_in_rw_matrix[1,1] := "E";
strings_in_rw_matrix[1,2] := "F";
assert ( strings_in_rw_matrix[0,0] == "A" );
assert ( strings_in_rw_matrix[0,1] == "B" );
assert ( strings_in_rw_matrix[0,2] == "C" );
assert ( strings_in_rw_matrix[1,0] == "D" );
assert ( strings_in_rw_matrix[1,1] == "E" );
assert ( strings_in_rw_matrix[1,2] == "F" );
};
fun exercise_rw_matrix_gets_and_sets_of_ints ()
=
{
ints_in_rw_matrix = rw_matrix::from_lists [ [ 10, 11, 12 ], [ 13, 14, 15 ] ];
assert ( rw_matrix::get (ints_in_rw_matrix, (0,0)) == 10 );
assert ( rw_matrix::get (ints_in_rw_matrix, (0,1)) == 11 );
assert ( rw_matrix::get (ints_in_rw_matrix, (0,2)) == 12 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,0)) == 13 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,1)) == 14 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,2)) == 15 );
rw_matrix::set (ints_in_rw_matrix, (0,0), 20 );
rw_matrix::set (ints_in_rw_matrix, (0,1), 21 );
rw_matrix::set (ints_in_rw_matrix, (0,2), 22 );
rw_matrix::set (ints_in_rw_matrix, (1,0), 23 );
rw_matrix::set (ints_in_rw_matrix, (1,1), 24 );
rw_matrix::set (ints_in_rw_matrix, (1,2), 25 );
assert ( rw_matrix::get (ints_in_rw_matrix, (0,0)) == 20 );
assert ( rw_matrix::get (ints_in_rw_matrix, (0,1)) == 21 );
assert ( rw_matrix::get (ints_in_rw_matrix, (0,2)) == 22 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,0)) == 23 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,1)) == 24 );
assert ( rw_matrix::get (ints_in_rw_matrix, (1,2)) == 25 );
# (_[]) = rw_matrix::(_[]);
# (_[]:=) = rw_matrix::(_[]:=);
ints_in_rw_matrix = rw_matrix::from_list (2,3) [ 10, 11, 12, 13, 14, 15 ];
assert ( ints_in_rw_matrix[0,0] == 10 );
assert ( ints_in_rw_matrix[0,1] == 11 );
assert ( ints_in_rw_matrix[0,2] == 12 );
assert ( ints_in_rw_matrix[1,0] == 13 );
assert ( ints_in_rw_matrix[1,1] == 14 );
assert ( ints_in_rw_matrix[1,2] == 15 );
ints_in_rw_matrix[0,0] := 20;
ints_in_rw_matrix[0,1] := 21;
ints_in_rw_matrix[0,2] := 22;
ints_in_rw_matrix[1,0] := 23;
ints_in_rw_matrix[1,1] := 24;
ints_in_rw_matrix[1,2] := 25;
assert ( ints_in_rw_matrix[0,0] == 20 );
assert ( ints_in_rw_matrix[0,1] == 21 );
assert ( ints_in_rw_matrix[0,2] == 22 );
assert ( ints_in_rw_matrix[1,0] == 23 );
assert ( ints_in_rw_matrix[1,1] == 24 );
assert ( ints_in_rw_matrix[1,2] == 25 );
};
fun exercise_rw_matrix_gets_and_sets_of_floats ()
=
{
floats_in_rw_matrix = rw_matrix::from_lists [ [ 1.0, 1.1, 1.2 ], [ 1.3, 1.4, 1.5 ] ];
assert ( rw_matrix::get (floats_in_rw_matrix, (0,0)) ==== 1.0 );
assert ( rw_matrix::get (floats_in_rw_matrix, (0,1)) ==== 1.1 );
assert ( rw_matrix::get (floats_in_rw_matrix, (0,2)) ==== 1.2 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,0)) ==== 1.3 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,1)) ==== 1.4 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,2)) ==== 1.5 );
rw_matrix::set (floats_in_rw_matrix, (0,0), 2.0 );
rw_matrix::set (floats_in_rw_matrix, (0,1), 2.1 );
rw_matrix::set (floats_in_rw_matrix, (0,2), 2.2 );
rw_matrix::set (floats_in_rw_matrix, (1,0), 2.3 );
rw_matrix::set (floats_in_rw_matrix, (1,1), 2.4 );
rw_matrix::set (floats_in_rw_matrix, (1,2), 2.5 );
assert ( rw_matrix::get (floats_in_rw_matrix, (0,0)) ==== 2.0 );
assert ( rw_matrix::get (floats_in_rw_matrix, (0,1)) ==== 2.1 );
assert ( rw_matrix::get (floats_in_rw_matrix, (0,2)) ==== 2.2 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,0)) ==== 2.3 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,1)) ==== 2.4 );
assert ( rw_matrix::get (floats_in_rw_matrix, (1,2)) ==== 2.5 );
# (_[]) = rw_matrix::(_[]);
# (_[]:=) = rw_matrix::(_[]:=);
floats_in_rw_matrix = rw_matrix::from_list (2,3) [ 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 ];
assert ( floats_in_rw_matrix[0,0] ==== 1.0 );
assert ( floats_in_rw_matrix[0,1] ==== 1.1 );
assert ( floats_in_rw_matrix[0,2] ==== 1.2 );
assert ( floats_in_rw_matrix[1,0] ==== 1.3 );
assert ( floats_in_rw_matrix[1,1] ==== 1.4 );
assert ( floats_in_rw_matrix[1,2] ==== 1.5 );
floats_in_rw_matrix[0,0] := 2.0;
floats_in_rw_matrix[0,1] := 2.1;
floats_in_rw_matrix[0,2] := 2.2;
floats_in_rw_matrix[1,0] := 2.3;
floats_in_rw_matrix[1,1] := 2.4;
floats_in_rw_matrix[1,2] := 2.5;
assert ( floats_in_rw_matrix[0,0] ==== 2.0 );
assert ( floats_in_rw_matrix[0,1] ==== 2.1 );
assert ( floats_in_rw_matrix[0,2] ==== 2.2 );
assert ( floats_in_rw_matrix[1,0] ==== 2.3 );
assert ( floats_in_rw_matrix[1,1] ==== 2.4 );
assert ( floats_in_rw_matrix[1,2] ==== 2.5 );
};
fun exercise_rw_matrix_of_eight_byte_floats ()
=
{
m64 = m64::from_lists [ [ 1.0, 1.1, 1.2 ], [ 1.3, 1.4, 1.5 ] ];
assert ( m64::get (m64, (0,0)) ==== 1.0 );
assert ( m64::get (m64, (0,1)) ==== 1.1 );
assert ( m64::get (m64, (0,2)) ==== 1.2 );
assert ( m64::get (m64, (1,0)) ==== 1.3 );
assert ( m64::get (m64, (1,1)) ==== 1.4 );
assert ( m64::get (m64, (1,2)) ==== 1.5 );
m64::set (m64, (0,0), 2.0 );
m64::set (m64, (0,1), 2.1 );
m64::set (m64, (0,2), 2.2 );
m64::set (m64, (1,0), 2.3 );
m64::set (m64, (1,1), 2.4 );
m64::set (m64, (1,2), 2.5 );
assert ( m64::get (m64, (0,0)) ==== 2.0 );
assert ( m64::get (m64, (0,1)) ==== 2.1 );
assert ( m64::get (m64, (0,2)) ==== 2.2 );
assert ( m64::get (m64, (1,0)) ==== 2.3 );
assert ( m64::get (m64, (1,1)) ==== 2.4 );
assert ( m64::get (m64, (1,2)) ==== 2.5 );
# (_[]) = m64::(_[]);
# (_[]:=) = m64::(_[]:=);
m64 = m64::from_list (2,3) [ 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 ];
assert ( m64[0,0] ==== 1.0 );
assert ( m64[0,1] ==== 1.1 );
assert ( m64[0,2] ==== 1.2 );
assert ( m64[1,0] ==== 1.3 );
assert ( m64[1,1] ==== 1.4 );
assert ( m64[1,2] ==== 1.5 );
m64[0,0] := 2.0;
m64[0,1] := 2.1;
m64[0,2] := 2.2;
m64[1,0] := 2.3;
m64[1,1] := 2.4;
m64[1,2] := 2.5;
assert ( m64[0,0] ==== 2.0 );
assert ( m64[0,1] ==== 2.1 );
assert ( m64[0,2] ==== 2.2 );
assert ( m64[1,0] ==== 2.3 );
assert ( m64[1,1] ==== 2.4 );
assert ( m64[1,2] ==== 2.5 );
};
fun exercise_rw_matrix_of_one_byte_unts ()
=
{
u10 = u1b::from_int 10;
u11 = u1b::from_int 11;
u12 = u1b::from_int 12;
u13 = u1b::from_int 13;
u14 = u1b::from_int 14;
u15 = u1b::from_int 15;
u20 = u1b::from_int 20;
u21 = u1b::from_int 21;
u22 = u1b::from_int 22;
u23 = u1b::from_int 23;
u24 = u1b::from_int 24;
u25 = u1b::from_int 25;
m1b = m1b::from_lists [ [ u10, u11, u12 ], [ u13, u14, u15 ] ];
assert ( m1b::get (m1b, (0,0)) == u10 );
assert ( m1b::get (m1b, (0,1)) == u11 );
assert ( m1b::get (m1b, (0,2)) == u12 );
assert ( m1b::get (m1b, (1,0)) == u13 );
assert ( m1b::get (m1b, (1,1)) == u14 );
assert ( m1b::get (m1b, (1,2)) == u15 );
m1b::set (m1b, (0,0), u20 );
m1b::set (m1b, (0,1), u21 );
m1b::set (m1b, (0,2), u22 );
m1b::set (m1b, (1,0), u23 );
m1b::set (m1b, (1,1), u24 );
m1b::set (m1b, (1,2), u25 );
assert ( m1b::get (m1b, (0,0)) == u20 );
assert ( m1b::get (m1b, (0,1)) == u21 );
assert ( m1b::get (m1b, (0,2)) == u22 );
assert ( m1b::get (m1b, (1,0)) == u23 );
assert ( m1b::get (m1b, (1,1)) == u24 );
assert ( m1b::get (m1b, (1,2)) == u25 );
# (_[]) = m1b::(_[]);
# (_[]:=) = m1b::(_[]:=);
m1b = m1b::from_list (2,3) [ u10, u11, u12, u13, u14, u15 ];
assert ( m1b[0,0] == u10 );
assert ( m1b[0,1] == u11 );
assert ( m1b[0,2] == u12 );
assert ( m1b[1,0] == u13 );
assert ( m1b[1,1] == u14 );
assert ( m1b[1,2] == u15 );
m1b[0,0] := u20;
m1b[0,1] := u21;
m1b[0,2] := u22;
m1b[1,0] := u23;
m1b[1,1] := u24;
m1b[1,2] := u25;
assert ( m1b[0,0] == u20 );
assert ( m1b[0,1] == u21 );
assert ( m1b[0,2] == u22 );
assert ( m1b[1,0] == u23 );
assert ( m1b[1,1] == u24 );
assert ( m1b[1,2] == u25 );
};
fun exercise_complex_numbers ()
=
{
c1 = { r => 2.0, i => 4.0 };
c2 = { r => 8.0, i => 16.0 };
c3 = c1 + c2;
#
assert ( c3.r == 10.0 );
assert ( c3.i == 20.0 );
c3 = c2 - c1;
#
assert ( c3.r == 6.0 );
assert ( c3.i == 12.0 );
c3 = c1 * c2;
#
assert ( c3.r == -48.0 );
assert ( c3.i == 64.0 );
};
fun exercise_quaternions ()
=
{
q1 = { r => 1.0, i => 2.0, j => 3.0, k => 4.0 };
q2 = { r => 5.0, i => 6.0, j => 7.0, k => 8.0 };
q3 = q1 + q2;
#
assert ( q3.r == 6.0 );
assert ( q3.i == 8.0 );
assert ( q3.j == 10.0 );
assert ( q3.k == 12.0 );
q3 = q2 - q1;
#
assert ( q3.r == 4.0 );
assert ( q3.i == 4.0 );
assert ( q3.j == 4.0 );
assert ( q3.k == 4.0 );
q3 = q1 * q2;
#
assert ( q3.r == -60.0 );
assert ( q3.i == 12.0 );
assert ( q3.j == 30.0 );
assert ( q3.k == 24.0 );
};
fun run ()
=
{
printf "\nDoing %s:\n" name;
exercise_vector_gets_and_sets ();
exercise_rw_matrix_gets_and_sets_of_chars (); #
src/lib/std/src/rw-matrix.pkg exercise_rw_matrix_gets_and_sets_of_strings (); #
src/lib/std/src/rw-matrix.pkg exercise_rw_matrix_gets_and_sets_of_ints (); #
src/lib/std/src/rw-matrix.pkg exercise_rw_matrix_gets_and_sets_of_floats (); #
src/lib/std/src/rw-matrix.pkg #
exercise_rw_matrix_of_eight_byte_floats (); #
src/lib/std/src/rw-matrix-of-eight-byte-floats.pkg exercise_rw_matrix_of_one_byte_unts (); #
src/lib/std/src/rw-matrix-of-one-byte-unts.pkg #
exercise_complex_numbers ();
exercise_quaternions ();
summarize_unit_tests name;
};
};
end;
## Code by Jeff Prothero: Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.