mem.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. extern crate compiler_builtins;
  2. use compiler_builtins::mem::{memcmp, memcpy, memmove, memset};
  3. #[test]
  4. fn memcpy_3() {
  5. let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  6. unsafe {
  7. let src = arr.as_ptr().offset(9);
  8. let dst = arr.as_mut_ptr().offset(1);
  9. assert_eq!(memcpy(dst, src, 3), dst);
  10. assert_eq!(arr, [0, 9, 10, 11, 4, 5, 6, 7, 8, 9, 10, 11]);
  11. }
  12. arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  13. unsafe {
  14. let src = arr.as_ptr().offset(1);
  15. let dst = arr.as_mut_ptr().offset(9);
  16. assert_eq!(memcpy(dst, src, 3), dst);
  17. assert_eq!(arr, [0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3]);
  18. }
  19. }
  20. #[test]
  21. fn memcpy_10() {
  22. let arr: [u8; 18] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
  23. let mut dst: [u8; 12] = [0; 12];
  24. unsafe {
  25. let src = arr.as_ptr().offset(1);
  26. assert_eq!(memcpy(dst.as_mut_ptr(), src, 10), dst.as_mut_ptr());
  27. assert_eq!(dst, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0]);
  28. }
  29. unsafe {
  30. let src = arr.as_ptr().offset(8);
  31. assert_eq!(memcpy(dst.as_mut_ptr(), src, 10), dst.as_mut_ptr());
  32. assert_eq!(dst, [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 0]);
  33. }
  34. }
  35. #[test]
  36. fn memcpy_big() {
  37. // Make the arrays cross 3 pages
  38. const SIZE: usize = 8193;
  39. let src: [u8; SIZE] = [22; SIZE];
  40. struct Dst {
  41. start: usize,
  42. buf: [u8; SIZE],
  43. end: usize,
  44. }
  45. let mut dst = Dst {
  46. start: 0,
  47. buf: [0; SIZE],
  48. end: 0,
  49. };
  50. unsafe {
  51. assert_eq!(
  52. memcpy(dst.buf.as_mut_ptr(), src.as_ptr(), SIZE),
  53. dst.buf.as_mut_ptr()
  54. );
  55. assert_eq!(dst.start, 0);
  56. assert_eq!(dst.buf, [22; SIZE]);
  57. assert_eq!(dst.end, 0);
  58. }
  59. }
  60. #[test]
  61. fn memmove_forward() {
  62. let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  63. unsafe {
  64. let src = arr.as_ptr().offset(6);
  65. let dst = arr.as_mut_ptr().offset(3);
  66. assert_eq!(memmove(dst, src, 5), dst);
  67. assert_eq!(arr, [0, 1, 2, 6, 7, 8, 9, 10, 8, 9, 10, 11]);
  68. }
  69. }
  70. #[test]
  71. fn memmove_backward() {
  72. let mut arr: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  73. unsafe {
  74. let src = arr.as_ptr().offset(3);
  75. let dst = arr.as_mut_ptr().offset(6);
  76. assert_eq!(memmove(dst, src, 5), dst);
  77. assert_eq!(arr, [0, 1, 2, 3, 4, 5, 3, 4, 5, 6, 7, 11]);
  78. }
  79. }
  80. #[test]
  81. fn memset_zero() {
  82. let mut arr: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
  83. unsafe {
  84. let ptr = arr.as_mut_ptr().offset(5);
  85. assert_eq!(memset(ptr, 0, 2), ptr);
  86. assert_eq!(arr, [0, 1, 2, 3, 4, 0, 0, 7]);
  87. // Only the LSB matters for a memset
  88. assert_eq!(memset(arr.as_mut_ptr(), 0x2000, 8), arr.as_mut_ptr());
  89. assert_eq!(arr, [0, 0, 0, 0, 0, 0, 0, 0]);
  90. }
  91. }
  92. #[test]
  93. fn memset_nonzero() {
  94. let mut arr: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
  95. unsafe {
  96. let ptr = arr.as_mut_ptr().offset(2);
  97. assert_eq!(memset(ptr, 22, 3), ptr);
  98. assert_eq!(arr, [0, 1, 22, 22, 22, 5, 6, 7]);
  99. // Only the LSB matters for a memset
  100. assert_eq!(memset(arr.as_mut_ptr(), 0x2009, 8), arr.as_mut_ptr());
  101. assert_eq!(arr, [9, 9, 9, 9, 9, 9, 9, 9]);
  102. }
  103. }
  104. #[test]
  105. fn memcmp_eq() {
  106. let arr1: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
  107. let arr2: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
  108. unsafe {
  109. assert_eq!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 8), 0);
  110. assert_eq!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 3), 0);
  111. }
  112. }
  113. #[test]
  114. fn memcmp_ne() {
  115. let arr1: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
  116. let arr2: [u8; 8] = [0, 1, 2, 3, 4, 5, 7, 7];
  117. unsafe {
  118. assert!(memcmp(arr1.as_ptr(), arr2.as_ptr(), 8) < 0);
  119. assert!(memcmp(arr2.as_ptr(), arr1.as_ptr(), 8) > 0);
  120. }
  121. }