繁体   English   中英

在 JUnit 中测试 CharSequences 的相等性

[英]Test CharSequences for equality in JUnit

以下测试成功运行:

assertEquals(a.toString(), b.toString());

以下不会:

assertEquals(a, b);

aStringBuilder ,而b是不同类型的CharSequence

有什么方法可以测试CharSequence的相等性而不先将它们转换为String吗?

您可以使用CharBuffer s:

assertEquals(CharBuffer.wrap(a), CharBuffer.wrap(b));

CharBuffer的 .equals 的 javadoc 保证了这一点:

Tells whether or not this buffer is equal to another object.

Two char buffers are equal if, and only if,

    They have the same element type,

    They have the same number of remaining elements, and

    The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM