Ich bin neu in der Programmierung. Ich habe einen JUnit-Test für dieses Programm zu schreiben, das GCD zu finden, die hier gezeigt:Wie kann ich einen geeigneten JUnit Test für diesen Code schreiben?
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;
}
}
}
Dies ist, was ich tun konnte:
public class CoPrimetest {
@Test
public void testing() {
assetEquals(1, GCDFinder.CoprimeNumbersTest);
}
}
es irgendwelche Ansätze, die ich vermisst, dass kann helfen, meinen Code zu verbessern?
@BrandonIbbotson 'public class CoPrimetest' vs' public class CoprimeNumbersTest' –
@BrandonIbbotson und es scheint, dass sie nicht in der gleichen Klasse – nullpointer
@ Dan sind - Was 'GCDFinder', eigentlich' assetEquals (1, GCDFinder.CoprimeNumbersTest); 'ergibt keinen Sinn für mich. Auch sein angenommen zu sein 'assertEquals' (r) – nullpointer