-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add C Result link test based on Ken's description of link failure.
- Loading branch information
Showing
8 changed files
with
258 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |