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 |
|
---|
44 | PCUT_INIT;
|
---|
45 |
|
---|
46 | PCUT_TEST_SUITE(sprintf);
|
---|
47 |
|
---|
48 | static char buffer[BUFFER_SIZE];
|
---|
49 |
|
---|
50 | PCUT_TEST_BEFORE
|
---|
51 | {
|
---|
52 | memset(buffer, 0, BUFFER_SIZE);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | SPRINTF_TEST(no_formatting, "This is a test.", "This is a test.");
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | SPRINTF_TEST(string_plain, "some text", "%s", "some text");
|
---|
62 |
|
---|
63 | SPRINTF_TEST(string_dynamic_width, " tex", "%*.*s", 5, 3, "text");
|
---|
64 |
|
---|
65 | SPRINTF_TEST(string_dynamic_width_align_left, "text ", "%-*.*s", 7, 7, "text");
|
---|
66 |
|
---|
67 | SPRINTF_TEST(string_pad, " text", "%8.10s", "text");
|
---|
68 |
|
---|
69 | SPRINTF_TEST(string_pad_but_cut, " very lon", "%10.8s", "very long text");
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | SPRINTF_TEST(char_basic, "[a]", "[%c]", 'a');
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|
77 | SPRINTF_TEST(int_various_padding, "[1] [ 02] [03 ] [004] [005]",
|
---|
78 | "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
|
---|
79 | 1, 2, 3, 4, 5);
|
---|
80 |
|
---|
81 | SPRINTF_TEST(int_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
|
---|
82 | "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
|
---|
83 | -1, -2, -3, -4, -5);
|
---|
84 |
|
---|
85 | SPRINTF_TEST(long_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
|
---|
86 | "[%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]",
|
---|
87 | (long long) -1, (long long) -2, (long long) -3, (long long) -4,
|
---|
88 | (long long) -5);
|
---|
89 |
|
---|
90 | SPRINTF_TEST(int_as_hex, "[0x11] [0x012] [0x013] [0x00014] [0x00015]",
|
---|
91 | "[%#x] [%#5.3x] [%#-5.3x] [%#3.5x] [%#-3.5x]",
|
---|
92 | 17, 18, 19, 20, 21);
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 | PCUT_EXPORT(sprintf);
|
---|