| 1 | package graph |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestNew(t *testing.T) { |
| 9 | directedType := reflect.TypeOf(&directed[int, int]{}) |
| 10 | undirectedType := reflect.TypeOf(&undirected[int, int]{}) |
| 11 | |
| 12 | tests := map[string]struct { |
| 13 | expectedType reflect.Type |
| 14 | options []func(*Traits) |
| 15 | }{ |
| 16 | "no options": { |
| 17 | options: []func(*Traits){}, |
| 18 | expectedType: undirectedType, |
| 19 | }, |
| 20 | "directed option": { |
| 21 | options: []func(*Traits){Directed()}, |
| 22 | expectedType: directedType, |
| 23 | }, |
| 24 | } |
| 25 | |
| 26 | for name, test := range tests { |
| 27 | graph := New(IntHash, test.options...) |
| 28 | actualType := reflect.TypeOf(graph) |
| 29 | |
| 30 | if actualType != test.expectedType { |
| 31 | t.Errorf("%s: graph type expectancy doesn't match: expected %v, got %v", name, test.expectedType, actualType) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestNewLike(t *testing.T) { |
| 37 | tests := map[string]struct { |
| 38 | g Graph[int, int] |
| 39 | vertices []int |
| 40 | }{ |
| 41 | "new directed graph of integers": { |
| 42 | g: New(IntHash, Directed()), |
| 43 | vertices: []int{1, 2, 3}, |
| 44 | }, |
| 45 | "new undirected weighted graph of integers": { |
| 46 | g: New(IntHash, Weighted()), |
| 47 | vertices: []int{1, 2, 3}, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | for name, test := range tests { |
| 52 | t.Run(name, func(t *testing.T) { |
| 53 | for _, vertex := range test.vertices { |
| 54 | _ = test.g.AddVertex(vertex) |
| 55 | } |
| 56 | |
| 57 | h := NewLike(test.g) |
| 58 | |
| 59 | if len(test.vertices) > 0 { |
| 60 | if _, err := h.Vertex(test.vertices[0]); err == nil { |
| 61 | t.Errorf("expected vertex %v not to exist in h", test.vertices[0]) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if test.g.Traits().IsDirected { |
| 66 | actual, ok := h.(*directed[int, int]) |
| 67 | if !ok { |
| 68 | t.Fatalf("type assertion to *directed failed") |
| 69 | } |
| 70 | |
| 71 | expected := test.g.(*directed[int, int]) |
| 72 | |
| 73 | if actual.hash(42) != expected.hash(42) { |
| 74 | t.Errorf("expected hash %v, got %v", expected.hash, actual.hash) |
| 75 | } |
| 76 | } else { |
| 77 | actual, ok := h.(*undirected[int, int]) |
| 78 | if !ok { |
| 79 | t.Fatalf("type assertion to *directed failed") |
| 80 | } |
| 81 | |
| 82 | expected := test.g.(*undirected[int, int]) |
| 83 | |
| 84 | if actual.hash(42) != expected.hash(42) { |
| 85 | t.Errorf("expected hash %v, got %v", expected.hash, actual.hash) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if !traitsAreEqual(h.Traits(), test.g.Traits()) { |
| 90 | t.Errorf("expected traits %+v, got %+v", test.g.Traits(), h.Traits()) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestStringHash(t *testing.T) { |
| 97 | tests := map[string]struct { |
| 98 | value string |
| 99 | expectedHash string |
| 100 | }{ |
| 101 | "string value": { |
| 102 | value: "London", |
| 103 | expectedHash: "London", |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | for name, test := range tests { |
| 108 | hash := StringHash(test.value) |
| 109 | |
| 110 | if hash != test.expectedHash { |
| 111 | t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestIntHash(t *testing.T) { |
| 117 | tests := map[string]struct { |
| 118 | value int |
| 119 | expectedHash int |
| 120 | }{ |
| 121 | "int value": { |
| 122 | value: 3, |
| 123 | expectedHash: 3, |
| 124 | }, |
| 125 | } |
| 126 | |
| 127 | for name, test := range tests { |
| 128 | hash := IntHash(test.value) |
| 129 | |
| 130 | if hash != test.expectedHash { |
| 131 | t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestEdgeWeight(t *testing.T) { |
| 137 | tests := map[string]struct { |
| 138 | expected EdgeProperties |
| 139 | weight int |
| 140 | }{ |
| 141 | "weight 4": { |
| 142 | weight: 4, |
| 143 | expected: EdgeProperties{ |
| 144 | Weight: 4, |
| 145 | }, |
| 146 | }, |
| 147 | } |
| 148 | |
| 149 | for name, test := range tests { |
| 150 | properties := EdgeProperties{} |
| 151 | |
| 152 | EdgeWeight(test.weight)(&properties) |
| 153 | |
| 154 | if properties.Weight != test.expected.Weight { |
| 155 | t.Errorf("%s: weight expectation doesn't match: expected %v, got %v", name, test.expected.Weight, properties.Weight) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestEdgeAttribute(t *testing.T) { |
| 161 | tests := map[string]struct { |
| 162 | key string |
| 163 | value string |
| 164 | expected EdgeProperties |
| 165 | }{ |
| 166 | "attribute label=my-label": { |
| 167 | key: "label", |
| 168 | value: "my-label", |
| 169 | expected: EdgeProperties{ |
| 170 | Attributes: map[string]string{ |
| 171 | "label": "my-label", |
| 172 | }, |
| 173 | }, |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | for name, test := range tests { |
| 178 | t.Run(name, func(t *testing.T) { |
| 179 | properties := EdgeProperties{ |
| 180 | Attributes: make(map[string]string), |
| 181 | } |
| 182 | |
| 183 | EdgeAttribute(test.key, test.value)(&properties) |
| 184 | |
| 185 | value, ok := properties.Attributes[test.key] |
| 186 | if !ok { |
| 187 | t.Errorf("attribute expectaton doesn't match: key %v doesn't exist", test.key) |
| 188 | } |
| 189 | |
| 190 | expectedValue := test.expected.Attributes[test.key] |
| 191 | |
| 192 | if value != expectedValue { |
| 193 | t.Errorf("value expectation doesn't match: expected %v, got %v", expectedValue, value) |
| 194 | } |
| 195 | }) |
| 196 | |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestEdgeAttributes(t *testing.T) { |
| 201 | tests := map[string]struct { |
| 202 | attributes map[string]string |
| 203 | expected map[string]string |
| 204 | }{ |
| 205 | "attribute label=my-label": { |
| 206 | attributes: map[string]string{ |
| 207 | "label": "my-label", |
| 208 | }, |
| 209 | expected: map[string]string{ |
| 210 | "label": "my-label", |
| 211 | }, |
| 212 | }, |
| 213 | } |
| 214 | |
| 215 | for name, test := range tests { |
| 216 | t.Run(name, func(t *testing.T) { |
| 217 | properties := EdgeProperties{ |
| 218 | Attributes: make(map[string]string), |
| 219 | } |
| 220 | |
| 221 | EdgeAttributes(test.attributes)(&properties) |
| 222 | |
| 223 | if !mapsAreEqual(test.expected, properties.Attributes) { |
| 224 | t.Errorf("expected %v, got %v", test.expected, properties.Attributes) |
| 225 | } |
| 226 | }) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func TestVertexAttribute(t *testing.T) { |
| 231 | tests := map[string]struct { |
| 232 | key string |
| 233 | value string |
| 234 | expected VertexProperties |
| 235 | }{ |
| 236 | "attribute label=my-label": { |
| 237 | key: "label", |
| 238 | value: "my-label", |
| 239 | expected: VertexProperties{ |
| 240 | Attributes: map[string]string{ |
| 241 | "label": "my-label", |
| 242 | }, |
| 243 | }, |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | for name, test := range tests { |
| 248 | t.Run(name, func(t *testing.T) { |
| 249 | properties := VertexProperties{ |
| 250 | Attributes: make(map[string]string), |
| 251 | } |
| 252 | |
| 253 | VertexAttribute(test.key, test.value)(&properties) |
| 254 | |
| 255 | value, ok := properties.Attributes[test.key] |
| 256 | if !ok { |
| 257 | t.Errorf("attribute expectaton doesn't match: key %v doesn't exist", test.key) |
| 258 | } |
| 259 | |
| 260 | expectedValue := test.expected.Attributes[test.key] |
| 261 | |
| 262 | if value != expectedValue { |
| 263 | t.Errorf("value expectation doesn't match: expected %v, got %v", expectedValue, value) |
| 264 | } |
| 265 | }) |
| 266 | |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func TestVertexAttributes(t *testing.T) { |
| 271 | tests := map[string]struct { |
| 272 | attributes map[string]string |
| 273 | expected map[string]string |
| 274 | }{ |
| 275 | "attribute label=my-label": { |
| 276 | attributes: map[string]string{ |
| 277 | "label": "my-label", |
| 278 | }, |
| 279 | expected: map[string]string{ |
| 280 | "label": "my-label", |
| 281 | }, |
| 282 | }, |
| 283 | } |
| 284 | |
| 285 | for name, test := range tests { |
| 286 | t.Run(name, func(t *testing.T) { |
| 287 | properties := VertexProperties{ |
| 288 | Attributes: make(map[string]string), |
| 289 | } |
| 290 | |
| 291 | VertexAttributes(test.attributes)(&properties) |
| 292 | |
| 293 | if !mapsAreEqual(test.expected, properties.Attributes) { |
| 294 | t.Errorf("expected %v, got %v", test.expected, properties.Attributes) |
| 295 | } |
| 296 | }) |
| 297 | } |
| 298 | } |