source: mainline/uspace/lib/c/test/sprintf.c

Last change on this file was 2ef2a0d, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Add a few printf() formatting tests

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2014 Vojtech Horky
3 * Copyright (c) 2025 Jiří Zárevúcky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31#include <str.h>
32#include <pcut/pcut.h>
33
34#pragma GCC diagnostic ignored "-Wformat"
35
36#define BUFFER_SIZE 8192
37#define TEQ(expected, actual) PCUT_ASSERT_STR_EQUALS(expected, actual)
38#define TF(expected, format, ...) TEQ(expected, fmt(format, ##__VA_ARGS__))
39
40#define SPRINTF_TEST(test_name, expected_string, actual_format, ...) \
41 PCUT_TEST(printf_##test_name) { \
42 snprintf(buffer, BUFFER_SIZE, actual_format, ##__VA_ARGS__); \
43 PCUT_ASSERT_STR_EQUALS(expected_string, buffer); \
44 }
45
46PCUT_INIT;
47
48PCUT_TEST_SUITE(sprintf);
49
50static char buffer[BUFFER_SIZE];
51
52PCUT_TEST_BEFORE
53{
54 memset(buffer, 0, BUFFER_SIZE);
55}
56
57SPRINTF_TEST(no_formatting, "This is a test.", "This is a test.");
58
59SPRINTF_TEST(string_plain, "some text", "%s", "some text");
60
61SPRINTF_TEST(string_dynamic_width, " tex", "%*.*s", 5, 3, "text");
62
63SPRINTF_TEST(string_dynamic_width_align_left, "text ", "%-*.*s", 7, 7, "text");
64
65SPRINTF_TEST(string_pad, " text", "%8.10s", "text");
66
67SPRINTF_TEST(string_pad_but_cut, " very lon", "%10.8s", "very long text");
68
69SPRINTF_TEST(char_basic, "[a]", "[%c]", 'a');
70
71SPRINTF_TEST(int_various_padding, "[1] [ 02] [03 ] [004] [005]",
72 "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
73 1, 2, 3, 4, 5);
74
75SPRINTF_TEST(int_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
76 "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
77 -1, -2, -3, -4, -5);
78
79SPRINTF_TEST(long_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
80 "[%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]",
81 (long long) -1, (long long) -2, (long long) -3, (long long) -4,
82 (long long) -5);
83
84SPRINTF_TEST(int_as_hex, "[1a] [ 02b] [03c ] [ 04d] [05e ] [0006f] [00080]",
85 "[%x] [%5.3x] [%-5.3x] [%7.3x] [%-7.3x] [%3.5x] [%-3.5x]",
86 26, 43, 60, 77, 94, 111, 128);
87
88SPRINTF_TEST(int_as_hex_alt, "[0x1a] [0x02b] [0x03c] [ 0x04d] [0x05e ] [0x0006f] [0x00080]",
89 "[%#x] [%#5.3x] [%#-5.3x] [%#7.3x] [%#-7.3x] [%#3.5x] [%#-3.5x]",
90 26, 43, 60, 77, 94, 111, 128);
91
92SPRINTF_TEST(int_as_hex_uc, "[1A] [ 02B] [03C ] [ 04D] [05E ] [0006F] [00080]",
93 "[%X] [%5.3X] [%-5.3X] [%7.3X] [%-7.3X] [%3.5X] [%-3.5X]",
94 26, 43, 60, 77, 94, 111, 128);
95
96SPRINTF_TEST(int_as_hex_alt_uc, "[0X1A] [0X02B] [0X03C] [ 0X04D] [0X05E ] [0X0006F] [0X00080]",
97 "[%#X] [%#5.3X] [%#-5.3X] [%#7.3X] [%#-7.3X] [%#3.5X] [%#-3.5X]",
98 26, 43, 60, 77, 94, 111, 128);
99
100SPRINTF_TEST(max_negative, "-9223372036854775808", "%" PRId64, INT64_MIN);
101
102SPRINTF_TEST(sign1, "[12] [ 12] [+12] [+12] [+12] [+12]", "[%d] [% d] [%+d] [% +d] [%+ d] [%++ ++ + ++++d]", 12, 12, 12, 12, 12, 12);
103SPRINTF_TEST(sign2, "[-12] [-12] [-12] [-12] [-12] [-12]", "[%d] [% d] [%+d] [% +d] [%+ d] [%++ ++ + ++++d]", -12, -12, -12, -12, -12, -12);
104
105/* When zero padding and precision and/or left justification are both specified, zero padding is ignored. */
106SPRINTF_TEST(zero_left_padding, "[ 0012] [0034 ] [56 ]", "[%08.4d] [%-08.4d] [%-08d]", 12, 34, 56);
107
108/* Zero padding comes after the sign, but space padding doesn't. */
109SPRINTF_TEST(sign_padding, "[00012] [ 12] [ 0012] [ 12] [+0012] [ +12]", "[%05d] [%5d] [%0 5d] [% 5d] [%0+5d] [%+5d]", 12, 12, 12, 12, 12, 12);
110SPRINTF_TEST(sign_padding2, "[-0012] [ -12] [-0012] [ -12] [-0012] [ -12]", "[%05d] [%5d] [%0 5d] [% 5d] [%0+5d] [%+5d]", -12, -12, -12, -12, -12, -12);
111
112SPRINTF_TEST(all_zero, "[00000] [0] [0] [0] [0] [0] [0] [0] [0]", "[%05d] [%d] [%x] [%#x] [%o] [%#o] [%b] [%#b] [%u]", 0, 0, 0, 0, 0, 0, 0, 0, 0);
113
114PCUT_EXPORT(sprintf);
Note: See TracBrowser for help on using the repository browser.