Debian Codegraph @

tests/test_ssprintf.cc

raw
1 // Tests for generic/util/ssprintf.
2 //
3 // Copyright (C) 2007-2008 Daniel Burrows
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; see the file COPYING. If not, write to
17 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
19
20 #include <cppunit/extensions/HelperMacros.h>
21
22 #include <cppunit/TestAssert.h>
23
24 #include <cwidget/generic/util/ssprintf.h>
25
26 // For displaying assertion failures.
27 #include <cwidget/generic/util/transcode.h>
28
29 #include <errno.h>
30 #include <string.h>
31
32 using cwidget::util::ssprintf;
33 using cwidget::util::swsprintf;
34
35 CPPUNIT_NS_BEGIN
36
37 template <>
38 struct assertion_traits<std::wstring>
39 {
40 static bool equal(const std::wstring &x, const std::wstring &y)
41 {
42 return x == y;
43 }
44
45 static std::string toString(const std::wstring &x)
46 {
47 return cwidget::util::transcode(x);
48 }
49 };
50
51 CPPUNIT_NS_END
52
53 class SSPrintfTest : public CppUnit::TestFixture
54 {
55 CPPUNIT_TEST_SUITE(SSPrintfTest);
56
57 CPPUNIT_TEST(test_sstrerror);
58 CPPUNIT_TEST(test_ssprintf);
59 CPPUNIT_TEST(test_swsprintf);
60
61 CPPUNIT_TEST_SUITE_END();
62 private:
63 void do_test_sstrerror(int errnum)
64 {
65 std::string expected = strerror(errnum);
66 CPPUNIT_ASSERT_EQUAL(expected, cwidget::util::sstrerror(errnum));
67 }
68
69 // This test was written because the original sstrerror was buggy
70 // (returned garbage) on some platforms. To verify that it works we
71 // check that it returns the same thing as strerror for a few error
72 // values.
73 void test_sstrerror()
74 {
75 do_test_sstrerror(0);
76 do_test_sstrerror(EINVAL);
77 do_test_sstrerror(EINTR);
78 do_test_sstrerror(EBADF);
79 }
80
81 void test_ssprintf()
82 {
83 // Test that inserting very long strings via ssprintf actually works.
84 std::string horriblelongthing = "abcdefghijklmnopqrstuvwxyz";
85 while(horriblelongthing.size() < 4096)
86 horriblelongthing += horriblelongthing;
87
88 CPPUNIT_ASSERT_EQUAL(horriblelongthing + " 20", ssprintf("%s %d", horriblelongthing.c_str(), 20));
89
90 // Test that we can generate empty strings.
91 CPPUNIT_ASSERT_EQUAL(std::string(), ssprintf("%s", ""));
92 }
93
94 void test_swsprintf()
95 {
96 // Test that inserting very long strings via ssprintf actually works.
97 std::wstring horriblelongthing = L"abcdefghijklmnopqrstuvwxyz";
98 while(horriblelongthing.size() < 4096)
99 horriblelongthing += horriblelongthing;
100
101 CPPUNIT_ASSERT_EQUAL(horriblelongthing + L" 20", swsprintf(L"%ls %d", horriblelongthing.c_str(), 20));
102
103 // Test that we can generate empty strings.
104 CPPUNIT_ASSERT_EQUAL(std::wstring(), swsprintf(L"%s", ""));
105 }
106 };
107
108 CPPUNIT_TEST_SUITE_REGISTRATION(SSPrintfTest);