Debian Codegraph @

internal/graph/index_test.go

raw symbols SCIP
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 graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/
6
7 import (
8 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
9 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
10 )
11
12 func assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, f func(), msg string) {
13 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().()
14 defer func() {
15 if r := recover(); r == nil {
16 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(msg)
17 }
18 }()
19 f()
20 }
21
22 func TestIndex_Identityscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestIndex_Identity().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
23 ix := NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(5)
24
25 // Valid inputs
26 for i := range 5 {
27 if got := ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(i); got != i {
28 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Value(%d) = %d, want %d", i, got, i)
29 }
30 if got := ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(i); got != i {
31 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Index(%d) = %d, want %d", i, got, i)
32 }
33 }
34
35 // Panics
36 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(-1) }, "Value(-1) should panic")
37 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(5) }, "Value(5) should panic")
38 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(-1) }, "Index(-1) should panic")
39 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(5) }, "Index(5) should panic")
40
41 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(-1) }, "NewIndentityIndex(-1) should panic")
42 }
43
44 func TestIndex_SortedIntsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestIndex_SortedInts().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
45 vals := []int{2, 4, 6, 8, 10}
46 ix := NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().(vals))
47
48 // Verify representation
49 if ix.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index. != nil {
50 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected nil map for sorted ints")
51 }
52
53 // Valid inputs
54 for i, v := range vals {
55 if got := ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(i); got != v {
56 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Value(%d) = %d, want %d", i, got, v)
57 }
58 if got := ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(v); got != i {
59 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Index(%d) = %d, want %d", v, got, i)
60 }
61 }
62
63 // Panics
64 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(-1) }, "Value(-1) should panic")
65 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(5) }, "Value(5) should panic")
66
67 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(3) }, "Index(3) should panic")
68 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(11) }, "Index(11) should panic")
69 }
70
71 func TestIndex_Fullscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestIndex_Full().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
72 vals := []string{"C", "A", "B"}
73 ix := NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().(vals))
74
75 // Verify representation
76 if ix.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index. == nil {
77 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(i); got != v {
83 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Value(%d) = %q, want %q", i, got, v)
84 }
85 if got := ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(v); got != i {
86 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Index(%q) = %d, want %d", v, got, i)
87 }
88 }
89
90 // Panics
91 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(-1) }, "Value(-1) should panic")
92 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(3) }, "Value(3) should panic")
93
94 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().("D") }, "Index(\"D\") should panic")
95 }
96
97 func TestIndex_UnsortedIntsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestIndex_UnsortedInts().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
98 vals := []int{4, 2, 6}
99 ix := NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().(vals))
100
101 // Verify representation
102 if ix.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index. == nil {
103 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected non-nil map for unsorted ints")
104 }
105
106 for i, v := range vals {
107 if got := ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(i); got != v {
108 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Value(%d) = %d, want %d", i, got, v)
109 }
110 if got := ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(v); got != i {
111 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Index(%d) = %d, want %d", v, got, i)
112 }
113 }
114
115 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(3) }, "Index(3) should panic")
116 }
117
118 func TestIndex_Emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestIndex_Empty().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
119 ix := NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().([]string{}))
120 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(0) }, "Value(0) on empty index should panic")
121 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().("A") }, "Index on empty index should panic")
122
123 ix2 := NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(0)
124 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix2.Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(0) }, "Value(0) on empty identity index should panic")
125 assertPanicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/assertPanic().(t, func() { ix2.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(0) }, "Index(0) on empty identity index should panic")
126 }