[2bff9860] | 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>
|
---|
[1d6dd2a] | 30 | #include <str.h>
|
---|
[134ac5d] | 31 | #include <pcut/pcut.h>
|
---|
[2bff9860] | 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 | #define SPRINTF_TEST(test_name, expected_string, actual_format, ...) \
|
---|
| 38 | PCUT_TEST(test_name) { \
|
---|
| 39 | snprintf(buffer, BUFFER_SIZE, actual_format, ##__VA_ARGS__); \
|
---|
| 40 | PCUT_ASSERT_STR_EQUALS(expected_string, buffer); \
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[3f932a7e] | 43 | PCUT_INIT;
|
---|
[2bff9860] | 44 |
|
---|
[c85a57f] | 45 | PCUT_TEST_SUITE(sprintf);
|
---|
[2bff9860] | 46 |
|
---|
| 47 | static char buffer[BUFFER_SIZE];
|
---|
| 48 |
|
---|
[3bacee1] | 49 | PCUT_TEST_BEFORE
|
---|
| 50 | {
|
---|
[2bff9860] | 51 | memset(buffer, 0, BUFFER_SIZE);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[48974d6] | 54 | SPRINTF_TEST(no_formatting, "This is a test.", "This is a test.");
|
---|
[2bff9860] | 55 |
|
---|
[48974d6] | 56 | SPRINTF_TEST(string_plain, "some text", "%s", "some text");
|
---|
[2bff9860] | 57 |
|
---|
[48974d6] | 58 | SPRINTF_TEST(string_dynamic_width, " tex", "%*.*s", 5, 3, "text");
|
---|
[2bff9860] | 59 |
|
---|
[48974d6] | 60 | SPRINTF_TEST(string_dynamic_width_align_left, "text ", "%-*.*s", 7, 7, "text");
|
---|
[2bff9860] | 61 |
|
---|
[48974d6] | 62 | SPRINTF_TEST(string_pad, " text", "%8.10s", "text");
|
---|
[2bff9860] | 63 |
|
---|
[48974d6] | 64 | SPRINTF_TEST(string_pad_but_cut, " very lon", "%10.8s", "very long text");
|
---|
[2bff9860] | 65 |
|
---|
[48974d6] | 66 | SPRINTF_TEST(char_basic, "[a]", "[%c]", 'a');
|
---|
[2bff9860] | 67 |
|
---|
| 68 | SPRINTF_TEST(int_various_padding, "[1] [ 02] [03 ] [004] [005]",
|
---|
| 69 | "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
|
---|
[48974d6] | 70 | 1, 2, 3, 4, 5);
|
---|
[2bff9860] | 71 |
|
---|
| 72 | SPRINTF_TEST(int_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
|
---|
| 73 | "[%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]",
|
---|
[48974d6] | 74 | -1, -2, -3, -4, -5);
|
---|
[2bff9860] | 75 |
|
---|
| 76 | SPRINTF_TEST(long_negative_various_padding, "[-1] [-02] [-03] [-004] [-005]",
|
---|
| 77 | "[%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]",
|
---|
| 78 | (long long) -1, (long long) -2, (long long) -3, (long long) -4,
|
---|
[48974d6] | 79 | (long long) -5);
|
---|
[2bff9860] | 80 |
|
---|
| 81 | SPRINTF_TEST(int_as_hex, "[0x11] [0x012] [0x013] [0x00014] [0x00015]",
|
---|
| 82 | "[%#x] [%#5.3x] [%#-5.3x] [%#3.5x] [%#-3.5x]",
|
---|
[48974d6] | 83 | 17, 18, 19, 20, 21);
|
---|
[2bff9860] | 84 |
|
---|
| 85 | PCUT_EXPORT(sprintf);
|
---|