| 1 |
// Copyright 2026 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package graph |
| 6 |
|
| 7 |
import ( |
| 8 |
"slices" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func assertPanic(t *testing.T, f func(), msg string) { |
| 13 |
t.Helper() |
| 14 |
defer func() { |
| 15 |
if r := recover(); r == nil { |
| 16 |
t.Error(msg) |
| 17 |
} |
| 18 |
}() |
| 19 |
f() |
| 20 |
} |
| 21 |
|
| 22 |
func TestIndex_Identity(t *testing.T) { |
| 23 |
ix := NewIdentityIndex(5) |
| 24 |
|
| 25 |
// Valid inputs |
| 26 |
for i := range 5 { |
| 27 |
if got := ix.Value(i); got != i { |
| 28 |
t.Errorf("Value(%d) = %d, want %d", i, got, i) |
| 29 |
} |
| 30 |
if got := ix.Index(i); got != i { |
| 31 |
t.Errorf("Index(%d) = %d, want %d", i, got, i) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Panics |
| 36 |
assertPanic(t, func() { ix.Value(-1) }, "Value(-1) should panic") |
| 37 |
assertPanic(t, func() { ix.Value(5) }, "Value(5) should panic") |
| 38 |
assertPanic(t, func() { ix.Index(-1) }, "Index(-1) should panic") |
| 39 |
assertPanic(t, func() { ix.Index(5) }, "Index(5) should panic") |
| 40 |
|
| 41 |
assertPanic(t, func() { NewIdentityIndex(-1) }, "NewIndentityIndex(-1) should panic") |
| 42 |
} |
| 43 |
|
| 44 |
func TestIndex_SortedInts(t *testing.T) { |
| 45 |
vals := []int{2, 4, 6, 8, 10} |
| 46 |
ix := NewIndex(slices.Values(vals)) |
| 47 |
|
| 48 |
// Verify representation |
| 49 |
if ix.index != nil { |
| 50 |
t.Errorf("Expected nil map for sorted ints") |
| 51 |
} |
| 52 |
|
| 53 |
// Valid inputs |
| 54 |
for i, v := range vals { |
| 55 |
if got := ix.Value(i); got != v { |
| 56 |
t.Errorf("Value(%d) = %d, want %d", i, got, v) |
| 57 |
} |
| 58 |
if got := ix.Index(v); got != i { |
| 59 |
t.Errorf("Index(%d) = %d, want %d", v, got, i) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Panics |
| 64 |
assertPanic(t, func() { ix.Value(-1) }, "Value(-1) should panic") |
| 65 |
assertPanic(t, func() { ix.Value(5) }, "Value(5) should panic") |
| 66 |
|
| 67 |
assertPanic(t, func() { ix.Index(3) }, "Index(3) should panic") |
| 68 |
assertPanic(t, func() { ix.Index(11) }, "Index(11) should panic") |
| 69 |
} |
| 70 |
|
| 71 |
func TestIndex_Full(t *testing.T) { |
| 72 |
vals := []string{"C", "A", "B"} |
| 73 |
ix := NewIndex(slices.Values(vals)) |
| 74 |
|
| 75 |
// Verify representation |
| 76 |
if ix.index == nil { |
| 77 |
t.Errorf("Expected non-nil map for unsorted/non-int keys") |
| 78 |
} |
| 79 |
|
| 80 |
// Valid inputs |
| 81 |
for i, v := range vals { |
| 82 |
if got := ix.Value(i); got != v { |
| 83 |
t.Errorf("Value(%d) = %q, want %q", i, got, v) |
| 84 |
} |
| 85 |
if got := ix.Index(v); got != i { |
| 86 |
t.Errorf("Index(%q) = %d, want %d", v, got, i) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Panics |
| 91 |
assertPanic(t, func() { ix.Value(-1) }, "Value(-1) should panic") |
| 92 |
assertPanic(t, func() { ix.Value(3) }, "Value(3) should panic") |
| 93 |
|
| 94 |
assertPanic(t, func() { ix.Index("D") }, "Index(\"D\") should panic") |
| 95 |
} |
| 96 |
|
| 97 |
func TestIndex_UnsortedInts(t *testing.T) { |
| 98 |
vals := []int{4, 2, 6} |
| 99 |
ix := NewIndex(slices.Values(vals)) |
| 100 |
|
| 101 |
// Verify representation |
| 102 |
if ix.index == nil { |
| 103 |
t.Errorf("Expected non-nil map for unsorted ints") |
| 104 |
} |
| 105 |
|
| 106 |
for i, v := range vals { |
| 107 |
if got := ix.Value(i); got != v { |
| 108 |
t.Errorf("Value(%d) = %d, want %d", i, got, v) |
| 109 |
} |
| 110 |
if got := ix.Index(v); got != i { |
| 111 |
t.Errorf("Index(%d) = %d, want %d", v, got, i) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
assertPanic(t, func() { ix.Index(3) }, "Index(3) should panic") |
| 116 |
} |
| 117 |
|
| 118 |
func TestIndex_Empty(t *testing.T) { |
| 119 |
ix := NewIndex(slices.Values([]string{})) |
| 120 |
assertPanic(t, func() { ix.Value(0) }, "Value(0) on empty index should panic") |
| 121 |
assertPanic(t, func() { ix.Index("A") }, "Index on empty index should panic") |
| 122 |
|
| 123 |
ix2 := NewIdentityIndex(0) |
| 124 |
assertPanic(t, func() { ix2.Value(0) }, "Value(0) on empty identity index should panic") |
| 125 |
assertPanic(t, func() { ix2.Index(0) }, "Index(0) on empty identity index should panic") |
| 126 |
} |