Removed redundancy in test suite naming.
[libsex.git] / tests / Utility.cxx
blob6d1c4f7de693ce6f20c6b60f7fea85ffdec98227
1 #include <tests/Utility.hxx>
2 #include <tests/framework/MockException.hxx>
4 #include <source/utility.hxx>
6 #include <sstream>
8 void tests::Utility::testShortTemplateWithoutFormatting()
10 char buf[100] = { 0 };
12 CPPUNIT_ASSERT(
13 libsex::format(
14 buf, 100,
15 "foobar.cxx", 42,
16 "Message template."));
18 std::string exp("foobar.cxx:42: Message template.");
19 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
22 void tests::Utility::testWhetherFalseIsReturnedWhenHeaderFillsBuffer()
24 char buf[100] = { 0 };
26 CPPUNIT_ASSERT(
27 !libsex::format(
28 buf, 16,
29 "foobar.cxx", 42,
30 "Message template."));
32 std::string exp("foobar.cxx:42: ");
33 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
36 void tests::Utility::testWhetherTrueIsReturnedOnExactFit()
38 char buf[100] = { 0 };
40 CPPUNIT_ASSERT(
41 libsex::format(
42 buf, 33,
43 "foobar.cxx", 42,
44 "Message template."));
46 std::string exp("foobar.cxx:42: Message template.");
47 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
50 void tests::Utility::testWhetherUnformattedOverflowsArePrevented()
52 // Array should be large enough to not really get
53 // overwritten. Just adjust the parameter 'length'
54 // to libsex::format().
55 char buf[100] = { 0 };
57 CPPUNIT_ASSERT(
58 !libsex::format(
59 buf, 10,
60 "foobar.cxx", 42,
61 "Message template."));
63 assertBoundary(buf, 10, 100);
66 void tests::Utility::testWhetherSimpleFormattingWorks()
68 char buf[100] = { 0 };
70 CPPUNIT_ASSERT(
71 libsex::format(
72 buf, 100,
73 "foobar.cxx", 42,
74 "String: '%s'.", "STRING"));
76 std::string exp("foobar.cxx:42: String: 'STRING'.");
77 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
80 void tests::Utility::testWhetherFormattedOverflowsArePreventedBeforeFormatstring()
82 // Array should be large enough to not really get
83 // overwritten. Just adjust the parameter 'length'
84 // to libsex::format().
85 char buf[100] = { 0 };
87 CPPUNIT_ASSERT(
88 !libsex::format(
89 buf, 10,
90 "foobar.cxx", 42,
91 "String: '%s'.", "STRING"));
93 assertBoundary(buf, 10, 100);
96 void tests::Utility::testWhetherFormattedOverflowsArePreventedInFormatstring()
98 // Array should be large enough to not really get
99 // overwritten. Just adjust the parameter 'length'
100 // to libsex::format().
101 char buf[100] = { 0 };
103 CPPUNIT_ASSERT(
104 !libsex::format(
105 buf, 28,
106 "foobar.cxx", 42,
107 "String: '%s'.", "STRING"));
109 assertBoundary(buf, 28, 100);
112 void tests::Utility::testMultipleFormatParameters()
114 char buf[100] = { 0 };
116 CPPUNIT_ASSERT(
117 libsex::format(
118 buf, 100,
119 "foobar.cxx", 42,
120 "%s=%d", "var", 42));
122 std::string exp("foobar.cxx:42: var=42");
123 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
126 void tests::Utility::testWhetherMultipleParametersDontOverflowBeforeFormatstring()
128 char buf[100] = { 0 };
130 CPPUNIT_ASSERT(
131 !libsex::format(
132 buf, 10,
133 "foobar.cxx", 42,
134 "%s=%d", "var", 42));
136 assertBoundary(buf, 10, 100);
139 void tests::Utility::testWhetherMultipleParametersDontOverflowInFormatstring()
141 char buf[100] = { 0 };
143 CPPUNIT_ASSERT(
144 !libsex::format(
145 buf, 18,
146 "foobar.cxx", 42,
147 "%s=%d", "var", 42));
149 assertBoundary(buf, 18, 100);
152 void tests::Utility::testTemplateFactory()
154 MockException e
155 = libsex::formatted<MockException>(
156 "foobar.cxx", 42,
157 "VAR", 42);
159 CPPUNIT_ASSERT_EQUAL(
160 std::string("foobar.cxx:42: VAR=42"),
161 std::string(e.what()));
164 void tests::Utility::testChainedTemplateFactory()
166 MockException e1
167 = libsex::formatted<MockException>(
168 "foobar.cxx", 42,
169 "VAR", 42);
171 MockException e = libsex::formatted<MockException>(
173 "foobar.cxx", 84,
174 "#exceptions", 1);
176 std::stringstream act;
177 e.backtrace(act);
179 CPPUNIT_ASSERT_EQUAL(
180 std::string("foobar.cxx:84: #exceptions=1\n"
181 "foobar.cxx:42: VAR=42"),
182 act.str());
185 void tests::Utility::assertBoundary(
186 const char* const buffer,
187 size_t boundary,
188 size_t length)
190 for (size_t i = boundary; i < length; ++i) {
191 CPPUNIT_ASSERT_EQUAL('\0', buffer[i]);