github.com/LdDl/ch@v1.7.8/vanilla_dijkstra_turn_restricted_test.go (about)

     1  package ch
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type V struct {
     8  	from   int64
     9  	to     int64
    10  	weight float64
    11  }
    12  
    13  func TestVanillaTurnRestrictedShortestPath(t *testing.T) {
    14  
    15  	vertices := []V{
    16  		{from: 1, to: 2, weight: 1.0},
    17  		{from: 2, to: 3, weight: 3.0},
    18  		{from: 3, to: 4, weight: 1.0},
    19  		{from: 4, to: 5, weight: 1.0},
    20  		{from: 5, to: 6, weight: 1.0},
    21  		{from: 5, to: 7, weight: 1.0},
    22  		{from: 2, to: 5, weight: 1.0},
    23  		{from: 8, to: 2, weight: 1.0},
    24  	}
    25  
    26  	graph := Graph{}
    27  	for i := range vertices {
    28  		err := graph.CreateVertex(vertices[i].from)
    29  		if err != nil {
    30  			t.Error(err)
    31  			return
    32  		}
    33  		err = graph.CreateVertex(vertices[i].to)
    34  		if err != nil {
    35  			t.Error(err)
    36  			return
    37  		}
    38  		err = graph.AddEdge(vertices[i].from, vertices[i].to, vertices[i].weight)
    39  		if err != nil {
    40  			t.Error(err)
    41  			return
    42  		}
    43  	}
    44  
    45  	restrictions := make(map[int64]map[int64]int64)
    46  	restrictions[1] = make(map[int64]int64)
    47  	restrictions[1][2] = 5
    48  	restrictions[2] = make(map[int64]int64)
    49  	restrictions[2][5] = 7
    50  
    51  	for source, turn := range restrictions {
    52  		for via, target := range turn {
    53  			err := graph.AddTurnRestriction(source, via, target)
    54  			if err != nil {
    55  				t.Error(err)
    56  				return
    57  			}
    58  		}
    59  	}
    60  
    61  	ans, path := graph.VanillaTurnRestrictedShortestPath(1, 5)
    62  	rightPath := []int64{1, 2, 3, 4, 5}
    63  	if len(path) != 5 {
    64  		t.Errorf("Run 1: num of vertices in path should be 5, but got %d", len(path))
    65  		return
    66  	}
    67  	for i := range path {
    68  		if path[i] != rightPath[i] {
    69  			t.Errorf("Run 1: vertex in path should be %d, but got %d", path[i], rightPath[i])
    70  			return
    71  		}
    72  	}
    73  	if ans != 6 {
    74  		t.Errorf("Run 1: length of path should be 6, but got %f", ans)
    75  		return
    76  	}
    77  
    78  	ans, path = graph.VanillaTurnRestrictedShortestPath(2, 7)
    79  	rightPath = []int64{2, 3, 4, 5, 7}
    80  	if len(path) != 5 {
    81  		t.Errorf("Run 2: num of vertices in path should be 5, but got %d", len(path))
    82  		return
    83  	}
    84  	for i := range path {
    85  		if path[i] != rightPath[i] {
    86  			t.Errorf("Run 2: vertex in path should be %d, but got %d", path[i], rightPath[i])
    87  			return
    88  		}
    89  	}
    90  	if ans != 6 {
    91  		t.Errorf("Run 2: length of path should be 6, but got %f", ans)
    92  		return
    93  	}
    94  
    95  	ans, path = graph.VanillaTurnRestrictedShortestPath(1, 7)
    96  	rightPath = []int64{1, 2, 3, 4, 5, 7}
    97  	if len(path) != 6 {
    98  		t.Errorf("Run 3: num of vertices in path should be 6, but got %d", len(path))
    99  		return
   100  	}
   101  	for i := range path {
   102  		if path[i] != rightPath[i] {
   103  			t.Errorf("Run 3: vertex in path should be %d, but got %d", path[i], rightPath[i])
   104  			return
   105  		}
   106  	}
   107  	if ans != 7 {
   108  		t.Errorf("Run 3: length of path should be 7, but got %f", ans)
   109  		return
   110  	}
   111  
   112  	t.Log("TestVanillaTurnRestrictedShortestPath is Ok!")
   113  }