java - How to correct my unit test for GCD function -


i have written simple gcd function implement euclid's algorithm computing greatest common divisor gcd(m, n), largest integer k dividing both m , n.

the function wrote compiles:

   public static int gcd(int m, int n) {       if (n == 0) return m;       return gcd(n, m%n);    } 

however, run error when write unit test on gcd:

   @test public void gcdtest() {       (int m = 0; m < 15; m++) {       (int n = 0; n < 15; n++) {          assertequals("divide m,n", m/n%m, recursion.gcd(m,n));          }       }    } 

the error comes in 'assertequals' line. unsure if maybe calculating method incorrectly writing m / n % m.

any tips or suggestions? in advance.

besides "math" thing here - using loops within unit tests should not immediately.

what mean is: before think testcases iterate , multiple asserts within loop, things like

@test public void gcdtest1_1() {    assertthat(recursion.gcd(1,1), is(1)); } 

in other words: write simple testcases test one thing only. , when first 1 passes, write next one. , then, when more confident, consider such looping solution.

as might have given idea dividing 0 ... not being thing have in tests!

edit upon comment: use see, core idea of unit tests find , fix bugs in code under test. looking @ example, 1 big obstacle there ... printing string "m, n". doesn't tell anything. know variables called m , n. better print values of m , n in case assert fails.

finally: changed assertthat; other style of assert, find lead "more readable" code. when using that, have use hamcrest matchers such is() though (google friend here).


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -