为什么我的测试 Java 方法只返回了列表中的第一个键?

huangapple go评论74阅读模式
英文:

Why is my test Java method only returning the first key in my list?

问题

public Key equalorLess(Key key) {
    if (first == null || first.key.compareTo(key) > 0) return null;
    Node x = first;
    while (x.next != null && x.next.key.compareTo(key) <= 0) {
        x = x.next;
    }
    return x.key;
}

这段代码用于返回表中最大的键该键小于或等于输入的键所以在字符串 "abcd"如果你调用 "e"你会得到 "d"或者如果你调用 "a"你会得到 "a"

我正在编写类似以下的测试代码注意我知道这有些混乱

public static void equalorLessTest(String key, String answer) {
    String keys = "abcdefijklxyz";
    LinkedList<String, Integer> newLst = new LinkedList<String, Integer>();
    for (int i = 0; i < keys.length(); i++) {
        newLst.put(keys.substring(i, i+1), i);
    }
    String result = newLst.equalorLessTest(key);
    if (answer == null) {
        if (result == null)
            StdOut.format("equalorLessTest: 正确  字符串 %s 预期结果: null\n", key, result);
        else
            StdOut.format("equalorLessTest: *错误*  字符串 %s 实际结果: %s\n", key, result);
        return;
    }
    if (result == null && answer != null) {
        StdOut.format("equalorLess: *错误*  字符串 %s 实际结果: null\n", key, result);
        return;
    }
    if (result.compareTo(answer) == 0)
        StdOut.format("equalorLess: 正确  字符串 %s 实际结果: %s\n", key, result);
    else
        StdOut.format("equalorLess: *错误*  字符串 %s 实际结果: %s\n", key, result);
}

请注意,这段翻译仅包括你提供的代码部分,不包含任何其他内容。

英文:
    public Key equalorLess(Key key) {
if (first == null || first.key.compareTo(key) &gt; 0) return null;
Node x = first;
while(x.next != null &amp;&amp; x.next.key.compareTo(key) &lt;= 0) {
x = x.next;
}
return x.key;
}  

This is code to return a the largest key in a table that is < or = to inputted key. So in "abcd" if you call "e" you'll return "d". Or if you called "a" you'd get "a".

I'm writing test code that looks like the following. Precursor, I know its a mess.

		public static void equalorLessTest( String key, String answer) {
String keys = &quot;abcdefijklxyz&quot;;
LinkedList&lt;String,Integer&gt; newLst= new LinkedList&lt;String,Integer&gt;();
for (int i =0; i &lt; keys.length(); i++) {
newLst.put(keys.substring(i, i+1),i);
}
String result = newLst.equalorLessTest(key);
if ( answer == null) {
if (result == null)
StdOut.format(&quot;equalorLessTest: Correct  String %s Answer: null\n&quot;, key,result);
else
StdOut.format(&quot;equalorLessTest: *Error*  String %s Actual: %s\n&quot;, key,result);
return;
}
if (result == null &amp;&amp; answer != null ) { 
StdOut.format(&quot;equalorLess: *Error*  String %s Actual: null\n&quot;, key,result);
return;
}
if ( result.compareTo(answer) ==0)  
StdOut.format(&quot;equalorLess: Correct  String %s Actual: %s\n&quot;, key,result);
else
StdOut.format(&quot;equalorLess: *Error*  String %s Actual: %s\n&quot;, key,result);
}

The only test that works is checking for null. I'm iterating over that test set of keys. So, if I call "b" as my input, I get "a". Every single one I try to test returns "a". And in other test cases, I tend to get nulls or the first key in the given list I create. Can somebody please look at my test method and tell me what's going on. I believe the actual function is right. And it needs to be done in this manner. I can't use alternate methods.

答案1

得分: 1

两个if分支是相同的:

if (temp != null && x.key.compareTo(temp) >= 0)
    temp = x.key;
else
    temp = x.key;

这肯定是个错误。

英文:

Both branches of the if are the same:

if(temp!= null &amp;&amp; x.key.compareTo(temp) &gt;= 0) 
temp = x.key;
else
temp = x.key;

That’s got to be a bug.

huangapple
  • 本文由 发表于 2020年9月27日 07:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定