feat(Go-StudyExample):2020/12/31:新增【Go每日一库】go-cmp结构体比较工具

This commit is contained in:
Huangzj
2020-12-31 14:48:58 +08:00
parent 20c2c7fab0
commit 6d079549f1
10 changed files with 465 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
/*
* @Author : huangzj
* @Time : 2020/12/30 15:11
* @Description
*/
package go_cmp
import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"math"
"testing"
)
type FloatPair struct {
X float64
Y float64
}
func TestBasicFloat(t *testing.T) {
//特殊的浮点数NaNNot a Number),它与任何浮点数都不等,包括它自己
p1 := FloatPair{X: math.NaN()}
p2 := FloatPair{X: math.NaN()}
fmt.Println("p1 equals p2?", cmp.Equal(p1, p2))
//受限于变量的存储空间,就会存在误差
f1 := 0.1
f2 := 0.2
f3 := 0.3
p3 := FloatPair{X: f1 + f2}
p4 := FloatPair{X: f3}
fmt.Println("p3 equals p4?", cmp.Equal(p3, p4))
//Go 语言中这些字面量的运算直接是在编译器完成的,所以同样的值虽然受限于变量的存储空间,但是结果是一样的.
p5 := FloatPair{X: 0.1 + 0.2}
p6 := FloatPair{X: 0.3}
fmt.Println("p5 equals p6?", cmp.Equal(p5, p6))
}
//通过equal方法测试NaN返回结果一致
func TestNanEqual(t *testing.T) {
p1 := FloatPair{X: math.NaN()}
p2 := FloatPair{X: math.NaN()}
fmt.Println("p1 equals p2?", cmp.Equal(p1, p2, cmpopts.EquateNaNs()))
}
//测试误差在一定的范围内 具体表示是:|x-y| ≤ max(fraction*min(|x|, |y|), margin)
func TestCountError(t *testing.T) {
f1 := 0.1
f2 := 0.2
f3 := 0.3
p3 := FloatPair{X: f1 + f2}
p4 := FloatPair{X: f3}
fmt.Println(fmt.Sprintf("实际存储的值分别是:\n %v \n %v", p3.X, p4.X))
fmt.Println("p3 equals p4?", cmp.Equal(p3, p4, cmpopts.EquateApprox(0.1, 0.001)))
}