5593c8
/*
5593c8
 *  GRUB  --  GRand Unified Bootloader
5593c8
 *  Copyright (C) 2016 Free Software Foundation, Inc.
5593c8
 *
5593c8
 *  GRUB is free software: you can redistribute it and/or modify
5593c8
 *  it under the terms of the GNU General Public License as published by
5593c8
 *  the Free Software Foundation, either version 3 of the License, or
5593c8
 *  (at your option) any later version.
5593c8
 *
5593c8
 *  GRUB is distributed in the hope that it will be useful,
5593c8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
5593c8
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5593c8
 *  GNU General Public License for more details.
5593c8
 *
5593c8
 *  You should have received a copy of the GNU General Public License
5593c8
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
5593c8
 */
5593c8
5593c8
#include <grub/test.h>
5593c8
#include <grub/dl.h>
5593c8
5593c8
GRUB_MOD_LICENSE ("GPLv3+");
5593c8
5593c8
static void
5593c8
strtoull_testcase (const char *input, int base, unsigned long long expected,
5593c8
		   int num_digits, grub_err_t error)
5593c8
{
5593c8
  char *output;
5593c8
  unsigned long long value;
5593c8
  grub_errno = 0;
5593c8
  value = grub_strtoull(input, &output, base);
5593c8
  grub_test_assert (grub_errno == error,
5593c8
		    "unexpected error. Expected %d, got %d. Input \"%s\"",
5593c8
		    error, grub_errno, input);
5593c8
  if (grub_errno)
5593c8
    {
5593c8
      grub_errno = 0;
5593c8
      return;
5593c8
    }
5593c8
  grub_test_assert (input + num_digits == output,
5593c8
		    "unexpected number of digits. Expected %d, got %d, input \"%s\"",
5593c8
		    num_digits, (int) (output - input), input);
5593c8
  grub_test_assert (value == expected,
5593c8
		    "unexpected return value. Expected %llu, got %llu, input \"\%s\"",
5593c8
		    expected, value, input);
5593c8
}
5593c8
5593c8
static void
5593c8
strtoull_test (void)
5593c8
{
5593c8
  strtoull_testcase ("9", 0, 9, 1, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("0xaa", 0, 0xaa, 4, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("0xff", 0, 0xff, 4, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("0", 10, 0, 1, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("8", 8, 0, 0, GRUB_ERR_BAD_NUMBER);
5593c8
  strtoull_testcase ("38", 8, 3, 1, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("7", 8, 7, 1, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("1]", 16, 1, 1, GRUB_ERR_NONE);
5593c8
  strtoull_testcase ("18446744073709551616", 10, 0, 0, GRUB_ERR_OUT_OF_RANGE);
5593c8
}
5593c8
5593c8
5593c8
GRUB_FUNCTIONAL_TEST (strtoull_test, strtoull_test);