Restructured test directories.
[libsex.git] / tests / src / TestUtility.cxx
blob1f4d7e0c91b4cba087b9b5182c9b4275e086c744
1 #include <tests/src/TestUtility.hxx>
2 #include <tests/framework/MockException.hxx>
4 #include <libsex/utility.hxx>
6 void TestUtility::testShortTemplateWithoutFormatting()
8 char buf[100] = { 0 };
10 CPPUNIT_ASSERT(
11 libsex::format(
12 buf, 100,
13 "foobar.cxx", 42,
14 "Message template."));
16 std::string exp("foobar.cxx:42: Message template.");
17 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
20 void TestUtility::testWhetherFalseIsReturnedWhenHeaderFillsBuffer()
22 char buf[100] = { 0 };
24 CPPUNIT_ASSERT(
25 !libsex::format(
26 buf, 16,
27 "foobar.cxx", 42,
28 "Message template."));
30 std::string exp("foobar.cxx:42: ");
31 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
34 void TestUtility::testWhetherTrueIsReturnedOnExactFit()
36 char buf[100] = { 0 };
38 CPPUNIT_ASSERT(
39 libsex::format(
40 buf, 33,
41 "foobar.cxx", 42,
42 "Message template."));
44 std::string exp("foobar.cxx:42: Message template.");
45 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
48 void TestUtility::testWhetherUnformattedOverflowsArePrevented()
50 // Array should be large enough to not really get
51 // overwritten. Just adjust the parameter 'length'
52 // to libsex::format().
53 char buf[100] = { 0 };
55 CPPUNIT_ASSERT(
56 !libsex::format(
57 buf, 10,
58 "foobar.cxx", 42,
59 "Message template."));
61 assertBoundary(buf, 10, 100);
64 void TestUtility::testWhetherSimpleFormattingWorks()
66 char buf[100] = { 0 };
68 CPPUNIT_ASSERT(
69 libsex::format(
70 buf, 100,
71 "foobar.cxx", 42,
72 "String: '%s'.", "STRING"));
74 std::string exp("foobar.cxx:42: String: 'STRING'.");
75 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
78 void TestUtility::testWhetherFormattedOverflowsArePreventedBeforeFormatstring()
80 // Array should be large enough to not really get
81 // overwritten. Just adjust the parameter 'length'
82 // to libsex::format().
83 char buf[100] = { 0 };
85 CPPUNIT_ASSERT(
86 !libsex::format(
87 buf, 10,
88 "foobar.cxx", 42,
89 "String: '%s'.", "STRING"));
91 assertBoundary(buf, 10, 100);
94 void TestUtility::testWhetherFormattedOverflowsArePreventedInFormatstring()
96 // Array should be large enough to not really get
97 // overwritten. Just adjust the parameter 'length'
98 // to libsex::format().
99 char buf[100] = { 0 };
101 CPPUNIT_ASSERT(
102 !libsex::format(
103 buf, 28,
104 "foobar.cxx", 42,
105 "String: '%s'.", "STRING"));
107 assertBoundary(buf, 28, 100);
110 void TestUtility::testMultipleFormatParameters()
112 char buf[100] = { 0 };
114 CPPUNIT_ASSERT(
115 libsex::format(
116 buf, 100,
117 "foobar.cxx", 42,
118 "%s=%d", "var", 42));
120 std::string exp("foobar.cxx:42: var=42");
121 CPPUNIT_ASSERT_EQUAL(exp, std::string(buf));
124 void TestUtility::testWhetherMultipleParametersDontOverflowBeforeFormatstring()
126 char buf[100] = { 0 };
128 CPPUNIT_ASSERT(
129 !libsex::format(
130 buf, 10,
131 "foobar.cxx", 42,
132 "%s=%d", "var", 42));
134 assertBoundary(buf, 10, 100);
137 void TestUtility::testWhetherMultipleParametersDontOverflowInFormatstring()
139 char buf[100] = { 0 };
141 CPPUNIT_ASSERT(
142 !libsex::format(
143 buf, 18,
144 "foobar.cxx", 42,
145 "%s=%d", "var", 42));
147 assertBoundary(buf, 18, 100);
150 void TestUtility::testTemplateFactory()
152 MockException e
153 = libsex::formatted<MockException>(
154 "foobar.cxx", 42,
155 "VAR", 42);
157 CPPUNIT_ASSERT_EQUAL(
158 std::string("foobar.cxx:42: VAR=42"),
159 std::string(e.what()));
162 void TestUtility::assertBoundary(
163 const char* const buffer,
164 size_t boundary,
165 size_t length)
167 for (size_t i = boundary; i < length; ++i) {
168 CPPUNIT_ASSERT_EQUAL('\0', buffer[i]);