Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed Sep 24, 2018
1 parent 4698d42 commit e5bcd61
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/stdcpp/src/string.cpp
@@ -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);
}
56 changes: 56 additions & 0 deletions test/stdcpp/src/string.d
@@ -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;
}

0 comments on commit e5bcd61

Please sign in to comment.