Skip to content

Commit

Permalink
Add: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmie committed Jul 19, 2022
1 parent 6657227 commit c9980b6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ cmake_minimum_required(VERSION 3.15)
project(Graph)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
install(FILES Graph.h DESTINATION include)
install(FILES Graph.h DESTINATION include)

project(Example)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(${PROJECT_NAME} Example.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)
target_link_libraries(${PROJECT_NAME} PRIVATE Graph)
55 changes: 55 additions & 0 deletions Graph/CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
},
{
"name": "WSL-GCC-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeExecutable": "cmake",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"wslPath": "${defaultWSLPath}",
"variables": []
},
{
"name": "WSL-GCC-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeExecutable": "cmake",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"wslPath": "${defaultWSLPath}",
"variables": []
}
]
}
29 changes: 29 additions & 0 deletions Graph/Example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <limits>
#include <numeric>
#include <algorithm>
#include <execution>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include "Graph.h"

int main()
{
Graph<std::string> graph{
{ "A", 9, "B" }, { "A", 5, "C" }, { "C", 2, "B" },
{ "A", 3, "D" }, { "B", 5, "D" }, { "D", 2, "C" },
{ "D", 2, "E" }, { "D", 5, "B" }, { "C", 4, "E" },
{ "B", 2, "F" }, { "F", 1, "E" }, { "C", 5, "F" },
};
graph.AddEdge("C", 3, "D");
auto out_path = graph.GetOutPath("C");
out_path.clear();
graph.RemoveEdge("C", "D");
graph.GetOutPath("C", [&out_path](auto&& new_path) {out_path.push_back(new_path); });
auto in_path = graph.GetInPath("B");
in_path.clear();
graph.GetInPath("B", [&in_path](auto&& new_path) {in_path.push_back(new_path); });
auto weight = graph.GetWeight("C", "D");
auto result = graph.GetBestPath("A", "E");
return 0;
}
2 changes: 1 addition & 1 deletion Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Graph
{
for (auto iterator = matrix.begin(); iterator != matrix.end(); ++iterator)
if (iterator->second.contains(vertex) && iterator->second[vertex] != INFINITE)
callback({ iterator->first, iterator->second[vertex] });
callback(PathType{ iterator->first, iterator->second[vertex] });
}

void SetBestPathAlgorithm(std::invocable<const Vertex&, const Vertex&, Compare> auto&& function)
Expand Down

0 comments on commit c9980b6

Please sign in to comment.