goimagehash/transforms/dct_test.go
2017-07-31 22:47:14 +09:00

75 lines
1.6 KiB
Go

// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transforms
import (
"testing"
)
const (
EPSILON float64 = 0.00000001
)
func TestDCT1D(t *testing.T) {
for _, tt := range []struct {
input []float64
output []float64
}{
{[]float64{1.0, 1.0, 1.0, 1.0}, []float64{2.0, 0, 0, 0}},
} {
out := DCT1D(tt.input)
pass := true
if len(tt.output) != len(out) {
t.Errorf("DCT1D(%v) is expected %v but got %v.", tt.input, tt.output, out)
}
for i := range out {
if (out[i]-tt.output[i]) > EPSILON || (tt.output[i]-out[i]) > EPSILON {
pass = false
}
}
if !pass || len(tt.output) != len(out) {
t.Errorf("DCT1D(%v) is expected %v but got %v.", tt.input, tt.output, out)
}
}
}
func TestDCT2D(t *testing.T) {
for _, tt := range []struct {
input [][]float64
output [][]float64
w int
h int
}{
{[][]float64{{1.0, 2.0, 3.0, 4.0},
{5.0, 6.0, 7.0, 8.0},
{9.0, 10.0, 11.0, 12.0},
{13.0, 14.0, 15.0, 16.0}},
[][]float64{{34.0, -4.46088499, 0.0, -0.31702534},
{-17.84353998, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0},
{-1.26810134, 0.0, 0.0, 0.0}},
4, 4},
} {
out := DCT2D(tt.input, tt.w, tt.h)
pass := true
for i := 0; i < tt.h; i++ {
for j := 0; j < tt.w; j++ {
if (out[i][j]-tt.output[i][j]) > EPSILON || (tt.output[i][j]-out[i][j]) > EPSILON {
pass = false
}
}
}
if !pass {
t.Errorf("DCT2D(%v) is expected %v but got %v.", tt.input, tt.output, out)
}
}
}