Skip to content

Commit

Permalink
cmake file: googletest, Catch2, benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxiangyao committed Sep 1, 2023
1 parent d72a801 commit 2afe05b
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The `myvas::cmdout` library provides facilities for executing shell commands and
```
include(FetchContent)
FetchContent_Declare(cmdout
URL https://github.com/myvas/cmdout/archive/refs/tags/0.1.12.tar.gz
URL https://github.com/myvas/cmdout/archive/refs/tags/0.1.13.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
FetchContent_MakeAvailable(cmdout)
Expand Down
20 changes: 17 additions & 3 deletions benchmarks/cmake/benchmark.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@
# benchmark::benchmark
###################################

find_package(benchmark QUIET)
set(benchmark_VERSION_REQUIRED 1.8.3)

find_package(benchmark ${benchmark_VERSION_REQUIRED} QUIET)
if(benchmark_FOUND)
message(STATUS "Found benchmark: ${benchmark_VERSION}")
else()
message(STATUS "CMake package benchmark >=1.8.3 not found! Please wait for fetching from github.com...")
message(STATUS "CMake package benchmark ${benchmark_VERSION_REQUIRED} or later not found!"
" Please wait for fetching from https://github.com/google/benchmark"
)

include(FetchContent)
###############################################
# Do not build and run googlebenchmark tests!
###############################################
set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "")
FetchContent_Declare(benchmark
URL https://github.com/google/benchmark/archive/refs/tags/v1.8.3.tar.gz
URL https://github.com/google/benchmark/archive/refs/tags/v${benchmark_VERSION_REQUIRED}.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
FetchContent_MakeAvailable(benchmark)

if(benchmark_SOURCE_DIR)
message(STATUS "Built benchmark: ${benchmark_VERSION_REQUIRED}")
else()
if(benchmark_FIND_REQUIRED)
message(FATAL_ERROR "Failed to build benchmark: ${benchmark_VERSION_REQUIRED}")
else()
message(WARNING "Failed to build benchmark: ${benchmark_VERSION_REQUIRED}")
endif()
endif()
endif()
17 changes: 12 additions & 5 deletions examples/example_myvas_cmdout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,43 @@ void example_myvas_cmdout() {
assert(result.status() == EXIT_SUCCESS);
}
{
auto result = myvas::cmdout("ls ~/").exec();
std::cout << std::endl;
auto result = myvas::cmdout("ls ~/ -C").exec();
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
std::cout << std::endl;
auto result = myvas::cmdout("ls not-exist 2>&1").exec();
std::cout << result << std::endl;
assert(result.status() == ENOENT);
}
{
auto result = myvas::cmdout("ls / -l").exec();
std::cout << std::endl;
auto result = myvas::cmdout("ls / -C").exec();
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(0)).exec();
std::cout << std::endl;
auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(0)).exec();
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(1)).exec();
std::cout << std::endl;
auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(1)).exec();
std::cout << result << std::endl;
assert(result.status() == ETIME);
}
{
auto result = myvas::cmdout("ls / -l", std::chrono::milliseconds(9)).exec();
std::cout << std::endl;
auto result = myvas::cmdout("ls / -C", std::chrono::milliseconds(9)).exec();
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
std::cout << std::endl;
auto result = myvas::cmdout("ls not-exist 2>&1", std::chrono::milliseconds(9)).exec();
std::cout << result << std::endl;
assert(result.status() == ENOENT);
Expand Down
19 changes: 14 additions & 5 deletions examples/example_myvas_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,56 @@ void example_myvas_system() {
std::cout << "* function myvas::system() *" << std::endl;
std::cout << "***********************************" << std::endl;
{
std::cout << std::endl;
auto result = myvas::system("exit 0");
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
std::cout << std::endl;
auto result = myvas::system("exit 1");
std::cout << result << std::endl;
assert(result.status() == EXIT_FAILURE);
}
{
std::cout << std::endl;
auto result = myvas::system("ls not-exist 2>&1");
std::cout << result << std::endl;
assert(result.status() == ENOENT);
}
{
auto result = myvas::system("ls ~/");
std::cout << std::endl;
auto result = myvas::system("ls ~/ -C");
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
std::cout << std::endl;
auto result = myvas::system("ls not-exist 2>&1");
std::cout << result << std::endl;
assert(result.status() == ENOENT);
}
{
auto result = myvas::system_timeout_ms("ls / -l", 0);
std::cout << std::endl;
auto result = myvas::system_timeout_ms("ls / -C", 0);
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
auto result = myvas::system_timeout_ms("ls / -l", 1);
std::cout << std::endl;
auto result = myvas::system_timeout_ms("ls / -C", 1);
std::cout << result << std::endl;
assert(result.status() == ETIME);
}
{
auto result = myvas::system_timeout("ls / -l", std::chrono::milliseconds(0)).exec();
std::cout << std::endl;
auto result = myvas::system_timeout("ls / -C", std::chrono::milliseconds(0)).exec();
std::cout << result << std::endl;
assert(result.status() == EXIT_SUCCESS);
}
{
auto result = myvas::system_timeout("ls / -l", std::chrono::milliseconds(1)).exec();
std::cout << std::endl;
auto result = myvas::system_timeout("ls / -C", std::chrono::milliseconds(1)).exec();
std::cout << result << std::endl;
assert(result.status() == ETIME);
}
Expand Down
33 changes: 22 additions & 11 deletions examples/example_std_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,42 @@ void example_std_system() {
std::cout << "* function std::system() *" << std::endl;
std::cout << "*********************************" << std::endl;
{
std::cout << std::endl;
auto result = std::system(nullptr);
std::cout << result << std::endl;
std::cout << "std::system(nullptr) returns " << result << std::endl;
// https://en.cppreference.com/w/cpp/utility/program/system
// If command is a null pointer, returns a nonzero value
// if and only if the command processor exists.
assert(result != 0);
assert(result == 1);
assert(WEXITSTATUS(result) == EXIT_SUCCESS);
}
{
auto result = std::system("exit 0");
std::cout << result << std::endl;
assert(result == EXIT_SUCCESS);
std::cout << std::endl;
auto cmd = "exit 0";
auto result = std::system(cmd);
std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl;
assert(WEXITSTATUS(result) == EXIT_SUCCESS);
}
{
std::cout << std::endl;
auto cmd = "exit 1";
auto result = std::system("exit 1");
std::cout << result << std::endl;
assert(result == EXIT_FAILURE);
std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl;
assert(WEXITSTATUS(result) == EXIT_FAILURE);
}
{
auto result = std::system("ls ~/");
std::cout << result << std::endl;
assert(result == EXIT_SUCCESS);
std::cout << std::endl;
auto cmd = "ls ~/ -C";
auto result = std::system(cmd);
std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl;
assert(WEXITSTATUS(result) == EXIT_SUCCESS);
}
{
auto result = std::system("ls not-exist 2>&1");
std::cout << result << std::endl;
std::cout << std::endl;
auto cmd = "ls not-exist 2>&1";
auto result = std::system(cmd);
std::cout << "std::system(\"" << cmd << "\") returns " << result << std::endl;
assert(WEXITSTATUS(result) == ENOENT);
}
}
Expand Down
11 changes: 6 additions & 5 deletions include/cmdout/cmdout_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

#include <ostream>

/**
* @brief Helper for printing a cmdout object.
*/
/**
* @brief Helper for printing a cmdout object.
*/
std::ostream& operator<<(std::ostream& os, const myvas::cmdout& value)
{
os << "cmdout {" << value.cmd() << "} return " << value.status()
<< " length " << value.out().length() << "\n" << value.out();
os << "cmdout: " << value.cmd() << "\n(return " << value.status()
<< " length " << value.out().length() << ")\n"
<< std::string(80, '-') << "\n" << value.out();
return os;
}
2 changes: 1 addition & 1 deletion include/cmdout/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @brief When you make backward compatible bug fixes, You'd increment the PATCH version.
*/
#define CMDOUT_VERSION_PATCH 12
#define CMDOUT_VERSION_PATCH 13

#define CMDOUT_TO_VERSION_NUMBER(major, minor, patch) (major * 10000 + minor * 100 + patch)
#define CMDOUT_VERSION_NUMBER CMDOUT_TO_VERSION_NUMBER(CMDOUT_VERSION_MAJOR, CMDOUT_VERSION_MINOR, CMDOUT_VERSION_PATCH)
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ if(NOT TARGET cmdout)
endif()

include(../cmake/utils.cmake)
include(cmake/catch2.cmake)
include(cmake/googletest.cmake)
include(cmake/Catch2.cmake)

#########################
# tests sources
Expand Down
19 changes: 15 additions & 4 deletions tests/cmake/Catch2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@
# apt install catch2
###################################

find_package(Catch2 3.3.2 QUIET)
set(Catch2_VERSION_REQUIRED 3.3.2)

find_package(Catch2 ${Catch2_VERSION_REQUIRED} QUIET)
if(Catch2_FOUND)
message(STATUS "Found Catch2: ${Catch2_VERSION}")
else()
message(WARNING "CMake package Catch2 >=3.3.2 not found! Please wait for fetching from github.com...")
message(STATUS "CMake package Catch2 ${Catch2_VERSION_REQUIRED} or later not found!"
" Please wait for fetching from https://github.com/catchorg/Catch2"
)

include(FetchContent)
FetchContent_Declare(Catch2
URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.3.2.tar.gz
URL https://github.com/catchorg/Catch2/archive/refs/tags/v${Catch2_VERSION_REQUIRED}.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(Catch2)
find_package(Catch2)
if(Catch2_FOUND)
# TODO: We should extract `REAL` version number by hand, since there is no Catch2_VERSION here!
message(STATUS "Built Catch2: ${Catch2_VERSION}")
else()
message(FATAL_ERROR "Failed to build Catch2: ${Catch2_VERSION_REQUIRED}")
endif()
endif()
34 changes: 34 additions & 0 deletions tests/cmake/googletest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2023 Myvas Foundation
# SPDX-License-Identifier: MIT

###################################
# Find or fetch a CMake package
#
# googletest >=1.10.0
# googletest
###################################

set(googletest_VERSION_REQUIRED 1.14.0)

find_package(googletest ${googletest_VERSION_REQUIRED} QUIET)
if(googletest_FOUND)
message(STATUS "Found googletest: ${googletest_VERSION}")
else()
message(STATUS "CMake package googletest ${googletest_VERSION_REQUIRED} or later not found!"
" Please wait for fetching from https://github.com/google/googletest"
)

include(FetchContent)
FetchContent_Declare(googletest
URL https://github.com/google/googletest/archive/refs/tags/v${googletest_VERSION_REQUIRED}.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
OVERRIDE_FIND_PACKAGE
)
find_package(googletest)
if(googletest_FOUND)
# TODO: We should extract `REAL` version number by hand, since there is no googletest_VERSION here!
message(STATUS "Built googletest: ${googletest_VERSION}")
else()
message(FATAL_ERROR "Failed to build googletest: ${googletest_VERSION_REQUIRED}")
endif()
endif()

0 comments on commit 2afe05b

Please sign in to comment.