MIPS: Fix early CM probing
[deliverable/linux.git] / lib / test_printf.c
index 1ce1a1dd8faf4ee3748cf4d911598fe575b3f799..4f6ae60433bc5d1e1d6df9b7c8d5d359512ad82a 100644 (file)
@@ -12,6 +12,8 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 
+#include <linux/bitmap.h>
+#include <linux/dcache.h>
 #include <linux/socket.h>
 #include <linux/in.h>
 
@@ -127,6 +129,7 @@ __test(const char *expect, int elen, const char *fmt, ...)
 
        p = kvasprintf(GFP_KERNEL, fmt, ap);
        if (p) {
+               total_tests++;
                if (memcmp(p, expect, elen+1)) {
                        pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
                                fmt, p, expect);
@@ -158,6 +161,30 @@ test_number(void)
        test("0x1234abcd  ", "%#-12x", 0x1234abcd);
        test("  0x1234abcd", "%#12x", 0x1234abcd);
        test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
+       test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
+       test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
+       test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
+       /*
+        * POSIX/C99: »The result of converting zero with an explicit
+        * precision of zero shall be no characters.« Hence the output
+        * from the below test should really be "00|0||| ". However,
+        * the kernel's printf also produces a single 0 in that
+        * case. This test case simply documents the current
+        * behaviour.
+        */
+       test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
+#ifndef __CHAR_UNSIGNED__
+       {
+               /*
+                * Passing a 'char' to a %02x specifier doesn't do
+                * what was presumably the intention when char is
+                * signed and the value is negative. One must either &
+                * with 0xff or cast to u8.
+                */
+               char val = -16;
+               test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
+       }
+#endif
 }
 
 static void __init
@@ -166,14 +193,23 @@ test_string(void)
        test("", "%s%.0s", "", "123");
        test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
        test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
+       test("1234      ", "%-10.4s", "123456");
+       test("      1234", "%10.4s", "123456");
        /*
-        * POSIX and C99 say that a missing precision should be
-        * treated as a precision of 0. However, the kernel's printf
-        * implementation treats this case as if the . wasn't
-        * present. Let's add a test case documenting the current
-        * behaviour; should anyone ever feel the need to follow the
-        * standards more closely, this can be revisited.
+        * POSIX and C99 say that a negative precision (which is only
+        * possible to pass via a * argument) should be treated as if
+        * the precision wasn't present, and that if the precision is
+        * omitted (as in %.s), the precision should be taken to be
+        * 0. However, the kernel's printf behave exactly opposite,
+        * treating a negative precision as 0 and treating an omitted
+        * precision specifier as if no precision was given.
+        *
+        * These test cases document the current behaviour; should
+        * anyone ever feel the need to follow the standards more
+        * closely, this can be revisited.
         */
+       test("    ", "%4.*s", -5, "123456");
+       test("123456", "%.s", "123456");
        test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
        test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
 }
@@ -291,9 +327,35 @@ uuid(void)
        test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
 }
 
+static struct dentry test_dentry[4] __initdata = {
+       { .d_parent = &test_dentry[0],
+         .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
+         .d_iname = "foo" },
+       { .d_parent = &test_dentry[0],
+         .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
+         .d_iname = "bravo" },
+       { .d_parent = &test_dentry[1],
+         .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
+         .d_iname = "alfa" },
+       { .d_parent = &test_dentry[2],
+         .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
+         .d_iname = "romeo" },
+};
+
 static void __init
 dentry(void)
 {
+       test("foo", "%pd", &test_dentry[0]);
+       test("foo", "%pd2", &test_dentry[0]);
+
+       test("romeo", "%pd", &test_dentry[3]);
+       test("alfa/romeo", "%pd2", &test_dentry[3]);
+       test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
+       test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
+       test("/bravo/alfa", "%pd4", &test_dentry[2]);
+
+       test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
+       test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
 }
 
 static void __init
@@ -306,6 +368,20 @@ struct_clk(void)
 {
 }
 
+static void __init
+large_bitmap(void)
+{
+       const int nbits = 1 << 16;
+       unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
+       if (!bits)
+               return;
+
+       bitmap_set(bits, 1, 20);
+       bitmap_set(bits, 60000, 15);
+       test("1-20,60000-60014", "%*pbl", nbits, bits);
+       kfree(bits);
+}
+
 static void __init
 bitmap(void)
 {
@@ -325,6 +401,8 @@ bitmap(void)
        bitmap_fill(bits, 20);
        test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
        test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
+
+       large_bitmap();
 }
 
 static void __init
This page took 0.031924 seconds and 5 git commands to generate.