Skip to content

Commit 823e809

Browse files
authored
Merge pull request #74426 from apple/susmonteiro/cxx-span
[cxx-interop] Add `std::span` tests
2 parents ce6bac4 + 76feea8 commit 823e809

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

test/Interop/Cxx/stdlib/Inputs/module.modulemap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ module StdVector {
44
export *
55
}
66

7+
module StdSpan {
8+
header "std-span.h"
9+
requires cplusplus
10+
export *
11+
}
12+
713
module StdMap {
814
header "std-map.h"
915
requires cplusplus
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_SPAN_H
2+
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_SPAN_H
3+
4+
#include <cstddef>
5+
#include <string>
6+
#include <span>
7+
8+
using Span = std::span<const int>;
9+
using SpanOfString = std::span<const std::string>;
10+
11+
static int iarray[]{1, 2, 3};
12+
static std::string sarray[]{"", "ab", "abc"};
13+
static Span ispan = {iarray};
14+
static SpanOfString sspan = {sarray};
15+
16+
inline Span initSpan() {
17+
const int a[]{1, 2, 3};
18+
return Span(a);
19+
}
20+
21+
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_SPAN_H
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20)
2+
3+
// FIXME swift-ci linux tests do not support std::span
4+
// UNSUPPORTED: OS=linux-gnu
5+
6+
import StdlibUnittest
7+
#if !BRIDGING_HEADER
8+
import StdSpan
9+
#endif
10+
import CxxStdlib
11+
12+
var StdSpanTestSuite = TestSuite("StdSpan")
13+
14+
StdSpanTestSuite.test("EmptySpan") {
15+
let s = Span()
16+
expectEqual(s.size(), 0)
17+
expectTrue(s.empty())
18+
}
19+
20+
StdSpanTestSuite.test("InitSpan") {
21+
let s = initSpan()
22+
expectEqual(s.size(), 3)
23+
expectFalse(s.empty())
24+
}
25+
26+
StdSpanTestSuite.test("InitStaticSpan") {
27+
expectEqual(ispan.size(), 3)
28+
expectFalse(ispan.empty())
29+
30+
expectEqual(ispan[0], 1)
31+
expectEqual(ispan[1], 2)
32+
expectEqual(ispan[2], 3)
33+
}
34+
35+
StdSpanTestSuite.test("InitStringSpan") {
36+
expectEqual(sspan.size(), 3)
37+
expectFalse(sspan.empty())
38+
39+
expectEqual(sspan[0], "")
40+
expectEqual(sspan[1], "ab")
41+
expectEqual(sspan[2], "abc")
42+
}
43+
44+
runAllTests()

0 commit comments

Comments
 (0)