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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dd7df1c was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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