コード例 #1
0
ファイル: test-TNodeEdgeNet.cpp プロジェクト: Aleyasen/Alaki
// Test set edge data
TEST(TNodeEdgeNet, SetEdgeData) {
  int NNodes = 10000;
  int NEdges = 100000;

  TPt <TNodeEdgeNet<TInt, TInt> > Net;
  TPt <TNodeEdgeNet<TInt, TInt> > Net1;
  TPt <TNodeEdgeNet<TInt, TInt> > Net2;
  int i;
  int n;
  int NCount;
  int x,y;

  Net = TNodeEdgeNet<TInt, TInt>::New();
  EXPECT_EQ(1,Net->Empty());

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Net->AddNode(i);
  }
  EXPECT_EQ(0,Net->Empty());
  EXPECT_EQ(NNodes,Net->GetNodes());

  // create random edges
  NCount = NEdges;
  while (NCount > 0) {
    x = (long) (drand48() * NNodes);
    y = (long) (drand48() * NNodes);
    n = Net->AddEdge(x, y);
    // printf("0a %d %d %d\n",x,y,n);
    NCount--;
  }

  EXPECT_EQ(NEdges,Net->GetEdges());

  EXPECT_EQ(0,Net->Empty());
  EXPECT_EQ(1,Net->IsOk());

  for (i = 0; i < NNodes; i++) {
    EXPECT_EQ(1,Net->IsNode(i));
  }

  EXPECT_EQ(0,Net->IsNode(NNodes));
  EXPECT_EQ(0,Net->IsNode(NNodes+1));
  EXPECT_EQ(0,Net->IsNode(2*NNodes));

  // add data to nodes, square of node ID
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    Net->SetNDat(NI.GetId(), NI.GetId()*NI.GetId());
  }

  // test node data
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    EXPECT_EQ(NI.GetId()*NI.GetId(), Net->GetNDat(NI.GetId()));
  }

  // set edge data, source node ID * dest node ID
  for (TNodeEdgeNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    Net->SetEDat(EI.GetId(),EI.GetSrcNId()*EI.GetDstNId());
  }

  // verify edge data
  for (TNodeEdgeNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    EXPECT_EQ(EI.GetSrcNId()*EI.GetDstNId(), Net->GetEDat(EI.GetId()));
  }

  // set edge data to 42.
  Net->SetAllEDat(42);

  // verify edge data
  for (TNodeEdgeNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    EXPECT_EQ(42, Net->GetEDat(EI.GetId()));
  }

  // test node data again
  for (TNodeEdgeNet<TInt, TInt>::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    EXPECT_EQ(NI.GetId()*NI.GetId(), Net->GetNDat(NI.GetId()));
  }
}
コード例 #2
0
ファイル: demo-TNodeEDatNet.cpp プロジェクト: Accio/snap
// Test set edge data
void SetEdgeData() {
  int NNodes = 10000;
  int NEdges = 100000;

  TPt <TNodeEDatNet<TInt, TInt> > Net;
  TPt <TNodeEDatNet<TInt, TInt> > Net1;
  TPt <TNodeEDatNet<TInt, TInt> > Net2;
  int i;
  int n;
  int NCount;
  int x,y;
  bool t;
  int SrcNId;
  int DstNId;
  int EdgeDat;
  int Value;
  bool ok;

  Net = TNodeEDatNet<TInt, TInt>::New();
  t = Net->Empty();

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Net->AddNode(i);
  }
  t = Net->Empty();
  n = Net->GetNodes();

  // create random edges
  NCount = NEdges;
  while (NCount > 0) {
    x = (long) (drand48() * NNodes);
    y = (long) (drand48() * NNodes);
    // Net->GetEdges() is not correct for the loops (x == y),
    // skip the loops in this test
    if (x != y  &&  !Net->IsEdge(x,y)) {
      n = Net->AddEdge(x, y);
      NCount--;
    }
  }
  PrintNStats("SetEdgeData:Net", Net);

  // set edge data, source node ID * dest node ID
  for (TNodeEDatNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    Net->SetEDat(EI.GetSrcNId(),EI.GetDstNId(),EI.GetSrcNId()*EI.GetDstNId());
  }

  // verify edge data
  ok = true;
  for (TNodeEDatNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    SrcNId = EI.GetSrcNId();
    DstNId = EI.GetDstNId();
    EdgeDat = Net->GetEDat(SrcNId, DstNId);
    Value = SrcNId*DstNId;
    if (EdgeDat != Value) {
      ok = false;
    }
  }
  printf("network SetEdgeData:Net, status1 %s\n", (ok == true) ? "ok" : "ERROR");

  // set edge data to 42.
  Net->SetAllEDat(42);

  // verify edge data
  ok = true;
  for (TNodeEDatNet<TInt, TInt>::TEdgeI EI = Net->BegEI(); EI < Net->EndEI(); EI++) {
    SrcNId = EI.GetSrcNId();
    DstNId = EI.GetDstNId();
    EdgeDat = Net->GetEDat(SrcNId, DstNId);
    if (EdgeDat != 42) {
      ok = false;
    }
  }
  printf("network SetEdgeData:Net, status2 %s\n", (ok == true) ? "ok" : "ERROR");
}