Base16Tests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Base16Tests.swift
  3. // Base32
  4. //
  5. // Created by 野村 憲男 on 2/7/15.
  6. //
  7. // Copyright (c) 2015 Norio Nomura
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. import XCTest
  28. @testable import Base32
  29. class Base16Tests: XCTestCase {
  30. let vectors: [(String, String)] = [
  31. ("", ""),
  32. ("f", "66"),
  33. ("fo", "666F"),
  34. ("foo", "666F6F"),
  35. ("foob", "666F6F62"),
  36. ("fooba", "666F6F6261"),
  37. ("foobar", "666F6F626172"),
  38. ]
  39. override func setUp() {
  40. super.setUp()
  41. // Put setup code here. This method is called before the invocation of each test method in the class.
  42. }
  43. override func tearDown() {
  44. // Put teardown code here. This method is called after the invocation of each test method in the class.
  45. super.tearDown()
  46. }
  47. // MARK: https://tools.ietf.org/html/rfc4648
  48. func test_RFC4648_base16Encode() {
  49. let convertedVectors = self.vectors.map {($0.dataUsingUTF8StringEncoding, $1)}
  50. self.measure{
  51. for _ in 0...100 {
  52. for (test, expect) in convertedVectors {
  53. let result = base16Encode(test)
  54. XCTAssertEqual(result, expect, "base16Encode for \(test)")
  55. }
  56. }
  57. }
  58. }
  59. func test_RFC4648_base16Decode() {
  60. let convertedVectors = self.vectors.map {($0.dataUsingUTF8StringEncoding, $1)}
  61. self.measure{
  62. for _ in 0...100 {
  63. for (expect, test) in convertedVectors {
  64. let result = base16DecodeToData(test)
  65. XCTAssertEqual(result!, expect, "base16Decode for \(test)")
  66. }
  67. }
  68. }
  69. }
  70. // MARK: -
  71. func test_Base16ExtensionString() {
  72. self.measure{
  73. for _ in 0...100 {
  74. for (test, expect) in self.vectors {
  75. let result = test.base16EncodedString
  76. XCTAssertEqual(result, expect, "\(test).base16EncodedString")
  77. let decoded = result.base16DecodedString()
  78. XCTAssertEqual(decoded!, test, "\(result).base16DecodedString()")
  79. }
  80. }
  81. }
  82. }
  83. func test_Base16ExtensionData() {
  84. let dataVectors = vectors.map {
  85. (
  86. $0.dataUsingUTF8StringEncoding,
  87. $1.dataUsingUTF8StringEncoding
  88. )
  89. }
  90. self.measure{
  91. for _ in 0...100 {
  92. for (test, expect) in dataVectors {
  93. let result = test.base16EncodedData
  94. XCTAssertEqual(result, expect, "\(test).base16EncodedData")
  95. let decoded = result.base16DecodedData
  96. XCTAssertEqual(decoded!, test, "\(result).base16DecodedData")
  97. }
  98. }
  99. }
  100. }
  101. func test_Base16ExtensionDataAndString() {
  102. let dataAndStringVectors = vectors.map {($0.dataUsingUTF8StringEncoding, $1)}
  103. self.measure{
  104. for _ in 0...100 {
  105. for (test, expect) in dataAndStringVectors {
  106. let result = test.base16EncodedString
  107. XCTAssertEqual(result, expect, "\(test).base16EncodedString")
  108. let decoded = result.base16DecodedData
  109. XCTAssertEqual(decoded!, test, "\(result).base16DecodedData")
  110. }
  111. }
  112. }
  113. }
  114. func test_lowercase() {
  115. let lowercaseDataString = "abcdef"
  116. let decodedArray = base16Decode(lowercaseDataString)!
  117. let encodedFromArray = base16Encode(decodedArray, uppercase: false)
  118. XCTAssertEqual(encodedFromArray, lowercaseDataString)
  119. let decodedData = base16DecodeToData(lowercaseDataString)!
  120. let encodedFromData = base16Encode(decodedData, uppercase: false)
  121. XCTAssertEqual(encodedFromData, lowercaseDataString)
  122. }
  123. }