misc.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use testcrate::*;
  2. /// Make sure that the the edge case tester and randomized tester don't break, and list examples of
  3. /// fuzz values for documentation purposes.
  4. #[test]
  5. fn fuzz_values() {
  6. const VALS: [u16; 47] = [
  7. 0b0, // edge cases
  8. 0b1111111111111111,
  9. 0b1111111111111110,
  10. 0b1111111111111100,
  11. 0b1111111110000000,
  12. 0b1111111100000000,
  13. 0b1110000000000000,
  14. 0b1100000000000000,
  15. 0b1000000000000000,
  16. 0b111111111111111,
  17. 0b111111111111110,
  18. 0b111111111111100,
  19. 0b111111110000000,
  20. 0b111111100000000,
  21. 0b110000000000000,
  22. 0b100000000000000,
  23. 0b11111111111111,
  24. 0b11111111111110,
  25. 0b11111111111100,
  26. 0b11111110000000,
  27. 0b11111100000000,
  28. 0b10000000000000,
  29. 0b111111111,
  30. 0b111111110,
  31. 0b111111100,
  32. 0b110000000,
  33. 0b100000000,
  34. 0b11111111,
  35. 0b11111110,
  36. 0b11111100,
  37. 0b10000000,
  38. 0b111,
  39. 0b110,
  40. 0b100,
  41. 0b11,
  42. 0b10,
  43. 0b1,
  44. 0b1010110100000, // beginning of random fuzzing
  45. 0b1100011001011010,
  46. 0b1001100101001111,
  47. 0b1101010100011010,
  48. 0b100010001,
  49. 0b1000000000000000,
  50. 0b1100000000000101,
  51. 0b1100111101010101,
  52. 0b1100010111111111,
  53. 0b1111110101111111,
  54. ];
  55. let mut i = 0;
  56. fuzz(10, |x: u16| {
  57. assert_eq!(x, VALS[i]);
  58. i += 1;
  59. });
  60. }
  61. #[test]
  62. fn leading_zeros() {
  63. use compiler_builtins::int::__clzsi2;
  64. use compiler_builtins::int::leading_zeros::{
  65. usize_leading_zeros_default, usize_leading_zeros_riscv,
  66. };
  67. fuzz(N, |x: usize| {
  68. let lz = x.leading_zeros() as usize;
  69. let lz0 = __clzsi2(x);
  70. let lz1 = usize_leading_zeros_default(x);
  71. let lz2 = usize_leading_zeros_riscv(x);
  72. if lz0 != lz {
  73. panic!("__clzsi2({}): std: {}, builtins: {}", x, lz, lz0);
  74. }
  75. if lz1 != lz {
  76. panic!(
  77. "usize_leading_zeros_default({}): std: {}, builtins: {}",
  78. x, lz, lz1
  79. );
  80. }
  81. if lz2 != lz {
  82. panic!(
  83. "usize_leading_zeros_riscv({}): std: {}, builtins: {}",
  84. x, lz, lz2
  85. );
  86. }
  87. })
  88. }
  89. #[test]
  90. fn float_extend() {
  91. fuzz_float(N, |x: f32| {
  92. let tmp0 = x as f64;
  93. let tmp1: f64 = compiler_builtins::float::extend::__extendsfdf2(x);
  94. if !compiler_builtins::float::Float::eq_repr(tmp0, tmp1) {
  95. panic!("__extendsfdf2({}): std: {}, builtins: {}", x, tmp0, tmp1);
  96. }
  97. });
  98. }
  99. // This doesn't quite work because of issues related to
  100. // https://github.com/rust-lang/rust/issues/73920.
  101. // TODO how do we resolve this?
  102. /*
  103. macro_rules! pow {
  104. ($($f:ty, $fn:ident);*;) => {
  105. $(
  106. fuzz_float_2(N, |x: $f, y: $f| {
  107. let n = y as i32;
  108. let tmp0: $f = x.powi(n);
  109. let tmp1: $f = $fn(x, n);
  110. if tmp0 != tmp1 {
  111. panic!(
  112. "{}({}, {}): std: {}, builtins: {}",
  113. stringify!($fn), x, y, tmp0, tmp1
  114. );
  115. }
  116. });
  117. )*
  118. };
  119. }
  120. #[test]
  121. fn float_pow() {
  122. use compiler_builtins::float::pow::{__powidf2, __powisf2};
  123. pow!(
  124. f32, __powisf2;
  125. f64, __powidf2;
  126. );
  127. }
  128. */