This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <string> | ||
|
||
int fromC_val(std::string); | ||
int fromC_ref(const std::string&); | ||
|
||
int sumOfElements_ref(const std::string& str) | ||
{ | ||
int r = 0; | ||
for (size_t i = 0; i < str.size(); ++i) | ||
r += str[i]; | ||
return r; | ||
} | ||
|
||
int sumOfElements_val(std::string str) | ||
{ | ||
return sumOfElements_ref(str) + fromC_ref(str) + fromC_val(str); | ||
} |
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,56 @@ | ||
module test.stdcpp.string; | ||
|
||
import core.stdcpp.string; | ||
|
||
extern (C++) int test_string() | ||
{ | ||
std_string str = std_string("Hello"); | ||
|
||
assert(str.size == 5); | ||
assert(str.length == 5); | ||
assert(str.empty == false); | ||
|
||
assert(sumOfElements_val(str) == 1500); | ||
assert(sumOfElements_ref(str) == 500); | ||
|
||
std_string str2 = std_string(Default); | ||
assert(str2.size == 0); | ||
assert(str2.length == 0); | ||
assert(str2.empty == true); | ||
assert(str2[] == []); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
extern(C++): | ||
|
||
// test the ABI for calls to C++ | ||
int sumOfElements_val(std_string); | ||
int sumOfElements_ref(ref const(std_string)); | ||
|
||
// test the ABI for calls from C++ | ||
int fromC_val(std_string str) | ||
{ | ||
assert(str[] == "Hello"); | ||
assert(str.front == 'H'); | ||
assert(str.back == 'o'); | ||
assert(str.at(2) == 'l'); | ||
|
||
// str.fill(2); | ||
|
||
int r; | ||
foreach (e; str) | ||
r += e; | ||
|
||
assert(r == 500); | ||
return r; | ||
} | ||
|
||
int fromC_ref(ref const(std_string) str) | ||
{ | ||
int r; | ||
foreach (e; str) | ||
r += e; | ||
return r; | ||
} |