Skip to content

Commit c35c481

Browse files
authored
Add OGGraphTracing API (#127)
* Add OGGraphTracing API * Remove openGraphSPICompatibilityTestTarget
1 parent 896aef9 commit c35c481

File tree

13 files changed

+259
-287
lines changed

13 files changed

+259
-287
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,6 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
196196
cSettings: sharedCSettings,
197197
swiftSettings: sharedSwiftSettings
198198
)
199-
let openGraphSPICompatibilityTestTarget = Target.testTarget(
200-
name: "OpenGraph_SPICompatibilityTests",
201-
dependencies: [
202-
.product(name: "Numerics", package: "swift-numerics"),
203-
],
204-
exclude: ["README.md"],
205-
cSettings: sharedCSettings,
206-
swiftSettings: sharedSwiftSettings
207-
)
208199

209200
// MARK: - Package
210201

@@ -226,7 +217,6 @@ let package = Package(
226217
openGraphSPITestTarget,
227218
openGraphShimsTestTarget,
228219
openGraphCompatibilityTestTarget,
229-
openGraphSPICompatibilityTestTarget,
230220
],
231221
cxxLanguageStandard: .cxx20
232222
)
@@ -278,10 +268,8 @@ if attributeGraphCondition {
278268
let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST")
279269
if compatibilityTestCondition && attributeGraphCondition {
280270
openGraphCompatibilityTestTarget.addCompatibilitySettings()
281-
openGraphSPICompatibilityTestTarget.addCompatibilitySettings()
282271
} else {
283272
openGraphCompatibilityTestTarget.dependencies.append("OpenGraph")
284-
openGraphSPICompatibilityTestTarget.dependencies.append("OpenGraph")
285273
}
286274

287275
extension [Platform] {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// OGGraphTracing.mm
3+
// OpenGraph_SPI
4+
5+
#include "OGGraphTracing.h"
6+
7+
void OGGraphStartTracing(_Nullable OGGraphRef graph, OGGraphTraceFlags options) {
8+
OGGraphStartTracing2(graph, options, NULL);
9+
}
10+
11+
void OGGraphStartTracing2(_Nullable OGGraphRef graph, OGGraphTraceFlags options, _Nullable CFArrayRef array) {
12+
// TODO
13+
}
14+
15+
void OGGraphStopTracing(_Nullable OGGraphRef graph) {
16+
// TODO
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// OGGraphTracing.h
3+
// OpenGraph_SPI
4+
5+
#ifndef OGGraphTracing_hpp
6+
#define OGGraphTracing_hpp
7+
8+
#include "OGBase.h"
9+
#include "OGGraph.h"
10+
11+
typedef OG_OPTIONS(uint32_t, OGGraphTraceFlags) {
12+
OGGraphTraceFlags_0 = 0,
13+
OGGraphTraceFlags_1 = 1 << 0,
14+
OGGraphTraceFlags_2 = 1 << 1,
15+
} OG_SWIFT_NAME(OGGraphRef.TraceFlags);
16+
17+
OG_ASSUME_NONNULL_BEGIN
18+
19+
OG_EXTERN_C_BEGIN
20+
21+
OG_EXPORT
22+
OG_REFINED_FOR_SWIFT
23+
void OGGraphStartTracing(_Nullable OGGraphRef graph, OGGraphTraceFlags options) OG_SWIFT_NAME(OGGraphRef.startTracing(_:options:));
24+
25+
OG_EXPORT
26+
OG_REFINED_FOR_SWIFT
27+
void OGGraphStartTracing2(_Nullable OGGraphRef graph, OGGraphTraceFlags options, _Nullable CFArrayRef array);
28+
29+
OG_EXPORT
30+
OG_REFINED_FOR_SWIFT
31+
void OGGraphStopTracing(_Nullable OGGraphRef graph) OG_SWIFT_NAME(OGGraphRef.stopTracing(_:));
32+
33+
OG_EXTERN_C_END
34+
35+
OG_ASSUME_NONNULL_END
36+
37+
#endif /* OGGraphTracing_hpp */

Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "OGDebugServer.h"
1313
#include "OGGraph.h"
1414
#include "OGGraphContext.h"
15+
#include "OGGraphTracing.h"
1516
#include "OGInputOptions.h"
1617
#include "OGSearchOptions.h"
1718
#include "OGSubgraph.h"

Tests/OpenGraphCompatibilityTests/Graph/GraphTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//
22
// GraphTests.swift
3-
//
4-
//
5-
//
3+
// OpenGraphCompatibilityTests
64

75
import Testing
86
import Foundation
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// GraphTracingTests.swift
3+
// OpenGraphCompatibilityTests
4+
5+
import Testing
6+
7+
struct GraphTracingTests {
8+
@Test
9+
func tracing() {
10+
let graph = Graph()
11+
Graph.startTracing(graph, options: [])
12+
Graph.stopTracing(graph)
13+
}
14+
15+
@Test
16+
func tracingAll() {
17+
Graph.startTracing(nil, options: [])
18+
Graph.stopTracing(nil)
19+
}
20+
21+
@Test
22+
func options() {
23+
let option = Graph.TraceFlags(rawValue: 1)
24+
#expect(option == ._1)
25+
}
26+
}

Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,153 @@
44

55
import Testing
66

7+
@Suite(.enabled(if: swiftToolchainSupported))
78
struct MetadataTests {
89
class T1 {
910
var a = 0
1011
var b: Double = 0
1112
}
12-
13+
1314
struct T2 {
1415
var a: Int
1516
var b: Double
1617
}
17-
18+
1819
enum T3 {
1920
case a, b
2021
}
21-
22+
23+
enum T4: Equatable {
24+
case none
25+
case empty
26+
case int(Int)
27+
case double(Double)
28+
}
29+
2230
protocol P {
2331
var a: Int { get }
2432
var b: Double { get }
2533
}
26-
34+
35+
@Test
36+
func kind() {
37+
#expect(Metadata(T1.self).kind == .class)
38+
#expect(Metadata(T2.self).kind == .struct)
39+
#expect(Metadata(T3.self).kind == .enum)
40+
41+
#expect(Metadata(Void?.self).kind == .optional)
42+
#expect(Metadata(Int?.self).kind == .optional)
43+
#expect(Metadata(T1?.self).kind == .optional)
44+
#expect(Metadata((T1, T2)?.self).kind == .optional)
45+
46+
#expect(Metadata(Void.self).kind == .tuple)
47+
#expect(Metadata((Int, Double?).self).kind == .tuple)
48+
#expect(Metadata((T1, T2, T3).self).kind == .tuple)
49+
50+
#expect(Metadata((() -> Void).self).kind == .function)
51+
52+
#expect(Metadata(P.self).kind == .existential)
53+
#expect(Metadata((any P).self).kind == .existential)
54+
55+
#expect(Metadata(P.Protocol.self).kind == .metatype)
56+
#expect(Metadata(type(of: Int.self)).kind == .metatype)
57+
}
58+
59+
@Test
60+
func enumType() {
61+
var none = T4.none
62+
var empty = T4.empty
63+
var int = T4.int(1)
64+
var double = T4.double(2.0)
65+
let metadata = Metadata(T4.self)
66+
67+
withUnsafePointer(to: none) { #expect(metadata.enumTag($0) == 2) }
68+
withUnsafePointer(to: empty) { #expect(metadata.enumTag($0) == 3) }
69+
withUnsafePointer(to: int) { #expect(metadata.enumTag($0) == 0) }
70+
withUnsafePointer(to: double) { #expect(metadata.enumTag($0) == 1) }
71+
72+
withUnsafeMutablePointer(to: &int) {
73+
metadata.projectEnumData($0)
74+
#expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Int.self).pointee == 1)
75+
}
76+
withUnsafeMutablePointer(to: &double) {
77+
metadata.projectEnumData($0)
78+
#expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Double.self).pointee.isApproximatelyEqual(to: 2.0))
79+
}
80+
withUnsafeMutablePointer(to: &none) {
81+
#expect($0.pointee == .none)
82+
metadata.injectEnumTag(tag: 3, $0)
83+
#expect($0.pointee == .empty)
84+
}
85+
withUnsafeMutablePointer(to: &empty) {
86+
#expect($0.pointee == .empty)
87+
metadata.injectEnumTag(tag: 2, $0)
88+
#expect($0.pointee == .none)
89+
}
90+
withUnsafeMutablePointer(to: &int) {
91+
#expect($0.pointee == .int(1))
92+
93+
metadata.injectEnumTag(tag: 1, $0)
94+
#expect(metadata.enumTag($0) == 1)
95+
#expect($0.pointee == .double(Double(bitPattern: 1)))
96+
97+
metadata.injectEnumTag(tag: 2, $0)
98+
#expect($0.pointee == .none)
99+
100+
metadata.injectEnumTag(tag: 3, $0)
101+
#expect($0.pointee == .empty)
102+
}
103+
}
104+
105+
#if OPENGRAPH_SUPPORT_2024_API
106+
func descriptor() {
107+
let t1 = Metadata(T1.self).descriptor
108+
let t2 = Metadata(T2.self).descriptor
109+
let t3 = Metadata(T3.self).descriptor
110+
let p = Metadata(P.self).descriptor
111+
let optionalP = Metadata(P?.self).descriptor
112+
113+
#expect(t1 != nil)
114+
#expect(t2 != nil)
115+
#expect(t3 != nil)
116+
117+
#expect(p == nil)
118+
#expect(optionalP != nil)
119+
120+
#expect(t1 == Metadata(T1.self).descriptor)
121+
}
122+
#endif
123+
124+
@Test
125+
func nominalDescriptor() {
126+
let t1 = Metadata(T1.self).nominalDescriptor
127+
let t2 = Metadata(T2.self).nominalDescriptor
128+
let t3 = Metadata(T3.self).nominalDescriptor
129+
let p = Metadata(P.self).nominalDescriptor
130+
let optionalP = Metadata(P?.self).nominalDescriptor
131+
132+
#expect(t1 == nil)
133+
#expect(t2 != nil)
134+
#expect(t3 != nil)
135+
#expect(p == nil)
136+
#expect(optionalP != nil)
137+
}
138+
139+
@Test
140+
func nominalDescriptorName() throws {
141+
let t1 = Metadata(T1.self).nominalDescriptorName
142+
let t2 = Metadata(T2.self).nominalDescriptorName
143+
let t3 = Metadata(T3.self).nominalDescriptorName
144+
let p = Metadata(P.self).nominalDescriptorName
145+
let optionalP = Metadata(P?.self).nominalDescriptorName
146+
147+
#expect(t1 == nil)
148+
try #expect(String(cString: #require(t2)) == "T2")
149+
try #expect(String(cString: #require(t3)) == "T3")
150+
#expect(p == nil)
151+
try #expect(String(cString: #require(optionalP)) == "Optional")
152+
}
153+
27154
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
28155
func description() {
29156
#expect(Metadata(T1.self).description == "MetadataTests.T1")

Tests/OpenGraphCompatibilityTests/Runtime/TupleTypeTests.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,57 @@ struct TupleTypeTests {
1010
var a = 0
1111
var b: Double = 0
1212
}
13-
13+
1414
struct T2 {
1515
var a: Int = 0
1616
var b: Double = 0
1717
}
18-
18+
1919
enum T3 {
2020
case a, b
2121
}
22-
22+
23+
@Test
24+
func newTupleType() {
25+
let elements: [Metadata] = [Metadata(T1.self), Metadata(T2.self), Metadata(T3.self)]
26+
let tupleType = TupleType(count: 3, elements: elements)
27+
#expect(tupleType.rawValue == Metadata((T1, T2, T3).self).rawValue)
28+
}
29+
30+
@Test
31+
func tupleCount() {
32+
#expect(TupleType(T1.self).count == 1)
33+
#expect(TupleType([T1.self, T2.self]).count == 2)
34+
#expect(TupleType([T1.self, T2.self, T3.self]).count == 3)
35+
}
36+
37+
@Test
38+
func tupleSize() {
39+
#expect(TupleType(T1.self).size == 8)
40+
#expect(TupleType([T1.self, T2.self]).size == 24)
41+
#expect(TupleType([T1.self, T2.self, T3.self]).size == 25)
42+
}
43+
44+
@Test
45+
func tupleElement() {
46+
let tupleType = TupleType([T1.self, T2.self, T3.self])
47+
#expect(tupleType.elementType(at: 0) == Metadata(T1.self))
48+
#expect(tupleType.elementType(at: 1) == Metadata(T2.self))
49+
#expect(tupleType.elementType(at: 2) == Metadata(T3.self))
50+
51+
#expect(tupleType.elementSize(at: 0) == 8)
52+
#expect(tupleType.elementSize(at: 1) == 16)
53+
#expect(tupleType.elementSize(at: 2) == 1)
54+
55+
#expect(tupleType.elementOffset(at: 0) == 0)
56+
#expect(tupleType.elementOffset(at: 1) == 8)
57+
#expect(tupleType.elementOffset(at: 2) == 24)
58+
59+
#expect(tupleType.elementOffset(at: 0, type: Metadata(T1.self)) == 0)
60+
#expect(tupleType.elementOffset(at: 1, type: Metadata(T2.self)) == 8)
61+
#expect(tupleType.elementOffset(at: 2, type: Metadata(T3.self)) == 24)
62+
}
63+
2364
@Test
2465
func initType() {
2566
#expect(TupleType(T1.self).rawValue == Metadata(T1.self).rawValue)

0 commit comments

Comments
 (0)