@@ -6,11 +6,14 @@ import (
66 "sync"
77 "testing"
88
9+ "github.com/elecbug/netkit/v2/graph"
910 "github.com/elecbug/netkit/v2/graph/standard"
1011)
1112
1213func TestStandardGraph (t * testing.T ) {
1314 fmt .Println ("Test Standard Graph Generation" )
15+ testGridGraph (t )
16+ testTriangleHexGraph (t )
1417 testBarabasiAlbertGraph (t )
1518 testErdosRenyiGraph (t )
1619 testRandomGeometricGraph (t )
@@ -553,6 +556,104 @@ func testWattsStrogatzGraph(t *testing.T) {
553556 }
554557}
555558
559+ // testGridGraph tests the grid graph generation function.
560+ func testGridGraph (t * testing.T ) {
561+ fmt .Println ("- Test Grid Graph" )
562+
563+ rows := 10
564+ cols := 10
565+ torus := false
566+
567+ g , err := standard .GridGraph (0 , false , standard .Unweighted (), rows , cols , torus )
568+ if err != nil {
569+ t .Fatalf ("failed to generate grid graph: %v" , err )
570+ }
571+
572+ expectedNodes := rows * cols
573+ if len (g .Nodes ()) != expectedNodes {
574+ t .Errorf ("expected %d nodes, got %d" , expectedNodes , len (g .Nodes ()))
575+ }
576+
577+ for i := 0 ; i < rows ; i ++ {
578+ for j := 0 ; j < cols ; j ++ {
579+ id := graph .NodeID (fmt .Sprintf ("%d" , i * cols + j ))
580+ node , err := g .Node (id )
581+ if err != nil {
582+ t .Errorf ("failed to get node: %v" , err )
583+ continue
584+ }
585+
586+ x , okX := node .Tag ("x" )
587+ y , okY := node .Tag ("y" )
588+ if ! okX || ! okY {
589+ t .Errorf ("node %s is missing tags" , id )
590+ continue
591+ }
592+
593+ if x != fmt .Sprintf ("%d" , i ) || y != fmt .Sprintf ("%d" , j ) {
594+ t .Errorf ("node %s has incorrect tags: x=%s, y=%s" , id , x , y )
595+ }
596+
597+ expectedDegree := 4
598+ if i == 0 || i == rows - 1 {
599+ expectedDegree --
600+ }
601+ if j == 0 || j == cols - 1 {
602+ expectedDegree --
603+ }
604+
605+ if node .Degree () != expectedDegree {
606+ t .Errorf ("node %s has degree %d, expected %d" , id , node .Degree (), expectedDegree )
607+ }
608+ }
609+ }
610+ }
611+
612+ // testTriangleHexGraph tests the triangle hex graph generation function.
613+ func testTriangleHexGraph (t * testing.T ) {
614+ fmt .Println ("- Test Triangle Hex Graph" )
615+
616+ edge := 3
617+
618+ g , err := standard .TriangleHexGraph (0 , false , standard .Unweighted (), edge )
619+ if err != nil {
620+ t .Fatalf ("failed to generate triangle hex graph: %v" , err )
621+ }
622+
623+ expectedNodes := 3 * edge * (edge + 1 )/ 2 + 1
624+ if len (g .Nodes ()) != expectedNodes {
625+ t .Errorf ("expected %d nodes, got %d" , expectedNodes , len (g .Nodes ()))
626+ }
627+
628+ for i := 0 ; i < expectedNodes ; i ++ {
629+ nodeID := graph .NodeID (fmt .Sprintf ("%d" , i ))
630+
631+ node , err := g .Node (nodeID )
632+ if err != nil {
633+ t .Errorf ("failed to get node: %v" , err )
634+ continue
635+ }
636+
637+ q , okQ := node .Tag ("q" )
638+ r , okR := node .Tag ("r" )
639+ if ! okQ || ! okR {
640+ t .Errorf ("node %s is missing tags" , nodeID )
641+ continue
642+ }
643+
644+ qInt := 0
645+ rInt := 0
646+ fmt .Sscanf (q , "%d" , & qInt )
647+ fmt .Sscanf (r , "%d" , & rInt )
648+
649+ expectedDegree := degreeOfTriangleHex (qInt , rInt , edge )
650+
651+ if node .Degree () != expectedDegree {
652+ t .Errorf ("node %s (q=%d, r=%d) has degree %d, expected %d" , nodeID , qInt , rInt , node .Degree (), expectedDegree )
653+ }
654+ }
655+ }
656+
556657// testGenerateFromConfig tests the StandardGraph function with various configurations.
557658func testGenerateFromConfig (t * testing.T ) {
558659 fmt .Println ("- Test StandardGraph with Config" )
@@ -673,3 +774,37 @@ func poissonLowerExtreme(n int, lambda float64) int {
673774
674775 return - 1
675776}
777+
778+ // degreeOfTriangleHex calculates the degree of a node in the triangle hex graph based on its q and r coordinates and the edge length.
779+ func degreeOfTriangleHex (q , r , n int ) int {
780+ dirs := [][2 ]int {
781+ {1 , 0 },
782+ {- 1 , 0 },
783+ {0 , 1 },
784+ {0 , - 1 },
785+ {1 , - 1 },
786+ {- 1 , 1 },
787+ }
788+
789+ degree := 0
790+
791+ for _ , d := range dirs {
792+ nq := q + d [0 ]
793+ nr := r + d [1 ]
794+
795+ if exists (nq , nr , n ) {
796+ degree ++
797+ }
798+ }
799+
800+ return degree
801+ }
802+
803+ // exists checks if the coordinates (q, r) are valid for a node in the triangle hex graph with edge length n.
804+ func exists (q , r , n int ) bool {
805+ limit := n - 1
806+
807+ return q >= - limit && q <= limit &&
808+ r >= - limit && r <= limit &&
809+ q + r >= - limit && q + r <= limit
810+ }
0 commit comments