authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2020-07-26 18:03:03+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-27 07:16:44+00:00
log616355807fc8b0a2e7ccc2e54cd4453776c0e187
tree4330e7593062f517542dcb5c5911fc1151d32fbc
parent5139aa7ba4ccbe1c8bc435dc3c8c38cb9887de6f

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

Fixes #5918

2 files changed, 23 insertions(+), 1 deletions(-)

lib/std/math/big/int.zig+1-1
...@@ -99,7 +99,7 @@ pub const Mutable = struct {...@@ -99,7 +99,7 @@ pub const Mutable = struct {
99 pub fn toManaged(self: Mutable, allocator: *Allocator) Managed {99 pub fn toManaged(self: Mutable, allocator: *Allocator) Managed {
100 return .{100 return .{
101 .allocator = allocator,101 .allocator = allocator,
102 .limbs = limbs,102 .limbs = self.limbs,
103 .metadata = if (self.positive)103 .metadata = if (self.positive)
104 self.len & ~Managed.sign_bit104 self.len & ~Managed.sign_bit
105 else105 else
lib/std/math/big/int_test.zig+22
...@@ -2,6 +2,7 @@ const std = @import("../../std.zig");...@@ -2,6 +2,7 @@ const std = @import("../../std.zig");
2const mem = std.mem;2const mem = std.mem;
3const testing = std.testing;3const testing = std.testing;
4const Managed = std.math.big.int.Managed;4const Managed = std.math.big.int.Managed;
5const Mutable = std.math.big.int.Mutable;
5const Limb = std.math.big.Limb;6const Limb = std.math.big.Limb;
6const DoubleLimb = std.math.big.DoubleLimb;7const DoubleLimb = std.math.big.DoubleLimb;
7const maxInt = std.math.maxInt;8const maxInt = std.math.maxInt;
...@@ -1453,3 +1454,24 @@ test "big.int gcd one large" {...@@ -1453,3 +1454,24 @@ test "big.int gcd one large" {
14531454
1454 testing.expect((try r.to(u64)) == 1);1455 testing.expect((try r.to(u64)) == 1);
1455}1456}
1457
1458test "big.int mutable to managed" {
1459 const allocator = testing.allocator;
1460 var limbs_buf = try allocator.alloc(Limb, 8);
1461 defer allocator.free(limbs_buf);
1462
1463 var a = Mutable.init(limbs_buf, 0xdeadbeef);
1464 var a_managed = a.toManaged(allocator);
1465
1466 testing.expect(a.toConst().eq(a_managed.toConst()));
1467}
1468
1469test "big.int const to managed" {
1470 var a = try Managed.initSet(testing.allocator, 123423453456);
1471 defer a.deinit();
1472
1473 var b = try a.toConst().toManaged(testing.allocator);
1474 defer b.deinit();
1475
1476 testing.expect(a.toConst().eq(b.toConst()));
1477}