我是编程新手。我写这个程序,找到GCD一个JUnit测试,如下所示:如何为此代码编写适当的JUnit测试?
public class CoprimeNumbersTest {
/**
* Given two integers, this returns true if they are relatively prime and false if they are not. Based upon the first
* webpage I found ({@link "https://primes.utm.edu/notes/faq/negative_primes.html"}), the primality of negative
* numbers is up for debate. This method will not treat negatives differently.
*
* @param a First integer to be tested
* @param b Second integer to be tested
* @return True when the greatest common divisor of these numbers is 1; false otherwise.
*/
public boolean isCoprime(int a, int b) {
// Continue using Euclid's algorithm until we find a common divisor
while (b != 0) {
// Remember b's value
int temp = b;
// Set b to the remainder of dividing a by b (e.g., a mod b).
b = a % b;
// Set a equal to b's old value.
a = temp;
}
// The gcd is the value in a. If this is 1 the numbers are coprime.
if (a == 1) {
return true;
}
// When they are not 1, they have a common divisor.
else {
return false;
}
}
}
这是我能想出:
public class CoPrimetest {
@Test
public void testing() {
assetEquals(1, GCDFinder.CoprimeNumbersTest);
}
}
有没有,我很想念,任何方法可以帮助改善我的代码?
@BrandonIbbotson'公共类CoPrimetest' VS'公共类CoprimeNumbersTest' –
@BrandonIbbotson,似乎他们并不在同一个班 – nullpointer
@丹 - 什么是'GCDFinder',其实'assetEquals(1,GCDFinder.CoprimeNumbersTest); '对我没有意义。另外它的假设是'assertEquals'(r) – nullpointer