ffsl, ffsll: new modules
[gnulib.git] / tests / test-ffsll.c
diff --git a/tests/test-ffsll.c b/tests/test-ffsll.c
new file mode 100644 (file)
index 0000000..c0fd426
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake.  */
+#include <config.h>
+
+#include <string.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (ffsll, int, (long long int));
+
+#include <limits.h>
+
+#include "macros.h"
+
+static int
+naive (long long int i)
+{
+  unsigned long long int j;
+  for (j = 0; j < CHAR_BIT * sizeof i; j++)
+    if (i & (1ULL << j))
+      return j + 1;
+  return 0;
+}
+
+int
+main (int argc, char *argv[])
+{
+  long long int i;
+
+  for (i = -128; i <= 128; i++)
+    ASSERT (ffsll (i) == naive (i));
+  for (i = 0; i < CHAR_BIT * sizeof i; i++)
+    {
+      ASSERT (ffsll (1ULL << i) == naive (1ULL << i));
+      ASSERT (ffsll (1ULL << i) == i + 1);
+    }
+  return 0;
+}