Skip to content

Commit

Permalink
Add C Result link test based on Ken's description of link failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
ned14 committed Oct 8, 2024
1 parent 2a8ff2b commit 355f247
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 6 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Outcome cmake
# (C) 2016-2023 Niall Douglas <http://www.nedproductions.biz/>
# (C) 2016-2024 Niall Douglas <http://www.nedproductions.biz/>
# File Created: June 2016
#
#
Expand Down Expand Up @@ -184,7 +184,7 @@ endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test" AND NOT outcome_IS_DEPENDENCY AND (NOT DEFINED BUILD_TESTING OR BUILD_TESTING))
# For all possible configurations of this library, add each test
list_filter(outcome_TESTS EXCLUDE REGEX "constexprs")
list_filter(outcome_TESTS EXCLUDE REGEX "constexprs|link")
set(outcome_TESTS_DISABLE_PRECOMPILE_HEADERS
"outcome_hl--coroutine-support"
"outcome_hl--core-result"
Expand Down Expand Up @@ -359,9 +359,12 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test" AND NOT outcome_IS_DEPENDENCY AND (
endforeach()
endif()

# Add in link tests
add_subdirectory("test/link")

# Turn on pedantic warnings for all tests, examples and snippets
if(NOT MSVC)
foreach(target ${outcome_TEST_TARGETS} ${outcome_EXAMPLE_TARGETS} ${example_bins})
foreach(target ${outcome_TEST_TARGETS} ${outcome_EXAMPLE_TARGETS} ${outcome_LINK_TARGETS} ${example_bins})
target_compile_options(${target} PUBLIC "-Wpedantic")
endforeach()
endif()
Expand Down
1 change: 1 addition & 0 deletions cmake/headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(outcome_HEADERS
"include/outcome/experimental/status-code/include/status-code/result.hpp"
"include/outcome/experimental/status-code/include/status-code/status_code.hpp"
"include/outcome/experimental/status-code/include/status-code/status_code_domain.hpp"
"include/outcome/experimental/status-code/include/status-code/status_code_gdb.h"
"include/outcome/experimental/status-code/include/status-code/status_error.hpp"
"include/outcome/experimental/status-code/include/status-code/std_error_code.hpp"
"include/outcome/experimental/status-code/include/status-code/system_code.hpp"
Expand Down
6 changes: 3 additions & 3 deletions include/outcome/detail/revision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Distributed under the Boost Software License, Version 1.0.
*/

// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define OUTCOME_PREVIOUS_COMMIT_REF 36beb5b34b24cf25d93ac421370ef5578f2f65ed
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 13:14:36 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 36beb5b3
#define OUTCOME_PREVIOUS_COMMIT_REF 2a8ff2b0ca37e4627b0d9919b653fee7d4cc6968
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 14:48:43 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 2a8ff2b0
55 changes: 55 additions & 0 deletions test/link/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Outcome cmake
# (C) 2016-2024 Niall Douglas <http://www.nedproductions.biz/>
# File Created: June 2016
#
#
# 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 in the accompanying file
# Licence.txt or 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.
#
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file Licence.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

cmake_minimum_required(VERSION 3.9 FATAL_ERROR)

set(outcome_LINK_TARGETS)
foreach(type STATIC SHARED)
foreach(visibility default hidden)
set(target outcome-link-test-experimental-c-result-type_${type}-vis_${visibility})
add_library(${target}-lib ${type}
"experimental-c-result/lib.c"
"experimental-c-result/lib.cpp")
target_compile_definitions(${target}-lib PRIVATE MYLIB_SOURCE=1)
set_target_properties(${target}-lib PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET "${visibility}"
CXX_VISIBILITY_PRESET "${visibility}"
DISABLE_PRECOMPILE_HEADERS On
)
target_link_libraries(${target}-lib PRIVATE outcome::hl)
list(APPEND outcome_LINK_TARGETS ${target}-lib)

add_executable(${target} "experimental-c-result/main.c")
set_target_properties(${target} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET "${visibility}"
CXX_VISIBILITY_PRESET "${visibility}"
DISABLE_PRECOMPILE_HEADERS On
)
target_link_libraries(${target} PRIVATE ${target}-lib outcome::hl)
list(APPEND outcome_LINK_TARGETS ${target})
endforeach()
endforeach()
35 changes: 35 additions & 0 deletions test/link/experimental-c-result/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Link testing for outcomes
(C) 2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
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 in the accompanying file
Licence.txt or 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.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "lib.h"

#include <errno.h>

mylib_result test_function(int x)
{
if(x < 0)
{
return make_failure_result(EINVAL);
}
return make_success_result(x);
}
26 changes: 26 additions & 0 deletions test/link/experimental-c-result/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Link testing for outcomes
(C) 2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
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 in the accompanying file
Licence.txt or 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.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

// This should be all that is necessary to create the C++ runtime
// side of things for the C code
#include "lib.h" // NOLINT
75 changes: 75 additions & 0 deletions test/link/experimental-c-result/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Link testing for outcomes
(C) 2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
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 in the accompanying file
Licence.txt or 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.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#ifndef OUTCOME_TEST_LINK_EXPERIMENTAL_C_RESULT_H
#define OUTCOME_TEST_LINK_EXPERIMENTAL_C_RESULT_H

#include <outcome/experimental/result.h>

#if MYLIB_SOURCE
#ifdef _MSC_VER
#define MYLIB_DECL __declspec(dllexport)
#else
#define MYLIB_DECL __attribute__((visibility("default")))
#endif
#else
#define MYLIB_DECL
#endif

CXX_DECLARE_RESULT_SYSTEM(mylib, intptr_t);

typedef CXX_RESULT_SYSTEM(mylib) mylib_result;

static int is_result_ok(mylib_result r)
{
return CXX_RESULT_HAS_VALUE(r);
}

static int is_result_failed(mylib_result r)
{
return CXX_RESULT_HAS_ERROR(r);
}

static mylib_result make_success_result(intptr_t v)
{
return CXX_MAKE_RESULT_SYSTEM_SUCCESS(mylib, v);
}

static mylib_result make_failure_result(int v)
{
return CXX_MAKE_RESULT_SYSTEM_FAILURE_SYSTEM(mylib, v);
}

static int is_result_equivalent(mylib_result r, int errcode)
{
return outcome_status_code_equal_generic(&r, errcode);
}

static const char *result_failure_message(mylib_result r)
{
return outcome_status_code_message(&r);
}

extern MYLIB_DECL mylib_result test_function(int x);

#endif
57 changes: 57 additions & 0 deletions test/link/experimental-c-result/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Link testing for outcomes
(C) 2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
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 in the accompanying file
Licence.txt or 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.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "lib.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
mylib_result r = test_function(5);
if(is_result_failed(r))
{
fprintf(stderr, "FATAL: test_function(5) failed with '%s'\n", result_failure_message(r));
abort();
}
if(r.value != 5)
{
fprintf(stderr, "FATAL: test_function(5) did not return 5\n");
abort();
}
r = test_function(-5);
if(!is_result_failed(r))
{
fprintf(stderr, "FATAL: test_function(-5) did not fail\n");
abort();
}
const char *msg = result_failure_message(r);
printf("test_function(-5) should fail with 'Invalid argument'\nIt failed with '%s'\n", msg);
if(0 != strcmp(msg, "Invalid argument"))
{
abort();
}
return 0;
}

0 comments on commit 355f247

Please sign in to comment.