diff --git a/.gitignore b/.gitignore index c2e8de74cd57..104214759091 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ Java.iml .idea/* out/ *.iml +target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000000..8306eb25fcfb --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.github.thealgorithms + java + 1.0 + jar + + + 5.5.1 + UTF-8 + 1.8 + 1.8 + + + + + org.junit.jupiter + junit-jupiter + ${junit.jupiter.version} + test + + + + + + + maven-compiler-plugin + 3.8.1 + + + maven-surefire-plugin + 2.22.2 + + + + \ No newline at end of file diff --git a/src/main/java/com/conversions/AnyBaseToAnyBase.java b/src/main/java/com/conversions/AnyBaseToAnyBase.java new file mode 100644 index 000000000000..e7f8655a28b9 --- /dev/null +++ b/src/main/java/com/conversions/AnyBaseToAnyBase.java @@ -0,0 +1,133 @@ +package com.conversions; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.InputMismatchException; +import java.util.Scanner; + +/** + * Class for converting from "any" base to "any" other base, when "any" means from 2-36. + * Works by going from base 1 to decimal to base 2. Includes auxiliary method for + * determining whether a number is valid for a given base. + * + * @author Michael Rolland + * @version 2017.10.10 + */ +public class AnyBaseToAnyBase { + + /** + * Smallest and largest base you want to accept as valid input + */ + static final int MINIMUM_BASE = 2; + static final int MAXIMUM_BASE = 36; + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String n; + int b1, b2; + while (true) { + try { + System.out.print("Enter number: "); + n = in.next(); + System.out.print("Enter beginning base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); + b1 = in.nextInt(); + if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) { + System.out.println("Invalid base!"); + continue; + } + if (!validForBase(n, b1)) { + System.out.println("The number is invalid for this base!"); + continue; + } + System.out.print("Enter end base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); + b2 = in.nextInt(); + if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) { + System.out.println("Invalid base!"); + continue; + } + break; + } catch (InputMismatchException e) { + System.out.println("Invalid input."); + in.next(); + } + } + System.out.println(base2base(n, b1, b2)); + } + + /** + * Checks if a number (as a String) is valid for a given base. + */ + public static boolean validForBase(String n, int base) { + char[] validDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', + 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', + 'W', 'X', 'Y', 'Z'}; + // digitsForBase contains all the valid digits for the base given + char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base); + + // Convert character array into set for convenience of contains() method + HashSet digitsList = new HashSet<>(); + for (int i = 0; i < digitsForBase.length; i++) + digitsList.add(digitsForBase[i]); + + // Check that every digit in n is within the list of valid digits for that base. + for (char c : n.toCharArray()) + if (!digitsList.contains(c)) + return false; + + return true; + } + + /** + * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal, + * then decimal to b2. + * + * @param n The integer to be converted. + * @param b1 Beginning base. + * @param b2 End base. + * @return n in base b2. + */ + public static String base2base(String n, int b1, int b2) { + // Declare variables: decimal value of n, + // character of base b1, character of base b2, + // and the string that will be returned. + int decimalValue = 0, charB2; + char charB1; + String output = ""; + // Go through every character of n + for (int i = 0; i < n.length(); i++) { + // store the character in charB1 + charB1 = n.charAt(i); + // if it is a non-number, convert it to a decimal value >9 and store it in charB2 + if (charB1 >= 'A' && charB1 <= 'Z') + charB2 = 10 + (charB1 - 'A'); + // Else, store the integer value in charB2 + else + charB2 = charB1 - '0'; + // Convert the digit to decimal and add it to the + // decimalValue of n + decimalValue = decimalValue * b1 + charB2; + } + + // Converting the decimal value to base b2: + // A number is converted from decimal to another base + // by continuously dividing by the base and recording + // the remainder until the quotient is zero. The number in the + // new base is the remainders, with the last remainder + // being the left-most digit. + + // While the quotient is NOT zero: + while (decimalValue != 0) { + // If the remainder is a digit < 10, simply add it to + // the left side of the new number. + if (decimalValue % b2 < 10) + output = decimalValue % b2 + output; + // If the remainder is >= 10, add a character with the + // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) + else + output = (char) ((decimalValue % b2) + 55) + output; + // Divide by the new base again + decimalValue /= b2; + } + return output; + } +} diff --git a/src/main/java/com/conversions/AnyBaseToDecimal.java b/src/main/java/com/conversions/AnyBaseToDecimal.java index 65f89f2952aa..0ddf355c731d 100644 --- a/src/main/java/com/conversions/AnyBaseToDecimal.java +++ b/src/main/java/com/conversions/AnyBaseToDecimal.java @@ -1,13 +1,28 @@ -package src.main.java.com.conversions; +package com.conversions; public class AnyBaseToDecimal { + + /** + * This method produces integer value of the input character and returns it + * + * @param c Char of which we need the integer value of + * @return integer value of input char + */ + + private static int valOfChar(char c) { + if (c >= '0' && c <= '9') { + return (int) c - '0'; + } else { + return (int) c - 'A' + 10; + } + } + /** * This method produces a decimal value of any given input number of any base * * @param inpNum String of which we need the decimal value and base in integer format * @return string format of the decimal value */ - public String convertToDecimal(String inpNum, int base) { int len = inpNum.length(); int num = 0; @@ -22,19 +37,4 @@ public String convertToDecimal(String inpNum, int base) { } return String.valueOf(num); } - - /** - * This method produces integer value of the input character and returns it - * - * @param c Char of which we need the integer value of - * @return integer value of input char - */ - - private static int valOfChar(char c) { - if (c >= '0' && c <= '9') { - return (int) c - '0'; - } else { - return (int) c - 'A' + 10; - } - } } \ No newline at end of file diff --git a/src/main/java/com/conversions/AnyToAny.java b/src/main/java/com/conversions/AnyToAny.java new file mode 100644 index 000000000000..8e645ccc4e38 --- /dev/null +++ b/src/main/java/com/conversions/AnyToAny.java @@ -0,0 +1,29 @@ +package com.conversions; + +import java.util.Scanner; +//given a source number , source base, destination base, this code can give you the destination number. +//sn ,sb,db ---> ()dn . this is what we have to do . + +public class AnyToAny { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + int sn = scn.nextInt(); + int sb = scn.nextInt(); + int db = scn.nextInt(); + int m = 1, dec = 0, dn = 0; + while (sn != 0) { + dec = dec + (sn % 10) * m; + m *= sb; + sn /= 10; + } + m = 1; + while (dec != 0) { + dn = dn + (dec % db) * m; + m *= 10; + dec /= db; + } + System.out.println(dn); + } + +} diff --git a/src/main/java/com/conversions/BinaryToGray.java b/src/main/java/com/conversions/BinaryToGray.java index 60896e0ff6d0..eb41d57359da 100644 --- a/src/main/java/com/conversions/BinaryToGray.java +++ b/src/main/java/com/conversions/BinaryToGray.java @@ -1,4 +1,4 @@ -package src.main.java.com.conversions; +package com.conversions; /** * Convert the binary number into gray code diff --git a/src/main/java/com/conversions/BinaryToHexadecimal.java b/src/main/java/com/conversions/BinaryToHexadecimal.java index c7810f11c78b..2321ecf694b0 100644 --- a/src/main/java/com/conversions/BinaryToHexadecimal.java +++ b/src/main/java/com/conversions/BinaryToHexadecimal.java @@ -1,4 +1,4 @@ -package src.main.java.com.conversions; +package com.conversions; import java.math.BigInteger; import java.util.HashMap; diff --git a/src/main/java/com/conversions/DecimalToAnyBase.java b/src/main/java/com/conversions/DecimalToAnyBase.java index 912d423d954f..48cbbde7cfa9 100644 --- a/src/main/java/com/conversions/DecimalToAnyBase.java +++ b/src/main/java/com/conversions/DecimalToAnyBase.java @@ -1,4 +1,4 @@ -package src.main.java.com.conversions; +package com.conversions; import java.util.ArrayList; diff --git a/src/main/java/com/conversions/DecimalToHexadecimal.java b/src/main/java/com/conversions/DecimalToHexadecimal.java index 0c812f520b39..c9b968bd73d7 100644 --- a/src/main/java/com/conversions/DecimalToHexadecimal.java +++ b/src/main/java/com/conversions/DecimalToHexadecimal.java @@ -1,4 +1,4 @@ -package src.main.java.com.conversions; +package com.conversions; import java.math.BigInteger; diff --git a/src/main/java/com/conversions/DecimalToOctal.java b/src/main/java/com/conversions/DecimalToOctal.java index 64542298853c..bf1cbff0b807 100644 --- a/src/main/java/com/conversions/DecimalToOctal.java +++ b/src/main/java/com/conversions/DecimalToOctal.java @@ -1,4 +1,4 @@ -package src.main.java.com.conversions; +package com.conversions; import java.math.BigInteger; diff --git a/src/main/java/com/crypto/codec/Base64.java b/src/main/java/com/crypto/codec/Base64.java index f65faf963bcb..9f0a3fe1e918 100644 --- a/src/main/java/com/crypto/codec/Base64.java +++ b/src/main/java/com/crypto/codec/Base64.java @@ -1,4 +1,4 @@ -package src.main.java.com.crypto.codec; +package com.crypto.codec; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -193,7 +193,7 @@ private static byte[] decodeBlock(byte[] block) { *

Removes the padding from the last block. * * @param block - * @return The decoded last block of data + * @return The decoded last block of data */ private static byte[] undoPadding(byte[] block) { int padCount = 0; diff --git a/src/main/java/com/crypto/hash/Sha2.java b/src/main/java/com/crypto/hash/Sha2.java index d5d277c232ef..69c1e39f00a5 100644 --- a/src/main/java/com/crypto/hash/Sha2.java +++ b/src/main/java/com/crypto/hash/Sha2.java @@ -1,4 +1,4 @@ -package src.main.java.com.crypto.hash; +package com.crypto.hash; import java.nio.ByteBuffer; @@ -22,6 +22,47 @@ */ public final class Sha2 { + private static final int[] K_int = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + }; + private static final long[] K_long = { + 0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, + 0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, + 0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, + 0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L, + 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, + 0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L, + 0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, + 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L, + 0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, + 0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL, + 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, + 0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L, + 0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, + 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, + 0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, + 0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL, + 0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, + 0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL, + 0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, + 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L + }; + /** *

Returns a SHA-224 message digest with a fixed length of 224 bit (28 byte). * By specification, the user-provided data can have a length of 0 <= L < 2^61 byte. @@ -52,7 +93,7 @@ public static String SHA224(byte[] data) { * * @param data the data/message to be digested * @return the message digest with a fixed length of 256 bit (32 byte) - * @see src.main.java.com.crypto.hash.Sha2#SHA224(byte[]) SHA224() + * @see com.crypto.hash.Sha2#SHA224(byte[]) SHA224() */ public static String SHA256(byte[] data) { final int[] initialHash = { @@ -100,7 +141,7 @@ public static String SHA384(byte[] data) { * * @param data the data/message to be digested * @return the message digest with a fixed length of 512 bit (64 byte) - * @see src.main.java.com.crypto.hash.Sha2#SHA384(byte[]) SHA384() + * @see com.crypto.hash.Sha2#SHA384(byte[]) SHA384() */ public static String SHA512(byte[] data) { final long[] initialHash = { @@ -117,7 +158,6 @@ public static String SHA512(byte[] data) { return builder.toString(); } - /** *

Returns an integer array, which holds the raw message digest.

*

This method is wrapped by SHA224() and SHA256(). Both algorithms differ @@ -211,6 +251,11 @@ private static byte[] pad(byte[] data, int blockSize) { return padding; } + /* + * Functions and Constants used + * https://tools.ietf.org/html/rfc6234#section-5 + */ + /** * Scrambles data blocks in a deterministic way. * @@ -303,53 +348,6 @@ private static void hashBlock(long[] dataBlock, long[] hash) { } } - /* - * Functions and Constants used - * https://tools.ietf.org/html/rfc6234#section-5 - */ - - private static final int[] K_int = { - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - }; - - private static final long[] K_long = { - 0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, - 0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, - 0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, - 0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L, - 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, - 0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L, - 0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, - 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L, - 0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, - 0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL, - 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, - 0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L, - 0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, - 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, - 0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, - 0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL, - 0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, - 0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL, - 0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, - 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L - }; - private static int CH(int x, int y, int z) { return (x & y) ^ ((~x) & z); } diff --git a/src/main/java/com/dataStructures/BinaryTree.java b/src/main/java/com/dataStructures/BinaryTree.java index 3542bce5a213..b3785556a349 100644 --- a/src/main/java/com/dataStructures/BinaryTree.java +++ b/src/main/java/com/dataStructures/BinaryTree.java @@ -1,4 +1,4 @@ -package src.main.java.com.dataStructures; +package com.dataStructures; /** * Binary tree for general value type, without redundancy diff --git a/src/main/java/com/dataStructures/DisjointSet.java b/src/main/java/com/dataStructures/DisjointSet.java index 3fb70f6c78c2..6a8544645908 100644 --- a/src/main/java/com/dataStructures/DisjointSet.java +++ b/src/main/java/com/dataStructures/DisjointSet.java @@ -1,4 +1,4 @@ -package src.main.java.com.dataStructures; +package com.dataStructures; import java.io.Serializable; import java.util.*; @@ -14,13 +14,18 @@ * 1. quickly unites two sets into a new set, requiring O(1) time. *

* 2. quickly query two elements whether contained in the same set, requiring about O(1) time. - * */ public class DisjointSet implements Serializable { private static final long serialVersionUID = 3134700471905625636L; private Map> nodeMap = new HashMap<>(); + private static void checkNotNull(Object obj, String msg) { + if (obj == null) { + throw new NullPointerException(msg + " must be not null"); + } + } + /** * Add an element to the disjoint-set forests as a set. */ @@ -96,7 +101,7 @@ public Collection> toSets() { Map, Set> setMap = new HashMap<>(); for (Map.Entry> entry : nodeMap.entrySet()) { setMap.computeIfAbsent(findSet(entry.getValue()), k -> new HashSet<>()) - .add(entry.getKey()); + .add(entry.getKey()); } return setMap.values(); } @@ -117,12 +122,6 @@ private Node findSet(Node node) { return node.parent; } - private static void checkNotNull(Object obj, String msg) { - if (obj == null) { - throw new NullPointerException(msg + " must be not null"); - } - } - static class Node { int rank; Node parent; diff --git a/src/main/java/com/dataStructures/GeneralQueue.java b/src/main/java/com/dataStructures/GeneralQueue.java index a141ab13bbc9..3d0920821c67 100644 --- a/src/main/java/com/dataStructures/GeneralQueue.java +++ b/src/main/java/com/dataStructures/GeneralQueue.java @@ -1,6 +1,6 @@ -package src.main.java.com.dataStructures; +package com.dataStructures; -import src.main.java.com.types.Queue; +import com.types.Queue; import java.util.Iterator; import java.util.LinkedList; diff --git a/src/main/java/com/dataStructures/Stack.java b/src/main/java/com/dataStructures/Stack.java index 0349a5223ecf..3b675018863e 100644 --- a/src/main/java/com/dataStructures/Stack.java +++ b/src/main/java/com/dataStructures/Stack.java @@ -1,4 +1,4 @@ -package src.main.java.com.dataStructures; +package com.dataStructures; import java.io.Serializable; import java.util.EmptyStackException; diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java index f48f80c13306..9216a005b670 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactory.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; /** * The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java b/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java index 47f277a6f10e..f8a980c7f609 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Circle.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class Circle implements Shape { @Override diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java index 40716aa7eef2..777aec4f4316 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryProvider.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class FactoryProvider { public static AbstractShapeFactory getShapeFactory(FactoryType factoryType) { diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java index 8ea56aa88bcb..63f4e8c785a0 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/FactoryType.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public enum FactoryType { TWO_D_FACTORY, diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Line.java b/src/main/java/com/designpatterns/creational/abstractfactory/Line.java index 620037c37e03..2c3ce0c32c33 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/Line.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Line.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class Line implements Shape { @Override diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java b/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java index 5e741319042c..f32b72d74d9f 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Shape.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public interface Shape { /** diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java b/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java index 8fc6ac9b5ab9..464ba8cfbcc0 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/ShapeType.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public enum ShapeType { LINE, diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java b/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java index 01b530c36a45..5482d4af83ea 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/Sphere.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class Sphere implements Shape { @Override diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java index 2a23234f0f9c..385a45cec08b 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/ThreeDShapeFactory.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class ThreeDShapeFactory extends AbstractShapeFactory { @Override diff --git a/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java b/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java index 5d553ef841b2..34f00eb59c7d 100644 --- a/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java +++ b/src/main/java/com/designpatterns/creational/abstractfactory/TwoDShapeFactory.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; public class TwoDShapeFactory extends AbstractShapeFactory { @Override diff --git a/src/main/java/com/designpatterns/creational/builder/Desktop.java b/src/main/java/com/designpatterns/creational/builder/Desktop.java index d66ecf36b9e1..68ee93ababe0 100644 --- a/src/main/java/com/designpatterns/creational/builder/Desktop.java +++ b/src/main/java/com/designpatterns/creational/builder/Desktop.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.builder; +package com.designpatterns.creational.builder; /** * The Builder is a design pattern designed to provide a flexible solution to various object creation problems in @@ -26,6 +26,18 @@ private Desktop(DesktopBuilder builder) { this.graphicCard = builder.graphicCard; } + @Override + public String toString() { + return "Desktop{" + + "CPU='" + CPU + '\'' + + ", RAM='" + RAM + '\'' + + ", isGraphicCardEnabled=" + isGraphicCardEnabled + + ", operatingSystem='" + operatingSystem + '\'' + + ", diskSizeGB=" + diskSizeGB + + ", graphicCard='" + graphicCard + '\'' + + '}'; + } + /** * Builder class for the above Desktop class. Constructs the Desktop by invoking the Desktop class constructor and * allows access to set optional fields in the Desktop class. @@ -67,16 +79,4 @@ public Desktop build() { return new Desktop(this); } } - - @Override - public String toString() { - return "Desktop{" + - "CPU='" + CPU + '\'' + - ", RAM='" + RAM + '\'' + - ", isGraphicCardEnabled=" + isGraphicCardEnabled + - ", operatingSystem='" + operatingSystem + '\'' + - ", diskSizeGB=" + diskSizeGB + - ", graphicCard='" + graphicCard + '\'' + - '}'; - } } diff --git a/src/main/java/com/designpatterns/creational/factory/Pentagon.java b/src/main/java/com/designpatterns/creational/factory/Pentagon.java index 2d70f0dd5869..08ad798a0175 100644 --- a/src/main/java/com/designpatterns/creational/factory/Pentagon.java +++ b/src/main/java/com/designpatterns/creational/factory/Pentagon.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; public class Pentagon implements Polygon { @Override diff --git a/src/main/java/com/designpatterns/creational/factory/Polygon.java b/src/main/java/com/designpatterns/creational/factory/Polygon.java index 0d5b4c9e8af2..355177503dd2 100644 --- a/src/main/java/com/designpatterns/creational/factory/Polygon.java +++ b/src/main/java/com/designpatterns/creational/factory/Polygon.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; public interface Polygon { /** diff --git a/src/main/java/com/designpatterns/creational/factory/PolygonFactory.java b/src/main/java/com/designpatterns/creational/factory/PolygonFactory.java index 8b3147248b15..bccaa6ca3eb6 100644 --- a/src/main/java/com/designpatterns/creational/factory/PolygonFactory.java +++ b/src/main/java/com/designpatterns/creational/factory/PolygonFactory.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; /** * In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal diff --git a/src/main/java/com/designpatterns/creational/factory/Square.java b/src/main/java/com/designpatterns/creational/factory/Square.java index 562233cd9929..fbb2ce200add 100644 --- a/src/main/java/com/designpatterns/creational/factory/Square.java +++ b/src/main/java/com/designpatterns/creational/factory/Square.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; public class Square implements Polygon { diff --git a/src/main/java/com/designpatterns/creational/factory/Triangle.java b/src/main/java/com/designpatterns/creational/factory/Triangle.java index d95228b041d2..5c00cf4690db 100644 --- a/src/main/java/com/designpatterns/creational/factory/Triangle.java +++ b/src/main/java/com/designpatterns/creational/factory/Triangle.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; public class Triangle implements Polygon { @Override diff --git a/src/main/java/com/designpatterns/creational/prototype/BlackColor.java b/src/main/java/com/designpatterns/creational/prototype/BlackColor.java index b70a18296f46..34902a76fbd7 100644 --- a/src/main/java/com/designpatterns/creational/prototype/BlackColor.java +++ b/src/main/java/com/designpatterns/creational/prototype/BlackColor.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; class BlackColor extends Color { diff --git a/src/main/java/com/designpatterns/creational/prototype/BlueColor.java b/src/main/java/com/designpatterns/creational/prototype/BlueColor.java index 231103fc0769..b9bfb25871c8 100644 --- a/src/main/java/com/designpatterns/creational/prototype/BlueColor.java +++ b/src/main/java/com/designpatterns/creational/prototype/BlueColor.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; class BlueColor extends Color { diff --git a/src/main/java/com/designpatterns/creational/prototype/Color.java b/src/main/java/com/designpatterns/creational/prototype/Color.java index 9706c2f00cb8..505fc63db233 100644 --- a/src/main/java/com/designpatterns/creational/prototype/Color.java +++ b/src/main/java/com/designpatterns/creational/prototype/Color.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; /** * The prototype pattern is used when the type of objects to create is determined by a prototypical instance, which diff --git a/src/main/java/com/designpatterns/creational/prototype/ColorStore.java b/src/main/java/com/designpatterns/creational/prototype/ColorStore.java index fc6b51961c70..9265466f84c2 100644 --- a/src/main/java/com/designpatterns/creational/prototype/ColorStore.java +++ b/src/main/java/com/designpatterns/creational/prototype/ColorStore.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/com/designpatterns/creational/prototype/RedColor.java b/src/main/java/com/designpatterns/creational/prototype/RedColor.java index 96392c432787..a895c45dd2d1 100644 --- a/src/main/java/com/designpatterns/creational/prototype/RedColor.java +++ b/src/main/java/com/designpatterns/creational/prototype/RedColor.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; class RedColor extends Color { diff --git a/src/main/java/com/designpatterns/creational/singleton/Singleton.java b/src/main/java/com/designpatterns/creational/singleton/Singleton.java index 9591303b5161..87818459ef3a 100644 --- a/src/main/java/com/designpatterns/creational/singleton/Singleton.java +++ b/src/main/java/com/designpatterns/creational/singleton/Singleton.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.creational.singleton; +package com.designpatterns.creational.singleton; /** * The singleton pattern is a design pattern that restricts the instantiation of a class to one "single" instance. diff --git a/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java index 4cffba4377a9..36f2eaa45c65 100644 --- a/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java +++ b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.structural.adapter; +package com.designpatterns.structural.adapter; public class BugattiVeyron implements Movable { @Override diff --git a/src/main/java/com/designpatterns/structural/adapter/Movable.java b/src/main/java/com/designpatterns/structural/adapter/Movable.java index eeca412a5801..266a297bb1a8 100644 --- a/src/main/java/com/designpatterns/structural/adapter/Movable.java +++ b/src/main/java/com/designpatterns/structural/adapter/Movable.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.structural.adapter; +package com.designpatterns.structural.adapter; public interface Movable { // Returns the speed of the movable in MPH diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java index 3cd11eac0c4b..b3c496460dcb 100644 --- a/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.structural.adapter; +package com.designpatterns.structural.adapter; /** * An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java index c8d819a1f165..e77ebe8ac1e3 100644 --- a/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java @@ -1,4 +1,4 @@ -package src.main.java.com.designpatterns.structural.adapter; +package com.designpatterns.structural.adapter; public class MovableAdapterImpl implements MovableAdapter { private Movable luxuryCars; diff --git a/src/main/java/com/generation/SimplexNoise.java b/src/main/java/com/generation/SimplexNoise.java index fb4473dcf293..e7071190c421 100644 --- a/src/main/java/com/generation/SimplexNoise.java +++ b/src/main/java/com/generation/SimplexNoise.java @@ -1,4 +1,4 @@ -package src.main.java.com.generation; +package com.generation; import java.util.Random; diff --git a/src/main/java/com/generation/SimplexNoiseOctave.java b/src/main/java/com/generation/SimplexNoiseOctave.java index b1e281d247e5..f236e7e9cb4b 100644 --- a/src/main/java/com/generation/SimplexNoiseOctave.java +++ b/src/main/java/com/generation/SimplexNoiseOctave.java @@ -1,4 +1,4 @@ -package src.main.java.com.generation; +package com.generation; import java.util.Random; diff --git a/src/main/java/com/matchings/stableMatching/GaleShapley.java b/src/main/java/com/matchings/stableMatching/GaleShapley.java index 18624f9be143..64d7323dc2a0 100644 --- a/src/main/java/com/matchings/stableMatching/GaleShapley.java +++ b/src/main/java/com/matchings/stableMatching/GaleShapley.java @@ -1,4 +1,4 @@ -package src.main.java.com.matchings.stableMatching; +package com.matchings.stableMatching; public class GaleShapley { diff --git a/src/main/java/com/others/FastPower.java b/src/main/java/com/others/FastPower.java index ef1bd8955600..fff0b85a7e64 100644 --- a/src/main/java/com/others/FastPower.java +++ b/src/main/java/com/others/FastPower.java @@ -1,4 +1,4 @@ -package src.main.java.com.others; +package com.others; import java.math.BigInteger; @@ -6,7 +6,6 @@ * We may calculate power with loops, but what if the index is too large ? * FastPower aims to calculate quickly in this circumstances with time complexity O(log k), * where k is the index. - * */ public class FastPower { public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) { diff --git a/src/main/java/com/search/BinarySearch.java b/src/main/java/com/search/BinarySearch.java index d7d365d46ee8..c1ae44d7aee8 100644 --- a/src/main/java/com/search/BinarySearch.java +++ b/src/main/java/com/search/BinarySearch.java @@ -1,4 +1,4 @@ -package src.main.java.com.search; +package com.search; /** * Binary search is an algorithm which finds the position of a target value within a sorted array diff --git a/src/main/java/com/search/BloomFilter.java b/src/main/java/com/search/BloomFilter.java index 70a7f1c3d990..9b780fd5ee02 100644 --- a/src/main/java/com/search/BloomFilter.java +++ b/src/main/java/com/search/BloomFilter.java @@ -1,4 +1,4 @@ -package src.main.java.com.search; +package com.search; import java.io.Serializable; import java.util.ArrayList; @@ -15,7 +15,6 @@ * if assert an element exists, but not necessarily. *

* The accuracy rate depends on capacity and hash functions. - * */ public class BloomFilter implements Serializable { private static final long serialVersionUID = -4466610350741278658L; @@ -52,6 +51,22 @@ public static Builder builder(int capacity) { return new Builder(capacity); } + private static void checkNotNull(String element, String msg) { + if (element == null) { + throw new NullPointerException(msg + " must be not null"); + } + } + + private static int nextPowerOf2(int i) { + int n = i - 1; + n |= n >>> 1; + n |= n >>> 2; + n |= n >>> 4; + n |= n >>> 8; + n |= n >>> 16; + return (n < 0) ? 1 : (n >= 0x40000000) ? 0x40000000 : n + 1; + } + /** * Add an element to the Bloom filter. */ @@ -90,22 +105,6 @@ public int size() { return size; } - private static void checkNotNull(String element, String msg) { - if (element == null) { - throw new NullPointerException(msg + " must be not null"); - } - } - - private static int nextPowerOf2(int i) { - int n = i - 1; - n |= n >>> 1; - n |= n >>> 2; - n |= n >>> 4; - n |= n >>> 8; - n |= n >>> 16; - return (n < 0) ? 1 : (n >= 0x40000000) ? 0x40000000 : n + 1; - } - /** * We need a list of unmodifiable hash functions. */ diff --git a/src/main/java/com/search/ExponentialSearch.java b/src/main/java/com/search/ExponentialSearch.java index 0f30f0906922..178ab84a9c52 100644 --- a/src/main/java/com/search/ExponentialSearch.java +++ b/src/main/java/com/search/ExponentialSearch.java @@ -1,4 +1,4 @@ -package src.main.java.com.search; +package com.search; import java.util.Arrays; diff --git a/src/main/java/com/search/FibonacciSearch.java b/src/main/java/com/search/FibonacciSearch.java index 3bbcd0f89a7e..e443d0292b83 100644 --- a/src/main/java/com/search/FibonacciSearch.java +++ b/src/main/java/com/search/FibonacciSearch.java @@ -1,4 +1,4 @@ -package src.main.java.com.search; +package com.search; import static java.lang.Math.min; diff --git a/src/main/java/com/search/InterpolationSearch.java b/src/main/java/com/search/InterpolationSearch.java index 4db1e3a74e78..8ce122bb5e92 100644 --- a/src/main/java/com/search/InterpolationSearch.java +++ b/src/main/java/com/search/InterpolationSearch.java @@ -1,60 +1,60 @@ -package src.main.java.com.search; +package com.search; public class InterpolationSearch { - /** - * A linear interpolation search algorithm that finds the position of a - * target value in an sorted array using its lowest value and highest value. - * - * Average time-complexity O(log log n) - uniformly distributed. - * Worst-case time-complexity O(n) - non-uniformly distributed - * Worst-case space complexity O(1) - * - * @param This is any comparable type - * @param arr This is the array where the element should be found - * @param key This is the element to find in the array - * @return The index of the key in the array - */ - public > int findIndex(T arr[], T key) { - return checkCondition(arr, key, 0, arr.length - 1); - } + /** + * A linear interpolation search algorithm that finds the position of a + * target value in an sorted array using its lowest value and highest value. + *

+ * Average time-complexity O(log log n) - uniformly distributed. + * Worst-case time-complexity O(n) - non-uniformly distributed + * Worst-case space complexity O(1) + * + * @param This is any comparable type + * @param arr This is the array where the element should be found + * @param key This is the element to find in the array + * @return The index of the key in the array + */ + public > int findIndex(T[] arr, T key) { + return checkCondition(arr, key, 0, arr.length - 1); + } - /** - * @param lowIndex The first and smallest element in the sorted array - * @param highIndex The last and largest element in the sorted array - * @return The found key's index in the array through iteration - */ - private > int checkCondition(T arr[], T key, int lowIndex, int highIndex) { - boolean conditionOne = lowIndex <= highIndex && key.compareTo(arr[lowIndex]) >= 0; - boolean conditionTwo = key.compareTo(arr[lowIndex]) == 0 && key.compareTo(arr[highIndex]) <= 0; - while (conditionOne || conditionTwo) { - int position = getPostion(arr, key, lowIndex, highIndex); - if (arr[position].equals(key)) - return position; + /** + * @param lowIndex The first and smallest element in the sorted array + * @param highIndex The last and largest element in the sorted array + * @return The found key's index in the array through iteration + */ + private > int checkCondition(T[] arr, T key, int lowIndex, int highIndex) { + boolean conditionOne = lowIndex <= highIndex && key.compareTo(arr[lowIndex]) >= 0; + boolean conditionTwo = key.compareTo(arr[lowIndex]) == 0 && key.compareTo(arr[highIndex]) <= 0; + while (conditionOne || conditionTwo) { + int position = getPostion(arr, key, lowIndex, highIndex); + if (arr[position].equals(key)) + return position; - if (arr[position].compareTo(key) < 0) - lowIndex = position + 1; - else - highIndex = position - 1; - } - return -1; - } + if (arr[position].compareTo(key) < 0) + lowIndex = position + 1; + else + highIndex = position - 1; + } + return -1; + } - /** - * @return The array's current retrieved index position - */ - private int getPostion(T arr[], T key, int lowIndex, int highIndex) { - String startValueString = arr[lowIndex].toString(); //First convert array element to String - int startValueInt = Integer.parseInt(startValueString); //Convert String to int to computate later - String endValueString = arr[highIndex].toString(); - int endValueInt = Integer.parseInt(endValueString); - String keyValueString = key.toString(); //Repeat for key to later computate - int keyValueInt = Integer.parseInt(keyValueString); + /** + * @return The array's current retrieved index position + */ + private int getPostion(T[] arr, T key, int lowIndex, int highIndex) { + String startValueString = arr[lowIndex].toString(); //First convert array element to String + int startValueInt = Integer.parseInt(startValueString); //Convert String to int to computate later + String endValueString = arr[highIndex].toString(); + int endValueInt = Integer.parseInt(endValueString); + String keyValueString = key.toString(); //Repeat for key to later computate + int keyValueInt = Integer.parseInt(keyValueString); - int arrayIndexRangeDiff = highIndex - lowIndex; - int arrayHighLowValuesDiff = endValueInt - startValueInt; - int keyMinusArrayLowValue = keyValueInt - startValueInt; - int position = lowIndex + (((arrayIndexRangeDiff) / (arrayHighLowValuesDiff) * (keyMinusArrayLowValue))); - return position; - } + int arrayIndexRangeDiff = highIndex - lowIndex; + int arrayHighLowValuesDiff = endValueInt - startValueInt; + int keyMinusArrayLowValue = keyValueInt - startValueInt; + int position = lowIndex + (((arrayIndexRangeDiff) / (arrayHighLowValuesDiff) * (keyMinusArrayLowValue))); + return position; + } } \ No newline at end of file diff --git a/src/main/java/com/search/JumpSearch.java b/src/main/java/com/search/JumpSearch.java index 949619e92919..144a586a6b2a 100644 --- a/src/main/java/com/search/JumpSearch.java +++ b/src/main/java/com/search/JumpSearch.java @@ -1,84 +1,84 @@ -package src.main.java.com.search; +package com.search; public class JumpSearch { - /** - * A jump search algorithm that finds the position of a key by moving over - * fixed block ranges in a sorted array, and linear searches back on itself to - * find it. - * - * Worst case time complexity: O(N^(1/2)) - square root n - * Average case time complexity: O(N^(1/2)) - square root n - * Worst case Space complexity: O(1) - * - * @param This is any comparable type - * @param arr This is the array where the element should be found - * @param key This is the element to find in the array - * @return The index of the key in the array - */ - public > int findIndex(T arr[], T key) { - return checkCondition(arr, key, arr.length); - } + /** + * A jump search algorithm that finds the position of a key by moving over + * fixed block ranges in a sorted array, and linear searches back on itself to + * find it. + *

+ * Worst case time complexity: O(N^(1/2)) - square root n + * Average case time complexity: O(N^(1/2)) - square root n + * Worst case Space complexity: O(1) + * + * @param This is any comparable type + * @param arr This is the array where the element should be found + * @param key This is the element to find in the array + * @return The index of the key in the array + */ + public > int findIndex(T[] arr, T key) { + return checkCondition(arr, key, arr.length); + } - /** - * @param arrLength The array's length - * @return The index position of the key in the array - */ - public > int checkCondition(T arr[], T key, int arrLength) { - int step = (int) Math.floor(Math.sqrt(arrLength)); // Find jump block - int previous = 0; // Find block where element is / or not present + /** + * @param arrLength The array's length + * @return The index position of the key in the array + */ + public > int checkCondition(T[] arr, T key, int arrLength) { + int step = (int) Math.floor(Math.sqrt(arrLength)); // Find jump block + int previous = 0; // Find block where element is / or not present - // Use ternary operator to find if step or array length is min value - // and then minus the min value by one - int minVal = (step < arrLength) ? step - 1 : arrLength - 1; + // Use ternary operator to find if step or array length is min value + // and then minus the min value by one + int minVal = (step < arrLength) ? step - 1 : arrLength - 1; - String arrayMinValIndexString = arr[minVal].toString(); - int arrayMinValIndexInt = Integer.parseInt(arrayMinValIndexString); - String keyValueString = key.toString(); - int keyValueInt = Integer.parseInt(keyValueString); + String arrayMinValIndexString = arr[minVal].toString(); + int arrayMinValIndexInt = Integer.parseInt(arrayMinValIndexString); + String keyValueString = key.toString(); + int keyValueInt = Integer.parseInt(keyValueString); - // Increment next step and previous step in block to find range block - while (arrayMinValIndexInt < keyValueInt) { - previous = step; - step += (int) Math.floor(Math.sqrt(arrLength)); - if (previous >= arrLength) - return -1; - minVal = (step < arrLength) ? step - 1 : arrLength - 1; - arrayMinValIndexString = arr[minVal].toString(); - arrayMinValIndexInt = Integer.parseInt(arrayMinValIndexString); - } - // Get key position in linear search - int position = linearSearchBlock(arr, key, step, previous, keyValueInt, arrLength, minVal); - return position; - } + // Increment next step and previous step in block to find range block + while (arrayMinValIndexInt < keyValueInt) { + previous = step; + step += (int) Math.floor(Math.sqrt(arrLength)); + if (previous >= arrLength) + return -1; + minVal = (step < arrLength) ? step - 1 : arrLength - 1; + arrayMinValIndexString = arr[minVal].toString(); + arrayMinValIndexInt = Integer.parseInt(arrayMinValIndexString); + } + // Get key position in linear search + int position = linearSearchBlock(arr, key, step, previous, keyValueInt, arrLength, minVal); + return position; + } - /** - * @param step The next block index in the array - * @param previous The previous block index in the array - * @param keyValueInt The key in the format of an integer - * @param minVal The minimum value of either next step or array length - * @return The index position of the key in the array - */ - public > int linearSearchBlock(T arr[], T key, int step, int previous, int keyValueInt, - int arrLength, int minVal) { + /** + * @param step The next block index in the array + * @param previous The previous block index in the array + * @param keyValueInt The key in the format of an integer + * @param minVal The minimum value of either next step or array length + * @return The index position of the key in the array + */ + public > int linearSearchBlock(T[] arr, T key, int step, int previous, int keyValueInt, + int arrLength, int minVal) { - // Linear search starting from previous block forwards. - String arrayPositionString = arr[previous].toString(); - int arrayPositionValue = Integer.parseInt(arrayPositionString); - while (arrayPositionValue < keyValueInt) { - // If in next block or end of array length, key not in array - if (previous == minVal) - return -1; - previous++; - // Update arrayPositionValue in iteration - minVal = (step < arrLength) ? step - 1 : arrLength - 1; - arrayPositionString = arr[previous].toString(); - arrayPositionValue = Integer.parseInt(arrayPositionString); + // Linear search starting from previous block forwards. + String arrayPositionString = arr[previous].toString(); + int arrayPositionValue = Integer.parseInt(arrayPositionString); + while (arrayPositionValue < keyValueInt) { + // If in next block or end of array length, key not in array + if (previous == minVal) + return -1; + previous++; + // Update arrayPositionValue in iteration + minVal = (step < arrLength) ? step - 1 : arrLength - 1; + arrayPositionString = arr[previous].toString(); + arrayPositionValue = Integer.parseInt(arrayPositionString); - } - // If the key is found - if (arrayPositionValue == keyValueInt) - return previous; - return -1; - } + } + // If the key is found + if (arrayPositionValue == keyValueInt) + return previous; + return -1; + } } \ No newline at end of file diff --git a/src/main/java/com/search/LinearSearch.java b/src/main/java/com/search/LinearSearch.java index 66db8aadff0f..20d1154ec3f0 100644 --- a/src/main/java/com/search/LinearSearch.java +++ b/src/main/java/com/search/LinearSearch.java @@ -1,8 +1,8 @@ -package src.main.java.com.search; +package com.search; /** * Linear search is an algorithm which finds the position of a target value within an array (Usually unsorted) - * + *

* Worst-case performance O(n) * Best-case performance O(1) * Average performance O(n) @@ -11,8 +11,8 @@ public final class LinearSearch { /** * @param array is an array where the element should be found - * @param key is an element which should be found - * @param is any comparable type + * @param key is an element which should be found + * @param is any comparable type * @return index of the element */ public static > int findIndex(T[] array, T key) { @@ -21,12 +21,12 @@ public static > int findIndex(T[] array, T key) { /** * @param array The array to search - * @param key The element you are looking for + * @param key The element you are looking for * @return the location of the key or -1 if the element is not found **/ - private static > int search(T[] array, T key){ - for(int i = 0; i < array.length;i++) { - if (array[i].compareTo(key) == 0){ + private static > int search(T[] array, T key) { + for (int i = 0; i < array.length; i++) { + if (array[i].compareTo(key) == 0) { return i; } } diff --git a/src/main/java/com/sorts/BubbleSort.java b/src/main/java/com/sorts/BubbleSort.java index c532a646f33b..b2b89647fd02 100644 --- a/src/main/java/com/sorts/BubbleSort.java +++ b/src/main/java/com/sorts/BubbleSort.java @@ -1,29 +1,29 @@ -package src.main.java.com.sorts; +package com.sorts; -import src.main.java.com.types.Sort; +import com.types.Sort; public class BubbleSort implements Sort { - /** - * This method implements the Generic Bubble Sort - * - * @param array The array to be sorted - * Sorts the array in increasing order - **/ + /** + * This method implements the Generic Bubble Sort + * + * @param array The array to be sorted + * Sorts the array in increasing order + **/ - @Override - public > T[] sort(T[] array) { - int last = array.length; - //Sorting - boolean swap; - do { - swap = false; - for (int count = 0; count < last - 1; count++) { - if (SortUtils.less(array[count + 1], array[count])) { - swap = SortUtils.swap(array, count, count + 1); - } - } - last--; - } while (swap); - return array; - } + @Override + public > T[] sort(T[] array) { + int last = array.length; + //Sorting + boolean swap; + do { + swap = false; + for (int count = 0; count < last - 1; count++) { + if (SortUtils.less(array[count + 1], array[count])) { + swap = SortUtils.swap(array, count, count + 1); + } + } + last--; + } while (swap); + return array; + } } \ No newline at end of file diff --git a/src/main/java/com/sorts/CountingSort.java b/src/main/java/com/sorts/CountingSort.java index 987700e1ae4e..32efbce14c70 100644 --- a/src/main/java/com/sorts/CountingSort.java +++ b/src/main/java/com/sorts/CountingSort.java @@ -1,4 +1,4 @@ -package src.main.java.com.sorts; +package com.sorts; public class CountingSort { diff --git a/src/main/java/com/sorts/CycleSort.java b/src/main/java/com/sorts/CycleSort.java index 5804a7b4bcd0..a1e5a5bf4b17 100644 --- a/src/main/java/com/sorts/CycleSort.java +++ b/src/main/java/com/sorts/CycleSort.java @@ -1,4 +1,4 @@ -package src.main.java.com.sorts; +package com.sorts; public class CycleSort { diff --git a/src/main/java/com/sorts/HeapSort.java b/src/main/java/com/sorts/HeapSort.java index 4135e5baf72d..f923f8b7b7de 100644 --- a/src/main/java/com/sorts/HeapSort.java +++ b/src/main/java/com/sorts/HeapSort.java @@ -1,14 +1,35 @@ -package src.main.java.com.sorts; +package com.sorts; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static src.main.java.com.sorts.SortUtils.less; -import static src.main.java.com.sorts.SortUtils.swap; +import static com.sorts.SortUtils.less; +import static com.sorts.SortUtils.swap; public class HeapSort { + public > T[] sort(T[] unsorted) { + return sort(Arrays.asList(unsorted)).toArray(unsorted); + } + + private > List sort(List unsorted) { + int size = unsorted.size(); + + @SuppressWarnings("unchecked") + Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()])); + + // make min heap using index 0 as root. + heap.makeMinHeap(0); + List sorted = new ArrayList<>(size); + while (size > 0) { + T min = heap.getRoot(--size); + sorted.add(min); + } + + return sorted; + } + private static class Heap> { /** * Array to store heap @@ -90,25 +111,4 @@ private T getRoot(int size) { } - - public > T[] sort(T[] unsorted) { - return sort(Arrays.asList(unsorted)).toArray(unsorted); - } - - private > List sort(List unsorted) { - int size = unsorted.size(); - - @SuppressWarnings("unchecked") - Heap heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()])); - - // make min heap using index 0 as root. - heap.makeMinHeap(0); - List sorted = new ArrayList<>(size); - while (size > 0) { - T min = heap.getRoot(--size); - sorted.add(min); - } - - return sorted; - } } \ No newline at end of file diff --git a/src/main/java/com/sorts/InsertionSort.java b/src/main/java/com/sorts/InsertionSort.java index e7deb8970149..776cf13b5e9f 100644 --- a/src/main/java/com/sorts/InsertionSort.java +++ b/src/main/java/com/sorts/InsertionSort.java @@ -1,27 +1,27 @@ -package src.main.java.com.sorts; +package com.sorts; public class InsertionSort { - /** - * This method implements the Generic Insertion Sort - * Sorts the array in increasing order - * - * @param array The array to be sorted - **/ - public > T[] sort(T[] array) { - for (int j = 1; j < array.length; j++) { + /** + * This method implements the Generic Insertion Sort + * Sorts the array in increasing order + * + * @param array The array to be sorted + **/ + public > T[] sort(T[] array) { + for (int j = 1; j < array.length; j++) { - // Picking up the key(Card) - T key = array[j]; - int i = j - 1; + // Picking up the key(Card) + T key = array[j]; + int i = j - 1; - while (i >= 0 && SortUtils.less(key, array[i])) { - array[i + 1] = array[i]; - i--; - } - // Placing the key (Card) at its correct position in the sorted subarray - array[i + 1] = key; + while (i >= 0 && SortUtils.less(key, array[i])) { + array[i + 1] = array[i]; + i--; + } + // Placing the key (Card) at its correct position in the sorted subarray + array[i + 1] = key; + } + return array; } - return array; - } } diff --git a/src/main/java/com/sorts/MergeSort.java b/src/main/java/com/sorts/MergeSort.java index 6d6516b2c1cc..dd61f7ba9287 100644 --- a/src/main/java/com/sorts/MergeSort.java +++ b/src/main/java/com/sorts/MergeSort.java @@ -1,21 +1,7 @@ -package src.main.java.com.sorts; +package com.sorts; public class MergeSort { - /** - * This method implements the Generic Merge Sort - * - * @param unsorted the array which should be sorted - * @param Comparable class - * @return sorted array - */ - @SuppressWarnings("unchecked") - public > T[] sort(T[] unsorted) { - T[] tmp = (T[]) new Comparable[unsorted.length]; - doSort(unsorted, tmp, 0, unsorted.length - 1); - return unsorted; - } - /** * @param arr The array to be sorted * @param temp The copy of the actual array @@ -72,4 +58,18 @@ private static > void merge(T[] arr, T[] temp, int left, k++; } } + + /** + * This method implements the Generic Merge Sort + * + * @param unsorted the array which should be sorted + * @param Comparable class + * @return sorted array + */ + @SuppressWarnings("unchecked") + public > T[] sort(T[] unsorted) { + T[] tmp = (T[]) new Comparable[unsorted.length]; + doSort(unsorted, tmp, 0, unsorted.length - 1); + return unsorted; + } } diff --git a/src/main/java/com/sorts/PigeonholeSort.java b/src/main/java/com/sorts/PigeonholeSort.java index 1f8b498b51fa..8080b587ea6d 100644 --- a/src/main/java/com/sorts/PigeonholeSort.java +++ b/src/main/java/com/sorts/PigeonholeSort.java @@ -1,4 +1,4 @@ -package src.main.java.com.sorts; +package com.sorts; public class PigeonholeSort { diff --git a/src/main/java/com/sorts/QuickSort.java b/src/main/java/com/sorts/QuickSort.java index e1a5f15d4928..8747f5f4bcc7 100644 --- a/src/main/java/com/sorts/QuickSort.java +++ b/src/main/java/com/sorts/QuickSort.java @@ -1,22 +1,10 @@ -package src.main.java.com.sorts; +package com.sorts; -import static src.main.java.com.sorts.SortUtils.less; -import static src.main.java.com.sorts.SortUtils.swap; +import static com.sorts.SortUtils.less; +import static com.sorts.SortUtils.swap; public class QuickSort { - /** - * This method implements the Generic Quick Sort - * - * @param array The array to be sorted - * Sorts the array in increasing order - **/ - public > T[] sort(T[] array) { - doSort(array, 0, array.length - 1); - return array; - } - - /** * The sorting process * @@ -61,4 +49,15 @@ private static > int partition(T[] array, int left, int } return left; } + + /** + * This method implements the Generic Quick Sort + * + * @param array The array to be sorted + * Sorts the array in increasing order + **/ + public > T[] sort(T[] array) { + doSort(array, 0, array.length - 1); + return array; + } } \ No newline at end of file diff --git a/src/main/java/com/sorts/SelectionSort.java b/src/main/java/com/sorts/SelectionSort.java index b498aa79cefd..ccea60a6e832 100644 --- a/src/main/java/com/sorts/SelectionSort.java +++ b/src/main/java/com/sorts/SelectionSort.java @@ -1,7 +1,7 @@ -package src.main.java.com.sorts; +package com.sorts; -import static src.main.java.com.sorts.SortUtils.less; -import static src.main.java.com.sorts.SortUtils.swap; +import static com.sorts.SortUtils.less; +import static com.sorts.SortUtils.swap; public class SelectionSort { diff --git a/src/main/java/com/sorts/ShellSort.java b/src/main/java/com/sorts/ShellSort.java index 8ec8e4159475..3cd0181965b0 100644 --- a/src/main/java/com/sorts/ShellSort.java +++ b/src/main/java/com/sorts/ShellSort.java @@ -1,7 +1,7 @@ -package src.main.java.com.sorts; +package com.sorts; -import static src.main.java.com.sorts.SortUtils.less; -import static src.main.java.com.sorts.SortUtils.swap; +import static com.sorts.SortUtils.less; +import static com.sorts.SortUtils.swap; public class ShellSort { diff --git a/src/main/java/com/sorts/SortUtils.java b/src/main/java/com/sorts/SortUtils.java index bd5e5a65c6a9..56a7f8868ef8 100644 --- a/src/main/java/com/sorts/SortUtils.java +++ b/src/main/java/com/sorts/SortUtils.java @@ -1,4 +1,4 @@ -package src.main.java.com.sorts; +package com.sorts; final class SortUtils { diff --git a/src/main/java/com/sorts/StoogeSort.java b/src/main/java/com/sorts/StoogeSort.java index b57d35df4208..8d108622f2b4 100644 --- a/src/main/java/com/sorts/StoogeSort.java +++ b/src/main/java/com/sorts/StoogeSort.java @@ -1,7 +1,7 @@ -package src.main.java.com.sorts; +package com.sorts; -import static src.main.java.com.sorts.SortUtils.swap; -import static src.main.java.com.sorts.SortUtils.less; +import static com.sorts.SortUtils.less; +import static com.sorts.SortUtils.swap; public class StoogeSort { @@ -9,8 +9,8 @@ public class StoogeSort { * This method implements recursion StoogeSort * * @param arr array to store number elements - * @param f first element in the array - * @param l last element in the array + * @param f first element in the array + * @param l last element in the array */ public > T[] sort(T[] arr, int f, int l) { diff --git a/src/main/java/com/types/DataStructure.java b/src/main/java/com/types/DataStructure.java index e1ffe043ef98..c8151ea32428 100644 --- a/src/main/java/com/types/DataStructure.java +++ b/src/main/java/com/types/DataStructure.java @@ -1,4 +1,4 @@ -package src.main.java.com.types; +package com.types; import java.util.Iterator; diff --git a/src/main/java/com/types/Queue.java b/src/main/java/com/types/Queue.java index 19c74e64a854..5498d3945234 100644 --- a/src/main/java/com/types/Queue.java +++ b/src/main/java/com/types/Queue.java @@ -1,4 +1,4 @@ -package src.main.java.com.types; +package com.types; import java.util.NoSuchElementException; diff --git a/src/main/java/com/types/Sort.java b/src/main/java/com/types/Sort.java index e3316b4ebc4d..cc9519caf8e4 100644 --- a/src/main/java/com/types/Sort.java +++ b/src/main/java/com/types/Sort.java @@ -1,4 +1,4 @@ -package src.main.java.com.types; +package com.types; @FunctionalInterface public interface Sort { diff --git a/src/test/java/com/conversions/AnyBaseToDecimalTest.java b/src/test/java/com/conversions/AnyBaseToDecimalTest.java index 2659234ba7b6..148fd7449b4a 100644 --- a/src/test/java/com/conversions/AnyBaseToDecimalTest.java +++ b/src/test/java/com/conversions/AnyBaseToDecimalTest.java @@ -1,39 +1,39 @@ -package src.test.java.com.conversions; +package com.conversions; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.conversions.AnyBaseToDecimal; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class AnyBaseToDecimalTest { - @Test - public void testAnyBaseToDecimal() { - AnyBaseToDecimal anyBaseToDecimal = new AnyBaseToDecimal(); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("2", 2)); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("3", 2)); - Assert.assertEquals("3", anyBaseToDecimal.convertToDecimal("11", 2)); - Assert.assertEquals("4", anyBaseToDecimal.convertToDecimal("100", 2)); - Assert.assertEquals("5", anyBaseToDecimal.convertToDecimal("101", 2)); - Assert.assertEquals("10", anyBaseToDecimal.convertToDecimal("1010", 2)); - Assert.assertEquals("1024", anyBaseToDecimal.convertToDecimal("10000000000", 2)); + @Test + public void testAnyBaseToDecimal() { + AnyBaseToDecimal anyBaseToDecimal = new AnyBaseToDecimal(); + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("2", 2)); + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("3", 2)); + assertEquals("3", anyBaseToDecimal.convertToDecimal("11", 2)); + assertEquals("4", anyBaseToDecimal.convertToDecimal("100", 2)); + assertEquals("5", anyBaseToDecimal.convertToDecimal("101", 2)); + assertEquals("10", anyBaseToDecimal.convertToDecimal("1010", 2)); + assertEquals("1024", anyBaseToDecimal.convertToDecimal("10000000000", 2)); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("8", 8)); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("9", 8)); - Assert.assertEquals("7", anyBaseToDecimal.convertToDecimal("7", 8)); - Assert.assertEquals("8", anyBaseToDecimal.convertToDecimal("10", 8)); - Assert.assertEquals("9", anyBaseToDecimal.convertToDecimal("11", 8)); - Assert.assertEquals("10", anyBaseToDecimal.convertToDecimal("12", 8)); - Assert.assertEquals("1024", anyBaseToDecimal.convertToDecimal("2000", 8)); + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("8", 8)); + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("9", 8)); + assertEquals("7", anyBaseToDecimal.convertToDecimal("7", 8)); + assertEquals("8", anyBaseToDecimal.convertToDecimal("10", 8)); + assertEquals("9", anyBaseToDecimal.convertToDecimal("11", 8)); + assertEquals("10", anyBaseToDecimal.convertToDecimal("12", 8)); + assertEquals("1024", anyBaseToDecimal.convertToDecimal("2000", 8)); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("A", 10)); - Assert.assertEquals("10", anyBaseToDecimal.convertToDecimal("10", 10)); - Assert.assertEquals("1024", anyBaseToDecimal.convertToDecimal("1024", 10)); + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("A", 10)); + assertEquals("10", anyBaseToDecimal.convertToDecimal("10", 10)); + assertEquals("1024", anyBaseToDecimal.convertToDecimal("1024", 10)); - Assert.assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("G", 16)); - Assert.assertEquals("16", anyBaseToDecimal.convertToDecimal("10", 16)); - Assert.assertEquals("17", anyBaseToDecimal.convertToDecimal("11", 16)); - Assert.assertEquals("100", anyBaseToDecimal.convertToDecimal("64", 16)); - Assert.assertEquals("225", anyBaseToDecimal.convertToDecimal("E1", 16)); - Assert.assertEquals("1024", anyBaseToDecimal.convertToDecimal("400", 16)); - } + assertEquals("Invalid Number", anyBaseToDecimal.convertToDecimal("G", 16)); + assertEquals("16", anyBaseToDecimal.convertToDecimal("10", 16)); + assertEquals("17", anyBaseToDecimal.convertToDecimal("11", 16)); + assertEquals("100", anyBaseToDecimal.convertToDecimal("64", 16)); + assertEquals("225", anyBaseToDecimal.convertToDecimal("E1", 16)); + assertEquals("1024", anyBaseToDecimal.convertToDecimal("400", 16)); + } } diff --git a/src/test/java/com/conversions/BinaryToGrayTest.java b/src/test/java/com/conversions/BinaryToGrayTest.java index 64de8925756c..2555341b8c80 100644 --- a/src/test/java/com/conversions/BinaryToGrayTest.java +++ b/src/test/java/com/conversions/BinaryToGrayTest.java @@ -1,9 +1,8 @@ -package src.test.java.com.conversions; +package com.conversions; -import src.main.java.com.conversions.BinaryToGray; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class BinaryToGrayTest { diff --git a/src/test/java/com/conversions/BinaryToHexadecimalTest.java b/src/test/java/com/conversions/BinaryToHexadecimalTest.java index 801366c78f88..397b48a3b46b 100644 --- a/src/test/java/com/conversions/BinaryToHexadecimalTest.java +++ b/src/test/java/com/conversions/BinaryToHexadecimalTest.java @@ -1,16 +1,16 @@ -package src.test.java.com.conversions; +package com.conversions; -import org.junit.Test; -import src.main.java.com.conversions.BinaryToHexadecimal; -import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class BinaryToHexadecimalTest { @Test - public void testBinaryToHexadecimal(){ + public void testBinaryToHexadecimal() { BinaryToHexadecimal binaryToHexadecimal = new BinaryToHexadecimal(); - Assert.assertEquals("Incorrect Conversion", "2A", binaryToHexadecimal.binToHex("101010")); - Assert.assertEquals("Incorrect Conversion", "24", binaryToHexadecimal.binToHex("100100")); - Assert.assertEquals("Incorrect Conversion", "AAAAAAAAAAAAAAAAAA1", binaryToHexadecimal.binToHex("1010101010101010101010101010101010101010101010101010101010101010101010100001")); + assertEquals("2A", binaryToHexadecimal.binToHex("101010")); + assertEquals("24", binaryToHexadecimal.binToHex("100100")); + assertEquals("AAAAAAAAAAAAAAAAAA1", binaryToHexadecimal.binToHex("1010101010101010101010101010101010101010101010101010101010101010101010100001")); } } diff --git a/src/test/java/com/conversions/DecimalToAnyBaseTest.java b/src/test/java/com/conversions/DecimalToAnyBaseTest.java index f6166a6d79bd..449932b1f6de 100644 --- a/src/test/java/com/conversions/DecimalToAnyBaseTest.java +++ b/src/test/java/com/conversions/DecimalToAnyBaseTest.java @@ -1,16 +1,15 @@ -package src.test.java.com.conversions; +package com.conversions; -import src.main.java.com.conversions.DecimalToAnyBase; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static junit.framework.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class DecimalToAnyBaseTest { @Test public void testDecimalToAnyBase() { DecimalToAnyBase decimalToAnyBase = new DecimalToAnyBase(); - assertEquals("Incorrect Conversion", "100", decimalToAnyBase.convertToAnyBase(4, 2)); - assertEquals("Incorrect Conversion", "11", decimalToAnyBase.convertToAnyBase(4, 3)); + assertEquals("100", decimalToAnyBase.convertToAnyBase(4, 2)); + assertEquals("11", decimalToAnyBase.convertToAnyBase(4, 3)); } } diff --git a/src/test/java/com/conversions/DecimalToHexadecimalTest.java b/src/test/java/com/conversions/DecimalToHexadecimalTest.java index 765acb8b9f19..8b8bbb67478c 100644 --- a/src/test/java/com/conversions/DecimalToHexadecimalTest.java +++ b/src/test/java/com/conversions/DecimalToHexadecimalTest.java @@ -1,18 +1,18 @@ -package src.test.java.com.conversions; +package com.conversions; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.conversions.DecimalToHexadecimal; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class DecimalToHexadecimalTest { @Test public void testDecimalToHexadecimalTest() { DecimalToHexadecimal decimalToHexadecimal = new DecimalToHexadecimal(); - Assert.assertEquals("Incorrect Conversion", "F", decimalToHexadecimal.decimalToHex("15")); - Assert.assertEquals("Incorrect Conversion", "121", decimalToHexadecimal.decimalToHex("289")); - Assert.assertEquals("Incorrect Conversion", "AAAAAAAAAAAAAAAAAA1", decimalToHexadecimal.decimalToHex("50371909150609548946081")); - Assert.assertEquals("Incorrect Conversion", "A", decimalToHexadecimal.decimalToHex("10")); - Assert.assertEquals("Incorrect Conversion", "8B2F", decimalToHexadecimal.decimalToHex("35631")); + assertEquals("F", decimalToHexadecimal.decimalToHex("15")); + assertEquals("121", decimalToHexadecimal.decimalToHex("289")); + assertEquals("AAAAAAAAAAAAAAAAAA1", decimalToHexadecimal.decimalToHex("50371909150609548946081")); + assertEquals("A", decimalToHexadecimal.decimalToHex("10")); + assertEquals("8B2F", decimalToHexadecimal.decimalToHex("35631")); } } diff --git a/src/test/java/com/conversions/DecimalToOctalTest.java b/src/test/java/com/conversions/DecimalToOctalTest.java index 74a10948506c..76ca4ce29115 100644 --- a/src/test/java/com/conversions/DecimalToOctalTest.java +++ b/src/test/java/com/conversions/DecimalToOctalTest.java @@ -1,17 +1,18 @@ -package src.test.java.com.conversions; +package com.conversions; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.conversions.DecimalToOctal; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class DecimalToOctalTest { + @Test public void testDecimalToOctalTest() { DecimalToOctal decimalToOctal = new DecimalToOctal(); - Assert.assertEquals("Incorrect Conversion", "41", decimalToOctal.decimalToOctal("33")); - Assert.assertEquals("Incorrect Conversion", "5512", decimalToOctal.decimalToOctal("2890")); - Assert.assertEquals("Incorrect Conversion", "12525252525252525252525241", decimalToOctal.decimalToOctal("50371909150609548946081")); - Assert.assertEquals("Incorrect Conversion", "13", decimalToOctal.decimalToOctal("11")); - Assert.assertEquals("Incorrect Conversion", "46703754", decimalToOctal.decimalToOctal("10192876")); + assertEquals("41", decimalToOctal.decimalToOctal("33")); + assertEquals("5512", decimalToOctal.decimalToOctal("2890")); + assertEquals("12525252525252525252525241", decimalToOctal.decimalToOctal("50371909150609548946081")); + assertEquals("13", decimalToOctal.decimalToOctal("11")); + assertEquals("46703754", decimalToOctal.decimalToOctal("10192876")); } } diff --git a/src/test/java/com/crypto/codec/Base64Test.java b/src/test/java/com/crypto/codec/Base64Test.java index dcda19ae5875..a9891886fc1f 100644 --- a/src/test/java/com/crypto/codec/Base64Test.java +++ b/src/test/java/com/crypto/codec/Base64Test.java @@ -1,10 +1,8 @@ -package src.test.java.com.crypto.codec; +package com.crypto.codec; -import src.main.java.com.crypto.codec.Base64; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static junit.framework.Assert.*; -import static org.junit.Assert.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.*; public class Base64Test { @@ -12,7 +10,6 @@ public class Base64Test { * Test vectors are taken from: * https://tools.ietf.org/html/rfc4648#section-10 */ - @Test public void TestBase64Encode() { assertEquals("", Base64.encode("".getBytes())); @@ -35,13 +32,15 @@ public void TestBase64Decode() { assertArrayEquals("foobar".getBytes(), Base64.decode("Zm9vYmFy")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testInvalidBase64String() { - Base64.decode("Z/+v&mF="); + Exception exception = assertThrows(IllegalArgumentException.class, () -> Base64.decode("Z/+v&mF=")); + assertEquals(true, exception != null); } - @Test(expected = IllegalArgumentException.class) + @Test() public void testInvalidLengthOfBase64String() { - Base64.decode("Zm9v" + "YmFy" + "d"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> Base64.decode("Zm9v" + "YmFy" + "d")); + assertEquals(true, exception != null); } } diff --git a/src/test/java/com/crypto/hash/Sha2Test.java b/src/test/java/com/crypto/hash/Sha2Test.java index d684587988b4..62aa4ef35656 100644 --- a/src/test/java/com/crypto/hash/Sha2Test.java +++ b/src/test/java/com/crypto/hash/Sha2Test.java @@ -1,10 +1,9 @@ -package src.test.java.com.crypto.hash; +package com.crypto.hash; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import src.main.java.com.crypto.hash.Sha2; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; public class Sha2Test { @@ -14,7 +13,7 @@ public class Sha2Test { */ private static byte[][] vector; - @BeforeClass + @BeforeAll public static void setUpClass() { System.out.println("@BeforeClass setUpClass"); @@ -35,129 +34,129 @@ public static void setUpClass() { @Test public void TestSha224Vector1() { String digest = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA224(vector[0])); + assertEquals(digest, Sha2.SHA224(vector[0])); } @Test public void TestSha224Vector2() { String digest = "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA224(vector[1])); + assertEquals(digest, Sha2.SHA224(vector[1])); } @Test public void TestSha224Vector3() { String digest = "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA224(vector[2])); + assertEquals(digest, Sha2.SHA224(vector[2])); } @Test public void TestSha224Vector4() { String digest = "c97ca9a559850ce97a04a96def6d99a9e0e0e2ab14e6b8df265fc0b3"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA224(vector[3])); + assertEquals(digest, Sha2.SHA224(vector[3])); } @Test public void TestSha224Vector5() { String digest = "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA224(vector[4])); + assertEquals(digest, Sha2.SHA224(vector[4])); } @Test public void TestSha256Vector1() { String digest = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA256(vector[0])); + assertEquals(digest, Sha2.SHA256(vector[0])); } @Test public void TestSha256Vector2() { String digest = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA256(vector[1])); + assertEquals(digest, Sha2.SHA256(vector[1])); } @Test public void TestSha256Vector3() { String digest = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA256(vector[2])); + assertEquals(digest, Sha2.SHA256(vector[2])); } @Test public void TestSha256Vector4() { String digest = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA256(vector[3])); + assertEquals(digest, Sha2.SHA256(vector[3])); } @Test public void TestSha256Vector5() { String digest = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA256(vector[4])); + assertEquals(digest, Sha2.SHA256(vector[4])); } @Test public void TestSha384Vector1() { String digest = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA384(vector[0])); + assertEquals(digest, Sha2.SHA384(vector[0])); } @Test public void TestSha384Vector2() { String digest = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA384(vector[1])); + assertEquals(digest, Sha2.SHA384(vector[1])); } @Test public void TestSha384Vector3() { String digest = "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA384(vector[2])); + assertEquals(digest, Sha2.SHA384(vector[2])); } @Test public void TestSha384Vector4() { String digest = "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA384(vector[3])); + assertEquals(digest, Sha2.SHA384(vector[3])); } @Test public void TestSha384Vector5() { String digest = "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA384(vector[4])); + assertEquals(digest, Sha2.SHA384(vector[4])); } @Test public void TestSha512Vector1() { String digest = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA512(vector[0])); + assertEquals(digest, Sha2.SHA512(vector[0])); } @Test public void TestSha512Vector2() { String digest = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA512(vector[1])); + assertEquals(digest, Sha2.SHA512(vector[1])); } @Test public void TestSha512Vector3() { String digest = "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA512(vector[2])); + assertEquals(digest, Sha2.SHA512(vector[2])); } @Test public void TestSha512Vector4() { String digest = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA512(vector[3])); + assertEquals(digest, Sha2.SHA512(vector[3])); } @Test public void TestSha512Vector5() { String digest = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"; - Assert.assertEquals("message digests are not equal", digest, Sha2.SHA512(vector[4])); + assertEquals(digest, Sha2.SHA512(vector[4])); } @Test public void TestInputByteArrayNotAltered() { byte[] array = vector[2]; Sha2.SHA224(array); - Assert.assertEquals("user vector altered", array, vector[2]); + assertEquals(array, vector[2]); } } diff --git a/src/test/java/com/dataStructures/BinaryTreeTest.java b/src/test/java/com/dataStructures/BinaryTreeTest.java index 383bc0aa05fe..7a623eaf6a33 100644 --- a/src/test/java/com/dataStructures/BinaryTreeTest.java +++ b/src/test/java/com/dataStructures/BinaryTreeTest.java @@ -1,15 +1,12 @@ -package src.test.java.com.dataStructures; +package com.dataStructures; -import org.junit.Test; -import src.main.java.com.dataStructures.BinaryTree; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; public class BinaryTreeTest { - public BinaryTreeTest() { - } - /** * Test of insert method, of class BinaryTree. */ diff --git a/src/test/java/com/dataStructures/DisjointSetTest.java b/src/test/java/com/dataStructures/DisjointSetTest.java index 5f13190df87a..70a86166064b 100644 --- a/src/test/java/com/dataStructures/DisjointSetTest.java +++ b/src/test/java/com/dataStructures/DisjointSetTest.java @@ -1,11 +1,12 @@ -package src.test.java.com.dataStructures; +package com.dataStructures; -import org.junit.Test; -import src.main.java.com.dataStructures.DisjointSet; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class DisjointSetTest { + @Test public void test() { DisjointSet set = new DisjointSet<>(); diff --git a/src/test/java/com/dataStructures/GeneralQueueTest.java b/src/test/java/com/dataStructures/GeneralQueueTest.java index 4bb07a3f54c4..212fd60b1b91 100644 --- a/src/test/java/com/dataStructures/GeneralQueueTest.java +++ b/src/test/java/com/dataStructures/GeneralQueueTest.java @@ -1,10 +1,9 @@ -package src.test.java.com.dataStructures; +package com.dataStructures; +import com.types.Queue; +import org.junit.jupiter.api.Test; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.dataStructures.GeneralQueue; -import src.main.java.com.types.Queue; +import static org.junit.jupiter.api.Assertions.assertEquals; public class GeneralQueueTest { @@ -19,27 +18,26 @@ public void testGeneralQueue() { myQueue.add(50); - Object[] myArray = myQueue.toArray(); - Assert.assertEquals(myArray.length, myQueue.size()); + Object[] myArray = myQueue.toArray(); + assertEquals(myArray.length, myQueue.size()); myQueue.remove(20); - Assert.assertEquals(myQueue.size(), 4); + assertEquals(myQueue.size(), 4); Boolean isEmpty = myQueue.isEmpty(); - Assert.assertEquals(Boolean.valueOf("false"), Boolean.valueOf(isEmpty)); + assertEquals(Boolean.valueOf("false"), Boolean.valueOf(isEmpty)); myQueue.offer(60); - Assert.assertEquals(5, myQueue.size()); + assertEquals(5, myQueue.size()); int polledElement = myQueue.poll(); - Assert.assertEquals(10, polledElement); + assertEquals(10, polledElement); int element = myQueue.element(); - Assert.assertEquals(30, element); + assertEquals(30, element); myQueue.poll(); int peekedElement = myQueue.peek(); - Assert.assertEquals(40, peekedElement); - + assertEquals(40, peekedElement); } } diff --git a/src/test/java/com/dataStructures/StackTest.java b/src/test/java/com/dataStructures/StackTest.java index 3cbbc86cd450..aaa965244717 100644 --- a/src/test/java/com/dataStructures/StackTest.java +++ b/src/test/java/com/dataStructures/StackTest.java @@ -1,11 +1,11 @@ -package src.test.java.com.dataStructures; +package com.dataStructures; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.dataStructures.Stack; +import org.junit.jupiter.api.Test; import java.util.EmptyStackException; +import static org.junit.jupiter.api.Assertions.*; + public class StackTest { @Test @@ -13,18 +13,17 @@ public void testEmpty() { Stack myStack = new Stack<>(); boolean isEmpty = myStack.empty(); - Assert.assertTrue(isEmpty); + assertTrue(isEmpty); myStack.push(10); isEmpty = myStack.empty(); - Assert.assertFalse(isEmpty); + assertFalse(isEmpty); } - @Test(expected = EmptyStackException.class) + @Test public void testPeekWithoutElements() { - Stack myStack = new Stack<>(); - myStack.peek(); + assertThrows(EmptyStackException.class, () -> myStack.peek()); } @Test @@ -36,15 +35,13 @@ public void testPeekWithElements() { myStack.push(30); myStack.push(40); - Assert.assertEquals(40, myStack.peek()); + assertEquals(40, myStack.peek()); } - @Test(expected = EmptyStackException.class) + @Test public void testPopWithoutElements() { - Stack myStack = new Stack<>(); - myStack.pop(); - + assertThrows(EmptyStackException.class, () -> myStack.pop()); } @Test @@ -57,7 +54,7 @@ public void testPopWithElements() { myStack.push(40); myStack.push(50); - Assert.assertEquals(50, myStack.pop()); + assertEquals(50, myStack.pop()); } @@ -75,7 +72,7 @@ public void testPushWithinInitialCapacity() { myStack.push(80); myStack.push(90); myStack.push(100); - Assert.assertEquals(10, myStack.size()); + assertEquals(10, myStack.size()); } @Test @@ -93,7 +90,7 @@ public void testPushOutsideInitialCapacity() { myStack.push(90); myStack.push(100); myStack.push(110); - Assert.assertEquals(11, myStack.size()); + assertEquals(11, myStack.size()); } @Test @@ -103,7 +100,7 @@ public void testSearchWithObjectUnavailable() { myStack.push(10); myStack.push(20); myStack.push(30); - Assert.assertEquals(-1,myStack.search(50)); + assertEquals(-1, myStack.search(50)); } @Test @@ -113,7 +110,7 @@ public void testSearchWithObjectAvailable() { myStack.push(10); myStack.push(20); myStack.push(30); - Assert.assertEquals(3,myStack.search(10)); + assertEquals(3, myStack.search(10)); } } diff --git a/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java b/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java index 96e4e34bb88e..0a038ff134cf 100644 --- a/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java +++ b/src/test/java/com/designpatterns/creational/abstractfactory/AbstractShapeFactoryTest.java @@ -1,10 +1,11 @@ -package src.test.java.com.designpatterns.creational.abstractfactory; +package com.designpatterns.creational.abstractfactory; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.designpatterns.creational.abstractfactory.*; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class AbstractShapeFactoryTest { + @Test public void testAbstractShapeFactory() { String failReason = ""; @@ -40,7 +41,6 @@ public void testAbstractShapeFactory() { failReason += "Surface area of Sphere is incorrect!.\n"; } - Assert.assertEquals(failReason, "", failReason); - + assertEquals(failReason, "", failReason); } } diff --git a/src/test/java/com/designpatterns/creational/builder/DesktopBuilderTest.java b/src/test/java/com/designpatterns/creational/builder/DesktopBuilderTest.java index 2f8e4cedb45b..90e0862105a0 100644 --- a/src/test/java/com/designpatterns/creational/builder/DesktopBuilderTest.java +++ b/src/test/java/com/designpatterns/creational/builder/DesktopBuilderTest.java @@ -1,8 +1,8 @@ -package src.test.java.com.designpatterns.creational.builder; +package com.designpatterns.creational.builder; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.designpatterns.creational.builder.Desktop; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class DesktopBuilderTest { private final String configOne = "Desktop{CPU='Intel i7', RAM='Corsair Vengeance 3000', isGraphicCardEnabled=true" + @@ -18,7 +18,7 @@ public void testDesktopBuilder() { .setGraphicCardEnabled(true) .setOperatingSystem("Windows 10") .build(); - Assert.assertEquals(d1.toString(), configOne); + assertEquals(d1.toString(), configOne); Desktop d2 = new Desktop.DesktopBuilder("Intel i5", "HyperX Fury v5") .setDiskSizeGB(16) @@ -26,7 +26,6 @@ public void testDesktopBuilder() { .setGraphicCardEnabled(true) .setOperatingSystem("Red Hat Enterprise") .build(); - Assert.assertEquals(d2.toString(), configTwo); + assertEquals(d2.toString(), configTwo); } - } diff --git a/src/test/java/com/designpatterns/creational/factory/PolygonFactoryTest.java b/src/test/java/com/designpatterns/creational/factory/PolygonFactoryTest.java index d8ea65918570..e9588ef03ef3 100644 --- a/src/test/java/com/designpatterns/creational/factory/PolygonFactoryTest.java +++ b/src/test/java/com/designpatterns/creational/factory/PolygonFactoryTest.java @@ -1,11 +1,11 @@ -package src.test.java.com.designpatterns.creational.factory; +package com.designpatterns.creational.factory; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.designpatterns.creational.factory.Polygon; -import src.main.java.com.designpatterns.creational.factory.PolygonFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class PolygonFactoryTest { + @Test public void testPolygonFactory() { String failReason = ""; @@ -40,6 +40,6 @@ public void testPolygonFactory() { failReason += "Pentagon area is incorrect!"; } - Assert.assertEquals(failReason, failReason, ""); + assertEquals(failReason, failReason, ""); } } diff --git a/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java b/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java index e1a2c7a6f23e..a48363467105 100644 --- a/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java +++ b/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java @@ -1,10 +1,11 @@ -package src.test.java.com.designpatterns.creational.prototype; +package com.designpatterns.creational.prototype; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.designpatterns.creational.prototype.ColorStore; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class PrototypeTest { + @Test public void testPrototype() { String testFailReason = ""; @@ -24,6 +25,6 @@ public void testPrototype() { if (!"Blue color added".equals(testFour)) { testFailReason += "TC 4 Failed: Blue couldn't be added\n"; } - Assert.assertEquals(testFailReason, "", testFailReason); + assertEquals(testFailReason, "", testFailReason); } } diff --git a/src/test/java/com/designpatterns/creational/singleton/SingletonTest.java b/src/test/java/com/designpatterns/creational/singleton/SingletonTest.java index 8374494939f7..8d6d5cc3ea11 100644 --- a/src/test/java/com/designpatterns/creational/singleton/SingletonTest.java +++ b/src/test/java/com/designpatterns/creational/singleton/SingletonTest.java @@ -1,14 +1,14 @@ -package src.test.java.com.designpatterns.creational.singleton; +package com.designpatterns.creational.singleton; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.designpatterns.creational.singleton.Singleton; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import static org.junit.jupiter.api.Assertions.assertFalse; + public class SingletonTest { private static volatile ArrayList hashCodeList = new ArrayList<>(); @@ -38,7 +38,7 @@ public void testSingleton() throws InterruptedException { testFailed = true; } } - Assert.assertFalse(testFailed); + assertFalse(testFailed); } } } diff --git a/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java index 17ff9e49458b..3759a2a0fb3b 100644 --- a/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java +++ b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java @@ -1,14 +1,11 @@ -package src.test.java.com.designpatterns.structural.adapter; +package com.designpatterns.structural.adapter; -import org.junit.Test; -import src.main.java.com.designpatterns.structural.adapter.BugattiVeyron; -import src.main.java.com.designpatterns.structural.adapter.Movable; -import src.main.java.com.designpatterns.structural.adapter.MovableAdapter; -import src.main.java.com.designpatterns.structural.adapter.MovableAdapterImpl; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class MovableAdapterTest { + @Test public void testMovableAdapter() { Movable bugattiVeyron = new BugattiVeyron(); diff --git a/src/test/java/com/generation/SimplexNoiseTest.java b/src/test/java/com/generation/SimplexNoiseTest.java index 336289d2b8f2..f4be05c76336 100644 --- a/src/test/java/com/generation/SimplexNoiseTest.java +++ b/src/test/java/com/generation/SimplexNoiseTest.java @@ -1,13 +1,15 @@ -package src.test.java.com.generation; +package com.generation; -import java.awt.Color; +import org.junit.jupiter.api.Test; + +import javax.imageio.ImageIO; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; -import javax.imageio.ImageIO; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.generation.SimplexNoise; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; public class SimplexNoiseTest { @@ -18,27 +20,27 @@ public void testGenerateHeightMap() { final int HEIGHT = 256; final int X = 0; final int Y = 0; - final String RESOURCE_NAME = "src/test/java/com/generation/expected-result.png"; float[][] heightmap = new SimplexNoise(50, 0.3F, 1111111111111111L).generateHeightMap(X, Y, WIDTH, HEIGHT); BufferedImage image = null; - try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) { + try (InputStream in = this.getClass().getResourceAsStream("expected-result.png")) { image = ImageIO.read(in); - Assert.assertEquals(WIDTH, image.getWidth()); - Assert.assertEquals(HEIGHT, image.getHeight()); + assertEquals(WIDTH, image.getWidth(), "width differs"); + assertEquals(HEIGHT, image.getHeight(), "height differs"); } catch (IOException | IllegalArgumentException exception) { - Assert.fail(exception.toString()); + exception.printStackTrace(); + fail(exception.toString()); } for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { - Assert.assertEquals(new Color(image.getRGB(x, y)).getRed(), (int) (heightmap[x][y] * 255)); + assertEquals(new Color(image.getRGB(x, y)).getRed(), (int) (heightmap[x][y] * 255), "image data differs"); } } } diff --git a/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java b/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java index 2e7d204fa88f..c08ea83200a3 100644 --- a/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java +++ b/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java @@ -1,14 +1,13 @@ -package src.test.java.com.matchings.stableMatching; +package com.matchings.stableMatching; -import src.main.java.com.matchings.stableMatching.GaleShapley; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; +import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Random; -import java.util.Collections; // for shuffling -import java.util.ArrayList; // for shuffling -import java.util.List; // for shuffling + +import static org.junit.jupiter.api.Assertions.assertEquals; public class GaleShapleyTest { @@ -16,7 +15,6 @@ public class GaleShapleyTest { * Test a number of GaleShapley executions on pseudo-random instances of the * stable marriage problem. */ - @Test public void testGaleShapley() { GaleShapley galeShapley = new GaleShapley(); @@ -37,11 +35,11 @@ public void testGaleShapley() { womenPrefs[i][j] = j; } shuffleArray(menPrefs[i], i); - shuffleArray(womenPrefs[i], n+i); + shuffleArray(womenPrefs[i], n + i); } // Now we have pseudo-random preferences for each man and each woman. GaleShapleyMenMatching = galeShapley.GaleShapleyStableMarriage(menPrefs, womenPrefs); - assertEquals("Unstable matching", true, isStable(GaleShapleyMenMatching, menPrefs, womenPrefs)); + assertEquals(true, isStable(GaleShapleyMenMatching, menPrefs, womenPrefs), "Unstable matching"); } } @@ -49,7 +47,7 @@ public void testGaleShapley() { * Determine if the proposed menMatching is stable, i.e. if there is no * potential couple in which both members would strictly prefer being with each * other than being with their current partner. - * + * * @param menMatching * @param menPrefs * @param womenPrefs @@ -107,7 +105,7 @@ public boolean isStable(int[] menMatching, int[][] menPrefs, int[][] womenPrefs) /** * Shuffle an array using Collections.shuffle - * + * * @param array array to be shuffled * @param seed fixed seed, for reproducibility */ diff --git a/src/test/java/com/others/FastPowerTest.java b/src/test/java/com/others/FastPowerTest.java index 5824dae2629d..fec003958ef4 100644 --- a/src/test/java/com/others/FastPowerTest.java +++ b/src/test/java/com/others/FastPowerTest.java @@ -1,11 +1,10 @@ -package src.test.java.com.others; +package com.others; -import org.junit.Test; -import src.main.java.com.others.FastPower; +import org.junit.jupiter.api.Test; import java.math.BigInteger; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class FastPowerTest { @@ -28,6 +27,5 @@ public void test() { testBigInteger(BigInteger.TEN, BigInteger.TEN, BigInteger.valueOf(4)); testBigInteger(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234")); testBigInteger(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890")); - } } diff --git a/src/test/java/com/search/BinarySearchTest.java b/src/test/java/com/search/BinarySearchTest.java index 0d4628721852..bfbee9295277 100644 --- a/src/test/java/com/search/BinarySearchTest.java +++ b/src/test/java/com/search/BinarySearchTest.java @@ -1,25 +1,25 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.search.BinarySearch; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class BinarySearchTest { @Test public void testBinarySearch() { - Integer[] arr1 = {1,2,3,4,5}; - Assert.assertEquals("Incorrect index", 2, BinarySearch.findIndex(arr1,3)); - Assert.assertEquals("Incorrect index", 0, BinarySearch.findIndex(arr1,1)); - Assert.assertEquals("Incorrect index", -1, BinarySearch.findIndex(arr1,8)); - Assert.assertEquals("Incorrect index", -1, BinarySearch.findIndex(arr1,-2)); + Integer[] arr1 = {1, 2, 3, 4, 5}; + assertEquals(2, BinarySearch.findIndex(arr1, 3)); + assertEquals(0, BinarySearch.findIndex(arr1, 1)); + assertEquals(-1, BinarySearch.findIndex(arr1, 8)); + assertEquals(-1, BinarySearch.findIndex(arr1, -2)); String[] arr2 = {"A", "B", "C", "D"}; - Assert.assertEquals("Incorrect index", 2, BinarySearch.findIndex(arr2,"C")); - Assert.assertEquals("Incorrect index", 1, BinarySearch.findIndex(arr2,"B")); - Assert.assertEquals("Incorrect index", -1, BinarySearch.findIndex(arr2,"F")); + assertEquals(2, BinarySearch.findIndex(arr2, "C")); + assertEquals(1, BinarySearch.findIndex(arr2, "B")); + assertEquals(-1, BinarySearch.findIndex(arr2, "F")); String[] arr3 = {}; - Assert.assertEquals("Incorrect index", -1, BinarySearch.findIndex(arr3, "")); + assertEquals(-1, BinarySearch.findIndex(arr3, "")); } } diff --git a/src/test/java/com/search/BloomFilterTest.java b/src/test/java/com/search/BloomFilterTest.java index 0d12b9118e0b..8dcf2d8409cc 100644 --- a/src/test/java/com/search/BloomFilterTest.java +++ b/src/test/java/com/search/BloomFilterTest.java @@ -1,16 +1,38 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Test; -import src.main.java.com.search.BloomFilter; +import org.junit.jupiter.api.Test; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertFalse; public class BloomFilterTest { + private static final char[] CHAR_POOL; + + static { + CHAR_POOL = new char[52]; + int i = 0; + for (char c = 'a'; c <= 'z'; c++) { + CHAR_POOL[i++] = c; + CHAR_POOL[i++] = (char) (c - 32); + } + } + + public static String randomString(int minLength, int maxLength) { + ThreadLocalRandom r = ThreadLocalRandom.current(); + int chLen = r.nextInt(minLength, maxLength), + poolSize = CHAR_POOL.length; + char[] chars = new char[chLen]; + for (int i = 0; i < chLen; i++) { + chars[i] = CHAR_POOL[r.nextInt(poolSize)]; + } + + return new String(chars); + } + @Test public void test() { int count = 100000; @@ -43,28 +65,5 @@ public void test() { System.out.println("total: " + total); System.out.println("error rate : " + (double) error / total); } - - public static String randomString(int minLength, int maxLength) { - ThreadLocalRandom r = ThreadLocalRandom.current(); - int chLen = r.nextInt(minLength, maxLength), - poolSize = CHAR_POOL.length; - char[] chars = new char[chLen]; - for (int i = 0; i < chLen; i++) { - chars[i] = CHAR_POOL[r.nextInt(poolSize)]; - } - - return new String(chars); - } - - private static final char[] CHAR_POOL; - static { - CHAR_POOL = new char[52]; - int i = 0; - for (char c = 'a'; c <= 'z'; c++) { - CHAR_POOL[i++] = c; - CHAR_POOL[i++] = (char) (c - 32); - } - } - } \ No newline at end of file diff --git a/src/test/java/com/search/ExponentialSearchTest.java b/src/test/java/com/search/ExponentialSearchTest.java index 1023e8e37283..31c2fc5b7311 100644 --- a/src/test/java/com/search/ExponentialSearchTest.java +++ b/src/test/java/com/search/ExponentialSearchTest.java @@ -1,10 +1,11 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.search.ExponentialSearch; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class ExponentialSearchTest { + @Test public void testExponentialSearch() { ExponentialSearch expSearch = new ExponentialSearch(); @@ -12,20 +13,20 @@ public void testExponentialSearch() { Integer[] arr = {11, 14, 23, 29, 36, 40, 42, 52}; int x = 36; int index = expSearch.findIndex(arr, x); - Assert.assertEquals("Incorrect index", 4, index); + assertEquals(4, index); Integer[] arrTwo = {-210, -190, -180, -160, -130, -120, -100}; x = -120; index = expSearch.findIndex(arrTwo, x); - Assert.assertEquals("Incorrect index", 5, index); + assertEquals(5, index); String[] arrString = {"101", "122", "136", "165", "225", "251", "291"}; String stringX = "122"; index = expSearch.findIndex(arrString, stringX); - Assert.assertEquals("Incorrect index", 1, index); + assertEquals(1, index); String[] arrThree = {}; - Assert.assertEquals("Incorrect index", -1, expSearch.findIndex(arrThree, "")); + assertEquals(-1, expSearch.findIndex(arrThree, "")); } } diff --git a/src/test/java/com/search/FibonacciSearchTest.java b/src/test/java/com/search/FibonacciSearchTest.java index 2adae9d72b64..3ef9efb3467c 100644 --- a/src/test/java/com/search/FibonacciSearchTest.java +++ b/src/test/java/com/search/FibonacciSearchTest.java @@ -1,10 +1,11 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.search.FibonacciSearch; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class FibonacciSearchTest { + @Test public void testFibonacciSearch() { FibonacciSearch fibonacciSearch = new FibonacciSearch(); @@ -12,19 +13,19 @@ public void testFibonacciSearch() { Integer[] arr = {11, 14, 23, 32, 36, 40, 54, 69}; int x = 54; int index = fibonacciSearch.findIndex(arr, x); - Assert.assertEquals("Incorrect index", 6, index); + assertEquals(6, index); Integer[] arrTwo = {-400, -283, -180, -160, -129, -120, -30}; x = -120; index = fibonacciSearch.findIndex(arrTwo, x); - Assert.assertEquals("Incorrect index", 5, index); + assertEquals(5, index); String[] arrString = {"101", "122", "136", "165", "225", "351", "458"}; String stringX = "136"; index = fibonacciSearch.findIndex(arrString, stringX); - Assert.assertEquals("Incorrect index", 2, index); + assertEquals(2, index); String[] arrThree = {}; - Assert.assertEquals("Incorrect index", -1, fibonacciSearch.findIndex(arrThree, "")); + assertEquals(-1, fibonacciSearch.findIndex(arrThree, "")); } } diff --git a/src/test/java/com/search/InterpolationSearchTest.java b/src/test/java/com/search/InterpolationSearchTest.java index c3a983146829..a8da6824297f 100644 --- a/src/test/java/com/search/InterpolationSearchTest.java +++ b/src/test/java/com/search/InterpolationSearchTest.java @@ -1,29 +1,28 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import src.main.java.com.search.InterpolationSearch; +import static org.junit.jupiter.api.Assertions.assertEquals; public class InterpolationSearchTest { - @Test - public void testInterpolationSearch() { - InterpolationSearch interpolationSearch = new InterpolationSearch(); - - Integer arr[] = {10, 12, 13, 16, 18, 19, 21}; - int x = 18; - int index = interpolationSearch.findIndex(arr, x); - Assert.assertEquals("Incorrect index", 4, index); + @Test + public void testInterpolationSearch() { + InterpolationSearch interpolationSearch = new InterpolationSearch(); - Integer arrTwo[] = {-210, -190, -180, -160, -130, -120, -100}; - x = -190; - index = interpolationSearch.findIndex(arrTwo, x); - Assert.assertEquals("Incorrect index", 1, index); - - String arrString[] = {"10", "12", "13", "16", "22", "25","29"}; - String stringX = "13"; - index = interpolationSearch.findIndex(arrString, stringX); - Assert.assertEquals("Incorrect index", 2, index); - } + Integer[] arr = {10, 12, 13, 16, 18, 19, 21}; + int x = 18; + int index = interpolationSearch.findIndex(arr, x); + assertEquals(4, index); + + Integer[] arrTwo = {-210, -190, -180, -160, -130, -120, -100}; + x = -190; + index = interpolationSearch.findIndex(arrTwo, x); + assertEquals(1, index); + + String[] arrString = {"10", "12", "13", "16", "22", "25", "29"}; + String stringX = "13"; + index = interpolationSearch.findIndex(arrString, stringX); + assertEquals(2, index); + } } \ No newline at end of file diff --git a/src/test/java/com/search/JumpSearchTest.java b/src/test/java/com/search/JumpSearchTest.java index 6587107fb3d3..53ebe7d0c859 100644 --- a/src/test/java/com/search/JumpSearchTest.java +++ b/src/test/java/com/search/JumpSearchTest.java @@ -1,29 +1,28 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Test; -import org.junit.Assert; +import org.junit.jupiter.api.Test; -import src.main.java.com.search.JumpSearch; +import static org.junit.jupiter.api.Assertions.assertEquals; public class JumpSearchTest { - @Test - public void testJumpSearch() { - JumpSearch jumpSearch = new JumpSearch(); - - Integer arr[] = {11, 15, 16, 29, 36, 40, 42, 52}; - int x = 36; - int index = jumpSearch.findIndex(arr, x); - Assert.assertEquals("Incorrect index", 4, index); + @Test + public void testJumpSearch() { + JumpSearch jumpSearch = new JumpSearch(); - Integer arrTwo[] = {-210, -190, -180, -160, -130, -120, -100}; - x = -120; - index = jumpSearch.findIndex(arrTwo, x); - Assert.assertEquals("Incorrect index", 5, index); - - String arrString[] = {"101", "122", "136", "165", "225", "251","291"}; - String stringX = "122"; - index = jumpSearch.findIndex(arrString, stringX); - Assert.assertEquals("Incorrect index", 1, index); - } + Integer[] arr = {11, 15, 16, 29, 36, 40, 42, 52}; + int x = 36; + int index = jumpSearch.findIndex(arr, x); + assertEquals(4, index); + + Integer[] arrTwo = {-210, -190, -180, -160, -130, -120, -100}; + x = -120; + index = jumpSearch.findIndex(arrTwo, x); + assertEquals(5, index); + + String[] arrString = {"101", "122", "136", "165", "225", "251", "291"}; + String stringX = "122"; + index = jumpSearch.findIndex(arrString, stringX); + assertEquals(1, index); + } } \ No newline at end of file diff --git a/src/test/java/com/search/LinearSearchTest.java b/src/test/java/com/search/LinearSearchTest.java index ff652b4626aa..43fc7921eac7 100644 --- a/src/test/java/com/search/LinearSearchTest.java +++ b/src/test/java/com/search/LinearSearchTest.java @@ -1,26 +1,27 @@ -package src.test.java.com.search; +package com.search; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.search.LinearSearch; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class LinearSearchTest { + @Test public void testLinearSearch() { - Integer[] arr1 = {1,2,3,4,5}; - Assert.assertEquals("Incorrect index", 2, LinearSearch.findIndex(arr1,3)); - Assert.assertEquals("Incorrect index", 0, LinearSearch.findIndex(arr1,1)); - Assert.assertEquals("Incorrect index", -1, LinearSearch.findIndex(arr1,8)); - Assert.assertEquals("Incorrect index", -1, LinearSearch.findIndex(arr1,-2)); + Integer[] arr1 = {1, 2, 3, 4, 5}; + assertEquals(2, LinearSearch.findIndex(arr1, 3)); + assertEquals(0, LinearSearch.findIndex(arr1, 1)); + assertEquals(-1, LinearSearch.findIndex(arr1, 8)); + assertEquals(-1, LinearSearch.findIndex(arr1, -2)); String[] arr2 = {"A", "B", "C", "D"}; - Assert.assertEquals("Incorrect index", 2, LinearSearch.findIndex(arr2,"C")); - Assert.assertEquals("Incorrect index", 1, LinearSearch.findIndex(arr2,"B")); - Assert.assertEquals("Incorrect index", -1, LinearSearch.findIndex(arr2,"F")); + assertEquals(2, LinearSearch.findIndex(arr2, "C")); + assertEquals(1, LinearSearch.findIndex(arr2, "B")); + assertEquals(-1, LinearSearch.findIndex(arr2, "F")); String[] arr3 = {}; - Assert.assertEquals("Incorrect index", -1, LinearSearch.findIndex(arr3, "")); + assertEquals(-1, LinearSearch.findIndex(arr3, "")); } } diff --git a/src/test/java/com/sorts/BubbleSortTest.java b/src/test/java/com/sorts/BubbleSortTest.java index e58d086a57a3..1428aed0204e 100644 --- a/src/test/java/com/sorts/BubbleSortTest.java +++ b/src/test/java/com/sorts/BubbleSortTest.java @@ -1,24 +1,22 @@ -package src.test.java.com.sorts; +package com.sorts; +import org.junit.jupiter.api.Test; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.BubbleSort; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class BubbleSortTest { - @Test - public void bubbleSortTest() { - BubbleSort bubbleSort = new BubbleSort(); + @Test + public void bubbleSortTest() { + BubbleSort bubbleSort = new BubbleSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, bubbleSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, bubbleSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar)); - - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/CountingSortTest.java b/src/test/java/com/sorts/CountingSortTest.java index d852e587e946..156487b1414b 100644 --- a/src/test/java/com/sorts/CountingSortTest.java +++ b/src/test/java/com/sorts/CountingSortTest.java @@ -1,8 +1,9 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.CountingSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class CountingSortTest { @@ -18,6 +19,6 @@ public void testCountingSort() { Integer[] sorted = new Integer[]{1, 1, 2, 2, 4, 5, 7}; // Comparing the two integer arrays - Assert.assertArrayEquals(sorted, countingSort.sort(unsorted)); + assertArrayEquals(sorted, countingSort.sort(unsorted)); } } diff --git a/src/test/java/com/sorts/CycleSortTest.java b/src/test/java/com/sorts/CycleSortTest.java index 4e121255748b..75699ad6b7c6 100644 --- a/src/test/java/com/sorts/CycleSortTest.java +++ b/src/test/java/com/sorts/CycleSortTest.java @@ -1,8 +1,9 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.CycleSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class CycleSortTest { @@ -14,22 +15,22 @@ public void cycleSortIntegerTest() { // Test case for integers Integer[] unsortedInt = new Integer[]{5, 1, 7, 0, 2, 9, 6, 3, 4, 8}; Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, cycleSort.sort(unsortedInt)); + assertArrayEquals(sortedInt, cycleSort.sort(unsortedInt)); // Test case for floating point numbers Float[] unsortedFloat = new Float[]{6.7f, 21.1f, 0.9f, -3.2f, 5.9f, -21.3f}; Float[] sortedFloat = new Float[]{-21.3f, -3.2f, 0.9f, 5.9f, 6.7f, 21.1f}; - Assert.assertArrayEquals(sortedFloat, cycleSort.sort(unsortedFloat)); + assertArrayEquals(sortedFloat, cycleSort.sort(unsortedFloat)); // Test case for characters Character[] unsortedChar = new Character[]{'c', 'a', 'b', 'A', 'C', 'B'}; Character[] sortedChar = new Character[]{'A', 'B', 'C', 'a', 'b', 'c'}; - Assert.assertArrayEquals(sortedChar, cycleSort.sort(unsortedChar)); + assertArrayEquals(sortedChar, cycleSort.sort(unsortedChar)); // Test case for Strings String[] unsortedStr = new String[]{"Edward", "Linus", "David", "Alan", "Dennis", "Robert", "Ken"}; String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"}; - Assert.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr)); + assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr)); } } diff --git a/src/test/java/com/sorts/HeapSortTest.java b/src/test/java/com/sorts/HeapSortTest.java index 9d7cd936b5f2..e1a2bfb9aa2e 100644 --- a/src/test/java/com/sorts/HeapSortTest.java +++ b/src/test/java/com/sorts/HeapSortTest.java @@ -1,22 +1,22 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.HeapSort; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class HeapSortTest { - @Test - public void heapSortTest() { - HeapSort heapSort = new HeapSort(); + @Test + public void heapSortTest() { + HeapSort heapSort = new HeapSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, heapSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, heapSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, heapSort.sort(unsortedChar)); - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, heapSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/InsertionSortTest.java b/src/test/java/com/sorts/InsertionSortTest.java index 1dcc6fef23c6..e30a09e83e23 100644 --- a/src/test/java/com/sorts/InsertionSortTest.java +++ b/src/test/java/com/sorts/InsertionSortTest.java @@ -1,34 +1,34 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.InsertionSort; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class InsertionSortTest { - @Test - public void insertionSortTest() { - InsertionSort insertionSort = new InsertionSort(); - - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); - - unsortedInt = new Integer[]{5,4,3,2,1,0}; - sortedInt = new Integer[]{0, 1, 2, 3, 4, 5}; - Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); - - unsortedInt = new Integer[]{-1,-2,-3,-4,-5}; - sortedInt = new Integer[]{-5,-4,-3,-2,-1}; - Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); - - unsortedInt = new Integer[]{-1,-5,-10,-990,990,1010}; - sortedInt = new Integer[]{-990,-10,-5,-1,990,1010}; - Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); - - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar)); - } + @Test + public void insertionSortTest() { + InsertionSort insertionSort = new InsertionSort(); + + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{5, 4, 3, 2, 1, 0}; + sortedInt = new Integer[]{0, 1, 2, 3, 4, 5}; + assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{-1, -2, -3, -4, -5}; + sortedInt = new Integer[]{-5, -4, -3, -2, -1}; + assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{-1, -5, -10, -990, 990, 1010}; + sortedInt = new Integer[]{-990, -10, -5, -1, 990, 1010}; + assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt)); + + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/MergeSortTest.java b/src/test/java/com/sorts/MergeSortTest.java index 258515237476..ef138957227b 100644 --- a/src/test/java/com/sorts/MergeSortTest.java +++ b/src/test/java/com/sorts/MergeSortTest.java @@ -1,34 +1,34 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.MergeSort; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class MergeSortTest { - @Test - public void mergeSortTest() { - MergeSort mergeSort = new MergeSort(); + @Test + public void mergeSortTest() { + MergeSort mergeSort = new MergeSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); - unsortedInt = new Integer[]{5, 4, 3, 2, 1, 0}; - sortedInt = new Integer[]{0, 1, 2, 3, 4, 5}; - Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + unsortedInt = new Integer[]{5, 4, 3, 2, 1, 0}; + sortedInt = new Integer[]{0, 1, 2, 3, 4, 5}; + assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); - unsortedInt = new Integer[]{-1, -2, -3, -4, -5}; - sortedInt = new Integer[]{-5, -4, -3, -2, -1}; - Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + unsortedInt = new Integer[]{-1, -2, -3, -4, -5}; + sortedInt = new Integer[]{-5, -4, -3, -2, -1}; + assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); - unsortedInt = new Integer[]{-1, -5, -10, -990, 990, 1010}; - sortedInt = new Integer[]{-990, -10, -5, -1, 990, 1010}; - Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + unsortedInt = new Integer[]{-1, -5, -10, -990, 990, 1010}; + sortedInt = new Integer[]{-990, -10, -5, -1, 990, 1010}; + assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, mergeSort.sort(unsortedChar)); - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, mergeSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/PigeonholeSortTest.java b/src/test/java/com/sorts/PigeonholeSortTest.java index 5195fc51d3dd..1e5482a6c615 100644 --- a/src/test/java/com/sorts/PigeonholeSortTest.java +++ b/src/test/java/com/sorts/PigeonholeSortTest.java @@ -1,8 +1,9 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.PigeonholeSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class PigeonholeSortTest { @@ -14,17 +15,17 @@ public void testPigeonholeSort() { // Test Case 1 Integer[] unsorted1 = new Integer[]{5, 1, 7, 2, 9, 6, 3, 4, 8}; Integer[] sorted1 = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sorted1, pigeonholeSort.sort(unsorted1)); + assertArrayEquals(sorted1, pigeonholeSort.sort(unsorted1)); // Test Case 2 Integer[] unsorted2 = new Integer[]{-5, 1, 7, 2, -9, 6, -3, 4, 8}; Integer[] sorted2 = new Integer[]{-9, -5, -3, 1, 2, 4, 6, 7, 8}; - Assert.assertArrayEquals(sorted2, pigeonholeSort.sort(unsorted2)); + assertArrayEquals(sorted2, pigeonholeSort.sort(unsorted2)); // Test Case 3 Integer[] unsorted3 = new Integer[]{-5, 1, 7, 2, -9, 6, -3, 4, 1, 8, 1, 1}; Integer[] sorted3 = new Integer[]{-9, -5, -3, 1, 1, 1, 1, 2, 4, 6, 7, 8}; - Assert.assertArrayEquals(sorted3, pigeonholeSort.sort(unsorted3)); + assertArrayEquals(sorted3, pigeonholeSort.sort(unsorted3)); } } diff --git a/src/test/java/com/sorts/QuickSortTest.java b/src/test/java/com/sorts/QuickSortTest.java index 00154c1f0232..5db425dd3086 100644 --- a/src/test/java/com/sorts/QuickSortTest.java +++ b/src/test/java/com/sorts/QuickSortTest.java @@ -1,22 +1,22 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.QuickSort; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class QuickSortTest { - @Test - public void quickSortTest() { - QuickSort quickSort = new QuickSort(); + @Test + public void quickSortTest() { + QuickSort quickSort = new QuickSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, quickSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, quickSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, quickSort.sort(unsortedChar)); - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, quickSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/SelectionSortTest.java b/src/test/java/com/sorts/SelectionSortTest.java index cb9411c75f71..060b0ed1c1cd 100644 --- a/src/test/java/com/sorts/SelectionSortTest.java +++ b/src/test/java/com/sorts/SelectionSortTest.java @@ -1,21 +1,22 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.SelectionSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class SelectionSortTest { - @Test - public void selectionSortTest() { - SelectionSort selectionSort = new SelectionSort(); + @Test + public void selectionSortTest() { + SelectionSort selectionSort = new SelectionSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, selectionSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, selectionSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, selectionSort.sort(unsortedChar)); - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, selectionSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/ShellSortTest.java b/src/test/java/com/sorts/ShellSortTest.java index a473de6908d6..6b59a38ec61d 100644 --- a/src/test/java/com/sorts/ShellSortTest.java +++ b/src/test/java/com/sorts/ShellSortTest.java @@ -1,21 +1,22 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.ShellSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class ShellSortTest { - @Test - public void shellSortTest() { - ShellSort shellSort = new ShellSort(); + @Test + public void shellSortTest() { + ShellSort shellSort = new ShellSort(); - Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; - Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - Assert.assertArrayEquals(sortedInt, shellSort.sort(unsortedInt)); + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertArrayEquals(sortedInt, shellSort.sort(unsortedInt)); - Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; - Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; - Assert.assertArrayEquals(sortedChar, shellSort.sort(unsortedChar)); - } + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + assertArrayEquals(sortedChar, shellSort.sort(unsortedChar)); + } } diff --git a/src/test/java/com/sorts/StoogeSortTest.java b/src/test/java/com/sorts/StoogeSortTest.java index 90ddbe696b2b..ac48acf35e0d 100644 --- a/src/test/java/com/sorts/StoogeSortTest.java +++ b/src/test/java/com/sorts/StoogeSortTest.java @@ -1,27 +1,28 @@ -package src.test.java.com.sorts; +package com.sorts; -import org.junit.Assert; -import org.junit.Test; -import src.main.java.com.sorts.StoogeSort; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class StoogeSortTest { - @Test - public void stoogeSortTest() { - StoogeSort stoogesort = new StoogeSort(); - - Integer unsortedArr[] = { 2, 4, 5, 3, 1 }; - Integer n = unsortedArr.length; - Integer sortedArr[] = { 1, 2, 3, 4, 5 }; - Assert.assertArrayEquals(sortedArr, stoogesort.sort(unsortedArr, 0, n - 1)); - - unsortedArr = new Integer[] { -22, -34, -25, -53, -11 }; - sortedArr = new Integer[] { -53, -34, -25, -22, -11 }; - Assert.assertArrayEquals(sortedArr, stoogesort.sort(unsortedArr, 0, n - 1)); - - Character[] unsortedCharArr = new Character[] { 'a', 'r', 'd', 'k', 'p' }; - n = unsortedCharArr.length; - Character[] sortedCharArr = new Character[] { 'a', 'd', 'k', 'p', 'r' }; - Assert.assertArrayEquals(sortedCharArr, stoogesort.sort(unsortedCharArr, 0, n - 1)); - } + @Test + public void stoogeSortTest() { + StoogeSort stoogesort = new StoogeSort(); + + Integer[] unsortedArr = {2, 4, 5, 3, 1}; + Integer n = unsortedArr.length; + Integer[] sortedArr = {1, 2, 3, 4, 5}; + assertArrayEquals(sortedArr, stoogesort.sort(unsortedArr, 0, n - 1)); + + unsortedArr = new Integer[]{-22, -34, -25, -53, -11}; + sortedArr = new Integer[]{-53, -34, -25, -22, -11}; + assertArrayEquals(sortedArr, stoogesort.sort(unsortedArr, 0, n - 1)); + + Character[] unsortedCharArr = new Character[]{'a', 'r', 'd', 'k', 'p'}; + n = unsortedCharArr.length; + Character[] sortedCharArr = new Character[]{'a', 'd', 'k', 'p', 'r'}; + assertArrayEquals(sortedCharArr, stoogesort.sort(unsortedCharArr, 0, n - 1)); + } } \ No newline at end of file diff --git a/src/test/java/com/generation/expected-result.png b/src/test/resources/com/generation/expected-result.png similarity index 100% rename from src/test/java/com/generation/expected-result.png rename to src/test/resources/com/generation/expected-result.png