Update test to reflect the recommended graph construction style:

First, graph inputs and their names:
  - Makes it clear what inputs graph has
  - Indirectly demands for type specification e.g. Stream<AnyType> a = graph.In(0); vs Stream<int> a = graph.In(0).Cast<int>();
Then graph nodes
  - Nodes are added and used as they needed
  - One node is not mixed in other nodes, only its outputs
  - Indirectly demands for type specification e.g. Stream<AnyType> a = node.Out(0); vs Stream<int> a = node.Out(0).Cast<int>();
Then graph outputs
  - Makes it clear what outputs graph has

The recommended structure keep C++ graph similar to pbtxt representation.

PiperOrigin-RevId: 504701023
This commit is contained in:
MediaPipe Team 2023-01-25 17:11:41 -08:00 committed by Copybara-Service
parent ff0ccfc209
commit be546d22fc

View File

@ -26,12 +26,21 @@ using ::mediapipe::api2::test::FooBar1;
TEST(BuilderTest, BuildGraph) { TEST(BuilderTest, BuildGraph) {
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> base = graph.In("IN").SetName("base");
SidePacket<AnyType> side = graph.SideIn("SIDE").SetName("side");
auto& foo = graph.AddNode("Foo"); auto& foo = graph.AddNode("Foo");
base >> foo.In("BASE");
side >> foo.SideIn("SIDE");
Stream<AnyType> foo_out = foo.Out("OUT");
auto& bar = graph.AddNode("Bar"); auto& bar = graph.AddNode("Bar");
graph.In("IN").SetName("base") >> foo.In("BASE"); foo_out >> bar.In("IN");
graph.SideIn("SIDE").SetName("side") >> foo.SideIn("SIDE"); Stream<AnyType> bar_out = bar.Out("OUT");
foo.Out("OUT") >> bar.In("IN");
bar.Out("OUT").SetName("out") >> graph.Out("OUT"); // Graph outputs.
bar_out.SetName("out") >> graph.Out("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -87,6 +96,7 @@ TEST(BuilderTest, CopyableStream) {
TEST(BuilderTest, BuildGraphWithFunctions) { TEST(BuilderTest, BuildGraphWithFunctions) {
Graph graph; Graph graph;
// Graph inputs.
Stream<int> base = graph.In("IN").SetName("base").Cast<int>(); Stream<int> base = graph.In("IN").SetName("base").Cast<int>();
SidePacket<float> side = graph.SideIn("SIDE").SetName("side").Cast<float>(); SidePacket<float> side = graph.SideIn("SIDE").SetName("side").Cast<float>();
@ -105,6 +115,7 @@ TEST(BuilderTest, BuildGraphWithFunctions) {
}; };
Stream<double> bar_out = bar_fn(foo_out, graph); Stream<double> bar_out = bar_fn(foo_out, graph);
// Graph outputs.
bar_out.SetName("out") >> graph.Out("OUT"); bar_out.SetName("out") >> graph.Out("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
@ -130,12 +141,21 @@ TEST(BuilderTest, BuildGraphWithFunctions) {
template <class FooT> template <class FooT>
void BuildGraphTypedTest() { void BuildGraphTypedTest() {
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> base = graph.In("IN").SetName("base");
SidePacket<AnyType> side = graph.SideIn("SIDE").SetName("side");
auto& foo = graph.AddNode<FooT>(); auto& foo = graph.AddNode<FooT>();
base >> foo.In(MPP_TAG("BASE"));
side >> foo.SideIn(MPP_TAG("BIAS"));
Stream<float> foo_out = foo.Out(MPP_TAG("OUT"));
auto& bar = graph.AddNode<Bar>(); auto& bar = graph.AddNode<Bar>();
graph.In("IN").SetName("base") >> foo.In(MPP_TAG("BASE")); foo_out >> bar.In(MPP_TAG("IN"));
graph.SideIn("SIDE").SetName("side") >> foo.SideIn(MPP_TAG("BIAS")); Stream<AnyType> bar_out = bar.Out(MPP_TAG("OUT"));
foo.Out(MPP_TAG("OUT")) >> bar.In(MPP_TAG("IN"));
bar.Out(MPP_TAG("OUT")).SetName("out") >> graph.Out("OUT"); // Graph outputs.
bar_out.SetName("out") >> graph.Out("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(
@ -165,12 +185,20 @@ TEST(BuilderTest, BuildGraphTyped2) { BuildGraphTypedTest<test::Foo2>(); }
TEST(BuilderTest, FanOut) { TEST(BuilderTest, FanOut) {
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> base = graph.In("IN").SetName("base");
auto& foo = graph.AddNode("Foo"); auto& foo = graph.AddNode("Foo");
base >> foo.In("BASE");
Stream<AnyType> foo_out = foo.Out("OUT");
auto& adder = graph.AddNode("FloatAdder"); auto& adder = graph.AddNode("FloatAdder");
graph.In("IN").SetName("base") >> foo.In("BASE"); foo_out >> adder.In("IN")[0];
foo.Out("OUT") >> adder.In("IN")[0]; foo_out >> adder.In("IN")[1];
foo.Out("OUT") >> adder.In("IN")[1]; Stream<AnyType> out = adder.Out("OUT");
adder.Out("OUT").SetName("out") >> graph.Out("OUT");
// Graph outputs.
out.SetName("out") >> graph.Out("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -193,12 +221,20 @@ TEST(BuilderTest, FanOut) {
TEST(BuilderTest, TypedMultiple) { TEST(BuilderTest, TypedMultiple) {
Graph graph; Graph graph;
auto& foo = graph.AddNode<test::Foo>(); // Graph inputs.
auto& adder = graph.AddNode<test::FloatAdder>(); Stream<AnyType> base = graph.In("IN").SetName("base");
graph.In("IN").SetName("base") >> foo.In(MPP_TAG("BASE"));
foo.Out(MPP_TAG("OUT")) >> adder.In(MPP_TAG("IN"))[0]; auto& foo = graph.AddNode<Foo>();
foo.Out(MPP_TAG("OUT")) >> adder.In(MPP_TAG("IN"))[1]; base >> foo.In(MPP_TAG("BASE"));
adder.Out(MPP_TAG("OUT")).SetName("out") >> graph.Out("OUT"); Stream<float> foo_out = foo.Out(MPP_TAG("OUT"));
auto& adder = graph.AddNode<FloatAdder>();
foo_out >> adder.In(MPP_TAG("IN"))[0];
foo_out >> adder.In(MPP_TAG("IN"))[1];
Stream<float> out = adder.Out(MPP_TAG("OUT"));
// Graph outputs.
out.SetName("out") >> graph.Out("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -221,13 +257,20 @@ TEST(BuilderTest, TypedMultiple) {
TEST(BuilderTest, TypedByPorts) { TEST(BuilderTest, TypedByPorts) {
Graph graph; Graph graph;
auto& foo = graph.AddNode<test::Foo>(); // Graph inputs.
auto& adder = graph.AddNode<FloatAdder>(); Stream<int> base = graph.In(FooBar1::kIn).SetName("base");
graph.In(FooBar1::kIn).SetName("base") >> foo[Foo::kBase]; auto& foo = graph.AddNode<test::Foo>();
foo[Foo::kOut] >> adder[FloatAdder::kIn][0]; base >> foo[Foo::kBase];
foo[Foo::kOut] >> adder[FloatAdder::kIn][1]; Stream<float> foo_out = foo[Foo::kOut];
adder[FloatAdder::kOut].SetName("out") >> graph.Out(FooBar1::kOut);
auto& adder = graph.AddNode<FloatAdder>();
foo_out >> adder[FloatAdder::kIn][0];
foo_out >> adder[FloatAdder::kIn][1];
Stream<float> out = adder[FloatAdder::kOut];
// Graph outputs.
out.SetName("out") >> graph.Out(FooBar1::kOut);
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -250,9 +293,15 @@ TEST(BuilderTest, TypedByPorts) {
TEST(BuilderTest, PacketGenerator) { TEST(BuilderTest, PacketGenerator) {
Graph graph; Graph graph;
// Graph inputs.
SidePacket<AnyType> side_in = graph.SideIn("IN");
auto& generator = graph.AddPacketGenerator("FloatGenerator"); auto& generator = graph.AddPacketGenerator("FloatGenerator");
graph.SideIn("IN") >> generator.SideIn("IN"); side_in >> generator.SideIn("IN");
generator.SideOut("OUT") >> graph.SideOut("OUT"); SidePacket<AnyType> side_out = generator.SideOut("OUT");
// Graph outputs.
side_out >> graph.SideOut("OUT");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -269,12 +318,21 @@ TEST(BuilderTest, PacketGenerator) {
TEST(BuilderTest, EmptyTag) { TEST(BuilderTest, EmptyTag) {
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> a = graph.In("A").SetName("a");
Stream<AnyType> c = graph.In("C").SetName("c");
Stream<AnyType> b = graph.In("B").SetName("b");
auto& foo = graph.AddNode("Foo"); auto& foo = graph.AddNode("Foo");
graph.In("A").SetName("a") >> foo.In("")[0]; a >> foo.In("")[0];
graph.In("C").SetName("c") >> foo.In("")[2]; c >> foo.In("")[2];
graph.In("B").SetName("b") >> foo.In("")[1]; b >> foo.In("")[1];
foo.Out("")[0].SetName("x") >> graph.Out("ONE"); Stream<AnyType> x = foo.Out("")[0];
foo.Out("")[1].SetName("y") >> graph.Out("TWO"); Stream<AnyType> y = foo.Out("")[1];
// Graph outputs.
x.SetName("x") >> graph.Out("ONE");
y.SetName("y") >> graph.Out("TWO");
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -301,10 +359,17 @@ TEST(BuilderTest, StringLikeTags) {
constexpr absl::string_view kC = "C"; constexpr absl::string_view kC = "C";
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> a = graph.In(kA).SetName("a");
Stream<AnyType> b = graph.In(kB).SetName("b");
auto& foo = graph.AddNode("Foo"); auto& foo = graph.AddNode("Foo");
graph.In(kA).SetName("a") >> foo.In(kA); a >> foo.In(kA);
graph.In(kB).SetName("b") >> foo.In(kB); b >> foo.In(kB);
foo.Out(kC).SetName("c") >> graph.Out(kC); Stream<AnyType> c = foo.Out(kC);
// Graph outputs.
c.SetName("c") >> graph.Out(kC);
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -323,12 +388,21 @@ TEST(BuilderTest, StringLikeTags) {
TEST(BuilderTest, GraphIndexes) { TEST(BuilderTest, GraphIndexes) {
Graph graph; Graph graph;
// Graph inputs.
Stream<AnyType> a = graph.In(0).SetName("a");
Stream<AnyType> c = graph.In(1).SetName("c");
Stream<AnyType> b = graph.In(2).SetName("b");
auto& foo = graph.AddNode("Foo"); auto& foo = graph.AddNode("Foo");
graph.In(0).SetName("a") >> foo.In("")[0]; a >> foo.In("")[0];
graph.In(1).SetName("c") >> foo.In("")[2]; c >> foo.In("")[2];
graph.In(2).SetName("b") >> foo.In("")[1]; b >> foo.In("")[1];
foo.Out("")[0].SetName("x") >> graph.Out(1); Stream<AnyType> x = foo.Out("")[0];
foo.Out("")[1].SetName("y") >> graph.Out(0); Stream<AnyType> y = foo.Out("")[1];
// Graph outputs.
x.SetName("x") >> graph.Out(1);
y.SetName("y") >> graph.Out(0);
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
@ -381,21 +455,20 @@ TEST(BuilderTest, AnyAndSameTypeHandledProperly) {
auto& node = graph.AddNode("AnyAndSameTypeCalculator"); auto& node = graph.AddNode("AnyAndSameTypeCalculator");
any_input >> node[AnyAndSameTypeCalculator::kAnyTypeInput]; any_input >> node[AnyAndSameTypeCalculator::kAnyTypeInput];
int_input >> node[AnyAndSameTypeCalculator::kIntInput]; int_input >> node[AnyAndSameTypeCalculator::kIntInput];
Stream<AnyType> any_type_output = Stream<AnyType> any_type_output =
node[AnyAndSameTypeCalculator::kAnyTypeOutput]; node[AnyAndSameTypeCalculator::kAnyTypeOutput];
any_type_output.SetName("any_type_output");
Stream<AnyType> same_type_output = Stream<AnyType> same_type_output =
node[AnyAndSameTypeCalculator::kSameTypeOutput]; node[AnyAndSameTypeCalculator::kSameTypeOutput];
same_type_output.SetName("same_type_output");
Stream<AnyType> recursive_same_type_output = Stream<AnyType> recursive_same_type_output =
node[AnyAndSameTypeCalculator::kRecursiveSameTypeOutput]; node[AnyAndSameTypeCalculator::kRecursiveSameTypeOutput];
recursive_same_type_output.SetName("recursive_same_type_output");
Stream<int> same_int_output = node[AnyAndSameTypeCalculator::kSameIntOutput]; Stream<int> same_int_output = node[AnyAndSameTypeCalculator::kSameIntOutput];
same_int_output.SetName("same_int_output");
Stream<int> recursive_same_int_type_output = Stream<int> recursive_same_int_type_output =
node[AnyAndSameTypeCalculator::kRecursiveSameIntOutput]; node[AnyAndSameTypeCalculator::kRecursiveSameIntOutput];
any_type_output.SetName("any_type_output");
same_type_output.SetName("same_type_output");
recursive_same_type_output.SetName("recursive_same_type_output");
same_int_output.SetName("same_int_output");
recursive_same_int_type_output.SetName("recursive_same_int_type_output"); recursive_same_int_type_output.SetName("recursive_same_int_type_output");
CalculatorGraphConfig expected = mediapipe::ParseTextProtoOrDie< CalculatorGraphConfig expected = mediapipe::ParseTextProtoOrDie<
@ -424,11 +497,10 @@ TEST(BuilderTest, AnyTypeCanBeCast) {
auto& node = graph.AddNode("AnyAndSameTypeCalculator"); auto& node = graph.AddNode("AnyAndSameTypeCalculator");
any_input >> node[AnyAndSameTypeCalculator::kAnyTypeInput]; any_input >> node[AnyAndSameTypeCalculator::kAnyTypeInput];
Stream<double> any_type_output = Stream<double> any_type_output =
node[AnyAndSameTypeCalculator::kAnyTypeOutput] node[AnyAndSameTypeCalculator::kAnyTypeOutput].Cast<double>();
.SetName("any_type_output")
.Cast<double>();
any_type_output >> graph.Out("GRAPH_ANY_OUTPUT").Cast<double>(); any_type_output.SetName("any_type_output") >>
graph.Out("GRAPH_ANY_OUTPUT").Cast<double>();
CalculatorGraphConfig expected = CalculatorGraphConfig expected =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(