TestNumDigitsLargeNegative is a regression test for a bug where NumDigits would panic with a nil pointer dereference when processing large negative numbers (more than 128 bits). Seen in https://github.com/cockroachdb/cockroach/issues/165525.
(t *testing.T)
| 177 | // numbers (more than 128 bits). Seen in |
| 178 | // https://github.com/cockroachdb/cockroach/issues/165525. |
| 179 | func TestNumDigitsLargeNegative(t *testing.T) { |
| 180 | // Create a BigInt directly with more than 128 bits. |
| 181 | var b BigInt |
| 182 | b.SetInt64(2) |
| 183 | exp := NewBigInt(200) |
| 184 | b.Exp(&b, exp, nil) |
| 185 | b.Neg(&b) |
| 186 | |
| 187 | numDigits := NumDigits(&b) |
| 188 | // 2^200 has 61 decimal digits. |
| 189 | expectedDigits := int64(61) |
| 190 | if numDigits != expectedDigits { |
| 191 | t.Errorf("expected %d digits, got %d", expectedDigits, numDigits) |
| 192 | } |
| 193 | |
| 194 | // Also test via Decimal. |
| 195 | d := NewWithBigInt(&b, 0) |
| 196 | numDigits2 := d.NumDigits() |
| 197 | if numDigits2 != expectedDigits { |
| 198 | t.Errorf("expected %d digits via Decimal, got %d", expectedDigits, numDigits2) |
| 199 | } |
| 200 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…