Verified Commit b9ff8fda authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add unit tests for ADM_strerror()

This commit brings the Catch2 dependency into the project
parent f8bdb597
Loading
Loading
Loading
Loading
+30 −3
Original line number Diff line number Diff line
@@ -123,6 +123,8 @@ message(STATUS "[${PROJECT_NAME}] server bind port: ${SCORD_BIND_PORT}")

option(SCORD_BUILD_EXAMPLES "Build examples (disabled by default)" OFF)

option(SCORD_BUILD_TESTS "Build tests (disabled by default)" OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ##############################################################################
@@ -225,6 +227,27 @@ FetchContent_Declare(

FetchContent_MakeAvailable(expected)

if (SCORD_BUILD_TESTS)

  enable_testing()

  ### catch2: required for unit testing
  message(STATUS "[${PROJECT_NAME}] Downloading and building Catch2")
  FetchContent_Declare(
    Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG 605a34765aa5d5ecbf476b4598a862ada971b0cc # v3.0.1
    GIT_SHALLOW ON
    GIT_PROGRESS ON
  )

  FetchContent_MakeAvailable(Catch2)

  # Ensure that CMake can find Catch2 extra CMake modules in case
  # they are needed
  list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/extras")
endif ()

### Mark any CMake variables imported from {fmt} and spdlog as advanced, so
### that they don't appear in cmake-gui or ccmake. Similarly for FETCHCONTENT
### variables.
@@ -248,3 +271,7 @@ add_subdirectory(src)
if (SCORD_BUILD_EXAMPLES)
  add_subdirectory(examples)
endif ()

if(SCORD_BUILD_TESTS)
  add_subdirectory(tests)
endif()
+31 −0
Original line number Diff line number Diff line
################################################################################
# Copyright 2021, Barcelona Supercomputing Center (BSC), Spain                 #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of scord.                                                  #
#                                                                              #
# scord is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# scord is distributed in the hope that it will be useful,                     #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with scord.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

add_executable(tests)

target_sources(tests PRIVATE test.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain adm_iosched)

include(Catch)
catch_discover_tests(tests)
+70 −3
Original line number Diff line number Diff line
//
// Created by amiranda on 28/06/22.
//
/******************************************************************************
 * Copyright 2021-2022, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of scord.
 *
 * scord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * scord is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with scord.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#include <catch2/catch_test_macros.hpp>
#include <admire.h>
#include <string>

SCENARIO("Error messages can be printed", "[lib][ADM_strerror]") {

    GIVEN("An error number") {
        WHEN("The error number is ADM_SUCCESS") {
            REQUIRE(std::string{ADM_strerror(ADM_SUCCESS)} == "Success");
        }

        WHEN("The error number is ADM_ESNAFU") {
            REQUIRE(std::string{ADM_strerror(ADM_ESNAFU)} == "Internal error");
        }

        WHEN("The error number is ADM_EBADARGS") {
            REQUIRE(std::string{ADM_strerror(ADM_EBADARGS)} == "Bad arguments");
        }

        WHEN("The error number is ADM_ENOMEM") {
            REQUIRE(std::string{ADM_strerror(ADM_ENOMEM)} ==
                    "Cannot allocate memory");
        }

        WHEN("The error number is ADM_EOTHER") {
            REQUIRE(std::string{ADM_strerror(ADM_EOTHER)} ==
                    "Undetermined error");
        }

        WHEN("The error number is larger than ADM_EOTHER and "
             "lower than ADM_ERR_MAX") {

            for(int i = ADM_EOTHER; i < ADM_ERR_MAX; ++i) {
                const auto e = static_cast<ADM_return_t>(i);
                REQUIRE(std::string{ADM_strerror(e)} == "Undetermined error");
            }
        }

        WHEN("The error number is larger than ADM_ERR_MAX") {
            for(int i = ADM_ERR_MAX; i < ADM_ERR_MAX * 2; ++i) {
                const auto e = static_cast<ADM_return_t>(i);
                REQUIRE(std::string{ADM_strerror(e)} == "Unknown error");
            }
        }
    }
}