Base10_test.c 1.7 KB

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