alelievr / libft-unit-test

457 stars 88 forks source link

ft_memcmp does not work in linux #39

Closed kryshac closed 6 years ago

kryshac commented 6 years ago

ft_memcmp: [OK] [OK] [OK] [FAILED] [FAILED] [OK] [OK] [OK] [OK] [OK] [fail]: your memcmp does not work with basic input [fail]: your memcmp does not cast the memory in unsigned char

See result.log for more informations ! kryshac:~/Desktop/42v2/libft-unit-test$> cat result.log ft_memcmp: [OK] [OK] [OK] [FAILED] [FAILED] [OK] [OK] [OK] [OK] [OK] [fail]: your memcmp does not work with basic input Test code:

uint8_t *s1 = (uint8_t *)"\xff\xaa\xde\xffMACOSX\xff";

uint8_t *s2 = (uint8_t *)"\xff\xaa\xde\x02";
size_t size = 8;
if (memcmp(s1, s2, size) == ft_memcmp(s1, s2, size))
    exit(TEST_SUCCESS);
exit(TEST_FAILED);

Diffs: memcmp: |64768| ft_memcmp: |253|

[fail]: your memcmp does not cast the memory in unsigned char Test code:

uint8_t *s1 = (uint8_t *)"\xff\xaa\xde\200";

uint8_t *s2 = (uint8_t *)"\xff\xaa\xde\0";
size_t size = 8;
if (memcmp(s1, s2, size) == ft_memcmp(s1, s2, size))
    exit(TEST_SUCCESS);
exit(TEST_FAILED);

Diffs: memcmp: |32768| ft_memcmp: |128|

my code:

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
    unsigned char   *p1;
    unsigned char   *p2;

    p1 = (unsigned char *)s1;
    p2 = (unsigned char *)s2;
    while (n--)
        if (*p1++ != *p2++)
            return (*--p1 - *--p2);
    return (0);
}

On MAC OS X all tests work correctly.

alelievr commented 6 years ago

Hello, can i have more informations about your linux distribution ?

kryshac commented 6 years ago

Hello, ubuntu 16.04 LTS 64-bit

alelievr commented 6 years ago

Humm, the OSX memcmp and linux memcmp behaviour differs, the only stable thing to test is the sign of the result. I can't just check the sign of the return value because the memcmp's man page specify that the return is the difference between the two first differing bytes. I'll add conditional compilation for these test -_-

Fixed in 1990f39ffc99dd1a1cb23a4af2b0ab54a68f40b0.