commit 616355807fc8b0a2e7ccc2e54cd4453776c0e187 (tree)
parent 5139aa7ba4ccbe1c8bc435dc3c8c38cb9887de6f
Author: joachimschmidt557 <joachim.schmidt557@outlook.com>
Date: Sun, 26 Jul 2020 18:03:03 +0200
Fix bug in big.int.Mutable.toManaged() and add tests
Fixes #5918
Diffstat:
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig
@@ -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
diff --git a/lib/std/math/big/int_test.zig b/lib/std/math/big/int_test.zig
@@ -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()));
+}