Files
whetstone_DSL/editor/tests/step1944_test.cpp

106 lines
3.1 KiB
C++
Raw Normal View History

// Step 1944: PipelineGraph
//
// t1: addStage and stageCount
// t2: addEdge; successors and predecessors
// t3: topologicalOrder on linear chain
// t4: hasCycle detects cycle; acyclic returns false
// t5: sourceStages and sinkStages
#include "PipelineGraph.h"
#include <algorithm>
#include <iostream>
namespace ws = whetstone;
static int p=0,f=0;
#define T(n) { std::cout<<" "<<#n<<"... "; }
#define P() { std::cout<<"PASS\n"; ++p; }
#define F(m) { std::cout<<"FAIL: "<<m<<"\n"; ++f; }
#define C(c,m) if(!(c)){F(m);return;}
void t1(){
T(addStage_and_stageCount);
ws::PipelineGraph g;
g.addStage("a");
g.addStage("b");
g.addStage("c");
C(g.stageCount() == 3, "three stages");
P();
}
void t2(){
T(addEdge_successors_predecessors);
ws::PipelineGraph g;
g.addEdge("a","b");
g.addEdge("b","c");
auto sb = g.successors("a");
C(sb.size()==1 && sb[0]=="b", "a->b");
auto sc = g.successors("b");
C(sc.size()==1 && sc[0]=="c", "b->c");
auto pb = g.predecessors("b");
C(pb.size()==1 && pb[0]=="a", "b pred a");
C(g.predecessors("a").empty(), "a no pred");
C(g.successors("c").empty(), "c no succ");
P();
}
void t3(){
T(topologicalOrder_linear_chain);
ws::PipelineGraph g;
g.addEdge("ingest","normalize");
g.addEdge("normalize","fanout");
g.addEdge("fanout","transform");
g.addEdge("transform","serialize");
auto order = g.topologicalOrder();
C(order.size() == 5, "five stages in order");
// ingest must come before normalize, etc.
auto pos = [&](const std::string& s){
return std::find(order.begin(),order.end(),s) - order.begin();
};
C(pos("ingest") < pos("normalize"), "ingest before normalize");
C(pos("normalize") < pos("fanout"), "normalize before fanout");
C(pos("fanout") < pos("transform"), "fanout before transform");
C(pos("transform") < pos("serialize"), "transform before serialize");
P();
}
void t4(){
T(hasCycle_detection);
ws::PipelineGraph acyclic;
acyclic.addEdge("a","b");
acyclic.addEdge("b","c");
C(!acyclic.hasCycle(), "acyclic has no cycle");
ws::PipelineGraph cyclic;
cyclic.addEdge("a","b");
cyclic.addEdge("b","c");
cyclic.addEdge("c","a");
C(cyclic.hasCycle(), "cycle detected");
C(cyclic.topologicalOrder().empty(), "topo empty on cycle");
P();
}
void t5(){
T(sourceStages_and_sinkStages);
ws::PipelineGraph g;
g.addEdge("ingest","normalize");
g.addEdge("normalize","serialize");
auto src = g.sourceStages();
C(src.size()==1 && src[0]=="ingest", "ingest is source");
auto sink = g.sinkStages();
C(sink.size()==1 && sink[0]=="serialize","serialize is sink");
// diamond: two sources, one sink
ws::PipelineGraph d;
d.addEdge("a","c");
d.addEdge("b","c");
C(d.sourceStages().size()==2, "diamond: two sources");
C(d.sinkStages().size()==1, "diamond: one sink");
P();
}
int main(){
std::cout << "Step 1944: PipelineGraph\n";
t1(); t2(); t3(); t4(); t5();
std::cout << "\n" << p << "/" << (p+f) << " passed\n";
return f > 0 ? 1 : 0;
}