Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <inttypes.h>
0003 #include <linux/compiler.h>
0004 #include <linux/types.h>
0005 #include <string.h>
0006 #include "tests.h"
0007 #include "units.h"
0008 #include "debug.h"
0009 
0010 static int test__unit_number__scnprint(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
0011 {
0012     struct {
0013         u64      n;
0014         const char  *str;
0015     } test[] = {
0016         { 1,            "1B"    },
0017         { 10*1024,      "10K"   },
0018         { 20*1024*1024,     "20M"   },
0019         { 30*1024*1024*1024ULL, "30G"   },
0020         { 0,            "0B"    },
0021         { 0,            NULL    },
0022     };
0023     unsigned i = 0;
0024 
0025     while (test[i].str) {
0026         char buf[100];
0027 
0028         unit_number__scnprintf(buf, sizeof(buf), test[i].n);
0029 
0030         pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
0031              test[i].n, test[i].str, buf);
0032 
0033         if (strcmp(test[i].str, buf))
0034             return TEST_FAIL;
0035 
0036         i++;
0037     }
0038 
0039     return TEST_OK;
0040 }
0041 
0042 DEFINE_SUITE("unit_number__scnprintf", unit_number__scnprint);