PenaltyFloat_test.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "memory/Allocator.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "switch/PenaltyFloat.h"
  19. #include "util/Assert.h"
  20. static void packUnpack(struct Random* rand)
  21. {
  22. for (int i = 0; i < 1000; i++) {
  23. uint64_t p = Random_uint64(rand);
  24. p >>= (i % 64);
  25. uint32_t c1 = PenaltyFloat_pack(p);
  26. uint64_t p1 = PenaltyFloat_unpack(c1);
  27. uint32_t c2 = PenaltyFloat_pack(p1);
  28. uint64_t p2 = PenaltyFloat_unpack(c2);
  29. Assert_true(!(c1 >> 22));
  30. Assert_true(c1 == c2);
  31. Assert_true(p1 == p2);
  32. }
  33. Assert_true(PenaltyFloat_MAX == PenaltyFloat_pack(UINT64_MAX));
  34. }
  35. int main()
  36. {
  37. struct Allocator* alloc = MallocAllocator_new(20000000);
  38. struct Random* rand = Random_new(alloc, NULL, NULL);
  39. packUnpack(rand);
  40. return 0;
  41. }