KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_richio.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
30
31// Code under test
32#include <richio.h>
33
38
39
43{
44 std::string output;
45
46 // Test 1: Basic strings and numbers
47 StrPrintf( &output, "Hello %s! ", "World" );
48 StrPrintf( &output, "Number: %d, ", 42 );
49 StrPrintf( &output, "Float: %.2f, ", 3.14 );
50 StrPrintf( &output, "Char: %c. ", 'A' );
51 BOOST_CHECK_EQUAL( output, std::string( "Hello World! Number: 42, Float: 3.14, Char: A. " ) );
52 output.clear();
53
54 // Test 2: Large string
55 std::string longString( 500, 'A' );
56 StrPrintf( &output, "%s", longString.c_str() );
57 BOOST_CHECK_EQUAL( output, longString );
58 output.clear();
59
60 // Test 3: Edge case with zero characters
61 #ifdef __GNUC__
62 #pragma GCC diagnostic ignored "-Wformat-zero-length"
63 #endif
64 StrPrintf( &output, "" );
65 #ifdef __GNUC__
66 #pragma GCC diagnostic warning "-Wformat-zero-length"
67 #endif
68 BOOST_ASSERT( output.empty() );
69
70 // Test 4: Mixing small and large strings
71 StrPrintf( &output, "Small, " );
72 StrPrintf( &output, "%s, ", longString.c_str() );
73 StrPrintf( &output, "End." );
74 BOOST_CHECK_EQUAL( output, std::string( "Small, " + longString + ", End." ) );
75 output.clear();
76
77 // Test 5: Formatting with various data types
78 StrPrintf( &output, "%d %s %c %.2f", 123, "Hello", 'X', 9.876 );
79 BOOST_CHECK_EQUAL( output, std::string( "123 Hello X 9.88" ) );
80 output.clear();
81}
82
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:68
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(VPrint)
Declare the test suite.
Definition: test_richio.cpp:42