summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKmindg <kmindg@gmail.com>2014-07-31 13:16:04 +0800
committerBen Pfaff <blp@nicira.com>2014-07-31 16:28:37 -0700
commitf831a47297a49b67772869ffb906c77d1e1410d0 (patch)
tree30b62a1c6c261bf66ccbead41f640ed7f40329cf /tests
parente18df54e5426b5e7fa2a881c690e85dc2ba94064 (diff)
downloadopenvswitch-f831a47297a49b67772869ffb906c77d1e1410d0.tar.gz
bitmap: Add test for bitmap_equal and bitmap_scan
Suggested-by: Ben Pfaff <blp@nicira.com> Signed-off-by: Kmindg <kmindg@gmail.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/automake.mk1
-rw-r--r--tests/library.at5
-rw-r--r--tests/test-bitmap.c160
3 files changed, 166 insertions, 0 deletions
diff --git a/tests/automake.mk b/tests/automake.mk
index 1361fc6d6..2b88ad29a 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -244,6 +244,7 @@ tests_ovstest_SOURCES = \
tests/test-stp.c \
tests/test-util.c \
tests/test-uuid.c \
+ tests/test-bitmap.c \
tests/test-vconn.c
if !WIN32
diff --git a/tests/library.at b/tests/library.at
index a5dfd82ba..ca174bb47 100644
--- a/tests/library.at
+++ b/tests/library.at
@@ -200,3 +200,8 @@ AT_CLEANUP
AT_SETUP([snprintf])
AT_CHECK([ovstest test-util snprintf])
AT_CLEANUP
+
+AT_SETUP([test bitmap functions])
+AT_CHECK([ovstest test-bitmap check], [0], [..
+])
+AT_CLEANUP
diff --git a/tests/test-bitmap.c b/tests/test-bitmap.c
new file mode 100644
index 000000000..b1274e3ed
--- /dev/null
+++ b/tests/test-bitmap.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2014 Kmindg <kmindg@gmail.com>
+ *
+ * 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 "bitmap.h"
+#include "ovstest.h"
+#include "timeval.h"
+#include "command-line.h"
+
+#undef NDEBUG
+#include <assert.h>
+
+enum { MAX_BITS = 20 * BITMAP_ULONG_BITS };
+
+static int
+elapsed(const struct timeval *start)
+{
+ struct timeval end;
+
+ xgettimeofday(&end);
+ return timeval_to_msec(&end) - timeval_to_msec(start);
+}
+
+/* Tests bitmap_equal. */
+static void
+test_bitmap_equal(void)
+{
+ unsigned long *a, *b;
+
+ a = bitmap_allocate(MAX_BITS);
+ b = bitmap_allocate(MAX_BITS);
+
+ /* equal test */
+ assert(bitmap_equal(a, b, MAX_BITS));
+ assert(bitmap_equal(a, b, MAX_BITS - 1));
+ assert(bitmap_equal(a, b, MAX_BITS - (BITMAP_ULONG_BITS - 1)));
+
+ bitmap_set_multiple(a, 10 * BITMAP_ULONG_BITS, BITMAP_ULONG_BITS, true);
+ assert(bitmap_equal(a, b, 10 * BITMAP_ULONG_BITS));
+
+ /* non-equal test */
+ assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS));
+ assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS - 1));
+ assert(!bitmap_equal(a, b,
+ 11 * BITMAP_ULONG_BITS - (BITMAP_ULONG_BITS - 1)));
+}
+
+/* Tests bitmap_scan. */
+static void
+test_bitmap_scan(void)
+{
+ unsigned long *a;
+
+ a = bitmap_allocate(MAX_BITS);
+
+ /* scan for 1 */
+ assert(bitmap_scan(a, true, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, true, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
+ == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS + 1)
+ == BITMAP_ULONG_BITS + 1);
+ assert(bitmap_scan(a, true, 0, 2 * BITMAP_ULONG_BITS - 1)
+ == 2 * BITMAP_ULONG_BITS - 1);
+
+ bitmap_set1(a, MAX_BITS - 1);
+ assert(bitmap_scan(a, true, 0, MAX_BITS) == MAX_BITS - 1);
+ bitmap_set1(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
+ assert(bitmap_scan(a, true, 0, MAX_BITS - 1)
+ == MAX_BITS - BITMAP_ULONG_BITS + 1);
+ bitmap_set1(a, BITMAP_ULONG_BITS - 1);
+ assert(bitmap_scan(a, true, 0, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
+ bitmap_set1(a, 0);
+ assert(bitmap_scan(a, true, 0, MAX_BITS - 1) == 0);
+
+ bitmap_set_multiple(a, 0, MAX_BITS, true);
+
+ /* scan for 0 */
+ assert(bitmap_scan(a, false, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, false, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
+ == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
+ assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS + 1)
+ == BITMAP_ULONG_BITS + 1);
+ assert(bitmap_scan(a, false, 0, 2 * BITMAP_ULONG_BITS - 1)
+ == 2 * BITMAP_ULONG_BITS - 1);
+
+ bitmap_set0(a, MAX_BITS - 1);
+ assert(bitmap_scan(a, false, 0, MAX_BITS) == MAX_BITS - 1);
+ bitmap_set0(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
+ assert(bitmap_scan(a, false, 0, MAX_BITS - 1)
+ == MAX_BITS - BITMAP_ULONG_BITS + 1);
+ bitmap_set0(a, BITMAP_ULONG_BITS - 1);
+ assert(bitmap_scan(a, false, 0, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
+ bitmap_set0(a, 0);
+ assert(bitmap_scan(a, false, 0, MAX_BITS - 1) == 0);
+}
+
+static void
+run_test(void (*function)(void))
+{
+ function();
+ printf(".");
+}
+
+static void
+run_tests(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ run_test(test_bitmap_equal);
+ run_test(test_bitmap_scan);
+ printf("\n");
+}
+
+static void
+run_benchmarks(int argc OVS_UNUSED, char *argv[])
+{
+ int n_iter = strtol(argv[1], NULL, 10);
+ struct timeval start;
+
+ xgettimeofday(&start);
+ for (int i = 0; i < n_iter; i++) {
+ test_bitmap_equal();
+ }
+ printf("bitmap equal: %5d ms\n", elapsed(&start));
+
+ xgettimeofday(&start);
+ for (int i = 0; i < n_iter; i++) {
+ test_bitmap_scan();
+ }
+ printf("bitmap scan: %5d ms\n", elapsed(&start));
+ printf("\n");
+}
+
+static const struct command commands[] = {
+ {"check", 0, 0, run_tests},
+ {"benchmark", 1, 1, run_benchmarks},
+ {NULL, 0, 0, NULL},
+};
+
+static void
+test_bitmap_main(int argc, char *argv[])
+{
+ set_program_name(argv[0]);
+ run_command(argc - 1, argv + 1, commands);
+}
+
+OVSTEST_REGISTER("test-bitmap", test_bitmap_main);