github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/encoding/graph6/graph6_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 graph6
     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 is available here: https://hog.grinvin.org/data/generators/decoders/showg
    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 : 3;
    34  		//   2 : 0;
    35  		//   3 : 1 4;
    36  		//   4 : 0 3;
    37  		g:   "DQc",
    38  		bin: "5:0100101001",
    39  		want: []set{
    40  			0: linksToInt(2, 4),
    41  			1: linksToInt(3),
    42  			2: linksToInt(0),
    43  			3: linksToInt(1, 4),
    44  			4: linksToInt(0, 3),
    45  		},
    46  	},
    47  	{
    48  		// Graph 1, order 4.
    49  		//   0 : 1 2 3;
    50  		//   1 : 0 2 3;
    51  		//   2 : 0 1 3;
    52  		//   3 : 0 1 2;
    53  		g:   "C~",
    54  		bin: "4:111111",
    55  		want: []set{
    56  			0: linksToInt(1, 2, 3),
    57  			1: linksToInt(0, 2, 3),
    58  			2: linksToInt(0, 1, 3),
    59  			3: linksToInt(0, 1, 2),
    60  		},
    61  	},
    62  	{
    63  		// Graph 1, order 6.
    64  		//   0 : 1 3 4;
    65  		//   1 : 0 2 5;
    66  		//   2 : 1 3 4;
    67  		//   3 : 0 2 5;
    68  		//   4 : 0 2 5;
    69  		//   5 : 1 3 4;
    70  		g:   "ElhW",
    71  		bin: "6:101101101001011",
    72  		want: []set{
    73  			0: linksToInt(1, 3, 4),
    74  			1: linksToInt(0, 2, 5),
    75  			2: linksToInt(1, 3, 4),
    76  			3: linksToInt(0, 2, 5),
    77  			4: linksToInt(0, 2, 5),
    78  			5: linksToInt(1, 3, 4),
    79  		},
    80  	},
    81  	{
    82  		// Graph 1, order 10.
    83  		//   0 : 1 2 3;
    84  		//   1 : 0 4 5;
    85  		//   2 : 0 6 7;
    86  		//   3 : 0 8 9;
    87  		//   4 : 1 6 8;
    88  		//   5 : 1 7 9;
    89  		//   6 : 2 4 9;
    90  		//   7 : 2 5 8;
    91  		//   8 : 3 4 7;
    92  		//   9 : 3 5 6;
    93  		g:   "IsP@PGXD_",
    94  		bin: "10:110100010001000001010001001000011001000101100",
    95  		want: []set{
    96  			0: linksToInt(1, 2, 3),
    97  			1: linksToInt(0, 4, 5),
    98  			2: linksToInt(0, 6, 7),
    99  			3: linksToInt(0, 8, 9),
   100  			4: linksToInt(1, 6, 8),
   101  			5: linksToInt(1, 7, 9),
   102  			6: linksToInt(2, 4, 9),
   103  			7: linksToInt(2, 5, 8),
   104  			8: linksToInt(3, 4, 7),
   105  			9: linksToInt(3, 5, 6),
   106  		},
   107  	},
   108  	{
   109  		// Graph 1, order 17.
   110  		//   0 : 1 15 16;
   111  		//   1 : 0 2 5;
   112  		//   2 : 1 3 14;
   113  		//   3 : 2 4 16;
   114  		//   4 : 3 5 15;
   115  		//   5 : 1 4 6;
   116  		//   6 : 5 7 16;
   117  		//   7 : 6 8 11;
   118  		//   8 : 7 9 13;
   119  		//   9 : 8 10 16;
   120  		//  10 : 9 11 14;
   121  		//  11 : 7 10 12;
   122  		//  12 : 11 13 16;
   123  		//  13 : 8 12 14;
   124  		//  14 : 2 10 13 15;
   125  		//  15 : 0 4 14;
   126  		//  16 : 0 3 6 9 12;
   127  		g:   "PhDGGC@?G?_H?@?Gc@KO@cc_",
   128  		bin: "17:1010010001010010000010000001000000010000000010000000001000000010010000000000010000000010001001000000010011000100000000011001001001001000",
   129  		want: []set{
   130  			0:  linksToInt(1, 15, 16),
   131  			1:  linksToInt(0, 2, 5),
   132  			2:  linksToInt(1, 3, 14),
   133  			3:  linksToInt(2, 4, 16),
   134  			4:  linksToInt(3, 5, 15),
   135  			5:  linksToInt(1, 4, 6),
   136  			6:  linksToInt(5, 7, 16),
   137  			7:  linksToInt(6, 8, 11),
   138  			8:  linksToInt(7, 9, 13),
   139  			9:  linksToInt(8, 10, 16),
   140  			10: linksToInt(9, 11, 14),
   141  			11: linksToInt(7, 10, 12),
   142  			12: linksToInt(11, 13, 16),
   143  			13: linksToInt(8, 12, 14),
   144  			14: linksToInt(2, 10, 13, 15),
   145  			15: linksToInt(0, 4, 14),
   146  			16: linksToInt(0, 3, 6, 9, 12),
   147  		},
   148  	},
   149  }
   150  
   151  func TestNumberOf(t *testing.T) {
   152  	for _, test := range testGraphs {
   153  		n := numberOf(Graph(test.g))
   154  		if n != int64(len(test.want)) {
   155  			t.Errorf("unexpected graph n: got:%d want:%d", n, len(test.want))
   156  		}
   157  	}
   158  }
   159  
   160  func TestGoString(t *testing.T) {
   161  	for _, test := range testGraphs {
   162  		gosyntax := Graph(test.g).GoString()
   163  		if gosyntax != test.bin {
   164  			t.Errorf("unexpected graph string: got:%s want:%s", gosyntax, test.bin)
   165  		}
   166  	}
   167  }
   168  
   169  func TestGraph(t *testing.T) {
   170  	for _, test := range testGraphs {
   171  		g := Graph(test.g)
   172  		if !IsValid(g) {
   173  			t.Errorf("unexpected invalid graph %q", g)
   174  		}
   175  		nodes := g.Nodes()
   176  		if nodes.Len() != len(test.want) {
   177  			t.Errorf("unexpected graph n: got:%d want:%d", nodes.Len(), len(test.want))
   178  		}
   179  		got := make([]set, nodes.Len())
   180  		for nodes.Next() {
   181  			n := nodes.Node()
   182  			got[n.ID()] = linksTo(graph.NodesOf(g.From(n.ID()))...)
   183  		}
   184  		if !reflect.DeepEqual(got, test.want) {
   185  			t.Errorf("unexpected graph: got:%v want:%v", got, test.want)
   186  		}
   187  		for i, s := range got {
   188  			f := g.From(int64(i)).Len()
   189  			if f != len(s) {
   190  				t.Errorf("unexpected number of nodes from %d: got:%d want:%d", i, f, len(s))
   191  			}
   192  		}
   193  
   194  		dst := simple.NewUndirectedGraph()
   195  		graph.Copy(dst, g)
   196  		enc := Encode(dst)
   197  		if enc != g {
   198  			t.Errorf("unexpected round trip: got:%q want:%q", enc, g)
   199  		}
   200  	}
   201  }
   202  
   203  type set map[int]struct{}
   204  
   205  func linksToInt(nodes ...int) map[int]struct{} {
   206  	s := make(map[int]struct{})
   207  	for _, n := range nodes {
   208  		s[n] = struct{}{}
   209  	}
   210  	return s
   211  }
   212  
   213  func linksTo(nodes ...graph.Node) map[int]struct{} {
   214  	s := make(map[int]struct{})
   215  	for _, n := range nodes {
   216  		s[int(n.ID())] = struct{}{}
   217  	}
   218  	return s
   219  }