Skip to content

Commit 0caefda

Browse files
committed
test(data-structures): implemented the tests for all the data structures
All the implementation for all the tests has now been added to the repository.
1 parent f82ea6f commit 0caefda

16 files changed

+2818
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package es.uniovi.algorithms.sort;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class BubbleSortTest {
8+
9+
private Integer[] intArr = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
10+
sortIntArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
11+
12+
private String[] strArr = { "d", "c", "b", "a" },
13+
sortStrArr = { "a", "b", "c", "d" };
14+
15+
private Double[] dblArr = { 1.9, 2.6, Double.MAX_VALUE, 0.00004, Double.MIN_VALUE },
16+
sortDblArr = { Double.MIN_VALUE, 0.00004, 1.9, 2.6, Double.MAX_VALUE };
17+
18+
@Test public void integerSortTest() {
19+
BubbleSort.sort( intArr );
20+
assertArrayEquals( sortIntArr, intArr );
21+
}
22+
23+
@Test public void stringSortTest() {
24+
BubbleSort.sort( strArr );
25+
assertArrayEquals( sortStrArr, strArr );
26+
}
27+
28+
@Test public void doubleSortTest() {
29+
BubbleSort.sort( dblArr );
30+
assertArrayEquals( sortDblArr, dblArr );
31+
}
32+
33+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package es.uniovi.data_structures.avl_tree;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class Carlos_EvalTest {
8+
9+
@Test public void testGetHeightCarlos() {
10+
11+
AVLTree<Integer> ti = new AVLTree<Integer>();
12+
13+
ti.add( 10 );
14+
ti.add( 6 );
15+
ti.add( 15 );
16+
ti.add( 3 );
17+
ti.add( 9 );
18+
ti.add( 14 );
19+
ti.add( 20 );
20+
ti.add( 2 );
21+
ti.add( 4 );
22+
ti.add( 7 );
23+
ti.add( 12 );
24+
ti.add( 1 );
25+
26+
assertEquals( "10(-1)6(-1)3(-1)2(-1)1(0)---4(0)--9(-1)7(0)---15(-1)14(-1)12(0)---20(0)--",
27+
ti.toString() );
28+
assertEquals( 4, ti.getHeight() );
29+
30+
// 20, 4, 10, 9, 6, 3.
31+
32+
ti.remove( 20 );
33+
34+
assertEquals( "6(0)3(-1)2(-1)1(0)---4(0)--10(0)9(-1)7(0)---14(0)12(0)--15(0)--",
35+
ti.toString() );
36+
assertEquals( 3, ti.getHeight() );
37+
38+
ti.remove( 4 );
39+
40+
assertEquals( "6(1)2(0)1(0)--3(0)--10(0)9(-1)7(0)---14(0)12(0)--15(0)--", ti.toString() );
41+
assertEquals( 3, ti.getHeight() );
42+
43+
ti.remove( 10 );
44+
45+
assertEquals( "6(1)2(0)1(0)--3(0)--9(1)7(0)--14(0)12(0)--15(0)--", ti.toString() );
46+
assertEquals( 3, ti.getHeight() );
47+
48+
ti.remove( 9 );
49+
50+
assertEquals( "6(1)2(0)1(0)--3(0)--14(-1)7(1)-12(0)--15(0)--", ti.toString() );
51+
assertEquals( 3, ti.getHeight() );
52+
53+
ti.remove( 6 );
54+
55+
assertEquals( "3(1)2(-1)1(0)---14(-1)7(1)-12(0)--15(0)--", ti.toString() );
56+
assertEquals( 3, ti.getHeight() );
57+
58+
ti.remove( 3 );
59+
60+
assertEquals( "7(0)2(-1)1(0)---14(0)12(0)--15(0)--", ti.toString() );
61+
assertEquals( 2, ti.getHeight() );
62+
}
63+
64+
@Test public void testSingleRotation() {
65+
// Single right rotations
66+
AVLTree<Character> a = new AVLTree<Character>();
67+
a.add( 'a' );
68+
a.add( 'b' );
69+
a.add( 'd' );
70+
assertEquals( "b(0)a(0)--d(0)--", a.toString() );
71+
72+
a.add( 'c' );
73+
a.add( 'e' );
74+
a.add( 'f' );
75+
assertEquals( "d(0)b(0)a(0)--c(0)--e(1)-f(0)--", a.toString() );
76+
77+
// Single left rotations
78+
AVLTree<Character> b = new AVLTree<Character>();
79+
b.add( 'f' );
80+
b.add( 'e' );
81+
b.add( 'c' );
82+
assertEquals( "e(0)c(0)--f(0)--", b.toString() );
83+
84+
b.add( 'd' );
85+
b.add( 'b' );
86+
b.add( 'a' );
87+
assertEquals( "c(0)b(-1)a(0)---e(0)d(0)--f(0)--", b.toString() );
88+
}
89+
90+
@Test public void testDoubleRotation() {
91+
// Double left rotation
92+
AVLTree<Character> a = new AVLTree<Character>();
93+
a.add( 'e' );
94+
a.add( 'g' );
95+
a.add( 'b' );
96+
a.add( 'd' );
97+
a.add( 'c' );
98+
assertEquals( "e(-1)c(0)b(0)--d(0)--g(0)--", a.toString() );
99+
100+
// Double right rotation
101+
AVLTree<Character> b = new AVLTree<Character>();
102+
b.add( 'd' );
103+
b.add( 'b' );
104+
b.add( 'g' );
105+
b.add( 'e' );
106+
b.add( 'f' );
107+
assertEquals( "d(1)b(0)--f(0)e(0)--g(0)--", b.toString() );
108+
}
109+
110+
@Test public void testRemoval() {
111+
// Double left rotation
112+
AVLTree<Integer> a = new AVLTree<Integer>();
113+
a.add( 10 );
114+
a.add( 2 );
115+
a.add( 35 );
116+
a.add( 1 );
117+
a.add( 8 );
118+
a.add( 20 );
119+
a.add( 40 );
120+
a.add( 5 );
121+
a.add( 15 );
122+
a.add( 25 );
123+
a.add( 50 );
124+
a.add( 30 );
125+
assertEquals( "10(1)2(1)1(0)--8(-1)5(0)---35(-1)20(1)15(0)--25(1)-30(0)--40(1)-50(0)--",
126+
a.toString() );
127+
128+
a.remove( 2 );
129+
assertEquals( "20(0)10(-1)5(0)1(0)--8(0)--15(0)--35(0)25(1)-30(0)--40(1)-50(0)--",
130+
a.toString() );
131+
132+
a.remove( 20 );
133+
assertEquals( "15(0)5(1)1(0)--10(-1)8(0)---35(0)25(1)-30(0)--40(1)-50(0)--", a.toString() );
134+
135+
a.remove( 5 );
136+
assertEquals( "15(1)8(0)1(0)--10(0)--35(0)25(1)-30(0)--40(1)-50(0)--", a.toString() );
137+
}
138+
139+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package es.uniovi.data_structures.avl_tree;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class Martin_Eval2Test {
8+
9+
// CAUTION
10+
// Use this call at the end of the remove and add methods in the AVL class
11+
// theRoot.updateHeight();
12+
// return (theRoot);
13+
//
14+
// Normal use:
15+
// return (updateBF (theRoot));
16+
//
17+
// Also use this statement in the AVLNode
18+
// getElement().toString() + ("+getHeight()+")";
19+
// ... instead of...
20+
// getElement().toString() + ("+getBF()+")";
21+
22+
@Test public void test_A() {
23+
24+
AVLTree<Character> charTree = new AVLTree<Character>();
25+
charTree.add( 'a' );
26+
charTree.add( 'b' );
27+
charTree.add( 'c' );
28+
29+
assertEquals( "b(0)a(0)--c(0)--", charTree.toString() );
30+
31+
charTree.add( 'd' );
32+
assertEquals( "b(1)a(0)--c(1)-d(0)--", charTree.toString() );
33+
34+
}
35+
36+
@Test public void test_B() {
37+
38+
// Example
39+
AVLTree<Character> a = new AVLTree<Character>();
40+
a.add( 'b' );
41+
a.add( 'a' );
42+
a.add( 'd' );
43+
a.add( 'c' );
44+
a.add( 'g' );
45+
a.add( 'i' );
46+
a.add( 'h' );
47+
assertEquals( "d(0)b(0)a(0)--c(0)--h(0)g(0)--i(0)--", a.toString() );
48+
49+
// Scenery III
50+
a.remove( 'd' );
51+
assertEquals( "c(0)b(-1)a(0)---h(0)g(0)--i(0)--", a.toString() );
52+
53+
// Scenery II
54+
a.remove( 'g' );
55+
assertEquals( "c(0)b(-1)a(0)---h(1)-i(0)--", a.toString() );
56+
57+
// Scenery I
58+
a.remove( 'a' );
59+
assertEquals( "c(1)b(0)--h(1)-i(0)--", a.toString() );
60+
}
61+
62+
@Test public void test_C_Joins() throws CloneNotSupportedException {
63+
64+
// Example
65+
AVLTree<Integer> a = new AVLTree<Integer>();
66+
a.add( 5 );
67+
a.add( 3 );
68+
a.add( 1 );
69+
a.add( 2 );
70+
a.add( 7 );
71+
assertEquals( "3(0)1(1)-2(0)--5(1)-7(0)--", a.toString() );
72+
73+
AVLTree<Integer> b = new AVLTree<Integer>();
74+
b.add( 7 );
75+
b.add( 6 );
76+
b.add( 8 );
77+
assertEquals( "7(0)6(0)--8(0)--", b.toString() );
78+
79+
assertEquals( "2(1)1(0)--5(0)3(0)--7(0)--", a.join( a ).toString() );
80+
assertEquals( "7(0)6(0)--8(0)--", b.join( b ).toString() );
81+
82+
// At least one must work (ask the student to explain his/her algorithm)
83+
// Depends on the traverse strategy and which tree is used as the pivot
84+
assertEquals( "5(0)2(0)1(0)--3(0)--7(0)6(0)--8(0)--", a.join( b ).toString() );
85+
assertEquals( "3(1)1(1)-2(0)--7(-1)6(-1)5(0)---8(0)--", b.join( a ).toString() );
86+
}
87+
88+
@Test public void test_D_Sample() {
89+
90+
AVLTree<Character> a = new AVLTree<Character>();
91+
a.add( 'b' );
92+
a.add( 'a' );
93+
a.add( 'd' );
94+
assertEquals( "b(0)a(0)--d(0)--", a.toString() );
95+
96+
AVLTree<Character> b = new AVLTree<Character>();
97+
b.add( 'c' );
98+
b.add( 'g' );
99+
b.add( 'i' );
100+
b.add( 'd' );
101+
assertEquals( "g(-1)c(1)-d(0)--i(0)--", b.toString() );
102+
assertEquals( "d(0)b(0)a(0)--c(0)--g(1)-i(0)--", a.join( b ).toString() );
103+
}
104+
105+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package es.uniovi.data_structures.avl_tree;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class Martin_EvalTest {
8+
9+
@Test public void addTest() {
10+
// Example.
11+
AVLTree<Character> a = new AVLTree<Character>();
12+
a.add( 'b' );
13+
assertEquals( "b(0)--", a.toString() );
14+
a.add( 'a' );
15+
assertEquals( "b(-1)a(0)---", a.toString() );
16+
a.add( 'd' );
17+
assertEquals( "b(0)a(0)--d(0)--", a.toString() );
18+
a.add( 'c' );
19+
assertEquals( "b(1)a(0)--d(-1)c(0)---", a.toString() );
20+
a.add( 'g' );
21+
assertEquals( "b(1)a(0)--d(0)c(0)--g(0)--", a.toString() );
22+
a.add( 'i' );
23+
assertEquals( "d(0)b(0)a(0)--c(0)--g(1)-i(0)--", a.toString() );
24+
a.add( 'h' );
25+
assertEquals( "d(0)b(0)a(0)--c(0)--h(0)g(0)--i(0)--", a.toString() );
26+
27+
}
28+
29+
@Test public void searchTest() {
30+
// Example
31+
AVLTree<Character> a = new AVLTree<Character>();
32+
a.add( 'b' );
33+
a.add( 'a' );
34+
a.add( 'd' );
35+
a.add( 'c' );
36+
a.add( 'g' );
37+
a.add( 'i' );
38+
a.add( 'h' );
39+
assertEquals( true, a.search( 'i' ) );
40+
assertEquals( false, a.search( 'f' ) );
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)