go-hep.org/x/hep@v0.38.1/fwk/utils/tarjan/tarjan_test.go (about)

     1  // Copyright (c) 2013 - Max Persson <max@looplab.se>
     2  // Copyright (c) 2010-2013 - Gustavo Niemeyer <gustavo@niemeyer.net>
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package tarjan
    17  
    18  import (
    19  	"fmt"
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  )
    24  
    25  func TestTypeString(t *testing.T) {
    26  	graph := make(map[any][]any)
    27  	graph["1"] = []any{"2"}
    28  	graph["2"] = []any{"3"}
    29  	graph["3"] = []any{"1"}
    30  	graph["4"] = []any{"2", "3", "5"}
    31  	graph["5"] = []any{"4", "6"}
    32  	graph["6"] = []any{"3", "7"}
    33  	graph["7"] = []any{"6"}
    34  	graph["8"] = []any{"5", "7", "8"}
    35  
    36  	o := Connections(graph)
    37  	output := sortConnections(o)
    38  	exp := [][]string{
    39  		{"1", "2", "3"},
    40  		{"6", "7"},
    41  		{"4", "5"},
    42  		{"8"},
    43  	}
    44  	if !reflect.DeepEqual(output, exp) {
    45  		t.Fatalf("FAIL.\nexp=%v\ngot=%v\n", exp, output)
    46  	}
    47  }
    48  
    49  func TestTypeInt(t *testing.T) {
    50  	graph := make(map[any][]any)
    51  	graph[1] = []any{2}
    52  	graph[2] = []any{3}
    53  	graph[3] = []any{1}
    54  	graph[4] = []any{2, 3, 5}
    55  	graph[5] = []any{4, 6}
    56  	graph[6] = []any{3, 7}
    57  	graph[7] = []any{6}
    58  	graph[8] = []any{5, 7, 8}
    59  
    60  	o := Connections(graph)
    61  	output := sortConnections(o)
    62  	exp := [][]string{
    63  		{"1", "2", "3"},
    64  		{"6", "7"},
    65  		{"4", "5"},
    66  		{"8"},
    67  	}
    68  	if !reflect.DeepEqual(output, exp) {
    69  		t.Fatalf("FAIL.\nexp=%v\ngot=%v\n", exp, output)
    70  	}
    71  }
    72  
    73  func TestTypeMixed(t *testing.T) {
    74  	graph := make(map[any][]any)
    75  	graph[1] = []any{2}
    76  	graph[2] = []any{"3"}
    77  	graph["3"] = []any{1}
    78  	graph[4] = []any{2, "3", 5}
    79  	graph[5] = []any{4, "6"}
    80  	graph["6"] = []any{"3", 7}
    81  	graph[7] = []any{"6"}
    82  	graph[8] = []any{5, 7, 8}
    83  
    84  	o := Connections(graph)
    85  	output := sortConnections(o)
    86  	exp := [][]string{
    87  		{"1", "2", "3"},
    88  		{"6", "7"},
    89  		{"4", "5"},
    90  		{"8"},
    91  	}
    92  	if !reflect.DeepEqual(output, exp) {
    93  		t.Fatalf("FAIL.\nexp=%v\ngot=%v\n", exp, output)
    94  	}
    95  }
    96  
    97  func TestEmptyGraph(t *testing.T) {
    98  	graph := make(map[any][]any)
    99  
   100  	output := Connections(graph)
   101  	if len(output) != 0 {
   102  		t.FailNow()
   103  	}
   104  }
   105  
   106  func TestSingleVertex(t *testing.T) {
   107  	graph := make(map[any][]any)
   108  	graph["1"] = []any{}
   109  
   110  	o := Connections(graph)
   111  	output := sortConnections(o)
   112  	if output[0][0] != "1" {
   113  		t.FailNow()
   114  	}
   115  }
   116  
   117  func TestSingleVertexLoop(t *testing.T) {
   118  	graph := make(map[any][]any)
   119  	graph["1"] = []any{"1"}
   120  
   121  	o := Connections(graph)
   122  	output := sortConnections(o)
   123  	if output[0][0] != "1" {
   124  		t.FailNow()
   125  	}
   126  }
   127  
   128  func TestMultipleVertexLoop(t *testing.T) {
   129  	graph := make(map[any][]any)
   130  	graph["1"] = []any{"2"}
   131  	graph["2"] = []any{"3"}
   132  	graph["3"] = []any{"1"}
   133  
   134  	o := Connections(graph)
   135  	output := sortConnections(o)
   136  	if output[0][0] != "1" {
   137  		t.FailNow()
   138  	}
   139  	if output[0][1] != "2" {
   140  		t.FailNow()
   141  	}
   142  	if output[0][2] != "3" {
   143  		t.FailNow()
   144  	}
   145  }
   146  
   147  func ExampleConnections() {
   148  	graph := make(map[any][]any)
   149  	graph["1"] = []any{"2"}
   150  	graph["2"] = []any{"3"}
   151  	graph["3"] = []any{"1"}
   152  	graph["4"] = []any{"2", "3", "5"}
   153  	graph["5"] = []any{"4", "6"}
   154  	graph["6"] = []any{"3", "7"}
   155  	graph["7"] = []any{"6"}
   156  	graph["8"] = []any{"5", "7", "8"}
   157  
   158  	o := Connections(graph)
   159  	output := sortConnections(o)
   160  	fmt.Println(output)
   161  
   162  	// Output:
   163  	// [[1 2 3] [6 7] [4 5] [8]]
   164  }
   165  
   166  func sortConnections(connections [][]any) [][]string {
   167  	out := make([][]string, 0, len(connections))
   168  	for _, cons := range connections {
   169  		slice := make([]string, 0, len(cons))
   170  		for _, v := range cons {
   171  			switch v := v.(type) {
   172  			case string:
   173  				slice = append(slice, v)
   174  			case int:
   175  				slice = append(slice, fmt.Sprintf("%d", v))
   176  			default:
   177  				panic(fmt.Errorf("invalid type [%T]", v))
   178  			}
   179  		}
   180  		sort.Strings(slice)
   181  		out = append(out, slice)
   182  	}
   183  	return out
   184  }