Files
swift-mirror/test/Interop/SwiftToCxx/stdlib/array/array-execution.cpp
Alex Lorenz 13a63f4b05 [interop][SwiftToCxx] incorporate argument labels into Stdlib API names in C++
This allows easier use and more wider API exposure from the Swift stdlib

In the future, all functions and method should incorporate argument labels, but it's not yet clear how
2023-03-03 14:15:48 -08:00

65 lines
1.7 KiB
C++

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -typecheck %t/use-array.swift -typecheck -module-name UseArray -enable-experimental-cxx-interop -emit-clang-header-path %t/UseArray.h
// RUN: %target-interop-build-clangxx -fno-exceptions -std=gnu++20 -c %t/array-execution.cpp -I %t -o %t/swift-stdlib-execution.o
// RUN: %target-build-swift %t/use-array.swift -o %t/swift-stdlib-execution -Xlinker %t/swift-stdlib-execution.o -module-name UseArray -Xfrontend -entry-point-function-name -Xfrontend swiftMain
// RUN: %target-codesign %t/swift-stdlib-execution
// RUN: %target-run %t/swift-stdlib-execution | %FileCheck %s
// REQUIRES: executable_test
//--- use-array.swift
@_expose(Cxx)
public func createArray(_ val: CInt) -> [CInt] {
return [val, val]
}
@_expose(Cxx)
public func passthroughArray(_ val: [CInt]) -> Array<CInt> {
return val
}
@_expose(Cxx)
public func printArray(_ val: Array<CInt>) {
print(val)
}
//--- array-execution.cpp
#include <cassert>
#include "UseArray.h"
int main() {
using namespace Swift;
{
Array<int> val = UseArray::createArray(2);
UseArray::printArray(UseArray::passthroughArray(val));
int count = 2;
for (int x: val) {
assert(x == 2);
--count;
}
assert(count == 0);
}
// CHECK: [2, 2]
{
auto val = Array<int>::init();
val.append(-11);
UseArray::printArray(val);
assert(val.getCount() == 1);
assert(val.getCapacity() >= 1);
auto zeroInt = val[0];
assert(zeroInt == -11);
auto firstInt = val.removeAt(0);
assert(firstInt == -11);
assert(val.getCount() == 0);
UseArray::printArray(val);
}
// CHECK-NEXT: [-11]
// CHECK-NEXT: []
return 0;
}