]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testString.cc
Use ERR_ACCESS_DENIED for HTTP 403 (Forbidden) errors (#1899)
[thirdparty/squid.git] / src / tests / testString.cc
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "compat/cppunit.h"
11 #include "mem/forward.h"
12 #include "SquidString.h"
13 #include "unitTestMain.h"
14
15 /*
16 * test the store framework
17 */
18
19 class TestString : public CPPUNIT_NS::TestFixture
20 {
21 CPPUNIT_TEST_SUITE(TestString);
22 CPPUNIT_TEST(testCmpDefault);
23 CPPUNIT_TEST(testCmpEmptyString);
24 CPPUNIT_TEST(testCmpNotEmptyDefault);
25 CPPUNIT_TEST(testSubstr);
26
27 CPPUNIT_TEST_SUITE_END();
28
29 protected:
30 void testCmpDefault();
31 void testCmpEmptyString();
32 void testCmpNotEmptyDefault();
33 void testSubstr();
34 };
35 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
36
37 void
38 TestString::testCmpDefault()
39 {
40 String left, right;
41 /* two default strings are equal */
42 CPPUNIT_ASSERT(!left.cmp(right));
43 CPPUNIT_ASSERT(!left.cmp(nullptr));
44 CPPUNIT_ASSERT(!left.cmp(nullptr, 1));
45 }
46
47 void
48 TestString::testCmpEmptyString()
49 {
50 String left("");
51 String right;
52 /* an empty string ("") is equal to a default string */
53 CPPUNIT_ASSERT(!left.cmp(right));
54 CPPUNIT_ASSERT(!left.cmp(nullptr));
55 CPPUNIT_ASSERT(!left.cmp(nullptr, 1));
56 /* reverse the order to catch corners */
57 CPPUNIT_ASSERT(!right.cmp(left));
58 CPPUNIT_ASSERT(!right.cmp(""));
59 CPPUNIT_ASSERT(!right.cmp("", 1));
60 }
61
62 void
63 TestString::testCmpNotEmptyDefault()
64 {
65 String left("foo");
66 String right;
67 /* empty string sorts before everything */
68 CPPUNIT_ASSERT(left.cmp(right) > 0);
69 CPPUNIT_ASSERT(left.cmp(nullptr) > 0);
70 CPPUNIT_ASSERT(left.cmp(nullptr, 1) > 0);
71 /* reverse for symmetry tests */
72 CPPUNIT_ASSERT(right.cmp(left) < 0);
73 CPPUNIT_ASSERT(right.cmp("foo") < 0);
74 CPPUNIT_ASSERT(right.cmp("foo", 1) < 0);
75 }
76
77 void TestString::testSubstr()
78 {
79 String s("0123456789");
80 String check=s.substr(3,5);
81 String ref("34");
82 CPPUNIT_ASSERT(check == ref);
83 }
84
85 /// customizes our test setup
86 class MyTestProgram: public TestProgram
87 {
88 public:
89 /* TestProgram API */
90 void startup() override { Mem::Init(); }
91 };
92
93 int
94 main(int argc, char *argv[])
95 {
96 return MyTestProgram().run(argc, argv);
97 }
98