Skip to content

Commit

Permalink
Adding JUnit Practice
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago Hyun committed May 2, 2023
1 parent a13d1a0 commit 661a93b
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ target/
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity
.yarn-integrity

InsertionSort.java
QuickSort.java
SelectionSort.java
21 changes: 21 additions & 0 deletions maven/j_unit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.unicauca</groupId>
<artifactId>practice.junit_git</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.unicauca.practice.junit_git.src.main;

import com.unicauca.practice.junit_git.src.sorting.SortingPractice;
import com.unicauca.practice.junit_git.src.sorting.util.SortingUtil;
/**
*
* @author sahydo
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SortingPractice practice = new SortingPractice(new int[] {3,25,6,8,83,32,5,7,1}, SortingUtil.BUBBLE_SORT);
practice.sort();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.unicauca.practice.junit_git.src.sorting;

import com.unicauca.practice.junit_git.src.sorting.util.BubbleSort;
import com.unicauca.practice.junit_git.src.sorting.util.InsertionSort;
import com.unicauca.practice.junit_git.src.sorting.util.QuickSort;
import com.unicauca.practice.junit_git.src.sorting.util.SelectionSort;
import com.unicauca.practice.junit_git.src.sorting.util.SortingUtil;

/**
*
* @author sahydo
*/
public class SortingPractice {

private int[] array;
private String sortingType = SortingUtil.BUBBLE_SORT;

public SortingPractice(int[] array, String sortingType) {
this.array = array;
this.sortingType = sortingType;
}

public SortingPractice(int[] array) {
this.array = array;
}

public int[] getArray() {
return array;
}

public void setArray(int[] array) {
this.array = array;
}

public String getSortingType() {
return sortingType;
}

public void setSortingType(String sortingType) {
this.sortingType = sortingType;
}

public void sort() {
SortingUtil sortingMethod;
switch (this.getSortingType()) {
case SortingUtil.BUBBLE_SORT:
sortingMethod = new BubbleSort(this.getSortingType());
// parameter by reference
sortingMethod.sort(this.getArray());
break;
case SortingUtil.INSERTION_SORT:
// sortingMethod = new InsertionSort(this.getSortingType());
// sortingMethod.sort(this.getArray());
System.err.println("Insertion sorting pending");
break;
case SortingUtil.QUICK_SORT:
// sortingMethod = new QuickSort(this.getSortingType());
// sortingMethod.sort(this.getArray());
System.err.println("Quick sorting pending");
break;
case SortingUtil.SELECTION_SORT:
// sortingMethod = new SelectionSort(this.getSortingType());
// sortingMethod.sort(this.getArray());
System.err.println("Selection sorting pending");
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.unicauca.practice.junit_git.src.sorting.util;

/**
*
* @author sahydo
*/
public class BubbleSort extends SortingUtil {

public BubbleSort(String type) {
super(type);
System.out.println("Ordering with " + this.getClass().getSimpleName());
}

@Override
public int[] sort(int[] array) {
int lastIndex = array.length - 1;
for (int j = 0; j < lastIndex; j++) {
for (int i = 0; i < lastIndex - j; i++) {
if (array[i] > array[i + 1]) {
int tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
}
}
}
return array;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.unicauca.practice.junit_git.src.sorting.util;

/**
*
* @author sahydo
*/
public abstract class SortingUtil {
public static final String BUBBLE_SORT = "bubble";
public static final String INSERTION_SORT = "insertion";
public static final String QUICK_SORT = "quick";
public static final String SELECTION_SORT = "selection";

protected String type;

public SortingUtil(String type) {
this.type = type;
}

protected String getType() {
return type;
}

protected void setType(String type) {
this.type = type;
}

public abstract int[] sort(int[] array);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.unicauca.practice.junit_git.src.sorting;

import com.unicauca.practice.junit_git.src.sorting.util.SortingUtil;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
*
* @author sahydo
*/
public class SortingPracticeTest {

private final int[] testArray = new int[]{4, 5, 2, 3, 1};
private final int[] orderedArray = new int[]{1, 2, 3, 4, 5};

public SortingPracticeTest() {
}

/**
* Test of sort method, of class SortingPractice.
*/
@Test
@DisplayName("Bubble sorting test")
public void testBubbleSort() {
SortingPractice sortingPractice = new SortingPractice(testArray.clone(), SortingUtil.BUBBLE_SORT);
sortingPractice.sort();
assertArrayEquals(orderedArray, sortingPractice.getArray());
}

@Test
@DisplayName("Insertion sorting test")
public void testInsertionSort() {
SortingPractice sortingPractice = new SortingPractice(testArray.clone(), SortingUtil.INSERTION_SORT);
sortingPractice.sort();
assertArrayEquals(orderedArray, sortingPractice.getArray());
fail("The test has failed");
}

@Test
@DisplayName("Selection sorting test")
public void testSelectionSort() {
SortingPractice sortingPractice = new SortingPractice(testArray.clone(), SortingUtil.SELECTION_SORT);
sortingPractice.sort();
assertArrayEquals(orderedArray, sortingPractice.getArray());
}

@Test
@DisplayName("Quick sorting test")
public void testQuickSort() {
// Not implemented yet
}

}

0 comments on commit 661a93b

Please sign in to comment.