Fix bug in big.int.Mutable.toManaged() and add tests

Fixes #5918
This commit is contained in:
joachimschmidt557 2020-07-26 18:03:03 +02:00 committed by Andrew Kelley
parent 5139aa7ba4
commit 616355807f
2 changed files with 23 additions and 1 deletions

View File

@ -99,7 +99,7 @@ pub const Mutable = struct {
pub fn toManaged(self: Mutable, allocator: *Allocator) Managed {
return .{
.allocator = allocator,
.limbs = limbs,
.limbs = self.limbs,
.metadata = if (self.positive)
self.len & ~Managed.sign_bit
else

View File

@ -2,6 +2,7 @@ const std = @import("../../std.zig");
const mem = std.mem;
const testing = std.testing;
const Managed = std.math.big.int.Managed;
const Mutable = std.math.big.int.Mutable;
const Limb = std.math.big.Limb;
const DoubleLimb = std.math.big.DoubleLimb;
const maxInt = std.math.maxInt;
@ -1453,3 +1454,24 @@ test "big.int gcd one large" {
testing.expect((try r.to(u64)) == 1);
}
test "big.int mutable to managed" {
const allocator = testing.allocator;
var limbs_buf = try allocator.alloc(Limb, 8);
defer allocator.free(limbs_buf);
var a = Mutable.init(limbs_buf, 0xdeadbeef);
var a_managed = a.toManaged(allocator);
testing.expect(a.toConst().eq(a_managed.toConst()));
}
test "big.int const to managed" {
var a = try Managed.initSet(testing.allocator, 123423453456);
defer a.deinit();
var b = try a.toConst().toManaged(testing.allocator);
defer b.deinit();
testing.expect(a.toConst().eq(b.toConst()));
}