/test/lib/numeric/mutable_big_integer/test_mutable_big_integer12.e

http://github.com/tybor/Liberty · Specman e · 174 lines · 135 code · 11 blank · 28 comment · 5 complexity · a08e4de05ef28204c6c046d15902bd4f MD5 · raw file

  1. -- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries.
  2. -- See the Copyright notice at the end of this file.
  3. --
  4. class TEST_MUTABLE_BIG_INTEGER12
  5. --
  6. -- Testing `shift_left' and `shift_right' as well.
  7. --
  8. insert
  9. ANY
  10. PLATFORM
  11. create {}
  12. make
  13. feature {ANY}
  14. make
  15. local
  16. mbi: MUTABLE_BIG_INTEGER
  17. do
  18. -- Checking `int64_nb_bit_set' first:
  19. assert(int64_nb_bit_set(1) = 1)
  20. assert(int64_nb_bit_set(-1) = 64)
  21. assert(int64_nb_bit_set(Minimum_integer_64) = 1)
  22. assert(int64_nb_bit_set(Maximum_integer_64) = 63)
  23. assert(int64_nb_bit_set(0) = 0)
  24. -- Checking `mbi_nb_bit_set' first:
  25. create mbi.from_integer_64(1)
  26. assert(mbi_nb_bit_set(mbi) = 1)
  27. create mbi.from_integer_64(0)
  28. assert(mbi_nb_bit_set(mbi) = 0)
  29. create mbi.from_integer_64(-1)
  30. assert(mbi_nb_bit_set(mbi) = 1)
  31. create mbi.from_integer_64(Maximum_integer_64)
  32. assert(mbi_nb_bit_set(mbi) = 63)
  33. check_with(1, 1, 1024)
  34. check_with(2, 1, 1024)
  35. check_with(3, 1, 1024)
  36. check_with(4, 1, 1024)
  37. check_with(0, 1, 1024)
  38. check_with(-1, 1, 1024)
  39. check_with(-2, 1, 1024)
  40. check_with(-3, 1, 1024)
  41. check_with(-4, 1, 1024)
  42. check_with(0x00000000FFFFFFFF, 1, 1024)
  43. check_with(0x00000000FFFFFFFE, 1, 1024)
  44. check_with(0x00000000FFFFFFFD, 1, 1024)
  45. check_with(0xFFFFFFFFFFFFFFFE, 1, 1024)
  46. check_with(Maximum_integer, 1, 1024)
  47. check_with(Maximum_integer_64, 1, 1024)
  48. check_with(Minimum_integer, 1, 1024)
  49. check_with(Minimum_integer_64, 1, 1024)
  50. end
  51. check_with (value: INTEGER_64; mini, maxi: INTEGER)
  52. require
  53. mini >= 1
  54. maxi > mini
  55. local
  56. s, nbb1, nbb2: INTEGER; mbi1, mbi2: MUTABLE_BIG_INTEGER
  57. do
  58. create mbi1.from_integer_64(value)
  59. nbb1 := mbi_nb_bit_set(mbi1)
  60. if value >= 0 then
  61. assert(nbb1 = int64_nb_bit_set(value))
  62. end
  63. from
  64. s := mini
  65. until
  66. s > maxi
  67. loop
  68. create mbi2.from_integer_64(value)
  69. -- A `shift_left' first not to looze bits:
  70. mbi2.shift_left(s)
  71. nbb2 := mbi_nb_bit_set(mbi2)
  72. assert(nbb1 = nbb2)
  73. -- Shifting right back of `s' must yield the same number of bits:
  74. mbi2.shift_right(s)
  75. nbb2 := mbi_nb_bit_set(mbi2)
  76. assert(nbb1 = nbb2)
  77. assert(mbi1.is_equal(mbi2))
  78. assert(mbi2.to_integer_64 = value)
  79. s := s + 1
  80. end
  81. end
  82. base: MUTABLE_BIG_INTEGER
  83. once
  84. create Result.from_integer_64(Maximum_integer.to_integer_64 + 1)
  85. end
  86. mbi_nb_bit_set (mbi: MUTABLE_BIG_INTEGER): INTEGER
  87. local
  88. i: INTEGER; mbi_bis, mbi_rem: MUTABLE_BIG_INTEGER
  89. do
  90. from
  91. create mbi_bis.copy(mbi)
  92. if mbi_bis.is_negative then
  93. mbi_bis.negate
  94. end
  95. create mbi_rem.from_integer(0)
  96. until
  97. mbi_bis.is_zero
  98. loop
  99. mbi_bis.divide_with_remainder_to(base, mbi_rem)
  100. Result := Result + int64_nb_bit_set(mbi_rem.to_integer_64)
  101. i := i + 1
  102. end
  103. end
  104. int64_nb_bit_set (v: INTEGER_64): INTEGER
  105. local
  106. i: INTEGER_8
  107. do
  108. from
  109. i := 0
  110. until
  111. i > 63
  112. loop
  113. if v.bit_test(i) then
  114. Result := Result + 1
  115. end
  116. i := i + 1
  117. end
  118. end
  119. int32_nb_bit_set (v: INTEGER_32): INTEGER
  120. local
  121. i: INTEGER_8
  122. do
  123. from
  124. i := 0
  125. until
  126. i > 31
  127. loop
  128. if v.bit_test(i) then
  129. Result := Result + 1
  130. end
  131. i := i + 1
  132. end
  133. end
  134. count: INTEGER
  135. assert (b: BOOLEAN)
  136. do
  137. count := count + 1
  138. if not b then
  139. sedb_breakpoint
  140. io.put_string("TEST_MUTABLE_BIG_INTEGER12 : ERROR Test # ")
  141. io.put_integer(count)
  142. io.put_string("%N")
  143. end
  144. end
  145. end -- class TEST_MUTABLE_BIG_INTEGER12
  146. --
  147. -- ------------------------------------------------------------------------------------------------------------------------------
  148. -- Copyright notice below. Please read.
  149. --
  150. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  151. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  152. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  153. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  154. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  155. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  156. --
  157. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  158. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  159. --
  160. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  161. --
  162. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  163. -- ------------------------------------------------------------------------------------------------------------------------------