|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Profile Recorder open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift Profile Recorder project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift Profile Recorder project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import XCTest |
| 16 | +import ProfileRecorderSampleConversion |
| 17 | + |
| 18 | +final class SampleConversionTests: XCTestCase { |
| 19 | + func testBasicExample() { |
| 20 | + let line = #"{"ip": "0x18fe24c08", "sp": "0x1702a6fe0"}"#[...] |
| 21 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 22 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1_8fe2_4c08, stackPointer: 0), actual) |
| 23 | + } |
| 24 | + |
| 25 | + func testZero() { |
| 26 | + let line = #"{"ip": "0x0", "sp": "0x1702a6fe0"}"#[...] |
| 27 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 28 | + XCTAssertEqual(StackFrame(instructionPointer: 0x0, stackPointer: 0), actual) |
| 29 | + } |
| 30 | + |
| 31 | + func testNoSpaces() { |
| 32 | + let line = #"{"ip":"0x18fe24c08","sp":"0x1702a6fe0"}"#[...] |
| 33 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 34 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1_8fe2_4c08, stackPointer: 0), actual) |
| 35 | + } |
| 36 | + |
| 37 | + func testOrder() { |
| 38 | + let line = #"{"sp":"0x1702a6fe0","ip":"0x18fe24c08"}"#[...] |
| 39 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 40 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1_8fe2_4c08, stackPointer: 0), actual) |
| 41 | + } |
| 42 | + |
| 43 | + func testExtraFieldsOrder() { |
| 44 | + let line = #"{"sp":"0x1702a6fe0","unknown-field":"hello","ip":"0x18fe24c08"}"#[...] |
| 45 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 46 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1_8fe2_4c08, stackPointer: 0), actual) |
| 47 | + } |
| 48 | + |
| 49 | + func testSimpleCase() { |
| 50 | + let line = #"{"ip":"0x18fe24c08","sp":"0x1702a6fe0"}"#[...] |
| 51 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 52 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1_8fe2_4c08, stackPointer: 0), actual) |
| 53 | + } |
| 54 | + |
| 55 | + func testIpFirst() { |
| 56 | + let line = #"{"ip":"0xABCDEF","sp":"0x1702a6fe0"}"#[...] |
| 57 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 58 | + XCTAssertEqual(StackFrame(instructionPointer: 0xABCDEF, stackPointer: 0), actual) |
| 59 | + } |
| 60 | + |
| 61 | + func testIpLast() { |
| 62 | + let line = #"{"sp":"0x1234","thread":"42","flags":"0x9","ip":"0xCAFEBABE"}"#[...] |
| 63 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 64 | + XCTAssertEqual(StackFrame(instructionPointer: 0xCAFE_BABE, stackPointer: 0), actual) |
| 65 | + } |
| 66 | + |
| 67 | + func testIpWithSpaces() { |
| 68 | + let line = #" { "ip" : "0xDEAD10CC" , "sp":"0x1702a6fe0" } "#[...] |
| 69 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 70 | + XCTAssertEqual(StackFrame(instructionPointer: 0xDEAD_10CC, stackPointer: 0), actual) |
| 71 | + } |
| 72 | + |
| 73 | + func testIpMixedFields() { |
| 74 | + let line = #"{"foo":1,"bar":true,"ip":"0x12345678","baz":[1,2,3]}"#[...] |
| 75 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 76 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1234_5678, stackPointer: 0), actual) |
| 77 | + } |
| 78 | + |
| 79 | + func testIpEmbeddedInText() { |
| 80 | + let line = #"{"message":"before","ip":"0xFEEDFACE","after":"something"}"#[...] |
| 81 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 82 | + XCTAssertEqual(StackFrame(instructionPointer: 0xFEED_FACE, stackPointer: 0), actual) |
| 83 | + } |
| 84 | + |
| 85 | + func testIpUpperLowerMix() { |
| 86 | + let line = #"{"ip":"0xDeadBeef"}"#[...] |
| 87 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 88 | + XCTAssertEqual(StackFrame(instructionPointer: 0xDEAD_BEEF, stackPointer: 0), actual) |
| 89 | + } |
| 90 | + |
| 91 | + func testIpOnly() { |
| 92 | + let line = #"{"ip":"0x11111111"}"#[...] |
| 93 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 94 | + XCTAssertEqual(StackFrame(instructionPointer: 0x1111_1111, stackPointer: 0), actual) |
| 95 | + } |
| 96 | + |
| 97 | + func testIPWithEscapedString() { |
| 98 | + let line = #"{"comment":"the ip is \"secret\"","ip":"0xFACEB00C"}"#[...] |
| 99 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 100 | + XCTAssertEqual(StackFrame(instructionPointer: 0xFACE_B00C, stackPointer: 0), actual) |
| 101 | + } |
| 102 | + |
| 103 | + func testNoIPAtAll() { |
| 104 | + let line = #"{"comment":"the ip is \"secret\""}"#[...] |
| 105 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 106 | + XCTAssertEqual(nil, actual) |
| 107 | + } |
| 108 | + |
| 109 | + func testPartialIP() { |
| 110 | + let line = #"{"comment":"the ip is \"secret\"", "ip": "0x123"#[...] |
| 111 | + let actual = line.utf8.attemptFastParseStackFrame() |
| 112 | + XCTAssertEqual(nil, actual) |
| 113 | + } |
| 114 | +} |
0 commit comments