+/*
+ * Copyright (c) 2011 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "random.h"
+#include "util.h"
+
+static void
+check(uint32_t x, int n)
+{
+ if (log_2_floor(x) != n) {
+ fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
+ x, log_2_floor(x), n);
+ abort();
+ }
+}
+
+int
+main(void)
+{
+ int n;
+
+ for (n = 0; n < 32; n++) {
+ /* Check minimum x that has log2(x) == n. */
+ check(1 << n, n);
+
+ /* Check maximum x that has log2(x) == n. */
+ check((1 << n) | ((1 << n) - 1), n);
+
+ /* Check a random value in the middle. */
+ check((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
+ }
+ return 0;
+}