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