github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/encoding/digraph6/digraph6_test.go (about)

     1  // Copyright ©2018 The Gonum 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 digraph6
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/jingcheng-WU/gonum/graph"
    12  	"github.com/jingcheng-WU/gonum/graph/simple"
    13  )
    14  
    15  var testGraphs = []struct {
    16  	g    string
    17  	bin  string
    18  	want []set
    19  }{
    20  	// Wanted graphs were obtained from showg using the input graph string.
    21  	// The showg output is included for comparison.
    22  	//
    23  	// showg with dgraph6 support, in nauty v2.7, is available here: http://pallini.di.uniroma1.it/
    24  	{
    25  		// Graph 1, order 0.
    26  		g:    "&?",
    27  		bin:  "0:0",
    28  		want: []set{},
    29  	},
    30  	{
    31  		// Graph 1, order 5.
    32  		//   0 : 2 4;
    33  		//   1 : ;
    34  		//   2 : ;
    35  		//   3 : 1 4;
    36  		//   4 : ;
    37  		g:   "&DI?AO?",
    38  		bin: "5:0010100000000000100100000",
    39  		want: []set{
    40  			0: linksToInt(2, 4),
    41  			1: linksToInt(),
    42  			2: linksToInt(),
    43  			3: linksToInt(1, 4),
    44  			4: linksToInt(),
    45  		},
    46  	},
    47  	{
    48  		// Graph 1, order 5.
    49  		//   0 : 1 3;
    50  		//   1 : 0 2 3 4;
    51  		//   2 : 0 1 3 4;
    52  		//   3 : 0 2;
    53  		//   4 : 0 1 2 3;
    54  		g:   "&DT^\\N?",
    55  		bin: "5:0101010111110111010011110",
    56  		want: []set{
    57  			0: linksToInt(1, 3),
    58  			1: linksToInt(0, 2, 3, 4),
    59  			2: linksToInt(0, 1, 3, 4),
    60  			3: linksToInt(0, 2),
    61  			4: linksToInt(0, 1, 2, 3),
    62  		},
    63  	},
    64  	{
    65  		// Graph 1, order 5.
    66  		//   0 : ;
    67  		//   1 : 3;
    68  		//   2 : 0;
    69  		//   3 : ;
    70  		//   4 : 0 3;
    71  		g:   "&D?I?H?",
    72  		bin: "5:0000000010100000000010010",
    73  		want: []set{
    74  			0: linksToInt(),
    75  			1: linksToInt(3),
    76  			2: linksToInt(0),
    77  			3: linksToInt(),
    78  			4: linksToInt(0, 3),
    79  		},
    80  	},
    81  	{
    82  		// Graph 1, order 6.
    83  		//   0 : 1 2 5;
    84  		//   1 : 2 3 4 5;
    85  		//   2 : 3 4;
    86  		//   3 : 0 4 5;
    87  		//   4 : 0 5;
    88  		//   5 : 2;
    89  		g:   "&EXNEb`G",
    90  		bin: "6:011001001111000110100011100001001000",
    91  		want: []set{
    92  			0: linksToInt(1, 2, 5),
    93  			1: linksToInt(2, 3, 4, 5),
    94  			2: linksToInt(3, 4),
    95  			3: linksToInt(0, 4, 5),
    96  			4: linksToInt(0, 5),
    97  			5: linksToInt(2),
    98  		},
    99  	},
   100  	{
   101  		// Graph 1, order 9.
   102  		//   0 : 1 3 5 7 8;
   103  		//   1 : 2 3 4 7 8;
   104  		//   2 : 0 3 4;
   105  		//   3 : 4 5 6 7 8;
   106  		//   4 : 0 5 6 8;
   107  		//   5 : 1 2 6 7 8;
   108  		//   6 : 0 1 2 7 8;
   109  		//   7 : 2 4 8;
   110  		//   8 : 2;
   111  		g:   "&HTXre?^`jFwXPG?",
   112  		bin: "9:010101011001110011100110000000011111100001101011000111111000011001010001001000000",
   113  		want: []set{
   114  			0: linksToInt(1, 3, 5, 7, 8),
   115  			1: linksToInt(2, 3, 4, 7, 8),
   116  			2: linksToInt(0, 3, 4),
   117  			3: linksToInt(4, 5, 6, 7, 8),
   118  			4: linksToInt(0, 5, 6, 8),
   119  			5: linksToInt(1, 2, 6, 7, 8),
   120  			6: linksToInt(0, 1, 2, 7, 8),
   121  			7: linksToInt(2, 4, 8),
   122  			8: linksToInt(2),
   123  		},
   124  	},
   125  	{
   126  		// Graph 1, order 12.
   127  		//   0 : 1 2 3 4 5 6 7 8 9 10 11;
   128  		//   1 : 2 3 4 5 6 7 8 11;
   129  		//   2 : 3 4 5 6 7 8 9 11;
   130  		//   3 : 4 5 6 7 8 10 11;
   131  		//   4 : 5 6 7 8 9 11;
   132  		//   5 : 6 7 9 10;
   133  		//   6 : 7 8 9 10 11;
   134  		//   7 : 8 9 10 11;
   135  		//   8 : 5 9 10;
   136  		//   9 : 1 3 10;
   137  		//  10 : 1 2 4 11;
   138  		//  11 : 5 8 9;
   139  		g:   "&K^~NxF|Bz@|?u?^?N@ESAY@@K",
   140  		bin: "12:011111111111001111111001000111111101000011111011000001111101000000110110000000011111000000001111000001000110010100000010011010000001000001001100",
   141  		want: []set{
   142  			0:  linksToInt(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
   143  			1:  linksToInt(2, 3, 4, 5, 6, 7, 8, 11),
   144  			2:  linksToInt(3, 4, 5, 6, 7, 8, 9, 11),
   145  			3:  linksToInt(4, 5, 6, 7, 8, 10, 11),
   146  			4:  linksToInt(5, 6, 7, 8, 9, 11),
   147  			5:  linksToInt(6, 7, 9, 10),
   148  			6:  linksToInt(7, 8, 9, 10, 11),
   149  			7:  linksToInt(8, 9, 10, 11),
   150  			8:  linksToInt(5, 9, 10),
   151  			9:  linksToInt(1, 3, 10),
   152  			10: linksToInt(1, 2, 4, 11),
   153  			11: linksToInt(5, 8, 9),
   154  		},
   155  	},
   156  	{
   157  		// Graph 1, order 17.
   158  		//   0 : 1 2 3 4 5 6 7 8 9 10 11 12 14 15 16;
   159  		//   1 : 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;
   160  		//   2 : 3 4 5 6 7 8 9 10 11 12 13 14 16;
   161  		//   3 : 4 5 6 7 8 9 10 11 12 13 14 15 16;
   162  		//   4 : 5 6 7 8 9 10 11 12 13 14 15 16;
   163  		//   5 : 6 7 8 9 10 11 12 13 14 15 16;
   164  		//   6 : 7 8 9 10 11 12 13 14 15;
   165  		//   7 : 8 9 10 11 12 13 14 15 16;
   166  		//   8 : 9 10 11 12 13 15 16;
   167  		//   9 : 10 11 12 13 14 15 16;
   168  		//  10 : 11 12 13 14 16;
   169  		//  11 : 12 13 14 15 16;
   170  		//  12 : 13 14 15 16;
   171  		//  13 : 0 14 15 16;
   172  		//  14 : 8 15;
   173  		//  15 : 2 10 16;
   174  		//  16 : 6 14;
   175  		g:   "&P^~m^~{^~g^~o^~_^~?^{?^{?^W?^o?]_?^??^??[?_P?OOGA?",
   176  		bin: "17:0111111111111011100111111111111111000111111111111010000111111111111100000111111111111000000111111111110000000111111111000000000111111111000000000111110110000000000111111100000000000111101000000000000111110000000000000111110000000000000111000000001000000100010000000100000100000010000000100",
   177  		want: []set{
   178  			0:  linksToInt(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16),
   179  			1:  linksToInt(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
   180  			2:  linksToInt(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16),
   181  			3:  linksToInt(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
   182  			4:  linksToInt(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
   183  			5:  linksToInt(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
   184  			6:  linksToInt(7, 8, 9, 10, 11, 12, 13, 14, 15),
   185  			7:  linksToInt(8, 9, 10, 11, 12, 13, 14, 15, 16),
   186  			8:  linksToInt(9, 10, 11, 12, 13, 15, 16),
   187  			9:  linksToInt(10, 11, 12, 13, 14, 15, 16),
   188  			10: linksToInt(11, 12, 13, 14, 16),
   189  			11: linksToInt(12, 13, 14, 15, 16),
   190  			12: linksToInt(13, 14, 15, 16),
   191  			13: linksToInt(0, 14, 15, 16),
   192  			14: linksToInt(8, 15),
   193  			15: linksToInt(2, 10, 16),
   194  			16: linksToInt(6, 14),
   195  		},
   196  	},
   197  }
   198  
   199  func TestNumberOf(t *testing.T) {
   200  	for _, test := range testGraphs {
   201  		n := numberOf(Graph(test.g))
   202  		if n != int64(len(test.want)) {
   203  			t.Errorf("unexpected graph n: got:%d want:%d", n, len(test.want))
   204  		}
   205  	}
   206  }
   207  
   208  func TestGoString(t *testing.T) {
   209  	for _, test := range testGraphs {
   210  		gosyntax := Graph(test.g).GoString()
   211  		if gosyntax != test.bin {
   212  			t.Errorf("unexpected graph string: got:%s want:%s", gosyntax, test.bin)
   213  		}
   214  	}
   215  }
   216  
   217  func TestGraph(t *testing.T) {
   218  	for _, test := range testGraphs {
   219  		g := Graph(test.g)
   220  		if !IsValid(g) {
   221  			t.Errorf("unexpected invalid graph %q", g)
   222  		}
   223  		nodes := g.Nodes()
   224  		if nodes.Len() != len(test.want) {
   225  			t.Errorf("unexpected graph n: got:%d want:%d", nodes.Len(), len(test.want))
   226  		}
   227  		got := make([]set, nodes.Len())
   228  		for nodes.Next() {
   229  			n := nodes.Node()
   230  			got[n.ID()] = linksTo(graph.NodesOf(g.From(n.ID()))...)
   231  		}
   232  		if !reflect.DeepEqual(got, test.want) {
   233  			t.Errorf("unexpected graph:\ngot: %v\nwant:%v", got, test.want)
   234  		}
   235  		reverse := make([]set, len(got))
   236  		for i := range reverse {
   237  			reverse[i] = make(set)
   238  		}
   239  		for i, s := range got {
   240  			for j := range s {
   241  				reverse[j][i] = struct{}{}
   242  			}
   243  		}
   244  		for i, s := range got {
   245  			from := g.From(int64(i)).Len()
   246  			if from != len(s) {
   247  				t.Errorf("unexpected number of nodes from %d: got:%d want:%d", i, from, len(s))
   248  			}
   249  			to := g.To(int64(i)).Len()
   250  			if to != len(reverse[i]) {
   251  				t.Errorf("unexpected number of nodes to %d: got:%d want:%d", i, to, len(reverse))
   252  			}
   253  		}
   254  
   255  		dst := simple.NewDirectedGraph()
   256  		graph.Copy(dst, g)
   257  		enc := Encode(dst)
   258  		if enc != g {
   259  			t.Errorf("unexpected round trip: got:%q want:%q", enc, g)
   260  		}
   261  	}
   262  }
   263  
   264  type set map[int]struct{}
   265  
   266  func linksToInt(nodes ...int) map[int]struct{} {
   267  	s := make(map[int]struct{})
   268  	for _, n := range nodes {
   269  		s[n] = struct{}{}
   270  	}
   271  	return s
   272  }
   273  
   274  func linksTo(nodes ...graph.Node) map[int]struct{} {
   275  	s := make(map[int]struct{})
   276  	for _, n := range nodes {
   277  		s[int(n.ID())] = struct{}{}
   278  	}
   279  	return s
   280  }