2014-05-02 23:30:12 +08:00
|
|
|
package pairwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
|
|
|
mat "github.com/skelterjohn/go.matrix"
|
|
|
|
)
|
|
|
|
|
2014-05-02 23:49:06 +08:00
|
|
|
func TestEuclidean(t *testing.T) {
|
2014-05-02 23:30:12 +08:00
|
|
|
euclidean := NewEuclidean()
|
|
|
|
|
|
|
|
Convey("Given two vectors", t, func() {
|
|
|
|
vectorX := mat.MakeDenseMatrix([]float64{1, 2, 3}, 3, 1)
|
2014-05-02 23:49:06 +08:00
|
|
|
vectorY := mat.MakeDenseMatrix([]float64{2, 4, 5}, 3, 1)
|
2014-05-02 23:30:12 +08:00
|
|
|
|
|
|
|
Convey("When doing inner product", func() {
|
|
|
|
result := euclidean.InnerProduct(vectorX, vectorY)
|
|
|
|
|
2014-05-02 23:49:06 +08:00
|
|
|
Convey("The result should be 25", func() {
|
|
|
|
So(result, ShouldEqual, 25)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When dimension not match", func() {
|
|
|
|
vectorZ := mat.MakeDenseMatrix([]float64{3, 4, 5}, 1, 3)
|
|
|
|
|
|
|
|
Convey("It should panic with Dimension mismatch", func() {
|
|
|
|
So(func() { euclidean.InnerProduct(vectorX, vectorZ) }, ShouldPanicWith, "Dimension mismatch")
|
|
|
|
})
|
|
|
|
|
2014-05-02 23:30:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2014-05-02 23:49:06 +08:00
|
|
|
Convey("When calculating distance", func() {
|
|
|
|
result, err := euclidean.Distance(vectorX, vectorY)
|
|
|
|
|
|
|
|
Convey("The err should be nil", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2014-05-02 23:30:12 +08:00
|
|
|
|
2014-05-02 23:49:06 +08:00
|
|
|
Convey("The result should be 3", func() {
|
|
|
|
So(result, ShouldEqual, 3)
|
2014-05-02 23:30:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|