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

Commit 6367fc1

Browse files
committed
Build and tests
1 parent cf0ac6c commit 6367fc1

File tree

4 files changed

+152
-2
lines changed

4 files changed

+152
-2
lines changed

setmscver.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ if exist dflags.txt del /q dflags.txt
99
if exist add_tests.txt del /q add_tests.txt
1010
if %_MSC_VER% GTR 1900 echo /std:c++17 > cflags.txt
1111
if %_MSC_VER% GTR 1900 echo -extern-std=c++17 > dflags.txt
12-
if %_MSC_VER% GTR 1900 echo string_view > add_tests.txt
12+
if %_MSC_VER% GTR 1900 echo optional string_view > add_tests.txt
1313
del ver.c ver_raw.txt

test/stdcpp/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ HASCPP17:=`echo wow | $(CXX) -std=c++17 -E -xc++ - > /dev/null 2>&1 && echo yes`
44

55
TESTS:=allocator new
66
TESTS11:=array
7-
TESTS17:=string_view
7+
TESTS17:=optional string_view
88
OLDABITESTS:=
99

1010
ifeq (osx,$(OS))

test/stdcpp/src/optional.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <optional>
2+
3+
extern int opt_refCount;
4+
5+
struct Complex
6+
{
7+
bool valid = false;
8+
9+
int buffer[16] = { 10 };
10+
11+
Complex() = delete;
12+
Complex(const Complex& rh)
13+
{
14+
valid = rh.valid;
15+
if (rh.valid)
16+
{
17+
++opt_refCount;
18+
for (int i = 0; i < 16; ++i)
19+
buffer[i] = rh.buffer[i];
20+
}
21+
}
22+
~Complex()
23+
{
24+
if (valid)
25+
--opt_refCount;
26+
}
27+
};
28+
29+
int fromC_val(bool, std::optional<int>, const std::optional<int>&,
30+
std::optional<void*>, const std::optional<void*>&,
31+
std::optional<Complex>, const std::optional<Complex>&);
32+
33+
int callC_val(bool set, std::optional<int> a1, const std::optional<int>& a2,
34+
std::optional<void*> a3, const std::optional<void*>& a4,
35+
std::optional<Complex> a5, const std::optional<Complex>& a6)
36+
{
37+
if (set)
38+
{
39+
if (!a1 || a1.value() != 10) return 1;
40+
if (!a2 || a2.value() != 10) return 1;
41+
if (!a3 || a3.value() != (void*)0x1234) return 1;
42+
if (!a4 || a4.value() != (void*)0x1234) return 1;
43+
if (!a5 || a5.value().buffer[0] != 20 || a5.value().buffer[15] != 20) return 1;
44+
if (!a6 || a6.value().buffer[0] != 20 || a6.value().buffer[15] != 20) return 1;
45+
}
46+
else
47+
{
48+
if (a1 || a2 || a3 || a4 || a5 || a6)
49+
return 1;
50+
}
51+
52+
return fromC_val(set, a1, a2, a3, a4, a5, a6);
53+
}

test/stdcpp/src/optional_test.d

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import core.stdcpp.optional;
2+
3+
unittest
4+
{
5+
optional!int o1;
6+
optional!int o2 = nullopt;
7+
auto o3 = optional!int(in_place, 10);
8+
assert(!o1 && o2.has_value == false && o3 && o3.value == 10);
9+
o1 = 20;
10+
assert(o1 && o1.value == 20);
11+
o1 = nullopt;
12+
assert(!o1);
13+
int temp = 30;
14+
assert(o1.value_or(temp) == 30);
15+
16+
optional!(void*) o4;
17+
auto o5 = optional!(void*)(in_place, cast(void*)0x1234);
18+
assert(!o4 && o5 && o5.value == cast(void*)0x1234);
19+
o4 = o5;
20+
o5 = null;
21+
assert(o5.value == null);
22+
o5 = o4;
23+
o5.reset();
24+
assert(!o5);
25+
26+
{
27+
optional!Complex o6;
28+
auto o7 = optional!Complex(in_place, Complex(20));
29+
assert(!o6 && o7 && o7.value.buffer[0] == 20 && o7.value.buffer[$-1] == 20);
30+
optional!Complex o8 = o6;
31+
assert(!o8);
32+
optional!Complex o9 = o7;
33+
assert(o9 && o9.value.buffer[0] == 20 && o9.value.buffer[$-1] == 20);
34+
o9 = o6;
35+
assert(!o9);
36+
o6 = o7;
37+
assert(o6 && o6.value.buffer[0] == 20 && o6.value.buffer[$-1] == 20);
38+
o7.reset();
39+
assert(!o7);
40+
41+
assert(callC_val(false, o1, o1, o5, o5, o7, o7) == 0);
42+
assert(callC_val(true, o3, o3, o4, o4, o6, o6) == 0);
43+
}
44+
assert(opt_refCount == 0);
45+
}
46+
47+
extern(C++):
48+
49+
__gshared int opt_refCount = 0;
50+
51+
struct Complex
52+
{
53+
bool valid = false;
54+
int[16] buffer;
55+
this(int val)
56+
{
57+
valid = true;
58+
buffer[] = val;
59+
++opt_refCount;
60+
}
61+
this(ref inout(Complex) rhs) inout
62+
{
63+
valid = rhs.valid;
64+
if (rhs.valid)
65+
{
66+
buffer[] = rhs.buffer[]; ++opt_refCount;
67+
}
68+
}
69+
~this()
70+
{
71+
if (valid)
72+
--opt_refCount;
73+
}
74+
}
75+
76+
int callC_val(bool, optional!int, ref const(optional!int), optional!(void*), ref const(optional!(void*)), optional!Complex, ref const(optional!Complex));
77+
78+
int fromC_val(bool set, optional!int a1, ref const(optional!int) a2,
79+
optional!(void*) a3, ref const(optional!(void*)) a4,
80+
optional!Complex a5, ref const(optional!Complex) a6)
81+
{
82+
if (set)
83+
{
84+
assert(a1 && a1.value == 10);
85+
assert(a2 && a2.value == 10);
86+
assert(a3 && a3.value == cast(void*)0x1234);
87+
assert(a4 && a4.value == cast(void*)0x1234);
88+
assert(a5 && a5.value.buffer[0] == 20 && a5.value.buffer[$-1] == 20);
89+
assert(a6 && a6.value.buffer[0] == 20 && a6.value.buffer[$-1] == 20);
90+
}
91+
else
92+
{
93+
assert(!a1 && !a2 && !a3 && !a4 && !a5 && !a6);
94+
}
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)