Base10_test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "memory/Allocator.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "crypto/random/Random.h"
  18. #include "wire/Message.h"
  19. #include "util/Base10.h"
  20. #include "util/Assert.h"
  21. #include "util/CString.h"
  22. #include <stdio.h>
  23. int main()
  24. {
  25. struct Allocator* alloc = MallocAllocator_new(1<<22);
  26. struct Random* rand = Random_new(alloc, NULL, NULL);
  27. struct Message* msg = Message_new(0, 32, alloc);
  28. uint8_t buff[32] = {0};
  29. for (int i = 0; i < 1000; i++) {
  30. // zero it
  31. Message_reset(msg);
  32. Message_push(msg, NULL, 32, NULL);
  33. Message_reset(msg);
  34. long long num = Random_int64(rand);
  35. Base10_write(msg, num, NULL);
  36. snprintf(buff, 31, "%lld", num);
  37. Assert_true(msg->length == (int)CString_strlen(buff));
  38. Assert_true(!Bits_memcmp(msg->bytes, buff, msg->length));
  39. int64_t read = Base10_read(msg, NULL);
  40. Assert_true(read == num);
  41. Assert_true(msg->length == 0);
  42. }
  43. Allocator_free(alloc);
  44. return 0;
  45. }