void checkTNEANetCorrect(PNEANet G, PNEANet G2) { EXPECT_EQ(G->GetNodes(), G2->GetNodes()); EXPECT_EQ(G->GetEdges(), G2->GetEdges()); TStr attr2 = "int"; TStr attr3 = "float"; TStr attr1 = "str"; EXPECT_EQ(G->GetNAIntI(attr2, 3).GetDat(), G2->GetNAIntI(attr2, 3).GetDat()); EXPECT_EQ(G->GetNAIntI(attr2, 50).GetDat(), G2->GetNAIntI(attr2, 50).GetDat()); EXPECT_EQ(G->GetNAFltI(attr3, 5).GetDat(), G2->GetNAFltI(attr3, 5).GetDat()); EXPECT_EQ(G->GetNAFltI(attr3, 50).GetDat(), G2->GetNAFltI(attr3, 50).GetDat()); EXPECT_EQ(G->GetNAStrI(attr1, 10).GetDat().LastCh(), G2->GetNAStrI(attr1, 10).GetDat().LastCh()); EXPECT_EQ(G->GetNAStrI(attr1, 20).GetDat().LastCh(), G2->GetNAStrI(attr1, 20).GetDat().LastCh()); }
// Test the default constructor TEST(TNEANet, DefaultConstructor) { PNEANet Graph; Graph = TNEANet::New(); EXPECT_EQ(0,Graph->GetNodes()); EXPECT_EQ(0,Graph->GetEdges()); EXPECT_EQ(1,Graph->IsOk()); EXPECT_EQ(1,Graph->Empty()); EXPECT_EQ(1,Graph->HasFlag(gfDirected)); }
// Test small graph TEST(TNEANet, GetSmallGraph) { PNEANet Graph; return; // not implemented //Graph = TNEANet::GetSmallGraph(); EXPECT_EQ(5,Graph->GetNodes()); EXPECT_EQ(6,Graph->GetEdges()); EXPECT_EQ(1,Graph->IsOk()); EXPECT_EQ(0,Graph->Empty()); EXPECT_EQ(1,Graph->HasFlag(gfDirected)); }
// Print graph statistics void PrintGStats(const char s[], PNEANet Graph) { printf("graph %s, nodes %d, edges %d, empty %s\n", s, Graph->GetNodes(), Graph->GetEdges(), Graph->Empty() ? "yes" : "no"); }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("Flow. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); double NetPRTimeSum = 0; double NetEKTimeSum = 0; int NumWins = 0; Try const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "", "Input file"); const int Iters = Env.GetIfArgPrefixInt("-n:", 10, "Number of runs per thread"); const int Threads = Env.GetIfArgPrefixInt("-t:", 4, "Number of threads"); printf("Integer Flow Test\n"); printf("Filename: %s\n", InFNm.CStr()); printf("Building Network...\n"); TFIn InFile(InFNm); // If the input file is a binary, use the following line to load the network PNEANet Net = TNEANet::Load(InFile); // If the input file is a text file, use the following to load the network and save as binary // PNEANet Net; // int MaxEdgeCap = BuildCapacityNetwork(InFNm, Net); // const TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "", "Output file"); // TFOut OutFile(OutFNm); // Net->Save(OutFile); printf("PNEANet Nodes: %d, Edges: %d\n\n", Net->GetNodes(), Net->GetEdges()); #pragma omp parallel for reduction(+:NetEKTimeSum,NetPRTimeSum,NumWins) schedule(static, 1) for (int t = 0; t < Threads; t++) { TRnd Random(t); for (int i = 0; i < Iters; i++) { int SrcNId = Net->GetRndNId(Random); int SnkNId = Net->GetRndNId(Random); double PRBeginTime = getcputime(); int NetMaxFlowPR = TSnap::GetMaxFlowIntPR(Net, SrcNId, SnkNId); double PREndTime = getcputime(); double NetPRFlowRunTime = PREndTime - PRBeginTime; double EKBeginTime = getcputime(); int NetMaxFlowEK = TSnap::GetMaxFlowIntEK(Net, SrcNId, SnkNId); double EKEndTime = getcputime(); double NetEKFlowRunTime = EKEndTime - EKBeginTime; IAssert(NetMaxFlowPR == NetMaxFlowEK); if (NetPRFlowRunTime < NetEKFlowRunTime) { NumWins++; } NetPRTimeSum += NetPRFlowRunTime; NetEKTimeSum += NetEKFlowRunTime; #pragma omp critical { #ifndef NOMP printf("Thread: %d\n", omp_get_thread_num()); #endif printf("Source: %d, Sink %d\n", SrcNId, SnkNId); printf("Max Flow: %d\n", NetMaxFlowEK); printf("PR CPU Time: %f\n", NetPRFlowRunTime); printf("EK CPU Time: %f\n", NetEKFlowRunTime); printf("\n"); } } } int TotalRuns = Iters*Threads; printf ("Avg PR PNEANet Time: %f\n", NetPRTimeSum/TotalRuns); printf ("Avg EK PNEANet Time: %f\n", NetEKTimeSum/TotalRuns); printf ("%d out of %d PR was faster\n", NumWins, TotalRuns); Catch return 0; }
// Test node, edge creation TEST(TNEANet, ManipulateNodesEdges) { int NNodes = 1000; int NEdges = 100000; const char *FName = "test.graph.dat"; PNEANet Graph; PNEANet Graph1; PNEANet Graph2; int i; int n; int NCount; int x,y; int Deg, InDeg, OutDeg; Graph = TNEANet::New(); EXPECT_EQ(1,Graph->Empty()); // create the nodes for (i = 0; i < NNodes; i++) { Graph->AddNode(i); } EXPECT_EQ(0,Graph->Empty()); EXPECT_EQ(NNodes,Graph->GetNodes()); // create random edges NCount = NEdges; while (NCount > 0) { x = (long) (drand48() * NNodes); y = (long) (drand48() * NNodes); n = Graph->AddEdge(x, y); NCount--; } EXPECT_EQ(NEdges,Graph->GetEdges()); EXPECT_EQ(0,Graph->Empty()); EXPECT_EQ(1,Graph->IsOk()); for (i = 0; i < NNodes; i++) { EXPECT_EQ(1,Graph->IsNode(i)); } EXPECT_EQ(0,Graph->IsNode(NNodes)); EXPECT_EQ(0,Graph->IsNode(NNodes+1)); EXPECT_EQ(0,Graph->IsNode(2*NNodes)); // nodes iterator NCount = 0; for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } EXPECT_EQ(NNodes,NCount); // edges per node iterator NCount = 0; for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NCount++; } } EXPECT_EQ(NEdges,NCount); // edges iterator NCount = 0; for (TNEANet::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { NCount++; } EXPECT_EQ(NEdges,NCount); // node degree for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { Deg = NI.GetDeg(); InDeg = NI.GetInDeg(); OutDeg = NI.GetOutDeg(); EXPECT_EQ(Deg,InDeg+OutDeg); } // assignment Graph1 = TNEANet::New(); *Graph1 = *Graph; EXPECT_EQ(NNodes,Graph1->GetNodes()); EXPECT_EQ(NEdges,Graph1->GetEdges()); EXPECT_EQ(0,Graph1->Empty()); EXPECT_EQ(1,Graph1->IsOk()); // saving and loading { TFOut FOut(FName); Graph->Save(FOut); FOut.Flush(); } { TFIn FIn(FName); Graph2 = TNEANet::Load(FIn); } EXPECT_EQ(NNodes,Graph2->GetNodes()); EXPECT_EQ(NEdges,Graph2->GetEdges()); EXPECT_EQ(0,Graph2->Empty()); EXPECT_EQ(1,Graph2->IsOk()); // remove all the nodes and edges for (i = 0; i < NNodes; i++) { n = Graph->GetRndNId(); Graph->DelNode(n); } EXPECT_EQ(0,Graph->GetNodes()); EXPECT_EQ(0,Graph->GetEdges()); EXPECT_EQ(1,Graph->IsOk()); EXPECT_EQ(1,Graph->Empty()); Graph1->Clr(); EXPECT_EQ(0,Graph1->GetNodes()); EXPECT_EQ(0,Graph1->GetEdges()); EXPECT_EQ(1,Graph1->IsOk()); EXPECT_EQ(1,Graph1->Empty()); }