From 34039478ba2b47bc1f9a1378f3dd2afa54e1c0e9 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 18:26:20 +0200 Subject: [PATCH 1/9] Added .gitignore file Created a .gitignore file using the .gitignore plugin in IntelliJ IDEA, ignoring most common IDE and OS related files. --- .gitignore | 356 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea1bd37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,356 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen### Kotlin template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk +### VisualStudioCode template +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +### Android template +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +.idea/caches + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md +### Eclipse template + +.metadata +tmp/ +*.tmp +*.bak +*.swp +*~.nib +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet +### Gradle template +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties +### Java template +# Compiled class file + +# Log file + +# BlueJ files + +# Mobile Tools for Java (J2ME) + +# Package Files # + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +### Linux template +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* +### Windows template +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk +### NetBeans template +nbproject/private/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests +### Vim template +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim + +# Temporary +.netrwhist +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# Created by https://www.gitignore.io/api/bluej + +### bluej ### +# BlueJ: A Java IDE for programming beginners. http://bluej.org + +# Editor preferences for a class etc, see http://lists.bluej.org/pipermail/bluej-discuss/2003-May/002351.html +# *.ctxt (ignored before in this file) + +# Duplicate of project file, see http://www.hs-owl.de/fb5/labor/it/de/if1/vl/IF1JavaOhneBluej.pdf, page 4 +bluej.pkh + + +# End of https://www.gitignore.io/api/bluej From 4b302a9867284a897a55323dd7470d053efc32a9 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 18:39:14 +0200 Subject: [PATCH 2/9] Moved Node class to a separate java file and created linkedlist package Moved Node class outside the LinkedList.java file to a new Node.java file, and moved both files to a new package called "linkedlist". --- src/exercise/Main.java | 2 ++ src/exercise/{ => linkedlist}/LinkedList.java | 18 ++++-------------- src/exercise/linkedlist/Node.java | 11 +++++++++++ 3 files changed, 17 insertions(+), 14 deletions(-) rename src/exercise/{ => linkedlist}/LinkedList.java (90%) create mode 100644 src/exercise/linkedlist/Node.java diff --git a/src/exercise/Main.java b/src/exercise/Main.java index 903f8e7..6497e83 100644 --- a/src/exercise/Main.java +++ b/src/exercise/Main.java @@ -1,5 +1,7 @@ package exercise; +import exercise.linkedlist.LinkedList; + public class Main { public static void main(String[] args) { diff --git a/src/exercise/LinkedList.java b/src/exercise/linkedlist/LinkedList.java similarity index 90% rename from src/exercise/LinkedList.java rename to src/exercise/linkedlist/LinkedList.java index 17cbc2b..804b683 100644 --- a/src/exercise/LinkedList.java +++ b/src/exercise/linkedlist/LinkedList.java @@ -1,14 +1,4 @@ -package exercise; - -class Node { - int elem; - Node next; - - Node(int i, Node n) { - elem = i; - next = n; - } -} +package exercise.linkedlist; public class LinkedList { @@ -24,7 +14,7 @@ public LinkedList() { * * @param i the integer to add */ - void add(int i) { + public void add(int i) { Node newNode = new Node(i, null); if (first == null) { // list is empty @@ -52,7 +42,7 @@ void add(int i) { * @return element at index * @throws {@link IndexOutOfBoundsException} if index is negative */ - int get(int index) { + public int get(int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } @@ -73,7 +63,7 @@ int get(int index) { } } - void printAll() { + public void printAll() { if (first == null) { // list is empty System.out.print(""); } else { diff --git a/src/exercise/linkedlist/Node.java b/src/exercise/linkedlist/Node.java new file mode 100644 index 0000000..908f0f1 --- /dev/null +++ b/src/exercise/linkedlist/Node.java @@ -0,0 +1,11 @@ +package exercise.linkedlist; + +class Node { + int elem; + Node next; + + Node(int i, Node n) { + elem = i; + next = n; + } +} From 03499e9b84ac85cb019e9e2acb537f7ee461a284 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 20:59:12 +0200 Subject: [PATCH 3/9] Add Node instance to LinkedList Added an add method to the LinkedList class allowing adding a Node class instance to it, besides the existing add by value one which is now named addValue(int i). --- src/exercise/Main.java | 4 ++-- src/exercise/linkedlist/LinkedList.java | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/exercise/Main.java b/src/exercise/Main.java index 6497e83..9192712 100644 --- a/src/exercise/Main.java +++ b/src/exercise/Main.java @@ -7,12 +7,12 @@ public class Main { public static void main(String[] args) { LinkedList l = new LinkedList(); - l.add(5); + l.addValue(5); int res = l.get(0); System.out.println("res: " + res); - l.add(7); + l.addValue(7); int res2 = l.get(1); System.out.println("res2: " + res2); diff --git a/src/exercise/linkedlist/LinkedList.java b/src/exercise/linkedlist/LinkedList.java index 804b683..ea51ce3 100644 --- a/src/exercise/linkedlist/LinkedList.java +++ b/src/exercise/linkedlist/LinkedList.java @@ -12,26 +12,35 @@ public LinkedList() { /** * Adds an integer to the end of this list. * - * @param i the integer to add + * @param i the integer to addValue */ - public void add(int i) { + public void addValue(int i) { Node newNode = new Node(i, null); - + add(newNode); + } + + /** + * Add a node to the list + * + * @param node Node to be added + */ + public void add(Node node) { + if (first == null) { // list is empty - first = newNode; + first = node; } else { // list is not empty // 1. step find the last node Node current = first; - + while (current.next != null) { // current is not the last node! current = current.next; } // current.next == null // that means that current is the last node! YAY! :-) - + // 2. step insert new node - current.next = newNode; + current.next = node; } } From 45765bee19323c38d71f07704b30529d92e6552b Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:05:08 +0200 Subject: [PATCH 4/9] Getting Node instance from LinkedList Added a get method to the LinkedList class allowing getting a Node class instance from it, besides the existing get by value one which is now named getValue(int i). --- src/exercise/Main.java | 4 +-- src/exercise/linkedlist/LinkedList.java | 48 +++++++++++++++---------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/exercise/Main.java b/src/exercise/Main.java index 9192712..f3eedf7 100644 --- a/src/exercise/Main.java +++ b/src/exercise/Main.java @@ -9,12 +9,12 @@ public static void main(String[] args) { LinkedList l = new LinkedList(); l.addValue(5); - int res = l.get(0); + int res = l.getValue(0); System.out.println("res: " + res); l.addValue(7); - int res2 = l.get(1); + int res2 = l.getValue(1); System.out.println("res2: " + res2); l.printAll(); diff --git a/src/exercise/linkedlist/LinkedList.java b/src/exercise/linkedlist/LinkedList.java index ea51ce3..2650163 100644 --- a/src/exercise/linkedlist/LinkedList.java +++ b/src/exercise/linkedlist/LinkedList.java @@ -51,26 +51,36 @@ public void add(Node node) { * @return element at index * @throws {@link IndexOutOfBoundsException} if index is negative */ - public int get(int index) { - if (index < 0) { - throw new IndexOutOfBoundsException(); - } - - int i = 0; - Node current = first; - - while (index != i && current.next != null) { - i++; - current = current.next; - } - - if (index <= i) { - // here, we are at the right place! - return current.elem; - } else { - throw new IndexOutOfBoundsException(); - } + public int getValue(int index) { + return get(index).elem; } + + /** + * Get Node at index + * @param index Index of Node + + * @return {@link Node} at the given index + */ + public Node get(int index) { + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + + int i = 0; + Node current = first; + + while (index != i && current.next != null) { + i++; + current = current.next; + } + + if (index <= i) { + // here, we are at the right place! + return current; + } else { + throw new IndexOutOfBoundsException(); + } + } public void printAll() { if (first == null) { // list is empty From a77115486c91068ce1c76bc936b848b07c2886c8 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:10:11 +0200 Subject: [PATCH 5/9] Added getters and setters for fields in Node.java Added getters and setters for fields in Node.java and changed usage in the LinkedList class to match these changes. --- src/exercise/linkedlist/LinkedList.java | 138 +++++++++++------------- src/exercise/linkedlist/Node.java | 33 ++++-- 2 files changed, 90 insertions(+), 81 deletions(-) diff --git a/src/exercise/linkedlist/LinkedList.java b/src/exercise/linkedlist/LinkedList.java index 2650163..e973d42 100644 --- a/src/exercise/linkedlist/LinkedList.java +++ b/src/exercise/linkedlist/LinkedList.java @@ -2,78 +2,72 @@ public class LinkedList { - // storage for elements - Node first; - - public LinkedList() { - first = null; - } - - /** - * Adds an integer to the end of this list. - * - * @param i the integer to addValue - */ - public void addValue(int i) { - Node newNode = new Node(i, null); - add(newNode); - } + // storage for elements + private Node first; - /** - * Add a node to the list - * - * @param node Node to be added - */ - public void add(Node node) { - - if (first == null) { // list is empty - first = node; - } else { // list is not empty - // 1. step find the last node - Node current = first; + public LinkedList() { + first = null; + } - while (current.next != null) { - // current is not the last node! - current = current.next; - } - // current.next == null - // that means that current is the last node! YAY! :-) + /** + * Adds an integer to the end of this list. + * + * @param i the integer to addValue + */ + public void addValue(int i) { + Node newNode = new Node(i, null); + add(newNode); + } - // 2. step insert new node - current.next = node; - } - } + /** + * Add a node to the list + * + * @param node Node to be added + */ + public void add(Node node) { + if (first == null) { // list is empty + first = node; + } else { // list is not empty + // 1. step find the last node + Node current = first; + while (current.getNext() != null) { + // current is not the last node! + current = current.getNext(); + } + // current.next == null + // that means that current is the last node! YAY! :-) + // 2. step insert new node + current.setNext(node); + } + } - /** - * Return element at given index. - * - * @param index - * @return element at index - * @throws {@link IndexOutOfBoundsException} if index is negative - */ - public int getValue(int index) { - return get(index).elem; - } + /** + * Return element at given index. + * + * @param index + * @return element at index + * @throws {@link IndexOutOfBoundsException} if index is negative + */ + public int getValue(int index) { + return get(index).getElem(); + } /** * Get Node at index + * * @param index Index of Node - * @return {@link Node} at the given index */ public Node get(int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } - int i = 0; Node current = first; - - while (index != i && current.next != null) { + while (index != i && current.getNext() != null) { i++; - current = current.next; + current = current.getNext(); } - if (index <= i) { // here, we are at the right place! return current; @@ -81,23 +75,21 @@ public Node get(int index) { throw new IndexOutOfBoundsException(); } } - - public void printAll() { - if (first == null) { // list is empty - System.out.print(""); - } else { - // we know that first is not null! - System.out.print("["); - - Node current = first; - while (current.next != null) { - System.out.print(current.elem + ","); - current = current.next; - } - // print last element - System.out.print(current.elem); - - System.out.print("]"); - } - } + + public void printAll() { + if (first == null) { // list is empty + System.out.print(""); + } else { + // we know that first is not null! + System.out.print("["); + Node current = first; + while (current.getNext() != null) { + System.out.print(current.getElem() + ","); + current = current.getNext(); + } + // print last element + System.out.print(current.getElem()); + System.out.print("]"); + } + } } diff --git a/src/exercise/linkedlist/Node.java b/src/exercise/linkedlist/Node.java index 908f0f1..5d8db8d 100644 --- a/src/exercise/linkedlist/Node.java +++ b/src/exercise/linkedlist/Node.java @@ -1,11 +1,28 @@ package exercise.linkedlist; -class Node { - int elem; - Node next; - - Node(int i, Node n) { - elem = i; - next = n; - } +public class Node { + + private int elem; + private Node next; + + public Node(int i, Node n) { + elem = i; + next = n; + } + + public int getElem() { + return elem; + } + + public void setElem(int elem) { + this.elem = elem; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } } From 367251dd32dec77c599ea80c8be729061fe87e1b Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:24:07 +0200 Subject: [PATCH 6/9] Added JUnit tests for Node class setters and getters --- test/exercise/linkedlist/NodeTest.java | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/exercise/linkedlist/NodeTest.java diff --git a/test/exercise/linkedlist/NodeTest.java b/test/exercise/linkedlist/NodeTest.java new file mode 100644 index 0000000..f0e28d3 --- /dev/null +++ b/test/exercise/linkedlist/NodeTest.java @@ -0,0 +1,50 @@ +package exercise.linkedlist; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NodeTest { + + public static final int VALUE = 123; + private Node node; + private Node nextNode; + + @BeforeEach + void setUp() { + nextNode = new Node(400, null); + node = new Node(VALUE, nextNode); + } + + @AfterEach + void tearDown() { + node = null; + nextNode = null; + } + + @Test + void getElem() { + assertEquals(VALUE, node.getElem()); + } + + @Test + void setElem() { + int expected = 109; + node.setElem(expected); + assertEquals(expected, node.getElem()); + } + + @Test + void getNext() { + assertEquals(nextNode,node.getNext()); + } + + @Test + void setNext() { + nextNode = new Node(300,null); + node.setNext(nextNode); + assertEquals(nextNode,node.getNext()); + } +} From 49fb3a355361bc1f623874a9a93f10466ca65783 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:29:27 +0200 Subject: [PATCH 7/9] Added JUnit tests for the LinkedList class --- test/exercise/linkedlist/LinkedListTest.java | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/exercise/linkedlist/LinkedListTest.java diff --git a/test/exercise/linkedlist/LinkedListTest.java b/test/exercise/linkedlist/LinkedListTest.java new file mode 100644 index 0000000..46cdb8a --- /dev/null +++ b/test/exercise/linkedlist/LinkedListTest.java @@ -0,0 +1,50 @@ +package exercise.linkedlist; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class LinkedListTest { + + private LinkedList linkedList; + + @BeforeEach + void setUp() { + linkedList = new LinkedList(); + } + + @AfterEach + void tearDown() { + linkedList = null; + } + + @Test + void addValue() { + int expected = 105; + linkedList.addValue(105); + assertEquals(expected, linkedList.getValue(0)); + } + + @Test + void add() { + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(0)); + } + + @Test + void getValue() { + int expected = 202; + linkedList.addValue(expected); + assertEquals(expected,linkedList.getValue(0)); + } + + @Test + void get() { + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(0)); + } +} From 8ec2febc6d8d726fd51652bef890f2963d1411cf Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:50:33 +0200 Subject: [PATCH 8/9] Added test for Index out of bound exception in LinkedListTest --- test/exercise/linkedlist/LinkedListTest.java | 12 +++++++++++- test/exercise/linkedlist/NodeTest.java | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/test/exercise/linkedlist/LinkedListTest.java b/test/exercise/linkedlist/LinkedListTest.java index 46cdb8a..bb98d27 100644 --- a/test/exercise/linkedlist/LinkedListTest.java +++ b/test/exercise/linkedlist/LinkedListTest.java @@ -4,7 +4,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.lang.reflect.Executable; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class LinkedListTest { @@ -38,7 +41,7 @@ void add() { void getValue() { int expected = 202; linkedList.addValue(expected); - assertEquals(expected,linkedList.getValue(0)); + assertEquals(expected, linkedList.getValue(0)); } @Test @@ -47,4 +50,11 @@ void get() { linkedList.add(expectedNode); assertEquals(expectedNode, linkedList.get(0)); } + + @Test + void outOfBoundException() { + Node newNode = new Node(303, null); + linkedList.add(newNode); + assertThrows(IndexOutOfBoundsException.class, () -> linkedList.getValue(1)); + } } diff --git a/test/exercise/linkedlist/NodeTest.java b/test/exercise/linkedlist/NodeTest.java index f0e28d3..bb822cd 100644 --- a/test/exercise/linkedlist/NodeTest.java +++ b/test/exercise/linkedlist/NodeTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; class NodeTest { From 8f0fbae73261ff2ec29612421a502a0cbdf0cfa3 Mon Sep 17 00:00:00 2001 From: Ahmed AlAskalany Date: Mon, 15 Oct 2018 21:59:33 +0200 Subject: [PATCH 9/9] Added test for Index out of bound exception in LinkedListTest --- test/exercise/linkedlist/LinkedListTest.java | 33 +++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test/exercise/linkedlist/LinkedListTest.java b/test/exercise/linkedlist/LinkedListTest.java index bb98d27..8341200 100644 --- a/test/exercise/linkedlist/LinkedListTest.java +++ b/test/exercise/linkedlist/LinkedListTest.java @@ -4,8 +4,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.lang.reflect.Executable; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -31,12 +29,32 @@ void addValue() { } @Test - void add() { + void addToEmptyLinkedList() { Node expectedNode = new Node(303, null); linkedList.add(expectedNode); assertEquals(expectedNode, linkedList.get(0)); } + @Test + void addToNonEmptyLinkedList() { + Node someNode = new Node(101, null); + linkedList.add(someNode); + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(1)); + } + + @Test + void addToNonEmptyLinkedListWithMoreThanOneNode() { + Node firstNode = new Node(909, null); + linkedList.add(firstNode); + Node someNode = new Node(101, null); + linkedList.add(someNode); + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(2)); + } + @Test void getValue() { int expected = 202; @@ -52,9 +70,16 @@ void get() { } @Test - void outOfBoundException() { + void positiveIndexOutOfBoundException() { Node newNode = new Node(303, null); linkedList.add(newNode); assertThrows(IndexOutOfBoundsException.class, () -> linkedList.getValue(1)); } + + @Test + void negativeIndexOutOfBoundException() { + Node newNode = new Node(303, null); + linkedList.add(newNode); + assertThrows(IndexOutOfBoundsException.class, () -> linkedList.getValue(-1)); + } }