authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-01-17 14:31:47+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-01-17 14:31:47+01:00
logfa2c3be341ac6a7eddc65397984dac1bf648be26
treef389038e0324fad415aa6a80096ebb5328f86950
parentdb0fc32ab2d5277655a87200be8f602473ed30d2

More tests, and fixed none negative bigint xor


2 files changed, 28 insertions(+), 8 deletions(-)

src/bigint.cpp+4-2
...@@ -1295,14 +1295,16 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1295,14 +1295,16 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1295 dest->is_negative = false;1295 dest->is_negative = false;
1296 const uint64_t *op1_digits = bigint_ptr(op1);1296 const uint64_t *op1_digits = bigint_ptr(op1);
1297 const uint64_t *op2_digits = bigint_ptr(op2);1297 const uint64_t *op2_digits = bigint_ptr(op2);
1298
1299 assert(op1->digit_count > 0 && op2->digit_count > 0);
1300 uint64_t first_digit = op1_digits[0] ^ op2_digits[0];
1298 if (op1->digit_count == 1 && op2->digit_count == 1) {1301 if (op1->digit_count == 1 && op2->digit_count == 1) {
1299 dest->digit_count = 1;1302 dest->digit_count = 1;
1300 dest->data.digit = op1_digits[0] ^ op2_digits[0];1303 dest->data.digit = first_digit;
1301 bigint_normalize(dest);1304 bigint_normalize(dest);
1302 return;1305 return;
1303 }1306 }
1304 // TODO this code path is untested1307 // TODO this code path is untested
1305 uint64_t first_digit = dest->data.digit;
1306 dest->digit_count = max(op1->digit_count, op2->digit_count);1308 dest->digit_count = max(op1->digit_count, op2->digit_count);
1307 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);1309 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);
1308 dest->data.digits[0] = first_digit;1310 dest->data.digits[0] = first_digit;
test/cases/math.zig+24-6
...@@ -349,6 +349,29 @@ test "big number shifting" {...@@ -349,6 +349,29 @@ test "big number shifting" {
349 }349 }
350}350}
351351
352test "xor" {
353 test_xor();
354 comptime test_xor();
355}
356
357fn test_xor() {
358 assert(0xFF ^ 0x00 == 0xFF);
359 assert(0xF0 ^ 0x0F == 0xFF);
360 assert(0xFF ^ 0xF0 == 0x0F);
361 assert(0xFF ^ 0x0F == 0xF0);
362 assert(0xFF ^ 0xFF == 0x00);
363}
364
365test "big number xor" {
366 comptime {
367 assert(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
368 assert(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
369 assert(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
370 assert(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
371 assert(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
372 }
373}
374
352test "f128" {375test "f128" {
353 test_f128();376 test_f128();
354 comptime test_f128();377 comptime test_f128();
...@@ -368,9 +391,4 @@ fn test_f128() {...@@ -368,9 +391,4 @@ fn test_f128() {
368391
369fn should_not_be_zero(x: f128) {392fn should_not_be_zero(x: f128) {
370 assert(x != 0.0);393 assert(x != 0.0);
371}394}
372
373test "xor with zero" {
374 assert(0xFF ^ 0x00 == 0xFF);
375 comptime assert(0xFF ^ 0x00 == 0xFF);
376}
\ No newline at end of file