tst-memmove.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (C) 2016 Rene Nielsen <rene.nielsen@microsemi.com>
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. #include <string.h>
  6. #include <stdio.h>
  7. #define LEN 1024
  8. int main(int argc, char *argv[])
  9. {
  10. unsigned char a[LEN], exp;
  11. int i, move_len, move_src, err = 0;
  12. // Fill the array with increasing values from 0 to LEN - 1 (wrap is fine)
  13. for (i = 0; i < LEN; i++) {
  14. a[i] = i;
  15. }
  16. // move_src is the index into the array to move from.
  17. // move_len is the number of indices to move. Here, we take the remainder.
  18. move_src = LEN / 64;
  19. move_len = LEN - move_src;
  20. printf("Moving %d entries from index %d to 0\n", move_len, move_src);
  21. memmove(a, &a[move_src], sizeof(a[0]) * move_len);
  22. // Check that the memmove went well
  23. for (i = 0; i < LEN; i++) {
  24. // Expect that the first #move_len entries contain increasing values
  25. // starting at #move_src and the last #move_src entries are untouched.
  26. exp = i >= move_len ? i : i + move_src;
  27. if (a[i] != exp) {
  28. printf("Error: memmove() failed. Expected a[%d] = %u, got %u\n", i, exp, a[i]);
  29. err = 1;
  30. }
  31. }
  32. if (!err) {
  33. printf("memmove() succeeded\n");
  34. }
  35. return err;
  36. }