Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Violet] Generic BigInt tests #101

Merged
merged 4 commits into from
Jun 5, 2022
Merged

Conversation

LiarPrincess
Copy link
Contributor

BigIntPowerTests

Following test fails at BigUInt.power -> precondition(!self.isZero):

  /// 0 ^ n = 0 (or sometimes 1)
  func test_base_zero() {
    let zero = BigInt(0)
    let one = BigInt(1)

    for exponent in generateIntValues(countButNotReally: 100) {
      let result = zero.power(exponent)

      // 0 ^ 0 = 1, otherwise 0
      let expected = exponent == 0 ? one : zero
      XCTAssertEqual(result, expected, "0 ^ \(exponent)")
    }
  }

BigIntStringInitTests

A lot of those tests depend on how you want to parse, so some of them are not bugs.

Empty string

For example this would throw in Violet:

  func test_empty_fails() {
    for radix in [2, 4, 7, 32] {
      let result = self.create(string: "", radix: radix)
      XCTAssertNil(result, "Radix: \(radix)")
    }
  }

Minus 0

This produces -0 (should be just 0):

  func test_zero_single_minus() {
    let zero = BigInt()

    for radix in [2, 4, 7, 32] {
      let result = self.create(string: "-0", radix: radix)
      XCTAssertEqual(result, zero)
    }
  }

Octal

Some octal tests fail, but this may be on Violet.
I would love for someone to validate, because I am obviously biased.

Underscores

This passes:

  func test_binary_twoWords() {
    self.run(
      cases: BinaryTestCases.twoWords,
      radix: 2
    )
  }

This fails to parse (nil):

  func test_underscore_binary() {
    let cases: [TestCase] = BinaryTestCases.twoWords.map { words, string in
      let s = self.insertUnderscores(string: string)
      return (words, s)
    }

    self.run(
      cases: cases,
      radix: 2
    )
    // Result: XCTAssertEqual failed: ("nil") is not equal to ("Optional(-115299542376169416438008772744045680824)") - 1_010_110_101_111_011_110_001_001_010_101_111_011_010_100_101_110_010_111_000_001_100_011_110_001_110_001_010_001_001_101_001_101_010_110_101_001_000_101_100_010_111_000
  }

The only difference is that we added some _ inside.

BigIntPropertyTests

Zero

This fails:

  func test_words_zero() {
    let value = BigInt(0)
    XCTAssertWords(value, [0])
  }

I think that inside the Swift.Int range BigInt should return the same values as Swift.Int. Although this is debatable.

There is a test that compares Int.words vs BigInt.words for the same values (it also fails)

  func test_words_int() {
    for (value, expected) in WordsTestCases.int {
      let bigInt = BigInt(value)
      XCTAssertWords(bigInt, expected)
      // Result: XCTAssertEqual failed: ("2") is not equal to ("1") - Count for -9223372036854775808
    }
  }

Bit width

I think that inside the Swift.Int range BigInt should return the same values as Swift.Int. Although this is debatable.

My comments and tests result are inline:

  func test_bitWidth_trivial() {
    let zero = BigInt(0)
    XCTAssertEqual(zero.bitWidth, 1) //  0 is just 0
    // Result: XCTAssertEqual failed: ("0") is not equal to ("1")

    let plus1 = BigInt(1)
    XCTAssertEqual(plus1.bitWidth, 2) // 1 needs '0' prefix -> '01'

    let minus1 = BigInt(-1)
    XCTAssertEqual(minus1.bitWidth, 1) // -1 is just 1
    // Result: XCTAssertEqual failed: ("2") is not equal to ("1")
  }

In general, I think that for negative powers of 2 attaswift returns +1.

@tgymnich tgymnich merged commit e022b9b into attaswift:master Jun 5, 2022
@tgymnich
Copy link
Collaborator

tgymnich commented Jun 5, 2022

Thanks. I disabled some of the tests, that require a consistent 2s complement view on the BigInt. Having a consistent binary representation is definitely a future goal for BigInt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants