gpt4 book ai didi

java - assertTrue() 不输入内部函数参数

转载 作者:行者123 更新时间:2023-12-02 09:05:44 26 4
gpt4 key购买 nike

所以我用这些变量创建了一个 Trie 类。

Node rootNode = new Node();
int nodeCount = 1;
int wordCount = 0;
static StringBuilder indexList1 = new StringBuilder();
static StringBuilder indexList2 = new StringBuilder();

我目前正在尝试为 trie 构建一个 equals 函数,我这样做的方式是遍历 trie 并将索引连接到静态字符串生成器中。当这两个对象都完成后,我会比较字符串。以下是 equals 函数的代码:

public void equalsHelper(Node n1, int List) {
if (List == 1) {
for (int i = 0; i < 26; i++) {
if(n1.nodes[i] != null) {
indexList1.append(i);
indexList1.append(n1.nodes[i].frequency);
equalsHelper(n1.nodes[i], List);
}
}
}
else if (List == 2) {
for (int i = 0; i < 26; i++) {
if(n1.nodes[i] != null) {
indexList2.append(i);
indexList2.append(n1.nodes[i].frequency);
equalsHelper(n1.nodes[i], List);
}
}
}
}

public boolean equals(Trie trie2) {
System.out.println("I was entered");
if (this.nodeCount == 1 && trie2.nodeCount == 1) return true;
if (indexList1.length() > 0) indexList1.setLength(0);
if (indexList2.length() > 0) indexList2.setLength(0);
if (this.wordCount != trie2.wordCount || this.nodeCount != trie2.nodeCount) return false;
equalsHelper(this.rootNode, 1);
equalsHelper(trie2.rootNode, 2);
return indexList1.toString() == indexList2.toString();
}

所以问题来了,当我通过这个测试用例运行它时,它甚至似乎没有输入函数等于

add("cares");
add("caress");
add("baboon");
studentTrie.add("car");
studentTrie2.add("car");
assertTrue(studentTrie.equals(studentTrie2), "Equal Trie objects found unequal");

它所做的只是立即返回 false。当我尝试单步执行调试器时,它甚至似乎没有进入 equals 调用,但在应该返回 true 时却以某种方式返回 false。这是它向我显示的错误。

org.opentest4j.AssertionFailedError: Equal Trie objects found unequal ==> expected: <true> but was: <false>

at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:40)
at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:42)
at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:160)
at passoff.TrieTest.testEquals(TrieTest.java:93)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:513)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:170)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:166)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:113)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:113)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

我想知道是否有关于assertFalse/assertTrue 函数我不知道的事情。无论如何,任何帮助将不胜感激。

最佳答案

字符串应该与 String#equals 进行比较,而不是 ==

return indexList1.toString() == indexList2.toString();

关于java - assertTrue() 不输入内部函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com