| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package sitesmatrix |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestDimensionsCompare(t *testing.T) { |
| 23 |
c := qt.New(t) |
| 24 |
|
| 25 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{1, 2, 8}), qt.Equals, -1) |
| 26 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{1, 2, 3}), qt.Equals, 0) |
| 27 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{1, 2, 0}), qt.Equals, 1) |
| 28 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{1, 0, 3}), qt.Equals, 1) |
| 29 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{0, 3, 2}), qt.Equals, 1) |
| 30 |
c.Assert(Vector{1, 2, 3}.Compare(Vector{0, 0, 0}), qt.Equals, 1) |
| 31 |
c.Assert(Vector{0, 0, 0}.Compare(Vector{1, 2, 3}), qt.Equals, -1) |
| 32 |
c.Assert(Vector{0, 0, 0}.Compare(Vector{0, 0, 0}), qt.Equals, 0) |
| 33 |
c.Assert(Vector{0, 0, 0}.Compare(Vector{1, 0, 0}), qt.Equals, -1) |
| 34 |
c.Assert(Vector{0, 0, 0}.Compare(Vector{0, 1, 0}), qt.Equals, -1) |
| 35 |
c.Assert(Vector{0, 0, 0}.Compare(Vector{0, 0, 1}), qt.Equals, -1) |
| 36 |
} |
| 37 |
|
| 38 |
func TestDimensionsDistance(t *testing.T) { |
| 39 |
c := qt.New(t) |
| 40 |
|
| 41 |
c.Assert(Vector{1, 2, 3}.Distance(Vector{1, 2, 8}), qt.Equals, -5) |
| 42 |
c.Assert(Vector{1, 2, 3}.Distance(Vector{1, 2, 3}), qt.Equals, 0) |
| 43 |
c.Assert(Vector{1, 2, 3}.Distance(Vector{1, 2, 0}), qt.Equals, 3) |
| 44 |
c.Assert(Vector{1, 2, 3}.Distance(Vector{1, 0, 3}), qt.Equals, 2) |
| 45 |
c.Assert(Vector{1, 2, 3}.Distance(Vector{0, 3, 2}), qt.Equals, 1) |
| 46 |
} |
| 47 |
|
| 48 |
func BenchmarkCompare(b *testing.B) { |
| 49 |
b.Run("Equal", func(b *testing.B) { |
| 50 |
v1 := Vector{1, 2, 3} |
| 51 |
v2 := Vector{1, 2, 3} |
| 52 |
for b.Loop() { |
| 53 |
v1.Compare(v2) |
| 54 |
} |
| 55 |
}) |
| 56 |
|
| 57 |
b.Run("First different", func(b *testing.B) { |
| 58 |
v1 := Vector{1, 2, 3} |
| 59 |
v2 := Vector{2, 2, 3} |
| 60 |
for b.Loop() { |
| 61 |
v1.Compare(v2) |
| 62 |
} |
| 63 |
}) |
| 64 |
|
| 65 |
b.Run("Last different", func(b *testing.B) { |
| 66 |
v1 := Vector{1, 2, 3} |
| 67 |
v2 := Vector{1, 2, 4} |
| 68 |
for b.Loop() { |
| 69 |
v1.Compare(v2) |
| 70 |
} |
| 71 |
}) |
| 72 |
} |