File: Duration.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (57 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

// Int128 operations are not supported on 32bit platforms, 128-bit types are not
// provided by the 32-bit LLVM. See `dividingFullWidth` in IntegerTypes.swift.gyb
// UNSUPPORTED: PTRSIZE=32

// These test a codepath that was fixed in the Swift 5.9 stdlib, so it will
// fail if run against earlier standard library versions.
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

import StdlibUnittest

var suite = TestSuite("StringIndexTests")
defer { runAllTests() }

if #available(SwiftStdlib 5.7, *) {
  suite.test("seconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.seconds(integerValue).components
      expectEqual(sec, Int64(integerValue))
      expectEqual(attosec, 0)
    }
    let quarterSecond = Duration.seconds(0.25)
    expectEqual(quarterSecond.components, (0, 250_000_000_000_000_000))
    // Value that overflows conversion from Double -> Int64, but should be
    // representable as a number of seconds:
    let huge: Double = 1.7e20
    let duration = Duration.seconds(huge)
    // Divide by 1000 to get back to a duration with representable components:
    let smallerDuration = duration / 1000
    expectEqual(smallerDuration.components, (170_000_000_000_000_000, 0))
    // Now check that the components of the original value trap:
    expectCrashLater()
    let _ = duration.components
  }
  
  suite.test("milliseconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.milliseconds(integerValue).components
      expectEqual(sec, Int64(integerValue) / 1000)
      expectEqual(attosec, Int64(integerValue) % 1000 * 1_000_000_000_000_000)
    }
  }
  
  suite.test("microseconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.microseconds(integerValue).components
      expectEqual(sec, Int64(integerValue) / 1_000_000)
      expectEqual(attosec, Int64(integerValue) % 1_000_000 * 1_000_000_000_000)
    }
  }
}