From c771089882008a00bfb1b4b06f859bdd48aa55a7 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 23 Aug 2020 12:40:30 +0800 Subject: [PATCH] test using rand numbers --- Maths/AbsoluteValue.java | 15 +++++++++------ Maths/Ceil.java | 17 +++++++---------- Maths/FindMax.java | 23 +++++++++++++++++++---- Maths/FindMaxRecursion.java | 20 +++++++++++++++----- Maths/FindMin.java | 21 ++++++++++++++++++--- Maths/FindMinRecursion.java | 28 +++++++++++++++++++++------- Maths/Floor.java | 17 +++++++---------- Maths/MaxValue.java | 28 +++++++++++++++++----------- Maths/MinValue.java | 28 +++++++++++++++++----------- 9 files changed, 130 insertions(+), 67 deletions(-) diff --git a/Maths/AbsoluteValue.java b/Maths/AbsoluteValue.java index da570d0b..440a7e23 100644 --- a/Maths/AbsoluteValue.java +++ b/Maths/AbsoluteValue.java @@ -1,14 +1,17 @@ package Maths; +import java.util.Random; + public class AbsoluteValue { public static void main(String[] args) { - assert absVal(-13) == 13; - assert absVal(0) == 0; - assert absVal(100) == 100; - - int value = -34; - System.out.println("The absolute value of " + value + " is " + absVal(value)); + Random random = new Random(); + + /* test 1000 random numbers */ + for (int i = 1; i <= 1000; ++i) { + int randomNumber = random.nextInt(); + assert absVal(randomNumber) == Math.abs(randomNumber); + } } /** diff --git a/Maths/Ceil.java b/Maths/Ceil.java index d84ab825..b6f08693 100644 --- a/Maths/Ceil.java +++ b/Maths/Ceil.java @@ -1,17 +1,14 @@ package Maths; +import java.util.Random; + public class Ceil { public static void main(String[] args) { - assert ceil(10) == Math.ceil(10); - assert ceil(-10) == Math.ceil(-10); - assert ceil(10.0) == Math.ceil(10.0); - assert ceil(-10.0) == Math.ceil(-10.0); - assert ceil(10.1) == Math.ceil(10.1); - assert ceil(-10.1) == Math.ceil(-10.1); - assert ceil(0) == Math.ceil(0); - assert ceil(-0) == Math.ceil(-0); - assert ceil(0.0) == Math.ceil(0.0); - assert ceil(-0.0) == Math.ceil(-0.0); + Random random = new Random(); + for (int i = 1; i <= 1000; ++i) { + double randomNumber = random.nextDouble(); + assert ceil(randomNumber) == Math.ceil(randomNumber); + } } /** diff --git a/Maths/FindMax.java b/Maths/FindMax.java index f98ec5d0..1a332367 100644 --- a/Maths/FindMax.java +++ b/Maths/FindMax.java @@ -1,18 +1,33 @@ package Maths; +import java.util.Arrays; +import java.util.Random; + public class FindMax { - //Driver + /** + * Driver Code + */ public static void main(String[] args) { - int[] array = {2, 4, 9, 7, 19, 94, 5}; - assert findMax(array) == 94; + Random random = new Random(); + + /* random size */ + int size = random.nextInt(100) + 1; + int[] array = new int[size]; + + /* init array with random numbers */ + for (int i = 0; i < size; i++) { + array[i] = random.nextInt() % 100; + } + + assert Arrays.stream(array).max().getAsInt() == findMax(array); } /** * find max of array * * @param array the array contains element - * @return max value + * @return max value of given array */ public static int findMax(int[] array) { int max = array[0]; diff --git a/Maths/FindMaxRecursion.java b/Maths/FindMaxRecursion.java index 6d061626..867390b3 100644 --- a/Maths/FindMaxRecursion.java +++ b/Maths/FindMaxRecursion.java @@ -1,13 +1,23 @@ package Maths; +import java.util.Arrays; +import java.util.Random; + public class FindMaxRecursion { public static void main(String[] args) { - int[] array = {2, 4, 9, 7, 19, 94, 5}; - int low = 0; - int high = array.length - 1; + Random rand = new Random(); - assert max(array, low, high) == 94; - assert max(array, array.length) == 94; + /* rand size */ + int size = rand.nextInt(100) + 1; + int[] array = new int[size]; + + /* init array with rand numbers */ + for (int i = 0; i < size; i++) { + array[i] = rand.nextInt() % 100; + } + + assert max(array, array.length) == Arrays.stream(array).max().getAsInt(); + assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt(); } /** diff --git a/Maths/FindMin.java b/Maths/FindMin.java index a52b3ff2..b8996cd1 100644 --- a/Maths/FindMin.java +++ b/Maths/FindMin.java @@ -1,11 +1,26 @@ package Maths; +import java.util.Arrays; +import java.util.Random; + public class FindMin { - //Driver + /** + * Driver Code + */ public static void main(String[] args) { - int[] array = {2, 4, 9, 7, 19, 94, 5}; - assert findMin(array) == 2; + Random random = new Random(); + + /* random size */ + int size = random.nextInt(100) + 1; + int[] array = new int[size]; + + /* init array with random numbers */ + for (int i = 0; i < size; i++) { + array[i] = random.nextInt() % 100; + } + + assert Arrays.stream(array).min().getAsInt() == findMin(array); } /** diff --git a/Maths/FindMinRecursion.java b/Maths/FindMinRecursion.java index c1b5f285..ba9b9c91 100644 --- a/Maths/FindMinRecursion.java +++ b/Maths/FindMinRecursion.java @@ -1,13 +1,27 @@ package Maths; -public class FindMinRecursion { - public static void main(String[] args) { - int[] array = {2, 4, 9, -7, 19, 94, 5}; - int low = 0; - int high = array.length - 1; +import java.util.Arrays; +import java.util.Random; - assert min(array, low, high) == -7; - assert min(array, array.length) == -7; +public class FindMinRecursion { + + /** + * Driver Code + */ + public static void main(String[] args) { + Random rand = new Random(); + + /* rand size */ + int size = rand.nextInt(100) + 1; + int[] array = new int[size]; + + /* init array with rand numbers */ + for (int i = 0; i < size; i++) { + array[i] = rand.nextInt() % 100; + } + + assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt(); + assert min(array, array.length) == Arrays.stream(array).min().getAsInt(); } /** diff --git a/Maths/Floor.java b/Maths/Floor.java index e8a43da4..5bb4b867 100644 --- a/Maths/Floor.java +++ b/Maths/Floor.java @@ -1,17 +1,14 @@ package Maths; +import java.util.Random; + public class Floor { public static void main(String[] args) { - assert floor(10) == Math.floor(10); - assert floor(-10) == Math.floor(-10); - assert floor(10.0) == Math.floor(10.0); - assert floor(-10.0) == Math.floor(-10.0); - assert floor(10.1) == Math.floor(10.1); - assert floor(-10.1) == Math.floor(-10.1); - assert floor(0) == Math.floor(0); - assert floor(-0) == Math.floor(-0); - assert floor(0.0) == Math.floor(0.0); - assert floor(-0.0) == Math.floor(-0.0); + Random random = new Random(); + for (int i = 1; i <= 1000; ++i) { + double randomNumber = random.nextDouble(); + assert floor(randomNumber) == Math.floor(randomNumber); + } } /** diff --git a/Maths/MaxValue.java b/Maths/MaxValue.java index 9be31cd1..d163bd3c 100644 --- a/Maths/MaxValue.java +++ b/Maths/MaxValue.java @@ -1,7 +1,24 @@ package Maths; +import java.util.Random; + public class MaxValue { + /** + * Driver Code + */ + public static void main(String[] args) { + Random rand = new Random(); + + /* test 100 times using rand numbers */ + for (int i = 1; i <= 100; ++i) { + /* generate number from -50 to 49 */ + int a = rand.nextInt(100) - 50; + int b = rand.nextInt(100) - 50; + assert max(a, b) == Math.max(a, b); + } + } + /** * Returns the greater of two {@code int} values. That is, the * result is the argument closer to the value of @@ -15,15 +32,4 @@ public class MaxValue { public static int max(int a, int b) { return a >= b ? a : b; } - - public static void main(String[] args) { - assert max(-3,3) == 3; - assert max(-6,-20) == -6; - assert max(100,32) == 100; - assert max(13,13) == 13; - - int a = 3; - int b = 4; - System.out.format("max:%d between %d and %d", max(a, b), a, b); - } } diff --git a/Maths/MinValue.java b/Maths/MinValue.java index 7bafd880..1e92169f 100644 --- a/Maths/MinValue.java +++ b/Maths/MinValue.java @@ -1,7 +1,24 @@ package Maths; +import java.util.Random; + public class MinValue { + /** + * Driver Code + */ + public static void main(String[] args) { + Random rand = new Random(); + + /* test 100 times using rand numbers */ + for (int i = 1; i <= 100; ++i) { + /* generate number from -50 to 49 */ + int a = rand.nextInt(100) - 50; + int b = rand.nextInt(100) - 50; + assert min(a, b) == Math.min(a, b); + } + } + /** * Returns the smaller of two {@code int} values. That is, * the result the argument closer to the value of @@ -15,15 +32,4 @@ public class MinValue { public static int min(int a, int b) { return a <= b ? a : b; } - - public static void main(String[] args) { - assert min(-3,3) == -3; - assert min(-6,-20) == -20; - assert min(100,32) == 32; - assert min(13,13) == 13; - - int a = 3; - int b = 4; - System.out.format("min:%d between %d and %d", min(a, b), a, b); - } }