使用数据结构进行单元测试

发布于 2024-09-19 22:18:14 字数 703 浏览 6 评论 0原文

我想在工作系统上进行一些单元测试和一些功能测试。然而,我有一个数据结构,主要由一个对象中的几个数组组成(好吧,它是 fortran,所以不是一个真实的对象,但你明白了。)对数据结构进行单元测试的示例会是什么样子?到目前为止我看到的所有示例都返回一个值,然后对其进行测试。我如何使用由数组组成的数据结构来做到这一点?

如果我能在 funit 中找到一个例子,那就太好了。这是我的数据结构的一个例子:

type :: myownobject
     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

我知道这可能是一个简单的问题,但我真的刚刚开始进行单元测试。我正在阅读 Roy Osherove 的《单元测试的艺术》。

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
     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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-09-26 22:18:14

对数据结构进行单元测试,或者用更 Fortranic 的术语来说,对派生数据类型进行单元测试,似乎是一个相当模糊的概念。也许您想要做的是测试派生类型变量的各种用途?

您提出问题的方式也表明您可能还没有学会如何编写返回数组的函数,或者我是否误解了您所写的内容?您还可以编写返回派生数据类型的函数。

也许您可以更明确地说明您想对您的类型做什么——这可能会建议您需要编写哪些单元测试。

编辑:以比评论允许的长度更长的长度来回答OP的评论..

像我这样的长期Fortran程序员并不真正知道单元测试框架是什么。我仍然相信任何已发布的框架(例如 fUnit)都将具有纯 Fortran 方法的寿命和可移植性。 10 年后我仍然使用 Fortran 编程的可能性有多大——相当高。 10 年后我将使用安装了 Ruby 的机器的可能性有多大——相当低。所以我学习fUnit的动力很低。

是的,我用 Fortran 编写单元测试。至于派生数据类型的测试:我不明白为什么你会遇到这个问题。你如何进行单元测试?首先,写下一组输入,以及如果给定这些输入,您希望例程产生什么输出。然后执行测试并将所得结果与预期结果进行比较。就这么简单。派生数据类型的大小和复杂性不会改变单元测试任务的简单性,尽管如果没有工具支持,它可能会变得更加费力。

如果您正在测试大型数据结构并且必须读取和写入大量数据,Matlab 等工具就变得非常有用 - 您可以非常轻松地生成输入数据集并以数字和图形方式比较实际输出和预期输出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文