Unit testing with datastructures
i want to undertake some unit tests and a few functional tests on a working system. However i have a datastructure that mostly consists of a few arrays in an object (well its fortran, so not a real object, but you get the idea.) How would an example look like to unit test a datastructure? all examples i have seen so far returned a single value that was then tested. how do i do this with a datastructure that consists of arrays?
If i could get an example in funit, that would be great. Here is an example of my datastructure:
type :: myownobject
开发者_C百科 integer :: front(300)
integer :: end(300)
integer :: size
end type
subroutine getmesugar(myob, moredata)
type(myownobject), intent(inout) :: myob
integer, intent(in) :: moredata (20)
! do something with myownobject
! perhaps add the data after a sanity check
! using the new data and the object
end subroutine
i know its perhaps a simple question, but i am really just starting out with unit testing. i am reading The Art Of Unit Testing by Roy Osherove.
Unit testing a data structure or, in more Fortranic terms, a derived data type, seems a rather vague notion. Perhaps what you want to do is test various uses of variables of derived type ?
The way you phrase your question also suggests that you may not have learned yet how to write functions which return arrays, or am I misinterpreting what you write ? You can also write functions which return derived data types.
Perhaps you could be more explicit about what you want to do with your type -- and that might suggest what unit tests you need to write.
EDIT: to answer OP's comments at a little more length than a comment allows ..
Long-term Fortran programmers, such as me, don't really know what a unit testing framework is. I remain to be convinced that any of the frameworks which have been published, such as fUnit, will have the longevity and portability of a pure Fortran approach. What are the chances that I will still be programming in Fortran in 10 years time -- quite high. What are the chances that I will be using a machine with a Ruby installation in 10 years time -- quite low. So my motivation for learning fUnit is low.
Yes, I write unit tests in Fortran. As to the testing of derived data types: I fail to see why you are having a problem with this. How do you unit test ? First you write down a set of inputs, and what outputs you expect your routine to produce if given those inputs. Then you execute the tests and compare what you get with what you expected to get. It's as simple as that. And the size and complexity of your derived data types doesn't alter the simplicity of the unit testing task, though it may make it more laborious without tool support.
If you are testing large data structures and have to read and write large volumes of data a tool such as Matlab becomes very useful -- you can very easily generate input data sets and compare actual and expected outputs both numerically and graphically.
精彩评论