2008/11/9 Dominique Pelle <[EMAIL PROTECTED]>:

> $ gcc -Wall -o test-memcpy-memmove test-memcpy-memmove.c
> $ ./memcpy-memmove
> testing descending memcpy()  with overlapping mem...OK
> testing ascending  memcpy()  with overlapping mem...FAIL
>  expected=[abcdeabcdeabcdepqrstuvwxyz]
>  actual  =[abcdeabcdefghijpqrstuvwxyz]
> testing descending memmove() with overlapping mem...OK
> testing ascending  memmove() with overlapping mem...OK


Oops, small mistake, which does not change the conclusion,
output should have been:

$ ./memcpy-memmove
testing descending memcpy()  with overlapping mem...OK
testing ascending  memcpy()  with overlapping mem...FAIL
  expected=[abcdeabcdefghijpqrstuvwxyz]
  actual  =[abcdeabcdeabcdepqrstuvwxyz]
testing descending memmove() with overlapping mem...OK
testing ascending  memmove() with overlapping mem...OK

-- Dominique

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

/*
 * Test whether memcpy()/memmove() give the correct
 * results when moving overlapping memory upward or
 * backward.  mempcy() is not guarantee to be correct
 * when memory overlaps, memmove() should be correct.
 *
 * Results on my system (Linux x86):
 *
 * $ gcc -O2 -Wall -o test-memcpy-memmove test-memcpy-memmove.c
 * $ ./test-memcpy-memmove 
 * testing descending memcpy()  with overlapping mem...OK
 * testing ascending  memcpy()  with overlapping mem...FAIL
 *   expected=[abcdeabcdefghijpqrstuvwxyz] 
 *   actual  =[abcdeabcdeabcdepqrstuvwxyz]
 * testing descending memmove() with overlapping mem...OK
 * testing ascending  memmove() with overlapping mem...OK
 *
 * Dominique Pelle <[EMAIL PROTECTED]>
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/* Compare expected and actual results, and print outcome */
static void compare(const char *expected, const char *actual)
{
  if (strcmp(expected, actual) == 0) {
    printf("OK\n");
  } else {
    printf("FAIL\n"
           "  expected=[%s]\n"
           "  actual  =[%s]\n", expected, actual);
  }
}

int main()
{
  /*                          0         1         2
   * .........................01234567890123456789012345 */
  const char *pristine_str = "abcdefghijklmnopqrstuvwxyz";
  char *str;

  /* Expected results when copying 10 char by 5 positions (hence
   * overlapping memory).
   *
   * When copying downward memmove(str, str + 5, 10)
   * (.) for unchanged char (X) for changed char:
   *
   *                  pristine:  abcdefghijklmnopqrstuvwxyx
   *                             XXXXXXXXXX................ */
  const const char *expected1 = "fghijklmnoklmnopqrstuvwxyz";

  /*                             
   * When copying upward memmove(str + 5, str, 10)
   * (.) for unchanged char (X) for changed char:
   *
   *                  pristine:  abcdefghijklmnopqrstuvwxyx
   *                             .....XXXXXXXXXX........... */
  const const char *expected2 = "abcdeabcdefghijpqrstuvwxyz";

  
  printf("testing descending memcpy()  with overlapping mem...");
  str = strdup(pristine_str);
  memcpy(str, str + 5, 10);
  compare(expected1, str);

  printf("testing ascending  memcpy()  with overlapping mem...");
  strcpy(str, pristine_str);
  memcpy(str + 5, str, 10);
  compare(expected2, str);

  printf("testing descending memmove() with overlapping mem...");
  strcpy(str, pristine_str);
  memmove(str, str + 5, 10);
  compare(expected1, str);

  printf("testing ascending  memmove() with overlapping mem...");
  strcpy(str, pristine_str);
  memmove(str + 5, str, 10);
  compare(expected2, str);

  free(str);
  return 0;
}

Raspunde prin e-mail lui