Back to index

glibc  2.9
Defines | Functions
stratcliff.c File Reference
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/param.h>
#include "../test-skeleton.c"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define _GNU_SOURCE   1
#define L(c)   c
#define CHAR   char
#define MEMSET   memset
#define STRLEN   strlen
#define STRNLEN   strnlen
#define STRCHR   strchr
#define STRRCHR   strrchr
#define STRCPY   strcpy
#define STRNCPY   strncpy
#define MEMCMP   memcmp
#define STPCPY   stpcpy
#define STPNCPY   stpncpy
#define MEMCPY   memcpy
#define MEMPCPY   mempcpy
#define STRINGIFY(s)   STRINGIFY2 (s)
#define STRINGIFY2(s)   #s
#define TEST_FUNCTION   do_test ()

Functions

static int do_test (void)

Define Documentation

#define _GNU_SOURCE   1

Definition at line 21 of file stratcliff.c.

#define CHAR   char

Definition at line 36 of file stratcliff.c.

#define L (   c)    c

Definition at line 35 of file stratcliff.c.

#define MEMCMP   memcmp

Definition at line 44 of file stratcliff.c.

#define MEMCPY   memcpy

Definition at line 47 of file stratcliff.c.

#define MEMPCPY   mempcpy

Definition at line 48 of file stratcliff.c.

#define MEMSET   memset

Definition at line 37 of file stratcliff.c.

#define STPCPY   stpcpy

Definition at line 45 of file stratcliff.c.

#define STPNCPY   stpncpy

Definition at line 46 of file stratcliff.c.

#define STRCHR   strchr

Definition at line 40 of file stratcliff.c.

#define STRCPY   strcpy

Definition at line 42 of file stratcliff.c.

#define STRINGIFY (   s)    STRINGIFY2 (s)

Definition at line 52 of file stratcliff.c.

#define STRINGIFY2 (   s)    #s

Definition at line 53 of file stratcliff.c.

#define STRLEN   strlen

Definition at line 38 of file stratcliff.c.

#define STRNCPY   strncpy

Definition at line 43 of file stratcliff.c.

#define STRNLEN   strnlen

Definition at line 39 of file stratcliff.c.

#define STRRCHR   strrchr

Definition at line 41 of file stratcliff.c.

#define TEST_FUNCTION   do_test ()

Definition at line 412 of file stratcliff.c.


Function Documentation

static int do_test ( void  ) [static]

Definition at line 57 of file stratcliff.c.

{
  int size = sysconf (_SC_PAGESIZE);
  int nchars = size / sizeof (CHAR);
  CHAR *adr;
  CHAR *dest;
  int result = 0;

  adr = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
                     MAP_PRIVATE | MAP_ANON, -1, 0);
  dest = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
                     MAP_PRIVATE | MAP_ANON, -1, 0);
  if (adr == MAP_FAILED || dest == MAP_FAILED)
    {
      if (errno == ENOSYS)
        puts ("No test, mmap not available.");
      else
        {
          printf ("mmap failed: %m");
          result = 1;
        }
    }
  else
    {
      int inner, middle, outer;

      mprotect (adr, size, PROT_NONE);
      mprotect (adr + 2 * nchars, size, PROT_NONE);
      adr += nchars;

      mprotect (dest, size, PROT_NONE);
      mprotect (dest + 2 * nchars, size, PROT_NONE);
      dest += nchars;

      MEMSET (adr, L('T'), nchars);

      /* strlen/wcslen test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             adr[inner] = L('\0');

             if (STRLEN (&adr[outer]) != (size_t) (inner - outer))
              {
                printf ("%s flunked for outer = %d, inner = %d\n",
                       STRINGIFY (STRLEN), outer, inner);
                result = 1;
              }

             adr[inner] = L('T');
           }
        }

      /* strnlen/wcsnlen test */
      for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             adr[inner] = L('\0');

             if (STRNLEN (&adr[outer], inner - outer + 1)
                != (size_t) (inner - outer))
              {
                printf ("%s flunked for outer = %d, inner = %d\n",
                       STRINGIFY (STRNLEN), outer, inner);
                result = 1;
              }

             adr[inner] = L('T');
           }
        }
      for (outer = nchars; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             if (STRNLEN (&adr[outer], inner - outer + 1)
                != (size_t) (inner - outer + 1))
              {
                printf ("%s flunked bounded for outer = %d, inner = %d\n",
                       STRINGIFY (STRNLEN), outer, inner);
                result = 1;
              }
           }
        }

      /* strchr/wcschr test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
         for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
           {
             for (inner = middle; inner < nchars; ++inner)
              {
                adr[middle] = L('V');
                adr[inner] = L('\0');

                CHAR *cp = STRCHR (&adr[outer], L('V'));

                if ((inner == middle && cp != NULL)
                    || (inner != middle
                       && (cp - &adr[outer]) != middle - outer))
                  {
                    printf ("%s flunked for outer = %d, middle = %d, "
                           "inner = %d\n",
                           STRINGIFY (STRCHR), outer, middle, inner);
                    result = 1;
                  }

                adr[inner] = L('T');
                adr[middle] = L('T');
              }
           }
        }

      /* Special test.  */
      adr[nchars - 1] = L('\0');
      if (STRCHR (&adr[nchars - 1], L('\n')) != NULL)
       {
         printf ("%s flunked test of empty string at end of page\n",
                STRINGIFY (STRCHR));
         result = 1;
       }

      /* strrchr/wcsrchr test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
         for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
           {
             for (inner = middle; inner < nchars; ++inner)
              {
                adr[middle] = L('V');
                adr[inner] = L('\0');

                CHAR *cp = STRRCHR (&adr[outer], L('V'));

                if ((inner == middle && cp != NULL)
                    || (inner != middle
                       && (cp - &adr[outer]) != middle - outer))
                  {
                    printf ("%s flunked for outer = %d, middle = %d, "
                           "inner = %d\n",
                           STRINGIFY (STRRCHR), outer, middle, inner);
                    result = 1;
                  }

                adr[inner] = L('T');
                adr[middle] = L('T');
              }
           }
        }

      /* This function only exists for single-byte characters.  */
#ifndef WCSTEST
      /* rawmemchr test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
         for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
           {
             adr[middle] = L('V');

             CHAR *cp = rawmemchr (&adr[outer], L('V'));

             if (cp - &adr[outer] != middle - outer)
              {
                printf ("%s flunked for outer = %d, middle = %d\n",
                       STRINGIFY (rawmemchr), outer, middle);
                result = 1;
              }

             adr[middle] = L('T');
           }
        }
#endif

      /* strcpy/wcscpy test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             adr[inner] = L('\0');

             if (STRCPY (dest, &adr[outer]) != dest
                || STRLEN (dest) != (size_t) (inner - outer))
              {
                printf ("%s flunked for outer = %d, inner = %d\n",
                       STRINGIFY (STRCPY), outer, inner);
                result = 1;
              }

             adr[inner] = L('T');
           }
        }

      /* strncpy tests */
      adr[nchars - 1] = L('T');
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
       {
         size_t len;

         for (len = 0; len < nchars - outer; ++len)
           {
             if (STRNCPY (dest, &adr[outer], len) != dest
                || MEMCMP (dest, &adr[outer], len) != 0)
              {
                printf ("outer %s flunked for outer = %d, len = %Zd\n",
                       STRINGIFY (STRNCPY), outer, len);
                result = 1;
              }
           }
        }
      adr[nchars - 1] = L('\0');

      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             size_t len;

             adr[inner] = L('\0');

             for (len = 0; len < nchars - outer + 64; ++len)
              {
                if (STRNCPY (dest, &adr[outer], len) != dest
                    || MEMCMP (dest, &adr[outer],
                             MIN (inner - outer, len)) != 0
                    || (inner - outer < len
                       && STRLEN (dest) != (inner - outer)))
                  {
                    printf ("%s flunked for outer = %d, inner = %d, "
                           "len = %Zd\n",
                           STRINGIFY (STRNCPY), outer, inner, len);
                    result = 1;
                  }
                if (STRNCPY (dest + 1, &adr[outer], len) != dest + 1
                    || MEMCMP (dest + 1, &adr[outer],
                             MIN (inner - outer, len)) != 0
                    || (inner - outer < len
                       && STRLEN (dest + 1) != (inner - outer)))
                  {
                    printf ("%s+1 flunked for outer = %d, inner = %d, "
                           "len = %Zd\n",
                           STRINGIFY (STRNCPY), outer, inner, len);
                    result = 1;
                  }
              }

             adr[inner] = L('T');
           }
        }

      /* stpcpy/wcpcpy test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
          for (inner = MAX (outer, nchars - 64); inner < nchars; ++inner)
           {
             adr[inner] = L('\0');

             if ((STPCPY (dest, &adr[outer]) - dest) != inner - outer)
              {
                printf ("%s flunked for outer = %d, inner = %d\n",
                       STRINGIFY (STPCPY), outer, inner);
                result = 1;
              }

             adr[inner] = L('T');
           }
        }

      /* stpncpy/wcpncpy test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
        {
          for (middle = MAX (outer, nchars - 64); middle < nchars; ++middle)
           {
             adr[middle] = L('\0');

             for (inner = 0; inner < nchars - outer; ++ inner)
              {
                if ((STPNCPY (dest, &adr[outer], inner) - dest)
                    != MIN (inner, middle - outer))
                  {
                    printf ("%s flunked for outer = %d, middle = %d, "
                           "inner = %d\n",
                           STRINGIFY (STPNCPY), outer, middle, inner);
                    result = 1;
                  }
              }

             adr[middle] = L('T');
           }
        }

      /* memcpy/wmemcpy test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
       for (inner = 0; inner < nchars - outer; ++inner)
         if (MEMCPY (dest, &adr[outer], inner) !=  dest)
           {
             printf ("%s flunked for outer = %d, inner = %d\n",
                    STRINGIFY (MEMCPY), outer, inner);
             result = 1;
           }

      /* mempcpy/wmempcpy test */
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
       for (inner = 0; inner < nchars - outer; ++inner)
         if (MEMPCPY (dest, &adr[outer], inner) !=  dest + inner)
           {
             printf ("%s flunked for outer = %d, inner = %d\n",
                    STRINGIFY (MEMPCPY), outer, inner);
             result = 1;
           }

      /* This function only exists for single-byte characters.  */
#ifndef WCSTEST
      /* memccpy test */
      memset (adr, '\0', nchars);
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
       for (inner = 0; inner < nchars - outer; ++inner)
         if (memccpy (dest, &adr[outer], L('\1'), inner) != NULL)
           {
             printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
                    outer, inner);
             result = 1;
           }
      for (outer = nchars - 1; outer >= MAX (0, nchars - 128); --outer)
       for (middle = 0; middle < nchars - outer; ++middle)
         {
           memset (dest, L('\2'), middle + 1);
           for (inner = 0; inner < middle; ++inner)
             {
              adr[outer + inner] = L('\1');

              if (memccpy (dest, &adr[outer], '\1', middle + 128)
                  !=  dest + inner + 1)
                {
                  printf ("\
memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
                         outer, middle, inner);
                  result = 1;
                }
              else if (dest[inner + 1] != L('\2'))
                {
                  printf ("\
memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
                         outer, middle, inner);
                  result = 1;
                }
              adr[outer + inner] = L('\0');
             }
         }
#endif
    }

  return result;
}

Here is the call graph for this function: