From d8115886f3603fc5951521bf095d0c1ee73b9b47 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sun, 21 Jan 2024 22:47:06 +0900 Subject: [PATCH] gh-63: Return error if other is nil (#64) --- .github/workflows/ci.yml | 2 +- .github/workflows/ci_gomodule.yml | 2 +- imagehash.go | 5 +++++ imagehash_test.go | 11 +++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14e8d36..db3f6ba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.6.x, 1.7.x, 1.8.x, 1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x] + go-version: [1.6.x, 1.7.x, 1.8.x, 1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x] platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/.github/workflows/ci_gomodule.yml b/.github/workflows/ci_gomodule.yml index 8ff0aeb..dd876f4 100644 --- a/.github/workflows/ci_gomodule.yml +++ b/.github/workflows/ci_gomodule.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x] + go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/imagehash.go b/imagehash.go index 9cc384a..f6239f4 100644 --- a/imagehash.go +++ b/imagehash.go @@ -13,6 +13,8 @@ import ( "io" ) +var errNoOther = errors.New("other should not be nil") + // Kind describes the kinds of hash. type Kind int @@ -54,6 +56,9 @@ func (h *ImageHash) Bits() int { // Distance method returns a distance between two hashes. func (h *ImageHash) Distance(other *ImageHash) (int, error) { + if other == nil { + return -1, errNoOther + } if h.GetKind() != other.GetKind() { return -1, errors.New("Image hashes's kind should be identical") } diff --git a/imagehash_test.go b/imagehash_test.go index 93ebf21..2b1a8e1 100644 --- a/imagehash_test.go +++ b/imagehash_test.go @@ -56,6 +56,17 @@ func TestNewImageHash(t *testing.T) { } } +func TestNil(t *testing.T) { + hash := NewImageHash(0, AHash) + dis, err := hash.Distance(nil) + if err != errNoOther { + t.Errorf("Expected err %s, actual %s", errNoOther, err) + } + if dis != -1 { + t.Errorf("Distance is expected as %d but got %d", -1, dis) + } +} + func TestSerialization(t *testing.T) { checkErr := func(err error) { if err != nil {