当前位置:网站首页>对数器
对数器
2022-06-27 02:31:00 【恒恒恒咩】
Part1对数器
基本概念
对数器是用来测试代码正确性的,我们在找不到合适的oj系统测试自己的代码时,可以自己写一个对数器对代码进行测试
基本步骤以及相关代码:
1.有一个你想要测的方法a;
public static void insertionSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
swap(arr, j, j + 1);
}
}
}
public static void swap(int[] arr, int i, int j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
2.实现一个绝对正确但是复杂度不好的方法b;
public static void comparator(int[] arr) {
Arrays.sort(arr);
}//java自带的排序方法
3.实现一个随机样本产生器;
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
//产生的数组长度是[0, size]
//产生的数组中的数的范围是-value ~ value
4.实现对比算法a和b的方法;
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
5.把方法a和方法b比对多次来验证方法a是否正确;
public static void main(String[] args) {
int testTime = 500000;//次数
int maxSize = 100;
int maxValue = 100;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr1 = generateRandomArray(maxSize, maxValue);
int[] arr2 = copyArray(arr1);
/*public static int[] copyArray(int[] arr) {
if (arr == null) {
return null;
}
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}*/
//比较方法,确定样本相同
insertionSort(arr1);
comparator(arr2);
if (!isEqual(arr1, arr2)) {
succeed = false;
break;
}
}
System.out.println(succeed ? "Nice!" : "Sorry");
6.如果有一个样本使得比对出错,打印样本分析是哪个方法出错;
7.当样本数量很多时比对测试依然正确,可以确定方法a已经正确。
[^相关资源]: https://www.bilibili.com/video/BV13g41157hK?p=3&vd_source=d810daa390a8d5e7a9e67f1a5e78ed57 P3
边栏推荐
- Learning Tai Chi Maker - mqtt (VII) advanced mqtt theme
- "All majors are persuading them to quit." is it actually the most friendly to college students?
- How does source insight (SI) display the full path? (do not display omitted paths) (turn off trim long path names with ellipses)
- ConstraintLayout(约束布局)开发指南
- LeetCode 785:判断二分图
- 1、项目准备与新建
- Flink学习2:应用场景
- D's appendto packaging
- Introduction to stm32
- Is the division of each capability domain of Dama, dcmm and other data management frameworks reasonable? Is there internal logic?
猜你喜欢

Flink学习3:数据处理模式(流批处理)

Why pass SPIF_ Sendchange flag systemparametersinfo will hang?

Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)

达梦数据库安装

Calculation of average wind direction and speed (unit vector method)

学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息

lottie.js创意开关按钮动物头像

Enterprise digital transformation: informatization and digitalization

Constraintlayout Development Guide

学习太极创客 — MQTT 第二章(二)ESP8266 QoS 应用
随机推荐
lottie.js创意开关按钮动物头像
jwt的认证流程和使用案例
Paddlepaddle 19 dynamically modify the last layer of the model
lodash get js代码实现
Oracle/PLSQL: VSize Function
Oracle/PLSQL: NumToDSInterval Function
Mmdetection uses yolox to train its own coco data set
Oracle/PLSQL: Replace Function
Enterprise digital transformation: informatization and digitalization
Flink学习4:flink技术栈
消费者追捧iPhone,在于它的性价比超越国产手机
Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work
学习太极创客 — MQTT(九)ESP8266 同时订阅和发布 MQTT 消息
Canvas particles: mouse following JS effect
YaLM 100B:来自俄罗斯Yandex的1000亿参数开源大模型,允许商业用途
Flink learning 5: how it works
Leetcode 785: judgment bipartite graph
学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息
three. JS domino JS special effect
Flink学习5:工作原理