zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit fa2c3be341ac6a7eddc65397984dac1bf648be26 (tree)
parent db0fc32ab2d5277655a87200be8f602473ed30d2
Author: Jimmi Holst Christensen <jhc@liab.dk>
Date:   Wed, 17 Jan 2018 14:31:47 +0100

More tests, and fixed none negative bigint xor

Diffstat:
Msrc/bigint.cpp | 6++++--
Mtest/cases/math.zig | 31+++++++++++++++++++++++++------
2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/bigint.cpp b/src/bigint.cpp @@ -1295,14 +1295,16 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) { dest->is_negative = false; const uint64_t *op1_digits = bigint_ptr(op1); const uint64_t *op2_digits = bigint_ptr(op2); + + assert(op1->digit_count > 0 && op2->digit_count > 0); + uint64_t first_digit = op1_digits[0] ^ op2_digits[0]; if (op1->digit_count == 1 && op2->digit_count == 1) { dest->digit_count = 1; - dest->data.digit = op1_digits[0] ^ op2_digits[0]; + dest->data.digit = first_digit; bigint_normalize(dest); return; } // TODO this code path is untested - uint64_t first_digit = dest->data.digit; dest->digit_count = max(op1->digit_count, op2->digit_count); dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count); dest->data.digits[0] = first_digit; diff --git a/test/cases/math.zig b/test/cases/math.zig @@ -349,6 +349,29 @@ test "big number shifting" { } } +test "xor" { + test_xor(); + comptime test_xor(); +} + +fn test_xor() { + assert(0xFF ^ 0x00 == 0xFF); + assert(0xF0 ^ 0x0F == 0xFF); + assert(0xFF ^ 0xF0 == 0x0F); + assert(0xFF ^ 0x0F == 0xF0); + assert(0xFF ^ 0xFF == 0x00); +} + +test "big number xor" { + comptime { + assert(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF); + assert(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000); + assert(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000); + } +} + test "f128" { test_f128(); comptime test_f128(); @@ -368,9 +391,4 @@ fn test_f128() { fn should_not_be_zero(x: f128) { assert(x != 0.0); -} - -test "xor with zero" { - assert(0xFF ^ 0x00 == 0xFF); - comptime assert(0xFF ^ 0x00 == 0xFF); -} +} +\ No newline at end of file