GiantUIntTests.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 testComparable() {
  20. XCTAssertTrue(GiantUInt([1, 2, 3]) == GiantUInt([1, 2, 3]), "equality check failed")
  21. XCTAssertTrue(GiantUInt([1, 2, 3]) > GiantUInt([1, 3, 2]), "greater than check failed")
  22. XCTAssertTrue(GiantUInt([1, 3, 2]) < GiantUInt([1, 2, 3]), "lower than check failed")
  23. }
  24. func testAddition() {
  25. let a = GiantUInt([1]) + GiantUInt([1])
  26. XCTAssertEqual(a, GiantUInt([2]), "simple addition failed")
  27. let b = GiantUInt([200]) + GiantUInt([200])
  28. XCTAssertEqual(b, GiantUInt([144, 1]), "addition with retenue failed")
  29. let c = GiantUInt([200, 200]) + GiantUInt([200, 200])
  30. XCTAssertEqual(c, GiantUInt([144, 145, 1]), "addition with double retenue failed")
  31. }
  32. func testSubtraction() {
  33. let a = GiantUInt([2]) - GiantUInt([1])
  34. XCTAssertEqual(a, GiantUInt([1]), "simple subtraction failed")
  35. let b = GiantUInt([0, 1]) - GiantUInt([255])
  36. XCTAssertEqual(b, GiantUInt([1]), "subtraction with retenue failed")
  37. }
  38. func testMultiplication() {
  39. let a = GiantUInt([2]) * GiantUInt([2])
  40. XCTAssertEqual(a, GiantUInt([4]), "simple multiplication failed")
  41. let b = GiantUInt([200]) * GiantUInt([200])
  42. XCTAssertEqual(b, GiantUInt([64, 156]), "multiplication with retenue failed")
  43. let c = GiantUInt([200, 200]) * GiantUInt([200, 200])
  44. XCTAssertEqual(c, GiantUInt([64, 28, 121, 157]), "multiplication with double retenue failed")
  45. }
  46. func testExponentiation() {
  47. let a = GiantUInt([3]) ^^ GiantUInt([7])
  48. XCTAssertEqual(a, GiantUInt([139, 8]), "simple exponentiation failed")
  49. }
  50. func testModulus() {
  51. let a = GiantUInt([10]) % GiantUInt([3])
  52. XCTAssertEqual(a, GiantUInt([1]), "simple modulus failed")
  53. }
  54. func testExponentiationWithModulus() {
  55. let a = GiantUInt.exponentiateWithModulus(rhs: GiantUInt([3]), lhs: GiantUInt([7]), modulus: GiantUInt([0, 1]))
  56. XCTAssertEqual(a, GiantUInt([139]), "simple exponentiation with modulus failed")
  57. }
  58. }
  59. extension GiantUIntTests {
  60. static func allTests() -> [(String, (GiantUIntTests) -> () -> Void)] {
  61. let tests = [
  62. ("testComparable", testComparable),
  63. ("testAddition", testAddition),
  64. ("testSubtraction", testSubtraction),
  65. ("testMultiplication", testMultiplication),
  66. ("testExponentiation", testExponentiation),
  67. ("testModulus", testModulus),
  68. ("testExponentiationWithModulus", testExponentiationWithModulus)
  69. ]
  70. return tests
  71. }
  72. }