diff --git a/.vscode/settings.json b/.vscode/settings.json index e112a70..5055dd9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,4 +4,6 @@ "java.project.referencedLibraries": [ "lib/**/*.jar" ] + } + diff --git a/bin/App.class b/bin/App.class deleted file mode 100644 index a228130..0000000 Binary files a/bin/App.class and /dev/null differ diff --git a/bin/lab10_lists/10-Lists-FR.pdf b/bin/lab10_lists/10-Lists-FR.pdf new file mode 100644 index 0000000..e8f53a2 Binary files /dev/null and b/bin/lab10_lists/10-Lists-FR.pdf differ diff --git a/bin/lab10_lists/App.class b/bin/lab10_lists/App.class new file mode 100644 index 0000000..bcb38e3 Binary files /dev/null and b/bin/lab10_lists/App.class differ diff --git a/bin/lab10_lists/LinkedList.class b/bin/lab10_lists/LinkedList.class new file mode 100644 index 0000000..89e25c9 Binary files /dev/null and b/bin/lab10_lists/LinkedList.class differ diff --git a/bin/lab10_lists/LinkedListTest$Task2_Basic_Operations.class b/bin/lab10_lists/LinkedListTest$Task2_Basic_Operations.class new file mode 100644 index 0000000..e79bb2c Binary files /dev/null and b/bin/lab10_lists/LinkedListTest$Task2_Basic_Operations.class differ diff --git a/bin/lab10_lists/LinkedListTest$Task3_Operations.class b/bin/lab10_lists/LinkedListTest$Task3_Operations.class new file mode 100644 index 0000000..978d808 Binary files /dev/null and b/bin/lab10_lists/LinkedListTest$Task3_Operations.class differ diff --git a/bin/lab10_lists/LinkedListTest$Task4_Operations.class b/bin/lab10_lists/LinkedListTest$Task4_Operations.class new file mode 100644 index 0000000..a94518f Binary files /dev/null and b/bin/lab10_lists/LinkedListTest$Task4_Operations.class differ diff --git a/bin/lab10_lists/LinkedListTest.class b/bin/lab10_lists/LinkedListTest.class new file mode 100644 index 0000000..c30a544 Binary files /dev/null and b/bin/lab10_lists/LinkedListTest.class differ diff --git a/bin/lab10_lists/Node.class b/bin/lab10_lists/Node.class new file mode 100644 index 0000000..b5214e0 Binary files /dev/null and b/bin/lab10_lists/Node.class differ diff --git a/bin/lab11_debugging/11-debug-profiling-FR.pdf b/bin/lab11_debugging/11-debug-profiling-FR.pdf new file mode 100644 index 0000000..01bc6f9 Binary files /dev/null and b/bin/lab11_debugging/11-debug-profiling-FR.pdf differ diff --git a/bin/lab11_debugging/DynamicStructuresComparison.class b/bin/lab11_debugging/DynamicStructuresComparison.class new file mode 100644 index 0000000..c250f18 Binary files /dev/null and b/bin/lab11_debugging/DynamicStructuresComparison.class differ diff --git a/bin/lab11_debugging/Launcher.class b/bin/lab11_debugging/Launcher.class new file mode 100644 index 0000000..34d1dbf Binary files /dev/null and b/bin/lab11_debugging/Launcher.class differ diff --git a/bin/lab11_debugging/NumberAnalyzer.class b/bin/lab11_debugging/NumberAnalyzer.class new file mode 100644 index 0000000..e69930b Binary files /dev/null and b/bin/lab11_debugging/NumberAnalyzer.class differ diff --git a/bin/lab11_debugging/SampleArray.class b/bin/lab11_debugging/SampleArray.class new file mode 100644 index 0000000..255f833 Binary files /dev/null and b/bin/lab11_debugging/SampleArray.class differ diff --git a/bin/lab11_debugging/SampleLinkedList.class b/bin/lab11_debugging/SampleLinkedList.class new file mode 100644 index 0000000..7353399 Binary files /dev/null and b/bin/lab11_debugging/SampleLinkedList.class differ diff --git a/bin/lab11_debugging/SampleVector.class b/bin/lab11_debugging/SampleVector.class new file mode 100644 index 0000000..6dd4933 Binary files /dev/null and b/bin/lab11_debugging/SampleVector.class differ diff --git a/lib/junit.jar b/lib/junit.jar new file mode 100644 index 0000000..d6dc60c Binary files /dev/null and b/lib/junit.jar differ diff --git a/src/App.java b/src/App.java deleted file mode 100644 index 0a839f9..0000000 --- a/src/App.java +++ /dev/null @@ -1,5 +0,0 @@ -public class App { - public static void main(String[] args) throws Exception { - System.out.println("Hello, World!"); - } -} diff --git a/src/lab10_lists/10-Lists-FR.pdf b/src/lab10_lists/10-Lists-FR.pdf new file mode 100644 index 0000000..e8f53a2 Binary files /dev/null and b/src/lab10_lists/10-Lists-FR.pdf differ diff --git a/src/lab10_lists/App.java b/src/lab10_lists/App.java new file mode 100644 index 0000000..23ee8f4 --- /dev/null +++ b/src/lab10_lists/App.java @@ -0,0 +1,32 @@ +package lab10_lists; + +import java.util.Vector; + +public class App { + public static void main(String[] args) throws Exception { + // 1 + Vector v = new Vector<>(); + v.add("Louis"); + v.add("Paul"); + v.add("Henri"); + v.add("Pascal"); + + // 2 + System.out.println("2: " + v.toString()); + + // 3 + System.out.print("3: "); + if(v.contains("Paul")){ + System.out.println("Paul is on this list"); + } else { + System.out.println("Paul is not on this list"); + } + + // 4 + v.remove("Paul"); + + // 5 + System.out.println("5: " + v.toString()); + + } +} diff --git a/src/lab10_lists/LinkedList.java b/src/lab10_lists/LinkedList.java new file mode 100644 index 0000000..1e50cec --- /dev/null +++ b/src/lab10_lists/LinkedList.java @@ -0,0 +1,179 @@ +package lab10_lists; + +public class LinkedList { + // Task 2 + public Node head; + public Node tail; + + public LinkedList(){ + this.head = null; + } + + public void addToStart(String s){ + Node n = new Node(s, head); + head = n; + } + + public int getSize(){ + Node foo = head; + int n = 0; + while (foo != null) { + n++; + foo = foo.next; + } + return n; + } + + public String toString(){ + Node foo; + if( this.head == null){ + foo = new Node(null, null); + } else { + foo = new Node(head); + } + String s = ""; + s += "List content (size " + this.getSize() + ") : "; + for (int i = 0; i <= this.getSize(); i++) { + if (i>0) { + s += " -> "; + } + if(foo == null){ + s += "null"; + } else { + s += foo.item; + foo = foo.next; + } + } + + return s; + } + + + // Task 3 + + public void removeFirstElement(){ + if(head == null) return; + Node foo = new Node(head); + this.head = foo.next; + } + + public Node getLastElement(){ + if(head == null) return null; + Node tailNode = head; + while (tailNode.next != null) { + tailNode = tailNode.next; + } + return tailNode; + } + + public void addToEnd(String element){ + Node newNode = new Node(element); + if(head == null) { + head = newNode; + tail = newNode; + } + else { + tail.next = newNode; + tail = newNode; + } + } + + public boolean isPresent(String e){ + if(head == null) return false; + boolean present = false; + Node foo = new Node(head); + while (foo != null) { + if (foo.item.equals(e)) {present = true;} + foo = foo.next; + } + return present; + } + + + // Task 4 + + public Node findElement(String s){ + if(head == null) return null; + Node tailNode = head; + while (tailNode != null) { + if(tailNode.item.equals(s)) return tailNode; + tailNode = tailNode.next; + } + return null; + } + + public void swapElements(String e1, String e2){ + Node n1 = findElement(e1); + Node n2 = findElement(e2); + if(n1!=null && n2!=null){ + n1.item = e2; + n2.item = e1; + } + } + + public void removeLastElement(){ + if (head == null) return; + if (head.next == null) { + head = null; + return; + } + Node n = head; + while (n.next.next != null) { + n = n.next; + } + n.next = null; + } + + public void removeElement(String e){ + Node n = head; + Node prev = null; + if (n != null && n.item.equals(e)) { + head = n.next; // Changed head + return; + } + while (n != null && !n.item.equals(e)) { + prev = n; + n = n.next; + } + if (n == null) return; + prev.next = n.next; + } + + public void insertAfter(String before, String after){ + if(head == null) return; + if(before == null) return; + if(after == null) return; + Node prevNode = findElement(before); + if(prevNode == null) return; + Node newNode = new Node(after, prevNode.next); + prevNode.next = newNode; + } + + + + public static void main(String[] args) { + LinkedList flightList = new LinkedList(); + System.out.println(flightList); + flightList.addToEnd("Bonn"); + System.out.println(flightList); + flightList.addToEnd("Zurich"); + System.out.println(flightList); + flightList.addToEnd("Rome"); + System.out.println(flightList); + System.out.println(flightList); + flightList.removeFirstElement(); + System.out.println(flightList); + flightList.addToEnd("London"); + System.out.println(flightList); + flightList.swapElements("Rome", "London"); + System.out.println(flightList); + flightList.removeLastElement(); + System.out.println(flightList); + flightList.removeLastElement(); + System.out.println(flightList); + flightList.removeLastElement(); + System.out.println(flightList); + flightList.removeLastElement(); + System.out.println(flightList); + } +} diff --git a/src/lab10_lists/LinkedListTest.java b/src/lab10_lists/LinkedListTest.java new file mode 100644 index 0000000..39654cd --- /dev/null +++ b/src/lab10_lists/LinkedListTest.java @@ -0,0 +1,312 @@ +package lab10_lists; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +/** + * JUnit 5 regression tests for LinkedList laboratory + * @author Marc Pignat + * @author P.-A. Mudry + */ + +@DisplayName("In this lab, a linked list that you have implemented") +class LinkedListTest { + + @Nested + @DisplayName("Has some basic operations that must work - Task 2") + class Task2_Basic_Operations { + + @DisplayName("The constructor must create an instance correctly") + @Test + void test_constructor() { + LinkedList n = new LinkedList(); + assertEquals(null, n.head, "head should be null after calling the constructor"); + } + + @DisplayName("The size of the list should be reported correctly") + @Test + void test_getSize() { + LinkedList n = new LinkedList(); + assertEquals(0, n.getSize(), "list is empty, getSize() MUST return 0"); + n.addToStart("a"); + assertEquals(1, n.getSize(), "list of 1 getSize() MUST return 1"); + n.addToStart("b"); + assertEquals(2, n.getSize(), "list of 2 getSize() MUST return 2"); + } + + @DisplayName("The toString method should be implemented") + @Test + void test_toStringImplemented() { + LinkedList n = new LinkedList(); + String defaultToString = n.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(n)); + assertNotEquals(n.toString(), defaultToString, "toString() MUST be implemented"); + } + + @DisplayName("The toString method should report the list elements correctly") + @Test + void test_toString() { + LinkedList n = new LinkedList(); + assertTrue(n.toString().contains("0"), "toString is expected to show the list size (0)"); + + n.addToStart("Roma"); + assertTrue(n.toString().contains("Roma"), "toString is expected to show the element (here 'Roma')"); + assertTrue(n.toString().contains("1"), "toString is expected to show the list size (1)"); + + n.addToStart("Geneva"); + assertTrue(n.toString().contains("Roma"), "toString is expected to show the element (here 'Roma')"); + assertTrue(n.toString().contains("Geneva"), "toString is expected to show the element (here 'Geneva')"); + assertTrue(n.toString().indexOf("Geneva") < n.toString().indexOf("Roma"), + "toString is expected to show 'Geneva' before 'Roma'"); + assertTrue(n.toString().contains("2"), "toString is expected to show the list size (2)"); + } + } + + @Nested + @DisplayName("Has a correct implementation of insertion and removal operators - Task 3") + class Task3_Operations { + @DisplayName("Remove first element works") + @Test + void testRemoveFirstElement() { + LinkedList n = new LinkedList(); + n.addToStart("Cathy"); + n.addToStart("Bob"); + n.addToStart("Alice"); + n.removeFirstElement(); + assertEquals(2, n.getSize(), "size MUST be 2 after removing an element from a 3 element list"); + assertEquals("Bob", n.head.item, "first element must be 'Bob'"); + n.removeFirstElement(); + assertEquals(1, n.getSize(), "size MUST be 1 after removing an element from a 2 element list"); + assertEquals("Cathy", n.head.item, "first element must be 'Cathy'"); + n.removeFirstElement(); + assertEquals(0, n.getSize(), "size MUST be 0 after removing an element from a 1 element list"); + assertEquals(null, n.head, "Head must be null"); + n.removeFirstElement(); + assertEquals(0, n.getSize(), "size MUST be 0 after removing an element from a 0 element list"); + assertEquals(null, n.head, "Head must be null"); + } + + @DisplayName("Getting the last element works") + @Test + void testGetLastElement1() { + LinkedList n = new LinkedList(); + assertEquals(null, n.getLastElement(), "last element must be null"); + n.addToStart("Cathy"); + n.addToStart("Bob"); + n.addToStart("Alice"); + assertEquals("Cathy", n.getLastElement().item, "last element must be 'Cathy'"); + assertEquals(3, n.getSize(), "size MUST be 3 (getLastElement MUST not remove the element)"); + } + + @DisplayName("Adding an element at the end works") + @Test + void testAddToEnd() { + LinkedList n = new LinkedList(); + n.addToEnd("Alice"); + assertEquals("Alice", n.getLastElement().item, "last element must be 'Alice'"); + n.addToEnd("Bob"); + assertEquals("Bob", n.getLastElement().item, "last element must be 'Bob'"); + n.addToEnd("Cathy"); + assertEquals("Cathy", n.getLastElement().item, "last element must be 'Cathy'"); + } + + @DisplayName("Must be able to find elements correctly") + @Test + void testIsPresent() { + LinkedList n = new LinkedList(); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in the empty list"); + assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in the empty list"); + assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in the empty list"); + n.addToEnd("Bob"); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list"); + assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list"); + assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in this list"); + n.addToEnd("Cathy"); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list"); + assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list"); + assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list"); + n.addToStart("Alice"); + assertTrue(n.isPresent("Alice"), "'Alice' MUST be in this list"); + assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list"); + assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list"); + n.removeFirstElement(); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list"); + assertTrue(n.isPresent("Bob"), "'Bob' MUST be in this list"); + assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list"); + n.removeFirstElement(); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in this list"); + assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in this list"); + assertTrue(n.isPresent("Cathy"), "'Cathy' MUST be in this list"); + n.removeFirstElement(); + assertFalse(n.isPresent("Alice"), "'Alice' MUST not be in the empty list"); + assertFalse(n.isPresent("Bob"), "'Bob' MUST not be in the empty list"); + assertFalse(n.isPresent("Cathy"), "'Cathy' MUST not be in the empty list"); + } + } + + @Nested + @DisplayName("Has a correct implementation complex operators - Task 4") + class Task4_Operations { + @Test + @DisplayName("Can find elements") + void testFindElement() { + LinkedList n = new LinkedList(); + assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in empty list"); + n.addToEnd("Alice"); + assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be the head element"); + assertEquals(n.getLastElement(), n.findElement("Alice"), "'Alice' MUST be the end element"); + n.addToEnd("Bob"); + assertEquals(n.getLastElement(), n.findElement("Bob"), "'Bob' MUST be the end element"); + assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in the list"); + n.addToEnd("Cathy"); + assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in the list"); + assertNotEquals(null, n.findElement("Bob"), "'Bob' MUST be in the list"); + assertEquals(n.getLastElement(), n.findElement("Cathy"), "'Cathy' MUST be the end element"); + } + + @Test + @DisplayName("Can swap elements") + void testSwapElements() { + LinkedList n = new LinkedList(); + n.addToEnd("Cathy"); + n.addToEnd("Bob"); + n.addToEnd("Alice"); + n.swapElements("Alice", "Bob"); + assertEquals("Bob", n.getLastElement().item, "'Bob' MUST be the last element"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Cathy", "Alice"); + assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be the head element"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Cathy", "Bob"); + assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be the last element"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + + n.swapElements("Alice", "Dan"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Dan", "Dan"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Bob", "Dan"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Dan", "Bob"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Alice", null); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Bob", null); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Cathy", null); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements("Dan", null); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements(null, "Alice"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements(null, "Bob"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements(null, "Cathy"); + assertEquals(3, n.getSize(), "size MUST not have changed after swap"); + n.swapElements(null, "Dan"); + + n = new LinkedList(); + n.swapElements("Alice", "Bob"); + assertEquals(0, n.getSize(), "size MUST not have changed after swap"); + } + + @Test + void testRemoveLastElement() { + LinkedList n = new LinkedList(); + n.addToEnd("Alice"); + n.addToEnd("Bob"); + n.addToEnd("Cathy"); + n.removeLastElement(); + assertEquals(2, n.getSize(), "size MUST be 2"); + assertEquals(null, n.findElement("Cathy"), "'Cathy' MUST not be in this list"); + assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in this list"); + assertNotEquals(null, n.findElement("Bob"), "'Bob' MUST be in this list"); + n.removeLastElement(); + assertEquals(1, n.getSize(), "size MUST be 1"); + assertEquals(null, n.findElement("Bob"), "'Bob' MUST not be in this list"); + assertNotEquals(null, n.findElement("Alice"), "'Alice' MUST be in this list"); + n.removeLastElement(); + assertEquals(0, n.getSize(), "size MUST be 0"); + assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list"); + n.removeLastElement(); + assertEquals(0, n.getSize(), "size MUST be 0"); + n = new LinkedList(); + n.removeLastElement(); + assertEquals(0, n.getSize(), "size MUST be 0"); + } + + @Test + @DisplayName("Can remove elements") + void testRemoveElement() { + LinkedList n = new LinkedList(); + n.addToEnd("Alice"); + n.addToEnd("Bob"); + n.addToEnd("Cathy"); + n.removeElement("Alice"); + assertEquals(2, n.getSize(), "size MUST be 2"); + assertEquals(n.head, n.findElement("Bob"), "'Bob' MUST be head"); + assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list"); + assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end"); + n.addToStart("Alice"); + n.removeElement("Bob"); + assertEquals(2, n.getSize(), "size MUST be 2"); + assertEquals(null, n.findElement("Bob"), "'Bob' MUST not be in this list"); + assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end"); + n = new LinkedList(); + n.addToEnd("Alice"); + n.addToEnd("Bob"); + n.addToEnd("Cathy"); + n.removeElement("Cathy"); + assertEquals(2, n.getSize(), "size MUST be 2"); + assertEquals(null, n.findElement("Cathy"), "'Bob' MUST not be in this list"); + assertEquals("Bob", n.getLastElement().item, "'Bob' MUST be at the end"); + n.removeElement("Dan"); + n = new LinkedList(); + n.removeElement("Dan"); + n.addToEnd("Alice"); + n.removeElement("Alice"); + assertEquals(0, n.getSize(), "size MUST be 0"); + assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list"); + n.removeElement("Alice"); + assertEquals(0, n.getSize(), "size MUST be 0"); + assertEquals(null, n.findElement("Alice"), "'Alice' MUST not be in this list"); + } + + @Test + @DisplayName("Can insert an element after another") + void testInsertAfter() { + LinkedList n = new LinkedList(); + n.addToEnd("Alice"); + n.insertAfter("Alice", "Cathy"); + assertEquals(2, n.getSize(), "size MUST be 2"); + assertEquals(n.head, n.findElement("Alice"), "'Alice' MUST be head"); + assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end"); + + n.insertAfter("Alice", "Bob"); + assertEquals(3, n.getSize(), "size MUST be 3"); + assertEquals("Cathy", n.getLastElement().item, "'Cathy' MUST be at the end"); + + n.insertAfter("Cathy", "Dan"); + assertEquals(4, n.getSize(), "size MUST be 4"); + assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end"); + + n.insertAfter("Erin", "Frank"); + assertEquals(4, n.getSize(), "size MUST be 4"); + assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end"); + assertFalse(n.isPresent("Erin"), "'Erin' MUST not be in the list"); + assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list"); + + n.insertAfter("Frank", null); + assertEquals(4, n.getSize(), "size MUST be 4"); + assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end"); + assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list"); + + n.insertAfter(null, "Frank"); + assertEquals(4, n.getSize(), "size MUST be 4"); + assertEquals("Dan", n.getLastElement().item, "'Dan' MUST be at the end"); + assertFalse(n.isPresent("Frank"), "'Frank' MUST not be in the list"); + } + } +} \ No newline at end of file diff --git a/src/lab10_lists/Node.java b/src/lab10_lists/Node.java new file mode 100644 index 0000000..becd195 --- /dev/null +++ b/src/lab10_lists/Node.java @@ -0,0 +1,19 @@ +package lab10_lists; + +public class Node { + String item; + Node next; + + Node(String item, Node next){ + this.item = item; + this.next = next; + } + Node(Node node){ + this.item = node.item; + this.next = node.next; + } + Node(String item){ + this.item = item; + this.next = null; + } +} diff --git a/src/lab11_debugging/11-debug-profiling-FR.pdf b/src/lab11_debugging/11-debug-profiling-FR.pdf new file mode 100644 index 0000000..01bc6f9 Binary files /dev/null and b/src/lab11_debugging/11-debug-profiling-FR.pdf differ diff --git a/src/lab11_debugging/DynamicStructuresComparison.java b/src/lab11_debugging/DynamicStructuresComparison.java new file mode 100644 index 0000000..13c4289 --- /dev/null +++ b/src/lab11_debugging/DynamicStructuresComparison.java @@ -0,0 +1,158 @@ +package lab11_debugging; + +/** + * Class used to compare resources consumption between different data structures + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author Pierre-André Mudry (pandre.mudry@hevs.ch) + * @version 1.2 + * + */ +public class DynamicStructuresComparison { + + private SampleArray tstArray; + private SampleVector tstVector; + private SampleLinkedList tstList; + private int size; + + /** + * Constructor. Produce the three data structures + * + * @param inputArray + */ + public DynamicStructuresComparison(int[] inputArray) { + tstArray = new SampleArray(inputArray); + tstVector = new SampleVector(inputArray); + tstList = new SampleLinkedList(inputArray); + + // Define the position of insertion for the tests + size = inputArray.length; + } + + /** + * Compare the difference between the data structures for the insertions + * + * @param nTimes + * Number of times the test is repeated + */ + public void testInsertion(int nTimes, int position) { + long start, end; + System.out.println("Insertion test with " + nTimes + + " insertions and an array size of " + size + " elements."); + + // Measure insertion with array + start = System.nanoTime(); + this.repeatInsertArray(nTimes, position); + end = System.nanoTime(); + System.out.println("Array insertion took : \t" + ((end- start)/1000.0) + " uS"); + + // Measure insertion with vector + start = System.nanoTime(); + this.repeatInsertVector(nTimes, position); + end = System.nanoTime(); + System.out.println("Vector insertion took: \t" + ((end - start)/1000.0) + " uS"); + + // Measure insertion with list + start = System.nanoTime(); + this.repeatInsertList(nTimes, position); + end = System.nanoTime(); + System.out.println("List insertion took : \t" + ((end - start)/1000.0) + " uS"); + } + + /** + * Compare the difference between the data structures for access + * + * @param nTimes + * Number of times the test is repeated + */ + public void testAccess(int nTimes, int position) { + long start, end; + System.out.println("Access test of the whole data with " + nTimes + + " repetitions and an array size of " + size + " elements."); + + // Measure insertion with array + start = System.nanoTime(); + this.repeatAccessArray(nTimes, position); + end = System.nanoTime(); + System.out.println("Array access took : \t\t" + ((end- start)/1000) + " uS"); + + // Measure insertion with list + start = System.nanoTime(); + this.repeatAccessVector(nTimes, position); + end = System.nanoTime(); + System.out.println("Vector access took : \t\t" + ((end- start)/1000) + " uS"); + + + // Measure insertion with list + start = System.nanoTime(); + this.repeatAccessList(nTimes, position); + end = System.nanoTime(); + System.out.println("List access took : \t\t" + ((end- start)/1000) + " uS"); + } + + /** + * Make a specific number of insertions + * + * @param number + */ + private void repeatInsertArray(int repetitions, int position) { + for (int i = 0; i < repetitions; i++) { + tstArray.insertZero(position); + } + } + + /** + * Make a specific number of insertions + * + * @param number + */ + private void repeatInsertVector(int repetitions, int position) { + for (int i = 0; i < repetitions; i++) { + tstVector.insertZero(position); + } + } + + /** + * Make a specific number of insertions + * + * @param number + */ + private void repeatInsertList(int repetitions, int position) { + for (int i = 0; i < repetitions; i++) { + tstList.insertZero(position); + } + } + + /** + * Make a specific number of access to the whole data + * + * @param number + */ + private void repeatAccessArray(int repetitions, int position){ + for (int i = 0; i < repetitions; i++) { + tstArray.increment(position); + } + } + + /** + * Make a specific number of access to the whole data + * + * @param number + */ + private void repeatAccessVector(int repetitions, int position){ + for (int i = 0; i < repetitions; i++) { + tstVector.increment(position); + } + } + + /** + * Make a specific number of access to the whole data + * + * @param number + */ + private void repeatAccessList(int repetitions, int position){ + for (int i = 0; i < repetitions; i++) { + tstList.increment(position); + } + } +} diff --git a/src/lab11_debugging/Launcher.java b/src/lab11_debugging/Launcher.java new file mode 100644 index 0000000..a62a9de --- /dev/null +++ b/src/lab11_debugging/Launcher.java @@ -0,0 +1,47 @@ +package lab11_debugging; + +import java.util.Random; + +/** + * Class to run the test + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author Pierre-André Mudry (pandre.mudry@hevs.ch) + * @version 1.2 + * + */ +public class Launcher { + static final boolean TEST_ACCESS = false; + static final boolean TEST_INSERTION = true; + + public static void main(String[] args) { + // Create a random array and create its perfect equivalent with a Vector or a list + final int size = 500000; + final int position = 0; + final int nTimes = 1000; + + int[] randomArray = createRandomArray(size, 10); + DynamicStructuresComparison test = new DynamicStructuresComparison(randomArray); + + if(TEST_ACCESS) + test.testAccess(nTimes, position); + if(TEST_INSERTION) + test.testInsertion(nTimes, position); + } + + /** + * Creates a random array + * + * @param size + * @param maxValue + * @return + */ + static int[] createRandomArray(int size, int maxValue) { + int[] result = new int[size]; + Random randomizer = new Random(); + for (int i = 0; i < size; i++) { + result[i] = randomizer.nextInt(maxValue); + } + return result; + } +} diff --git a/src/lab11_debugging/NumberAnalyzer.java b/src/lab11_debugging/NumberAnalyzer.java new file mode 100644 index 0000000..7105dfd --- /dev/null +++ b/src/lab11_debugging/NumberAnalyzer.java @@ -0,0 +1,66 @@ +package lab11_debugging; + +import java.util.Random; + +/** + * Class to try the debugger + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author PA Mudry (pierre-andre.mudry@hevs.ch) + * @version 1.1 + * + */ +public class NumberAnalyzer { + public static void main(String[] args) { + int[] array = createRandomArray(100, 100); + displayArray(array); + int position = firstRepeatedNumber(array); + System.out.println("The first repeated element is at location " + position); + } + + /** + * Function search for the first element repeated in the vector. It returns + * the position of this duplicated element. + * + * @param inputArray The input array + * @return The position of the first repeated element + */ + public static int firstRepeatedNumber(int[] inputArray) { + for (int i = 0; i < inputArray.length; i++) { + if (inputArray[i] == inputArray[i + 1]) + return i; + } + return -1; + } + + /** + * Create a random array + * + * @param size + * Number of element of the array produced + * @param maxValue + * Maximal value of the generated elements + * @return The generated array + */ + static int[] createRandomArray(int size, int maxValue) { + int[] result = new int[size]; + Random randomizer = new Random(); + for (int i = 0; i < size; i++) { + result[i] = randomizer.nextInt(maxValue); + } + return result; + } + + /** + * Display the array + * + * @param array + * The array to display + */ + static void displayArray(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + ","); + } + System.out.println(); + } +} diff --git a/src/lab11_debugging/SampleArray.java b/src/lab11_debugging/SampleArray.java new file mode 100644 index 0000000..8bd76b5 --- /dev/null +++ b/src/lab11_debugging/SampleArray.java @@ -0,0 +1,54 @@ +package lab11_debugging; + +/** + * Class to test efficiency of arrays + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author Pierre-André Mudry + * @version 1.2 + * + */ +public class SampleArray { + /** + * Stored values + */ + int[] data; + + /** + * Constructor + */ + SampleArray(int[] inputArray) { + data = inputArray.clone(); + } + + /** + * Increment an element by one + */ + void increment(int position) { + if(position < data.length) + data[position]++; + } + + /** + * Insert a zero at the specified position + * + * @param position + * @return + */ + void insertZero(int position) { + int[] tmpArray; + tmpArray = new int[data.length + 1]; + + // Copy the elements till position + for (int i = 0; i < position; i++) { + tmpArray[i] = data[i]; + } + // Move all the other element one position on the right + for (int i = position; i < data.length; i++) { + tmpArray[i + 1] = data[i]; + } + // Insert a zero at position + tmpArray[position] = 0; + data = tmpArray; + } +} diff --git a/src/lab11_debugging/SampleLinkedList.java b/src/lab11_debugging/SampleLinkedList.java new file mode 100644 index 0000000..b0aebfe --- /dev/null +++ b/src/lab11_debugging/SampleLinkedList.java @@ -0,0 +1,48 @@ +package lab11_debugging; + +import java.util.LinkedList; +import java.util.ListIterator; + +/** + * Class to test efficiency of Linked Lists + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author Pierre-André Mudry (pandre.mudry@hevs.ch) + * @version 1.2 (mui) - Changed incrementAll with iterator + * + */ +public class SampleLinkedList { + /** + * Stored values + */ + LinkedList data; + + /** + * Constructor + * + * @param inputArray + */ + public SampleLinkedList(int[] inputArray) { + data = new LinkedList(); + + // TODO Add your code here + + } + + /** + * Increment an element by one + */ + public void increment(int position) { + // TODO Add your code here + } + + /** + * Insert a zero at the specified position + * + * @param position + */ + public void insertZero(int position) { + // TODO Add your code here + } + +} diff --git a/src/lab11_debugging/SampleVector.java b/src/lab11_debugging/SampleVector.java new file mode 100644 index 0000000..f7cd6cc --- /dev/null +++ b/src/lab11_debugging/SampleVector.java @@ -0,0 +1,49 @@ +package lab11_debugging; + +import java.util.Vector; + +/** + * Class to test efficiency of Vectors + * + * @author Pierre Roduit (pierre.roduit@hevs.ch) + * @author Pierre-André Mudry (pandre.mudry@hevs.ch) + * @version 1.2 (mui) - In {@link #insertZero(int)}, changed insertElementAt with add + * + */ +public class SampleVector { + /** + * Stored values + */ + Vector data; + + /** + * Constructor + * + * @param inputArray + */ + SampleVector(int[] inputArray) { + data = new Vector(); + + // TODO Complete this + + } + + /** + * Increment an element by one + * + * @return + */ + void increment(int position) { + // TODO Complete this + } + + /** + * Insert a zero at the specified position + * + * @param position + * @return + */ + void insertZero(int position) { + // TODO Complete this + } +}