blob: bcb31b29a9ee605d69165ee567266b2b0c7f6dbb [file]
// Copyright 2025 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "critical_path.h"
#include <inttypes.h>
#include <stddef.h>
#include <vector>
#include "test.h"
namespace {
std::string eAA = "AA";
std::string eBB = "BB";
std::string eCC = "CC";
std::string eDD = "DD";
const std::string* GetEdgeNamePtr(const void* edge_ptr) {
return reinterpret_cast<const std::string*>(edge_ptr);
}
} // namespace
TEST(CriticalPathTest, EmptyBuild) {
auto get_edge_name = [](const void* edge_ptr) -> std::string {
return "edge";
};
auto get_edge_inputs = [](const void* edge_ptr) -> std::vector<const void*> {
return {};
};
std::vector<CriticalPath::BuiltEdgeInfo> built_edges = {};
auto result =
CriticalPath::Compute(built_edges, get_edge_inputs, get_edge_name);
ASSERT_TRUE(result.HasValues()) << result.error;
const auto& path = result.critical_path;
ASSERT_EQ(path.size(), 0u);
}
TEST(CriticalPathTest, SimpleBuild) {
auto get_edge_name = [](const void* edge_ptr) {
return *GetEdgeNamePtr(edge_ptr);
};
auto get_edge_inputs = [](const void* edge_ptr) -> std::vector<const void*> {
const std::string* edge = GetEdgeNamePtr(edge_ptr);
if (*edge == "AA")
return {};
if (*edge == "BB")
return { &eAA };
if (*edge == "CC")
return { &eAA };
if (*edge == "DD")
return { &eBB, &eCC };
else
return {};
};
std::vector<CriticalPath::BuiltEdgeInfo> built_edges = {
{ &eAA, 100, 110 },
{ &eBB, 110, 120 },
{ &eCC, 110, 140 },
{ &eDD, 150, 170 },
};
auto result =
CriticalPath::Compute(built_edges, get_edge_inputs, get_edge_name);
ASSERT_TRUE(result.HasValues()) << result.error;
const auto& path = result.critical_path;
ASSERT_EQ(path.size(), 3u);
EXPECT_EQ(path[0], &eAA);
EXPECT_EQ(path[1], &eCC);
EXPECT_EQ(path[2], &eDD);
}