VLCTimeTest.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*****************************************************************************
  2. * VLCTimeTest.swift
  3. *****************************************************************************
  4. * Copyright (C) 2018 VLC authors and VideoLAN
  5. * Copyright (C) 2018 Mike JS. Choi
  6. * $Id$
  7. *
  8. * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. import XCTest
  25. struct TimeResult {
  26. let value: NSNumber?
  27. let string: String
  28. let verboseString: String
  29. let minuteString: String
  30. let intValue: Int32
  31. func assertEqual(_ time: VLCTime, file: StaticString = #file, line: UInt = #line) {
  32. XCTAssertEqual(time.value, value, file: file, line: line)
  33. XCTAssertEqual(time.stringValue, string, file: file, line: line)
  34. XCTAssertEqual(time.verboseStringValue, verboseString, file: file, line: line)
  35. XCTAssertEqual(time.minuteStringValue, minuteString, file: file, line: line)
  36. XCTAssertEqual(time.intValue, intValue, file: file, line: line)
  37. }
  38. }
  39. class VLCTimeTest: XCTestCase {
  40. // MARK: Init
  41. func testNullTime() {
  42. guard let nullTime = VLCTime.null() else {
  43. XCTFail("Could not initialize null VLCTime")
  44. return
  45. }
  46. let expected = TimeResult(value: nil, string: "--:--", verboseString: "", minuteString: "", intValue: 0)
  47. expected.assertEqual(nullTime)
  48. }
  49. func testInitializers() {
  50. let testTime: Int32 = 100000
  51. guard let timeFromInt = VLCTime(int: testTime) else {
  52. XCTFail("Could not initialize VLCTime with int")
  53. return
  54. }
  55. let expected = TimeResult(value: NSNumber(value: testTime), string: "01:40", verboseString: "1 minute 40 seconds", minuteString: "1", intValue: testTime)
  56. expected.assertEqual(timeFromInt)
  57. }
  58. func testInitWithNumber() {
  59. let testTime: Int32 = 100000
  60. guard let timeFromNumber = VLCTime(number: NSNumber(value: testTime)) else {
  61. XCTFail("Could not initialize VLCTime with NSNumber")
  62. return
  63. }
  64. let expected = TimeResult(value: NSNumber(value: testTime), string: "01:40", verboseString: "1 minute 40 seconds", minuteString: "1", intValue: testTime)
  65. expected.assertEqual(timeFromNumber)
  66. }
  67. // MARK: String representations
  68. func testStringConversion(_ tests: [(Int32, String)], assert: (VLCTime, String) -> ()) {
  69. for (milliseconds, expected) in tests {
  70. guard let time = VLCTime(int: milliseconds) else {
  71. XCTFail("Could not initialize VLCTime with int \(milliseconds) ms")
  72. return
  73. }
  74. assert(time, expected)
  75. }
  76. }
  77. func testTimeToStringDescription() {
  78. let tests: [(time: Int32, expected: String)] = [
  79. (-10000, "-00:10"),
  80. (10000, "00:10"),
  81. (70000, "01:10"),
  82. (3630000, "1:00:30"),
  83. (15650000, "4:20:50")
  84. ]
  85. testStringConversion(tests) { (time, expected) in
  86. XCTAssertEqual(time.description, expected)
  87. }
  88. }
  89. func testTimeToVerboseString() {
  90. let tests: [(time: Int32, expected: String)] = [
  91. (-10000, "10 seconds remaining"),
  92. (10000, "10 seconds"),
  93. (70000, "1 minute 10 seconds"),
  94. (200000, "3 minutes 20 seconds"),
  95. (15600000, "4 hours 20 minutes"),
  96. (4830000, "1 hour 20 minutes 30 seconds"),
  97. (3630000, "1 hour 30 seconds")
  98. ]
  99. testStringConversion(tests) { (time, expected) in
  100. XCTAssertEqual(time.verboseStringValue, expected)
  101. }
  102. }
  103. func testTimeToMinuteString() {
  104. let tests: [(time: Int32, expected: String)] = [
  105. (10000, "0"),
  106. (-70000, "1"),
  107. (70000, "1"),
  108. (400000, "6"),
  109. (3600000, "60"),
  110. (15600000, "260")
  111. ]
  112. testStringConversion(tests) { (time, expected) in
  113. XCTAssertEqual(time.minuteStringValue, expected)
  114. }
  115. }
  116. // MARK: ETC
  117. func testCompare() {
  118. guard let greater = VLCTime(int: 2000), let smaller = VLCTime(int: 1100) else {
  119. XCTFail("Could not initialize VLCTime with int")
  120. return
  121. }
  122. XCTAssertEqual(greater.compare(smaller), ComparisonResult.orderedDescending)
  123. XCTAssertEqual(smaller.compare(greater), ComparisonResult.orderedAscending)
  124. XCTAssertEqual(greater.compare(greater), ComparisonResult.orderedSame)
  125. XCTAssertEqual(greater.isEqual(smaller), false)
  126. }
  127. func testHash() {
  128. let time = VLCTime(int: 10)
  129. XCTAssertNotNil(time?.hash())
  130. }
  131. func testNumberValue() {
  132. let expected = NSNumber(value: 10)
  133. let time = VLCTime(number: expected)
  134. let output = time?.numberValue
  135. XCTAssertEqual(output, expected)
  136. }
  137. }