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

Commit

Permalink
Build and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed Sep 1, 2019
1 parent f60f2dc commit 1d44aa3
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setmscver.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ if exist dflags.txt del /q dflags.txt
if exist add_tests.txt del /q add_tests.txt
if %_MSC_VER% GTR 1900 echo /std:c++17 > cflags.txt
if %_MSC_VER% GTR 1900 echo -extern-std=c++17 > dflags.txt
if %_MSC_VER% GTR 1900 echo string_view > add_tests.txt
if %_MSC_VER% GTR 1900 echo optional string_view > add_tests.txt
del ver.c ver_raw.txt
2 changes: 1 addition & 1 deletion test/stdcpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ HASCPP17:=`echo wow | $(CXX) -std=c++17 -E -xc++ - > /dev/null 2>&1 && echo yes`

TESTS:=allocator new
TESTS11:=array
TESTS17:=string_view
TESTS17:=optional string_view
OLDABITESTS:=

ifeq (osx,$(OS))
Expand Down
53 changes: 53 additions & 0 deletions test/stdcpp/src/optional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <optional>

extern int opt_refCount;

struct Complex
{
bool valid = false;

int buffer[16] = { 10 };

Complex() = delete;
Complex(const Complex& rh)
{
valid = rh.valid;
if (rh.valid)
{
++opt_refCount;
for (int i = 0; i < 16; ++i)
buffer[i] = rh.buffer[i];
}
}
~Complex()
{
if (valid)
--opt_refCount;
}
};

int fromC_val(bool, std::optional<int>, const std::optional<int>&,
std::optional<void*>, const std::optional<void*>&,
std::optional<Complex>, const std::optional<Complex>&);

int callC_val(bool set, std::optional<int> a1, const std::optional<int>& a2,
std::optional<void*> a3, const std::optional<void*>& a4,
std::optional<Complex> a5, const std::optional<Complex>& a6)
{
if (set)
{
if (!a1 || a1.value() != 10) return 1;
if (!a2 || a2.value() != 10) return 1;
if (!a3 || a3.value() != (void*)0x1234) return 1;
if (!a4 || a4.value() != (void*)0x1234) return 1;
if (!a5 || a5.value().buffer[0] != 20 || a5.value().buffer[15] != 20) return 1;
if (!a6 || a6.value().buffer[0] != 20 || a6.value().buffer[15] != 20) return 1;
}
else
{
if (a1 || a2 || a3 || a4 || a5 || a6)
return 1;
}

return fromC_val(set, a1, a2, a3, a4, a5, a6);
}
97 changes: 97 additions & 0 deletions test/stdcpp/src/optional_test.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import core.stdcpp.optional;

unittest
{
optional!int o1;
optional!int o2 = nullopt;
auto o3 = optional!int(in_place, 10);
assert(!o1 && o2.has_value == false && o3 && o3.value == 10);
o1 = 20;
assert(o1 && o1.value == 20);
o1 = nullopt;
assert(!o1);
int temp = 30;
assert(o1.value_or(temp) == 30);

optional!(void*) o4;
auto o5 = optional!(void*)(in_place, cast(void*)0x1234);
assert(!o4 && o5 && o5.value == cast(void*)0x1234);
o4 = o5;
o5 = null;
assert(o5.value == null);
o5 = o4;
o5.reset();
assert(!o5);

{
optional!Complex o6;
auto o7 = optional!Complex(in_place, Complex(20));
assert(!o6 && o7 && o7.value.buffer[0] == 20 && o7.value.buffer[$-1] == 20);
optional!Complex o8 = o6;
assert(!o8);
optional!Complex o9 = o7;
assert(o9 && o9.value.buffer[0] == 20 && o9.value.buffer[$-1] == 20);
o9 = o6;
assert(!o9);
o6 = o7;
assert(o6 && o6.value.buffer[0] == 20 && o6.value.buffer[$-1] == 20);
o7.reset();
assert(!o7);

assert(callC_val(false, o1, o1, o5, o5, o7, o7) == 0);
assert(callC_val(true, o3, o3, o4, o4, o6, o6) == 0);
}
assert(opt_refCount == 0);
}

extern(C++):

__gshared int opt_refCount = 0;

struct Complex
{
bool valid = false;
int[16] buffer;
this(int val)
{
valid = true;
buffer[] = val;
++opt_refCount;
}
this(ref inout(Complex) rhs) inout
{
valid = rhs.valid;
if (rhs.valid)
{
buffer[] = rhs.buffer[]; ++opt_refCount;
}
}
~this()
{
if (valid)
--opt_refCount;
}
}

int callC_val(bool, optional!int, ref const(optional!int), optional!(void*), ref const(optional!(void*)), optional!Complex, ref const(optional!Complex));

int fromC_val(bool set, optional!int a1, ref const(optional!int) a2,
optional!(void*) a3, ref const(optional!(void*)) a4,
optional!Complex a5, ref const(optional!Complex) a6)
{
if (set)
{
assert(a1 && a1.value == 10);
assert(a2 && a2.value == 10);
assert(a3 && a3.value == cast(void*)0x1234);
assert(a4 && a4.value == cast(void*)0x1234);
assert(a5 && a5.value.buffer[0] == 20 && a5.value.buffer[$-1] == 20);
assert(a6 && a6.value.buffer[0] == 20 && a6.value.buffer[$-1] == 20);
}
else
{
assert(!a1 && !a2 && !a3 && !a4 && !a5 && !a6);
}

return 0;
}

0 comments on commit 1d44aa3

Please sign in to comment.