Я новичок в программировании. Я должен написать тест 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' общественности против' общественного класса CoprimeNumbersTest' –
@BrandonIbbotson и кажется, что они не находятся в том же классе – nullpointer
@Dan - Что такое 'GCDFinder', на самом деле' assetEquals (1, GCDFinder.CoprimeNumbersTest); 'не имеет смысла для меня. Также предположим, что это 'assertEquals' (r) – nullpointer