1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// Copyright ©2014 The Gonum 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 testgraphs
import (
"bytes"
"errors"
"reflect"
"strings"
"testing"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
)
var _ graph.Graph = (*Grid)(nil)
func join(g ...string) string { return strings.Join(g, "\n") }
type node int64
func (n node) ID() int64 { return int64(n) }
func TestGrid(t *testing.T) {
t.Parallel()
g := NewGrid(4, 4, false)
got := g.String()
want := join(
"****",
"****",
"****",
"****",
)
if got != want {
t.Fatalf("unexpected grid rendering:\ngot: %q\nwant:%q", got, want)
}
var ops = []struct {
r, c int
state bool
want string
}{
{
r: 0, c: 1,
state: true,
want: join(
"*.**",
"****",
"****",
"****",
),
},
{
r: 0, c: 1,
state: false,
want: join(
"****",
"****",
"****",
"****",
),
},
{
r: 0, c: 1,
state: true,
want: join(
"*.**",
"****",
"****",
"****",
),
},
{
r: 0, c: 2,
state: true,
want: join(
"*..*",
"****",
"****",
"****",
),
},
{
r: 1, c: 2,
state: true,
want: join(
"*..*",
"**.*",
"****",
"****",
),
},
{
r: 2, c: 2,
state: true,
want: join(
"*..*",
"**.*",
"**.*",
"****",
),
},
{
r: 3, c: 2,
state: true,
want: join(
"*..*",
"**.*",
"**.*",
"**.*",
),
},
}
for _, test := range ops {
g.Set(test.r, test.c, test.state)
got := g.String()
if got != test.want {
t.Fatalf("unexpected grid rendering after set (%d, %d) open state to %t:\ngot: %q\nwant:%q",
test.r, test.c, test.state, got, test.want)
}
}
// Match the last state from the loop against the
// explicit description of the grid.
got = NewGridFrom(
"*..*",
"**.*",
"**.*",
"**.*",
).String()
want = g.String()
if got != want {
t.Fatalf("unexpected grid rendering from NewGridFrom:\ngot: %q\nwant:%q", got, want)
}
var paths = []struct {
path []graph.Node
diagonal bool
want string
}{
{
path: nil,
diagonal: false,
want: join(
"*..*",
"**.*",
"**.*",
"**.*",
),
},
{
path: []graph.Node{node(1), node(2), node(6), node(10), node(14)},
diagonal: false,
want: join(
"*So*",
"**o*",
"**o*",
"**G*",
),
},
{
path: []graph.Node{node(1), node(6), node(10), node(14)},
diagonal: false,
want: join(
"*S.*",
"**!*",
"**.*",
"**.*",
),
},
{
path: []graph.Node{node(1), node(6), node(10), node(14)},
diagonal: true,
want: join(
"*S.*",
"**o*",
"**o*",
"**G*",
),
},
{
path: []graph.Node{node(1), node(5), node(9)},
diagonal: false,
want: join(
"*S.*",
"*!.*",
"**.*",
"**.*",
),
},
}
for _, test := range paths {
g.AllowDiagonal = test.diagonal
got, err := g.Render(test.path)
errored := err != nil
if bytes.Contains(got, []byte{'!'}) != errored {
t.Fatalf("unexpected error return: got:%v want:%v", err, errors.New("grid: not a path in graph"))
}
if string(got) != test.want {
t.Fatalf("unexpected grid path rendering for %v:\ngot: %q\nwant:%q", test.path, got, want)
}
}
var coords = []struct {
r, c int
id int64
}{
{r: 0, c: 0, id: 0},
{r: 0, c: 3, id: 3},
{r: 3, c: 0, id: 12},
{r: 3, c: 3, id: 15},
}
for _, test := range coords {
if id := g.NodeAt(test.r, test.c).ID(); id != test.id {
t.Fatalf("unexpected ID for node at (%d, %d):\ngot: %d\nwant:%d", test.r, test.c, id, test.id)
}
if r, c := g.RowCol(test.id); r != test.r || c != test.c {
t.Fatalf("unexpected row/col for node %d:\ngot: (%d, %d)\nwant:(%d, %d)", test.id, r, c, test.r, test.c)
}
}
var reach = []struct {
from graph.Node
diagonal bool
to []graph.Node
}{
{
from: node(0),
diagonal: false,
to: nil,
},
{
from: node(2),
diagonal: false,
to: []graph.Node{simple.Node(1), simple.Node(6)},
},
{
from: node(1),
diagonal: false,
to: []graph.Node{simple.Node(2)},
},
{
from: node(1),
diagonal: true,
to: []graph.Node{simple.Node(2), simple.Node(6)},
},
}
for _, test := range reach {
g.AllowDiagonal = test.diagonal
got := graph.NodesOf(g.From(test.from.ID()))
if !reflect.DeepEqual(got, test.to) {
t.Fatalf("unexpected nodes from %d with allow diagonal=%t:\ngot: %v\nwant:%v",
test.from, test.diagonal, got, test.to)
}
}
}
|