GiantUIntTests.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. import Foundation
  16. import XCTest
  17. @testable import CryptoSwift
  18. final class GiantUIntTests: XCTestCase {
  19. func testAddition() {
  20. let a = GiantUInt([1]) + GiantUInt([1])
  21. XCTAssertEqual(a, GiantUInt([2]), "simple addition failed")
  22. let b = GiantUInt([200]) + GiantUInt([200])
  23. XCTAssertEqual(b, GiantUInt([144, 1]), "addition with retenue failed")
  24. let c = GiantUInt([200, 200]) + GiantUInt([200, 200])
  25. XCTAssertEqual(c, GiantUInt([144, 145, 1]), "addition with double retenue failed")
  26. }
  27. func testMultiplication() {
  28. let a = GiantUInt([2]) * GiantUInt([2])
  29. XCTAssertEqual(a, GiantUInt([4]), "simple multiplication failed")
  30. let b = GiantUInt([200]) * GiantUInt([200])
  31. XCTAssertEqual(b, GiantUInt([64, 156]), "multiplication with retenue failed")
  32. let c = GiantUInt([200, 200]) * GiantUInt([200, 200])
  33. XCTAssertEqual(c, GiantUInt([64, 28, 121, 157]), "multiplication with double retenue failed")
  34. }
  35. }
  36. extension GiantUIntTests {
  37. static func allTests() -> [(String, (GiantUIntTests) -> () -> Void)] {
  38. let tests = [
  39. ("testAddition", testAddition),
  40. ("testMultiplication", testMultiplication)
  41. ]
  42. return tests
  43. }
  44. }