Re: missing tests for cast()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: missing tests for cast()

Rik-4
On 01/15/2014 07:31 PM, [hidden email] wrote:

> Message: 3
> Date: Wed, 15 Jan 2014 22:31:27 -0500
> From: mike sander <[hidden email]>
> To: [hidden email]
> Subject: missing tests
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> i chose a function from http://wiki.octave.org/FilesMissingTests: 
> miscellaneous/cast.m
>
> the following is what i have so far.   Looking for some feedback before
> i continue.  Do the tests look sensible?  Is there a class of testing
> related to cast to int8 (& other types) that i am missing?  I have tried
> to follow formatting/style from other tests.
>
> Thanks in advance for any comments/suggestions.
>
>
> <code>
> %% Test input validation
> %!error cast (1)
> %!error <cast: expecting TYPE name as second argument> cast (1,1)
> %!error <cast: type name 'int' is not a built-in type> cast (1,"int")
The name of the function and the colon are automatically inserted by the
error function so they don't really need to be checked.  I just use the
actual warning message itself so I think this could be written as:

%!error <expecting TYPE name as second argument> cast (1,1)
%!error <type name 'int' is not a built-in type> cast (1,"int")

The input validation tests are also of secondary importance when compared
to the functional tests.  I usually put the functional tests first and the
input validation is always the last set of tests in the file.  As you might
have seen in the m-files, there are always 2 blank lines after the last
line of actual code (usually the keyword "endfunction") and the start of
the %!tests.

> ## Test int8 cast.  input values will be limited to int8 range -128 to
> 127 after cast.  input values within range unchanged.
> %!assert (cast (int8 (-128), "int8"), int8 (-128))
> %!assert (cast (int8 (0), "int8"), int8 (0))
This strategy will work just fine, although you will end up with pages and
pages of tests.  You could explore using a for loop and 'eval' to compact
things, or you might just use a script to generate all the required
combinations and paste that in to cast.m.

Cheers,
Rik

Reply | Threaded
Open this post in threaded view
|

Re: missing tests for cast()

John W. Eaton
Administrator
On 01/16/2014 12:54 PM, Rik wrote:

> This strategy will work just fine, although you will end up with pages and
> pages of tests.  You could explore using a for loop and 'eval' to compact
> things, or you might just use a script to generate all the required
> combinations and paste that in to cast.m.

If we are using a script to generate the tests, then it might as well be
part of the build process, similar to the way we generate a large number
of tests for sparse.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: missing tests for cast()

msander
On 14-01-16 01:06 PM, John W. Eaton wrote:

> On 01/16/2014 12:54 PM, Rik wrote:
>
>> This strategy will work just fine, although you will end up with
>> pages and
>> pages of tests.  You could explore using a for loop and 'eval' to
>> compact
>> things, or you might just use a script to generate all the required
>> combinations and paste that in to cast.m.
>
> If we are using a script to generate the tests, then it might as well
> be part of the build process, similar to the way we generate a large
> number of tests for sparse.
>
> jwe
>
>
>
at least initially i think i will stay away from integrating with the
build system.  i think i'll be able to condense the tests with arrays &
looping.   i will look at the tests for sparse as suggested.

thanks.

Reply | Threaded
Open this post in threaded view
|

Re: missing tests for cast()

msander
In reply to this post by John W. Eaton
On 14-01-16 01:06 PM, John W. Eaton wrote:

> On 01/16/2014 12:54 PM, Rik wrote:
>
>
> If we are using a script to generate the tests, then it might as well
> be part of the build process, similar to the way we generate a large
> number of tests for sparse.
>
> jwe
>
>
>
I have taken feedback from earlier in this thread.  Thanks for your
ideas.  I think i have come up with a concise set of tests for cast()
using loops & cell arrays.   See below.

The tests basically consist of casting each type specific min & max
value to all output types & comparing the value using built in type
conversion.

The tests do not include some of the suggestions, specifically:
o random values.  I believe testing the extreme of each input should be
sufficient.
o complex numbers.  As far a i could tell, cast() does not support complex.
o does not handle "char" type yet.


I am open to further comments / suggestions.

cheers.

<code>

## for each input type, get the type appropriate min & max (val).
##   cast val to each output type & verify equivalence to built in type
conversion.
## for example convert max int32 to uint8 would look like:
## val = intmax("int32")
## assert (cast (val, "uint8"), uint8(val))
%!test
%! in = { "logical", "int8", "uint8", "int16", "uint16", "int32",
"uint32", "int64", "uint64", "double", "single"};
%! for i = 1:numel(in)
%!    if (any (strcmp (in{i}, {"int8"; "uint8"; "int16"; "uint16";
"int32"; "uint32"; "int64"; "uint64";})))
%!          val = intmin(in{i});
%!       elseif (any (strcmp (in{i}, {"double"; "single"})))
%!          val = realmin(in{i});
%!       elseif (any (strcmp (in{i}, {"logical"})))
%!       val = false;
%!       else
%!          error ("unhandled type");
%!      endif
%!   out = { {"logical",logical(val)}, {"int8",int8(val)},
{"uint8",uint8(val)}, {"int16",int16(val)}, {"uint16",uint16(val)}, \
%!           {"int32",int32(val)}, {"uint32",uint32(val)},
{"int64",int64(val)}, {"uint64",uint64(val)}, {"double",double(val)}, \
%!           {"single",single(val)} };
%!   for o = 1:numel(out)
%!     assert (cast (out{i}{2}, out{o}{1}), out{o}{2})
%!   end
%! end
%! for i = 1:numel(in)
%!    if (any (strcmp (in{i}, {"int8"; "uint8"; "int16"; "uint16";
"int32"; "uint32"; "int64"; "uint64";})))
%!          val = intmax(in{i});
%!       elseif (any (strcmp (in{i}, {"double"; "single"})))
%!          val = realmax(in{i});
%!       elseif (any (strcmp (in{i}, {"logical"})))
%!       val = true;
%!       else
%!          error ("unhandled type");
%!      endif
%!   out = { {"logical",logical(val)}, {"int8",int8(val)},
{"uint8",uint8(val)}, {"int16",int16(val)}, {"uint16",uint16(val)}, \
%!           {"int32",int32(val)}, {"uint32",uint32(val)},
{"int64",int64(val)}, {"uint64",uint64(val)}, {"double",double(val)}, \
%!           {"single",single(val)} };
%!   for o = 1:numel(out)
%!     assert (cast (out{i}{2}, out{o}{1}), out{o}{2})
%!   end
%! end

</code>