-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomialTestOne.java
More file actions
executable file
·54 lines (47 loc) · 1.65 KB
/
Copy pathPolynomialTestOne.java
File metadata and controls
executable file
·54 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.util.zip.CRC32;
public class PolynomialTestOne {
@Test public void testPolynomial() {
int[] c1 = {5, 0, -2, 3};
Polynomial p1 = new Polynomial(c1);
assertEquals(3, p1.getDegree());
assertEquals(-2, p1.getCoefficient(2));
assertEquals(5, p1.evaluate(0));
assertEquals(21, p1.evaluate(2));
c1[3] = 5555;
assertEquals(3, p1.getCoefficient(3));
int[] c2 = {42, 99, -3, -10, -4};
Polynomial p2 = new Polynomial(c2);
assertEquals(4, p2.getDegree());
assertEquals(99, p2.getCoefficient(1));
int[] c3 = {99, 0, 0, 0, 0, 0, 0, 0};
Polynomial p3 = new Polynomial(c3);
assertEquals(0, p3.getDegree());
assertEquals(99, p3.getCoefficient(0));
}
private static final int SEED = 12345;
private static final int TRIALS = 1000000;
@Test public void massTest() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < TRIALS; i++) {
int deg = rng.nextInt(10);
int[] c = new int[deg + 1];
for(int j = 0; j < deg + 1; j++) {
c[j] = rng.nextInt(20) - 10;
}
Polynomial p = new Polynomial(c);
check.update(p.getDegree());
for(int j = -5; j <= 5; j++) {
check.update((int) p.evaluate(j));
}
}
assertEquals(427606002L, check.getValue());
}
}