Skip to content

Instantly share code, notes, and snippets.

@AnasAboreeda
Last active January 17, 2022 15:08
Show Gist options
  • Save AnasAboreeda/c40fda9b7c6ac651819eb76c6dc5f66f to your computer and use it in GitHub Desktop.
Save AnasAboreeda/c40fda9b7c6ac651819eb76c6dc5f66f to your computer and use it in GitHub Desktop.
Testing Fibonacci Number modulo M
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
class FibonacciHugeTest {
@Test
void getPisanoPeriod() {
assertEquals(8, FibonacciHuge.getPisanoPeriod(3));
}
@Test
void getFibonacciHugeFastTest() {
assertEquals(44, FibonacciHuge.getFibonacciHugeFast(438, 900));
}
@Test
void getFibonacciHugeFastTest2() {
assertEquals(185, FibonacciHuge.getFibonacciHugeFast(1548276540, 235));
}
@Test
void getFibonacciHugeFastTest3() {
assertEquals(15075, FibonacciHuge.getFibonacciHugeFast(100, 100000));
}
@Test
void getFibonacciHugeFastTest4() {
assertEquals(11963, FibonacciHuge.getFibonacciHugeFast(281621358815590L, 30524));
}
@Test
void stressTest() {
for (int i = 0; i < 1000; i++) {
long randNo1 = CustomTestGenerator.getRandomInt(1, 10000);
long randNo2 = CustomTestGenerator.getRandomInt(2, 10000);
System.out.print(randNo1 + ", " + randNo2);
long startTimeFast = System.nanoTime();
long fastResult = FibonacciHuge.getFibonacciHugeFast(randNo1, randNo2);
long stopTimeFast = System.nanoTime();
long elapsedTimeFast = stopTimeFast - startTimeFast;
long elapsedFastSec = TimeUnit.SECONDS.convert(elapsedTimeFast, TimeUnit.NANOSECONDS);
System.out.print(" -> " + elapsedFastSec + " s : " + fastResult);
long startTimeNaive = System.nanoTime();
long slowResult = FibonacciHuge.getFibonacciHugeNaive(randNo1, randNo2);
long stopTimeNaive = System.nanoTime();
long elapsedTimeNaive = stopTimeNaive - startTimeNaive;
long elapsedNaiveSec = TimeUnit.SECONDS.convert(elapsedTimeNaive, TimeUnit.NANOSECONDS);
System.out.print(" === " + slowResult + " : " + elapsedNaiveSec + " s");
System.out.println();
assertEquals(slowResult, fastResult);
assertTrue(elapsedFastSec < 2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment