-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionTest.java
More file actions
74 lines (67 loc) · 2.9 KB
/
RecursionTest.java
File metadata and controls
74 lines (67 loc) · 2.9 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Brianna Penkala
* This is the testing class for Recursion
*/
class RecursionTest {
/** A field that initializes an instance of the recursion class */
Recursion recursion = new Recursion();
/** A field that initializes another instance of the recursion class */
Recursion recursionTwo = new Recursion();
/** A field that initializes a linked list */
LinkedList list = new LinkedList();
/** A method that sets up a LinkedList to be used in the testing methods* */
@BeforeEach
public void setUp() {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
}
/** This test checks the sumDigits method with positive, 0, and negative numbers*/
@Test
void sumDigits() {
assertEquals(5050, recursion.sumDigits(100));
assertEquals(10, recursion.sumDigits(4));
assertEquals(0, recursion.sumDigits(0));
assertThrows(IllegalArgumentException.class, () -> recursion.sumDigits(-1));
}
/** This test checks the gcd method with whole, 0, and a zero in the denominator*/
@Test
void gcd() {
assertEquals(6, recursion.gcd(270, 192));
assertEquals(10, recursion.gcd(0, 10));
assertEquals(2, recursion.gcd(2, 4));
assertThrows(ArithmeticException.class, () -> recursion.gcd(5, 0));
}
/** This test checks the isPalindrome method with long, short, and one letter words*/
@Test
void isPalindrome() {
assertTrue(recursion.isPalindrome("mom"));
assertFalse(recursion.isPalindrome("apple"));
assertTrue(recursion.isPalindrome("a"));
assertTrue(recursion.isPalindrome("neveroddoreven"));
}
/** This test checks the swapNodesInPairs method with large, medium, and small numbers of values*/
@Test
void swapNodesInPairs() {
Node firstNode = list.getFirstNode(); //these tests show the results of the tests when the method returns a LinkedList- easier to see that it works
Node test = recursion.swapNodesInPairs(firstNode); //note this method is only meant to work with at least 3 elements, cannot swap otherwise
assertEquals("2, 1, 4, 3, 5", LinkedList.toString(test));
LinkedList otherList = list.sublist(1, 3);
Node secondNode = otherList.getFirstNode();
Node test2 = recursionTwo.swapNodesInPairs(secondNode);
assertEquals("2, 1, 3", LinkedList.toString(test2));
}
/** This test checks the binomial method with whole, 0, negative values*/
@Test
void binomial() {
assertEquals(5006386, recursion.binomial(59, 54));
assertEquals(35, recursion.binomial(7, 3));
assertEquals(1, recursion.binomial(0,0)); //0! = 1
assertThrows(IllegalArgumentException.class, () -> recursion.binomial(-1, -1));
}
}