(root)/
findutils-4.9.0/
gnulib-tests/
test-strtoull.c
       1  /*
       2   * Copyright (C) 2011-2022 Free Software Foundation, Inc.
       3   *
       4   * This program is free software: you can redistribute it and/or modify
       5   * it under the terms of the GNU General Public License as published by
       6   * the Free Software Foundation, either version 3 of the License, or
       7   * (at your option) any later version.
       8   *
       9   * This program is distributed in the hope that it will be useful,
      10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12   * GNU General Public License for more details.
      13   *
      14   * You should have received a copy of the GNU General Public License
      15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  #include <stdlib.h>
      20  
      21  #include "signature.h"
      22  #ifndef strtoull
      23  SIGNATURE_CHECK (strtoull, unsigned long long, (const char *, char **, int));
      24  #endif
      25  
      26  #include <errno.h>
      27  
      28  #include "macros.h"
      29  
      30  int
      31  main (void)
      32  {
      33    /* Subject sequence empty or invalid.  */
      34    {
      35      const char input[] = "";
      36      char *ptr;
      37      unsigned long long result;
      38      errno = 0;
      39      result = strtoull (input, &ptr, 10);
      40      ASSERT (result == 0);
      41      ASSERT (ptr == input);
      42      ASSERT (errno == 0 || errno == EINVAL);
      43    }
      44    {
      45      const char input[] = " ";
      46      char *ptr;
      47      unsigned long long result;
      48      errno = 0;
      49      result = strtoull (input, &ptr, 10);
      50      ASSERT (result == 0);
      51      ASSERT (ptr == input);
      52      ASSERT (errno == 0 || errno == EINVAL);
      53    }
      54    {
      55      const char input[] = " +";
      56      char *ptr;
      57      unsigned long long result;
      58      errno = 0;
      59      result = strtoull (input, &ptr, 10);
      60      ASSERT (result == 0);
      61      ASSERT (ptr == input);
      62      ASSERT (errno == 0 || errno == EINVAL);
      63    }
      64    {
      65      const char input[] = " -";
      66      char *ptr;
      67      unsigned long long result;
      68      errno = 0;
      69      result = strtoull (input, &ptr, 10);
      70      ASSERT (result == 0);
      71      ASSERT (ptr == input);
      72      ASSERT (errno == 0 || errno == EINVAL);
      73    }
      74  
      75    /* Simple integer values.  */
      76    {
      77      const char input[] = "0";
      78      char *ptr;
      79      unsigned long long result;
      80      errno = 0;
      81      result = strtoull (input, &ptr, 10);
      82      ASSERT (result == 0);
      83      ASSERT (ptr == input + 1);
      84      ASSERT (errno == 0);
      85    }
      86    {
      87      const char input[] = "+0";
      88      char *ptr;
      89      unsigned long long result;
      90      errno = 0;
      91      result = strtoull (input, &ptr, 10);
      92      ASSERT (result == 0);
      93      ASSERT (ptr == input + 2);
      94      ASSERT (errno == 0);
      95    }
      96    {
      97      const char input[] = "-0";
      98      char *ptr;
      99      unsigned long long result;
     100      errno = 0;
     101      result = strtoull (input, &ptr, 10);
     102      ASSERT (result == 0);
     103      ASSERT (ptr == input + 2);
     104      ASSERT (errno == 0);
     105    }
     106    {
     107      const char input[] = "23";
     108      char *ptr;
     109      unsigned long long result;
     110      errno = 0;
     111      result = strtoull (input, &ptr, 10);
     112      ASSERT (result == 23);
     113      ASSERT (ptr == input + 2);
     114      ASSERT (errno == 0);
     115    }
     116    {
     117      const char input[] = " 23";
     118      char *ptr;
     119      unsigned long long result;
     120      errno = 0;
     121      result = strtoull (input, &ptr, 10);
     122      ASSERT (result == 23);
     123      ASSERT (ptr == input + 3);
     124      ASSERT (errno == 0);
     125    }
     126    {
     127      const char input[] = "+23";
     128      char *ptr;
     129      unsigned long long result;
     130      errno = 0;
     131      result = strtoull (input, &ptr, 10);
     132      ASSERT (result == 23);
     133      ASSERT (ptr == input + 3);
     134      ASSERT (errno == 0);
     135    }
     136    {
     137      const char input[] = "-23";
     138      char *ptr;
     139      unsigned long long result;
     140      errno = 0;
     141      result = strtoull (input, &ptr, 10);
     142      ASSERT (result == - 23ULL);
     143      ASSERT (ptr == input + 3);
     144      ASSERT (errno == 0);
     145    }
     146  
     147    /* Large integer values.  */
     148    {
     149      const char input[] = "2147483647";
     150      char *ptr;
     151      unsigned long long result;
     152      errno = 0;
     153      result = strtoull (input, &ptr, 10);
     154      ASSERT (result == 2147483647);
     155      ASSERT (ptr == input + 10);
     156      ASSERT (errno == 0);
     157    }
     158    {
     159      const char input[] = "-2147483648";
     160      char *ptr;
     161      unsigned long long result;
     162      errno = 0;
     163      result = strtoull (input, &ptr, 10);
     164      ASSERT (result == - 2147483648ULL);
     165      ASSERT (ptr == input + 11);
     166      ASSERT (errno == 0);
     167    }
     168    {
     169      const char input[] = "4294967295";
     170      char *ptr;
     171      unsigned long long result;
     172      errno = 0;
     173      result = strtoull (input, &ptr, 10);
     174      ASSERT (result == 4294967295U);
     175      ASSERT (ptr == input + 10);
     176      ASSERT (errno == 0);
     177    }
     178  
     179    /* Hexadecimal integer syntax.  */
     180    {
     181      const char input[] = "0x2A";
     182      char *ptr;
     183      unsigned long long result;
     184      errno = 0;
     185      result = strtoull (input, &ptr, 10);
     186      ASSERT (result == 0ULL);
     187      ASSERT (ptr == input + 1);
     188      ASSERT (errno == 0);
     189    }
     190    {
     191      const char input[] = "0x2A";
     192      char *ptr;
     193      unsigned long long result;
     194      errno = 0;
     195      result = strtoull (input, &ptr, 16);
     196      ASSERT (result == 42ULL);
     197      ASSERT (ptr == input + 4);
     198      ASSERT (errno == 0);
     199    }
     200    {
     201      const char input[] = "0x2A";
     202      char *ptr;
     203      unsigned long long result;
     204      errno = 0;
     205      result = strtoull (input, &ptr, 0);
     206      ASSERT (result == 42ULL);
     207      ASSERT (ptr == input + 4);
     208      ASSERT (errno == 0);
     209    }
     210    {
     211      const char input[] = "0x";
     212      char *ptr;
     213      unsigned long long result;
     214      errno = 0;
     215      result = strtoull (input, &ptr, 10);
     216      ASSERT (result == 0ULL);
     217      ASSERT (ptr == input + 1);
     218      ASSERT (errno == 0);
     219    }
     220    {
     221      const char input[] = "0x";
     222      char *ptr;
     223      unsigned long long result;
     224      errno = 0;
     225      result = strtoull (input, &ptr, 16);
     226      ASSERT (result == 0ULL);
     227      ASSERT (ptr == input + 1);
     228      ASSERT (errno == 0);
     229    }
     230    {
     231      const char input[] = "0x";
     232      char *ptr;
     233      unsigned long long result;
     234      errno = 0;
     235      result = strtoull (input, &ptr, 0);
     236      ASSERT (result == 0ULL);
     237      ASSERT (ptr == input + 1);
     238      ASSERT (errno == 0);
     239    }
     240  
     241    return 0;
     242  }