perf tests: Add test for bitmap_scnprintf function
authorJiri Olsa <jolsa@kernel.org>
Mon, 1 Aug 2016 18:02:32 +0000 (20:02 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 2 Aug 2016 19:33:27 +0000 (16:33 -0300)
Automatically test the bitmap_scnprintf function.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1470074555-24889-5-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/Build
tools/perf/tests/bitmap.c [new file with mode: 0644]
tools/perf/tests/builtin-test.c
tools/perf/tests/tests.h

index cb20ae1c0d350a8547f5d217b25f4895e451be9d..dc51bc570e51fe5e3eac5fc8a7f21e360f6b7ed7 100644 (file)
@@ -41,6 +41,7 @@ perf-y += event-times.o
 perf-y += backward-ring-buffer.o
 perf-y += sdt.o
 perf-y += is_printable_array.o
+perf-y += bitmap.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
        $(call rule_mkdir)
diff --git a/tools/perf/tests/bitmap.c b/tools/perf/tests/bitmap.c
new file mode 100644 (file)
index 0000000..9abe6c1
--- /dev/null
@@ -0,0 +1,53 @@
+#include <linux/compiler.h>
+#include <linux/bitmap.h>
+#include "tests.h"
+#include "cpumap.h"
+#include "debug.h"
+
+#define NBITS 100
+
+static unsigned long *get_bitmap(const char *str, int nbits)
+{
+       struct cpu_map *map = cpu_map__new(str);
+       unsigned long *bm = NULL;
+       int i;
+
+       bm = bitmap_alloc(nbits);
+
+       if (map && bm) {
+               bitmap_zero(bm, nbits);
+
+               for (i = 0; i < map->nr; i++)
+                       set_bit(map->map[i], bm);
+       }
+
+       if (map)
+               cpu_map__put(map);
+       return bm;
+}
+
+static int test_bitmap(const char *str)
+{
+       unsigned long *bm = get_bitmap(str, NBITS);
+       char buf[100];
+       int ret;
+
+       bitmap_scnprintf(bm, NBITS, buf, sizeof(buf));
+       pr_debug("bitmap: %s\n", buf);
+
+       ret = !strcmp(buf, str);
+       free(bm);
+       return ret;
+}
+
+int test__bitmap_print(int subtest __maybe_unused)
+{
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,5"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3,5,7,9,11,13,15,17,19,21-40"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("2-5"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
+       TEST_ASSERT_VAL("failed to convert map", test_bitmap("1-10,12-20,22-30,32-40"));
+       return 0;
+}
index 10eb30686c9c3d06b80fcc13d176390ee0b934ee..778668a2a9661c7402135d62f4cefe69ceb04001 100644 (file)
@@ -225,6 +225,10 @@ static struct test generic_tests[] = {
                .desc = "Test is_printable_array function",
                .func = test__is_printable_array,
        },
+       {
+               .desc = "Test bitmap print",
+               .func = test__bitmap_print,
+       },
        {
                .func = NULL,
        },
index 9bfc0e06c61aaebae51319a896c6ed8a82096445..7c196c585472f482755bda72a3abf1bcd68a6c44 100644 (file)
@@ -90,6 +90,7 @@ int test__backward_ring_buffer(int subtest);
 int test__cpu_map_print(int subtest);
 int test__sdt_event(int subtest);
 int test__is_printable_array(int subtest);
+int test__bitmap_print(int subtest);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT