Skip to content

Instantly share code, notes, and snippets.

@tiam-bloom
Created March 28, 2023 09:05
Show Gist options
  • Save tiam-bloom/fe27fa4afd01c141daa9e50434342999 to your computer and use it in GitHub Desktop.
Save tiam-bloom/fe27fa4afd01c141daa9e50434342999 to your computer and use it in GitHub Desktop.
测试排序方法的测试类, 包括一次, 多次, 执行时间, 生成随机数组, 判断数组是否在一定范围内 等方法
package utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.function.Consumer;
import java.util.stream.IntStream;
/**
* @author Tiam
* @date 2023/3/27 19:13
* @description
*/
public class TestSort {
@Test
public void test() {
// testIsOutOfBound((int) 1e7);
SortMethods sortMethods = new SortMethodsImpl();
// testSortMethod((int) 1e7, sortMethods::bubbleSort);
// testSortMethodStability((int) 1e8, sortMethods::bubbleSort); // 27xxx
// testSortMethodStability((int) 1e8, sortMethods::selectSort); // 28xxx
// testSortMethodStability((int) 1e8, Arrays::sort); // 21xxx
testSortMethodStability((int) 1e7, sortMethods::insertSort); // 23xxx
// testSortMethodStability((int) 1e7, sortMethods::quickSort); // 31xxx
}
private long startTime;
@Before
public void setStartTime() {
startTime = System.currentTimeMillis();
}
@After
public void calExecuteTime() {
long all = System.currentTimeMillis() - startTime;
System.out.println("执行总耗时" + all + "毫秒");
}
/**
* 与java自带的排序方法作比较, 测试排序方法的正确性
*
* @param testTimes 测试次数
* @param sortMethod 待测试的排序方法
*/
public void testSortMethod(int testTimes, Consumer<int[]> sortMethod) {
for (int i = 0; i < testTimes; i++) {
int[] arr = generateRandomArray(10);
sortMethod.accept(arr);
if (!isRightSort(arr)) {
System.out.println("第" + (i + 1) + "次排序出问题啦!!!");
System.out.println("sortMethod 排序结果: " + Arrays.toString(arr));
return;
}
}
System.out.println("通过" + testTimes + "次测试, 哎哟不错哦");
}
/**
* 判断arr数组是否有序
*
* @param arr
* @return
*/
public boolean isRightSort(int[] arr) {
int[] arrCopy = Arrays.copyOf(arr, arr.length);
Arrays.sort(arrCopy);
return Arrays.equals(arrCopy, arr);
}
/**
* 测试排序方法的稳定性, 成功率
*
* @param testTimes
* @param sortMethod
*/
public void testSortMethodStability(int testTimes, Consumer<int[]> sortMethod) {
int count = 0;
for (int i = 0; i < testTimes; i++) {
int[] arr = generateRandomArray(10);
sortMethod.accept(arr);
if (!isRightSort(arr)) count++;
}
NumberFormat nt = NumberFormat.getPercentInstance();//获取格式化对象
nt.setMinimumFractionDigits(2);
double failPercent = (testTimes * 1.0 - count) / testTimes;
System.out.println("经过" + testTimes + "次测试,未通过" + count + "次,通过率为: " + (nt.format(failPercent)));
}
/**
* 生成一个范围在[min, max]的随机数组
*
* @param length 数组长度
* @param min 数组中最小值
* @param max 最大值
* @return 随机数组
*/
public int[] generateRandomArray(int length, int min, int max) {
if (max - min < 1 || length < 1) {
throw new IllegalArgumentException("参数异常");
}
int[] array = new int[length];
double random;
for (int i = 0; i < length; i++) {
// 范围 [min, max+1)
random = Math.random() * (max + 1 - min) + min;
array[i] = (int) random;
}
return array;
}
/**
* 方法重载(默认参数)
*
* @param length 长度
* @return 随机数组范围[-100,100]
*/
public int[] generateRandomArray(int length) {
return this.generateRandomArray(length, -100, 100);
}
/**
* 测试生成的数组是否在范围内
*
* @param testTimes 测试次数
*/
public void testIsOutOfBound(int testTimes) {
int min = -10, max = 10;
int[] arr;
for (int i = 0; i < testTimes; i++) {
arr = generateRandomArray(10, min, max);
if (IsOutOfBound(arr, min, max)) {
System.out.print(Arrays.toString(arr));
System.out.println("数组超界!");
}
}
System.out.println("通过" + testTimes + "次测试, Nice! It's very good!");
}
/**
* 判断arr是否在[min,max]范围内
*
* @param arr
* @param min
* @param max
* @return
*/
public boolean IsOutOfBound(int[] arr, int min, int max) {
if (getArrayLimitVal(arr, false) < min || getArrayLimitVal(arr, true) > max) {
System.out.println("数组超界!");
System.out.println(Arrays.toString(arr));
return true;
}
return false;
}
/**
* @param arr 数组
* @param isMaxVal 是否需要最大值
* @return 数组的最大值或最小值
*/
public int getArrayLimitVal(int[] arr, boolean isMaxVal) {
IntStream stream = Arrays.stream(arr);
OptionalInt res = isMaxVal ? stream.max() : stream.min();
return res.getAsInt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment