我如何junit测试布尔插入(int索引,对象更新)方法.. [英] How do I junit test for the boolean insert(int index, object update) method pls..

查看:72
本文介绍了我如何junit测试布尔插入(int索引,对象更新)方法..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boolean insert(int index,Object update) - 在索引处添加一个对象,并在插入的对象后面的索引处移动项目。如果索引<返回false 0 || index>插入完成前的长度()。



我尝试过:



public boolean insert(int index,Object update)

{

//如果index为< = length

,则返回true (index = 0; index< length; index ++)

{

if(index< 0 || index> length())

{

返回false;

}

}

返回true;

}

boolean insert(int index, Object update) - add an object at index and move items at the index and beyond after the inserted object. Return false if index < 0 || index > length() before the insert is complete.

What I have tried:

public boolean insert(int index, Object update)
{
//returns true if index is <= length
for(index = 0; index < length; index++)
{
if (index < 0 || index > length())
{
return false;
}
}
return true;
}

推荐答案

首先应该定义插入函数所期望的确切行为。据我们所知,它不会向任何东西插入任何内容。



您只是创建一个从0到(length - 1)的索引变量。 br />
然后你测试索引是否在0和长度(包括)之间。

索引永远不会小于零(因为它的第一个初始化值为零)。 />
索引永远不会大于length(循环将在此之前退出)。



所以,你必须清楚地定义和理解行为你需要的;然后在纸上写下你需要的不同步骤。最后,使用你写下的内容来编写函数代码并进行测试。
You should first define the exact behaviour you are expecting from your insert function. As far as we can see, it does not insert anything to anything.

You are just creating an index variable that goes from 0 to (length - 1).
Then you test for index if it is between 0 and length (inclusive).
index will never be less than zero (because its first initialization value is zero).
index will never be greater than length (the loop will exit before that happens).

So, you have to clearly define and understand the behaviour that you need; then write down on a paper the different steps that you will need. And finally, use what you wrote down to code your function and test it.


这篇关于我如何junit测试布尔插入(int索引,对象更新)方法..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆