作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想对返回 Result
的函数进行单元测试(见下文)。
我的问题是:如何轻松检查结果在数字上是否等于预期值?
这是完全匹配的版本。
type QuadraticResult =
| ComplexResult of Complex * Complex
| DoubleResult of float
| TwoResults of float * float
type Result=
| QuadraticResult of QuadraticResult
| LinearResult of LinearFormulaSolver.Result
/// Solves a x² + bx + c = 0
let Compute (a,b,c) : Result =
[<Fact>]
member test.``the solution for x² = 0.0 is a double 0.0`` ()=
let result = Compute (1.0, 0.0, 0.0)
let expected = Result.QuadraticResult (DoubleResult 0.0)
// only exact match, I'd like to test if difference is below a certain threshold
Assert.Equal (result, expected)
let ComplexEquality distance (x : Complex) (y : Complex )=
let dx = x.Real - y.Real
let dy = x.Imaginary - y.Imaginary
abs (dx) < distance && abs(dy) < distance
let QuadraticEquality distance x y = match (x,y) with
| (ComplexResult (a,b),ComplexResult(c,d)) -> (ComplexEquality distance a c && ComplexEquality distance b d) || (ComplexEquality distance a d && ComplexEquality distance b c)
| (DoubleResult a,DoubleResult b) -> abs (a - b) < distance
| (TwoResults (a,b),TwoResults(c,d)) -> (abs(a - c) < distance && (b - d) < distance) || (abs(a - d) < distance && (b - c) < distance)
| _ -> false
let LinearEquality distance x y = match (x , y) with
| (SingleResult a, SingleResult b) -> abs (a-b) < distance
| (NoResults, NoResults) | (InfiniteResults, InfiniteResults) -> true
| _ -> false
let ResultEquality distance x y = match (x,y) with
| (QuadraticResult a,QuadraticResult b) -> QuadraticEquality distance a b
| (LinearResult a,LinearResult b) -> LinearEquality distance a b
| _ -> false
[<Fact>]
member test.``the solution for x² = 0 is a double 0`` ()=
let result = QuadraticFormulaSolver.Compute (1.0, 0.0, 0.0)
let expected = Result.QuadraticResult (QuadraticFormulaSolver.DoubleResult 0.00001)
Assert.True( ResultEquality 0.001 result expected)
最佳答案
我认为没有任何“魔术”可以让您自动执行此操作。我认为你有三个选择:
float
执行特殊类型的比较值 float
上写一个包装器实现自定义比较,然后在可区分联合中使用此类型 [<Struct; CustomComparison; CustomEquality>]
type ApproxFloat(f:float) =
member x.Value = f
override x.GetHashCode() = f.GetHashCode()
override x.Equals(another) =
match another with
| :? ApproxFloat as y -> abs (x.Value - y.Value) <= 0.001
| _ -> false
interface System.IComparable with
member x.CompareTo(another) =
match another with
| :? ApproxFloat as y -> compare x.Value y.Value
| _ -> failwith "Cannot compare"
type Complex =
| Complex of ApproxFloat * ApproxFloat
type Result =
| Result of Complex
Result(Complex(ApproxFloat(1.0), ApproxFloat(1.0))) =
Result(Complex(ApproxFloat(1.0001), ApproxFloat(1.0001)))
关于f# - 如何比较深度嵌套的歧视工会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484587/
我一直在使用标记的联合类型来处理 typescript 歧视,但我遇到了一些奇怪的事情。如果我打开实际的对象属性,一切都会按预期工作。但是如果我使用解构,typescript 会报错。我认为它与编译时
我正在创建一个动态小部件,它可以是空的也可以是已填充的。它应该默认为空。当populated={true} , 我想要 totalGross和 totalNet需要的值。否则,不应允许使用这 2 个
我是一名优秀的程序员,十分优秀!