strtoull_test.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2016 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/test.h>
  19. #include <grub/dl.h>
  20. GRUB_MOD_LICENSE ("GPLv3+");
  21. static void
  22. strtoull_testcase (const char *input, int base, unsigned long long expected,
  23. int num_digits, grub_err_t error)
  24. {
  25. char *output;
  26. unsigned long long value;
  27. grub_errno = 0;
  28. value = grub_strtoull(input, &output, base);
  29. grub_test_assert (grub_errno == error,
  30. "unexpected error. Expected %d, got %d. Input \"%s\"",
  31. error, grub_errno, input);
  32. if (grub_errno)
  33. {
  34. grub_errno = 0;
  35. return;
  36. }
  37. grub_test_assert (input + num_digits == output,
  38. "unexpected number of digits. Expected %d, got %d, input \"%s\"",
  39. num_digits, (int) (output - input), input);
  40. grub_test_assert (value == expected,
  41. "unexpected return value. Expected %llu, got %llu, input \"\%s\"",
  42. expected, value, input);
  43. }
  44. static void
  45. strtoull_test (void)
  46. {
  47. strtoull_testcase ("9", 0, 9, 1, GRUB_ERR_NONE);
  48. strtoull_testcase ("0xaa", 0, 0xaa, 4, GRUB_ERR_NONE);
  49. strtoull_testcase ("0xff", 0, 0xff, 4, GRUB_ERR_NONE);
  50. strtoull_testcase ("0", 10, 0, 1, GRUB_ERR_NONE);
  51. strtoull_testcase ("8", 8, 0, 0, GRUB_ERR_BAD_NUMBER);
  52. strtoull_testcase ("38", 8, 3, 1, GRUB_ERR_NONE);
  53. strtoull_testcase ("7", 8, 7, 1, GRUB_ERR_NONE);
  54. strtoull_testcase ("1]", 16, 1, 1, GRUB_ERR_NONE);
  55. strtoull_testcase ("18446744073709551616", 10, 0, 0, GRUB_ERR_OUT_OF_RANGE);
  56. }
  57. GRUB_FUNCTIONAL_TEST (strtoull_test, strtoull_test);