@@ -498,6 +498,47 @@ UnsafeRawBufferPointerTestSuite.test("load.after")
498
498
}
499
499
}
500
500
501
+ UnsafeRawBufferPointerTestSuite . test ( " load.aligned " ) {
502
+ var data : [ UInt8 ] = [ 0 , 0 , 0 , 0 , . max, . max, . max, . max]
503
+ data. withUnsafeBytes {
504
+ let x = $0. load ( fromByteOffset: 4 , as: UInt32 . self)
505
+ expectEqual ( x, . max)
506
+ }
507
+ data. withUnsafeMutableBytes {
508
+ let x = $0. load ( fromByteOffset: 0 , as: UInt32 . self)
509
+ expectEqual ( x, 0 )
510
+ }
511
+ }
512
+
513
+ UnsafeRawBufferPointerTestSuite . test ( " load.invalid " )
514
+ . skip ( . custom( { !_isDebugAssertConfiguration( ) } , // require debugAssert
515
+ reason: " This tests a debug precondition.. " ) )
516
+ . code {
517
+ let data : [ UInt8 ] = [ 0 , 0 , 0 , . max, . max, . max, . max, 0 ]
518
+ let i = data. firstIndex ( of: . max) !
519
+ expectCrashLater ( )
520
+ _ = data. withUnsafeBytes {
521
+ $0. load ( fromByteOffset: i, as: UInt32 . self)
522
+ }
523
+ }
524
+
525
+ UnsafeRawBufferPointerTestSuite . test ( " load.unaligned " )
526
+ . skip ( . custom( { // require SwiftStdlib 5.7
527
+ if #available( SwiftStdlib 5 . 7 , * ) { return false } else { return true }
528
+ } , reason: " Requires stdlib from Swift 5.7 " ) )
529
+ . code {
530
+ var data : [ UInt8 ] = [ 0 , 0 , 0 , . max, . max, . max, . max, 0 ]
531
+ let i = data. firstIndex ( of: . max) !
532
+ data. withUnsafeBytes {
533
+ let x = $0. loadUnaligned ( fromByteOffset: i, as: UInt32 . self)
534
+ expectEqual ( x, . max)
535
+ }
536
+ data. withUnsafeMutableBytes {
537
+ let x = $0. loadUnaligned ( fromByteOffset: i- 1 , as: UInt32 . self)
538
+ expectEqual ( UInt32 ( littleEndian: x) , 0xffffff00 )
539
+ }
540
+ }
541
+
501
542
UnsafeRawBufferPointerTestSuite . test ( " store.before " )
502
543
. skip ( . custom(
503
544
{ !_isDebugAssertConfiguration( ) } ,
@@ -521,6 +562,33 @@ UnsafeRawBufferPointerTestSuite.test("store.after")
521
562
}
522
563
}
523
564
565
+ UnsafeRawBufferPointerTestSuite . test ( " store.invalid " )
566
+ . skip ( . custom( { !_isDebugAssertConfiguration( ) } , // require debugAssert
567
+ reason: " This tests a debug precondition.. " ) )
568
+ . skip ( . custom( { // require SwiftStdlib 5.7
569
+ if #available( SwiftStdlib 5 . 7 , * ) { return false } else { return true }
570
+ } , reason: " Requires stdlib from Swift 5.7 " ) )
571
+ . code {
572
+ let t = " Text that is longer than fits in a small String. "
573
+ let p1 = UnsafeMutableRawPointer . allocate (
574
+ byteCount: MemoryLayout< String> . size,
575
+ alignment: MemoryLayout< String> . alignment
576
+ )
577
+ defer { p1. deallocate ( ) }
578
+ expectCrashLater ( )
579
+ p1. storeBytes ( of: t, as: String . self)
580
+ expectUnreachable ( )
581
+ }
582
+
583
+ UnsafeRawBufferPointerTestSuite . test ( " store.valid " ) {
584
+ let value32 = UInt32 . max
585
+ var value64 = Int64 . zero
586
+ withUnsafeMutableBytes ( of: & value64) {
587
+ $0. storeBytes ( of: value32, toByteOffset: MemoryLayout< UInt32> . stride, as: UInt32 . self)
588
+ }
589
+ expectEqual ( value64, 0xffffffff << 32 )
590
+ }
591
+
524
592
UnsafeRawBufferPointerTestSuite . test ( " copy.bytes.overflow " )
525
593
. skip ( . custom(
526
594
{ !_isDebugAssertConfiguration( ) } ,
0 commit comments