- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是测试新手。所以,我有一个学习任务。
该程序计算二次方程。
例如,如何使用 JUnit 检查 c != 0 且 D > 0?
我想以正确的方式做到这一点。
我尝试从 JUnit 调用 input(),但测试变得无休无止。
public class Main {
public static Scanner input;
public static double a, b, c;
public static double d;
public static double x1, x2;
public static void main(String args []) {
input();
}
public static void input() {
System.out.println("ax^2 + bx + c = 0");
input = new Scanner(System.in);
try {
System.out.print("a -> ");
a = input.nextFloat();
System.out.print("b -> ");
b = input.nextFloat();
System.out.print("c -> ");
c = input.nextFloat();
} catch (InputMismatchException e) {
System.out.println("Input number!\n");
input();
}
if(a == 0 || b == 0 || c == 0) {
System.out.println("Not right\n");
input();
return;
}
calculate();
}
public static void calculate() {
d = (b * b) - (4 * a * c);
System.out.println("D = " + d);
if(d < 0) {
System.out.print("No answer");
} else if(d == 0) {
x1 = (-b) / (2 * a);
System.out.println ("x = " + x1);
} else {
x1 = (-b + Math.sqrt(d)) / (2 * a);
x2 = (-b - Math.sqrt(d)) / (2 * a);
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
}
最佳答案
您的程序在当前形式下不可测试。这是一个替代方案:
public class Parabola {
// These are required in the constructor. Omitted for brevity
private final double a;
private final double b;
private final double c;
// Public API. Test this
public double x1() { /*...*/ }
public double x2() { /*...*/ }
}
现在编写测试很简单:
@Test public void test1() {
Parabola target = new Parabola(1, 2, 3);
assertEquals(target.x1(), 44.23);
assertEquals(target.x2(), 17.23);
}
@Test public void test2() {
Parabola target = new Parabola(1, 0, 0);
assertEquals(target.x1(), -1);
assertEquals(target.x2(), 13.43);
}
最后,这是 main()
的样子:
public void main(String... args) throws Exception {
Scanner console = ...;
System.out.println("Type a:");
Double a = Double.parseDouble(console.nextLine());
// Same for b and c
Parabola p = new Parabola(a, b, c);
System.out.println("X axis intersected in " + p.x1() + " and " + p.x2());
}
奖励:在单元测试中比较 double 时,您可能需要使用 this version of assertEquals()
由于浮点算术的工作原理和/或二进制数的十进制表示的工作原理,它接受 threshold
参数
关于java - JUnit 中的测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42988241/
过去 7-8 个月以来,我们一直在使用 Firebase。 这是一个非常棒的工具,感谢您的努力。 这里我有一个问题,是否有一种方法可以在不实际写入数据库的情况下修改数据。 因为大多数情况下,当我们调试
我正在尝试创建一个 W2V 模型,然后生成用于我的模型的训练和测试数据。我的问题是,在使用训练数据创建 W2V 模型后,如何生成测试数据。 最佳答案 Word2Vec 被认为是一种“无监督”算法,因此
from scipy.cluster.hierarchy import dendrogram, linkage,fcluster import numpy as np import matplotli
我有一个组件,我想在单元测试中检查选择器 @Component({ selector: 'my-component', )} 我想这样测试 describe('My Component',
想知道哪个好。 我必须将一些 JUnit 测试数据保存在不同的文件中。让我们将其称为 TestingData.java。我想到了两种方法。 第一种方式 TestingData.java public
这个问题在这里已经有了答案: 关闭 14 年前。 我想对我的一些 SQL 查询进行压力测试,找出糟糕的查询计划和瓶颈。我计划用随机测试数据填充一些表。 是否有工具或一组脚本可用于此目的,最好是用于
我正在尝试预测以下内容: list( [ 收盘价(当日) - 开盘价(当日) ] ) 使用以下内容作为输入: list( [ 开盘价(当天) - 收盘价(昨天) ] ) 但是,我的 test_pred
我已经编写了一个函数库,可以根据 ISO/IEC 7813 从卡片(信用卡、ID 等)上的磁条解析轨道 1 和轨道 2 数据。我希望针对一些有效的(但显然不是)严格测试这个库真正的信用卡')跟踪 2
我的问题如下。我有 6 个数据集(从不同的日子收集),我想为其构建决策树分类器。我需要找到最佳的树深度参数以避免过度拟合并对新数据进行良好的预测。 我的第一次尝试是将每个数据集按 50%/25%/25
我想在我的仪器测试中使用大文件(> 2 GB 的 zip 存档和视频文件)来测试从 SD 卡/内部存储加载文件。 如何编写这些仪器测试并为它们配备所需的文件?对于其他测试,我只需要非常小的文件,所以我
我在理解 MXNet ImageRecordIter 的工作原理时遇到了一些困难。 Here是我一直在使用的引用 首先,--test-ratio 标志实际上是做什么的?生成lst文件时,我无法分辨哪些
考虑具有成本函数的线性回归模型: 这里有 = 模型的权重 我们添加正则化参数以避免过度拟合数据。正则化项通过根据模型的权重对模型进行惩罚来阻止使用大的权重,转而使用较小的权重。问题是: 为什么保持模型
我们正在为基于 REST 的后端构建 Grails 前端。我们有很多值 (DTO) 编码到 JSON 或从 JSON 编码。 我正在寻找一种方便的方法来构建这些 DTO 的模拟以用于测试目的。我喜欢
我在家里的电脑和笔记本电脑上都在做一个 Python/MongoDB 项目。自然地,文档存储中的模式最好由数据本身表示 - 这就是为什么我想通过 Mercurial 分发我的测试数据以及代码本身。 最
Spring Test 有助于回滚测试方法中对数据库所做的任何更改。这意味着不必在每个测试方法之前花时间删除/重新加载测试数据。 但是,如果您使用 @BeforeClass Junit 注释,则会强制
我试图理解这行代码: msk = np.random.rand(len(df)) 小于 ( 大于或等于 (>=) 0.8。 ~msk 将 True 翻转为 False 并将 False 翻转为 Tr
我已按照说明使用 JMonkey 创建 Gradle 项目,但无法按照教程中所述加载任何 Assets : http://wiki.jmonkeyengine.org/doku.php/jme3:be
我试图理解这行代码: msk = np.random.rand(len(df)) 小于 ( 大于或等于 (>=) 0.8。 ~msk 将 True 翻转为 False 并将 False 翻转为 Tr
我正在尝试使用此处介绍的 Excel 数据阅读器 http://fabiouechi.blogspot.fi/2010/07/excel-data-driven-tests-with-nunit.ht
我想知道是否可以在根 html 元素级别向第 3 方包添加唯一标识符。例如,我通过 NPM 使用包 myDatePicker。 我可以将 ID 标记添加到实际的 html 元素,但嵌套在该元素内部的是
我是一名优秀的程序员,十分优秀!