要测试Java中round函数的准确性,你可以创建一个测试类,使用JUnit框架编写测试用例
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
RoundFunctionTest
的测试类:import org.junit.Assert; import org.junit.Test; public class RoundFunctionTest { // 在这里编写测试用例 }
@Test public void testPositiveNumber() { double input = 12.5; long expectedOutput = 13; long actualOutput = Math.round(input); Assert.assertEquals(expectedOutput, actualOutput); } @Test public void testNegativeNumber() { double input = -12.5; long expectedOutput = -12; long actualOutput = Math.round(input); Assert.assertEquals(expectedOutput, actualOutput); } @Test public void testZero() { double input = 0; long expectedOutput = 0; long actualOutput = Math.round(input); Assert.assertEquals(expectedOutput, actualOutput); }
注意:这些测试用例仅覆盖了基本场景。你可以根据需要添加更多测试用例,以确保round函数在各种情况下都能正常工作。