diff options
| author | joachimschmidt557 <joachim.schmidt557@outlook.com> | 2020-07-26 18:03:03 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-07-27 07:16:44 +0000 |
| commit | 616355807fc8b0a2e7ccc2e54cd4453776c0e187 (patch) | |
| tree | 4330e7593062f517542dcb5c5911fc1151d32fbc /lib/std/math/big | |
| parent | 5139aa7ba4ccbe1c8bc435dc3c8c38cb9887de6f (diff) | |
| download | zig-616355807fc8b0a2e7ccc2e54cd4453776c0e187.tar.gz zig-616355807fc8b0a2e7ccc2e54cd4453776c0e187.zip | |
Fix bug in big.int.Mutable.toManaged() and add tests
Fixes #5918
Diffstat (limited to 'lib/std/math/big')
| -rw-r--r-- | lib/std/math/big/int.zig | 2 | ||||
| -rw-r--r-- | lib/std/math/big/int_test.zig | 22 |
2 files changed, 23 insertions, 1 deletions
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index b6d7731f1a..92b3f80429 100644 --- 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 index d7e354879e..a86b55d2de 100644 --- 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())); +} |
