GNU Linux-libre 4.19.211-gnu1
[releases.git] / tools / perf / tests / unit_number__scnprintf.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <linux/compiler.h>
4 #include <linux/types.h>
5 #include "tests.h"
6 #include "units.h"
7 #include "debug.h"
8
9 int test__unit_number__scnprint(struct test *t __maybe_unused, int subtest __maybe_unused)
10 {
11         struct {
12                 u64              n;
13                 const char      *str;
14         } test[] = {
15                 { 1,                    "1B"    },
16                 { 10*1024,              "10K"   },
17                 { 20*1024*1024,         "20M"   },
18                 { 30*1024*1024*1024ULL, "30G"   },
19                 { 0,                    "0B"    },
20                 { 0,                    NULL    },
21         };
22         unsigned i = 0;
23
24         while (test[i].str) {
25                 char buf[100];
26
27                 unit_number__scnprintf(buf, sizeof(buf), test[i].n);
28
29                 pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
30                          test[i].n, test[i].str, buf);
31
32                 if (strcmp(test[i].str, buf))
33                         return TEST_FAIL;
34
35                 i++;
36         }
37
38         return TEST_OK;
39 }