PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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