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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * Copyright (c) 2014 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <str.h>
31#include <pcut/pcut.h>
32
33#define BUFFER_SIZE 8192
34#define TEQ(expected, actual) PCUT_ASSERT_STR_EQUALS(expected, actual)
35#define TF(expected, format, ...) TEQ(expected, fmt(format, ##__VA_ARGS__))
36
37
38#define SPRINTF_TEST(test_name, expected_string, actual_format, ...) \
39 PCUT_TEST(test_name) { \
40 snprintf(buffer, BUFFER_SIZE, actual_format, ##__VA_ARGS__); \
41 PCUT_ASSERT_STR_EQUALS(expected_string, buffer); \
42 }
43
44PCUT_INIT
45
46PCUT_TEST_SUITE(sprintf);
47
48static char buffer[BUFFER_SIZE];
49
50PCUT_TEST_BEFORE {
51 memset(buffer, 0, BUFFER_SIZE);
52}
53
54
55
56SPRINTF_TEST(no_formatting, "This is a test.", "This is a test.")
57
58
59
60SPRINTF_TEST(string_plain, "some text", "%s", "some text")
61
62SPRINTF_TEST(string_dynamic_width, " tex", "%*.*s", 5, 3, "text")
63
64SPRINTF_TEST(string_dynamic_width_align_left, "text ", "%-*.*s", 7, 7, "text")
65
66SPRINTF_TEST(string_pad, " text", "%8.10s", "text")
67
68SPRINTF_TEST(string_pad_but_cut, " very lon", "%10.8s", "very long text")
69
70
71
72SPRINTF_TEST(char_basic, "[a]", "[%c]", 'a')
73
74
75
76SPRINTF_TEST(int_various_padding, "[1] [ 02] [03 ] [004] [005]",
77 "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
78 1, 2, 3, 4, 5)
79
80SPRINTF_TEST(int_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
81 "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
82 -1, -2, -3, -4, -5)
83
84SPRINTF_TEST(long_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
85 "[%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]",
86 (long long) -1, (long long) -2, (long long) -3, (long long) -4,
87 (long long) -5)
88
89SPRINTF_TEST(int_as_hex, "[0x11] [0x012] [0x013] [0x00014] [0x00015]",
90 "[%#x] [%#5.3x] [%#-5.3x] [%#3.5x] [%#-3.5x]",
91 17, 18, 19, 20, 21)
92
93
94
95
96PCUT_EXPORT(sprintf);
Note: See TracBrowser for help on using the repository browser.