forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadraticEquationSolverTest.java
More file actions
50 lines (42 loc) · 1.68 KB
/
QuadraticEquationSolverTest.java
File metadata and controls
50 lines (42 loc) · 1.68 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
package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class QuadraticEquationSolverTest {
private final QuadraticEquationSolver quadraticEquationSolver = new QuadraticEquationSolver();
@Test
public void testSolveEquationRealRoots() {
// 4.2x^2 + 8x + 1.9 = 0
double a = 4.2;
double b = 8;
double c = 1.9;
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(2, roots.length, 2);
Assertions.assertEquals(-0.27810465435684306, roots[0].real);
Assertions.assertNull(roots[0].imaginary);
Assertions.assertEquals(-1.6266572504050616, roots[1].real);
Assertions.assertNull(roots[1].imaginary);
}
@Test
public void testSolveEquationEqualRoots() {
// x^2 + 2x + 1 = 0
double a = 1;
double b = 2;
double c = 1;
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(1, roots.length);
Assertions.assertEquals(-1, roots[0].real);
}
@Test
public void testSolveEquationComplexRoots() {
// 2.3x^2 + 4x + 5.6 = 0
double a = 2.3;
double b = 4;
double c = 5.6;
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(2, roots.length);
Assertions.assertEquals(-0.8695652173913044, roots[0].real);
Assertions.assertEquals(1.2956229935435948, roots[0].imaginary);
Assertions.assertEquals(-0.8695652173913044, roots[1].real);
Assertions.assertEquals(-1.2956229935435948, roots[1].imaginary);
}
}