Back to index

glibc  2.9
test-ffs.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1997, 2000, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Joel Sherrill (jsherril@redstone-emh2.army.mil),
00004      On-Line Applications Research Corporation.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 
00025 int
00026 main (void)
00027 {
00028   int failures = 0;
00029   int i;
00030 
00031   auto void try (const char *name, long long int param, int value,
00032                int expected);
00033 
00034   void try (const char *name, long long int param, int value, int expected)
00035     {
00036       if (value != expected)
00037        {
00038          printf ("%s(%#llx) expected %d got %d\n",
00039                 name, param, expected, value);
00040          ++failures;
00041        }
00042       else
00043        printf ("%s(%#llx) as expected %d\n", name, param, value);
00044     }
00045 
00046 #define TEST(fct, type) \
00047   try (#fct, 0, fct ((type) 0), 0);                                         \
00048   for (i=0 ; i < 8 * sizeof (type); i++)                             \
00049     try (#fct, 1ll << i, fct (((type) 1) << i), i + 1);                     \
00050   for (i=0 ; i < 8 * sizeof (type) ; i++)                            \
00051     try (#fct, (~((type) 0) >> i) << i, fct ((~((type) 0) >> i) << i), i + 1);\
00052   try (#fct, 0x80008000, fct ((type) 0x80008000), 16)
00053 
00054   TEST (ffs, int);
00055   TEST (ffsl, long int);
00056   TEST (ffsll, long long int);
00057 
00058   if (failures)
00059     printf ("Test FAILED!  %d failure%s.\n", failures, &"s"[failures == 1]);
00060   else
00061     puts ("Test succeeded.");
00062 
00063   return failures;
00064 }