testString.cc
Go to the documentation of this file.
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 {
26 
28 
29 protected:
30  void testCmpDefault();
31  void testCmpEmptyString();
33  void testSubstr();
34 };
36 
37 void
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
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
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 
78 {
79  String s("0123456789");
80  String check=s.substr(3,5);
81  String ref("34");
82  CPPUNIT_ASSERT(check == ref);
83 }
84 
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 
void testCmpDefault()
Definition: testString.cc:38
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
CPPUNIT_TEST_SUITE_END()
int cmp(char const *) const
Definition: String.cc:236
void testCmpEmptyString()
Definition: testString.cc:48
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST_SUITE_REGISTRATION(TestString)
void testCmpNotEmptyDefault()
Definition: testString.cc:63
void Init()
Definition: old_api.cc:281
customizes our test setup
String substr(size_type from, size_type to) const
Definition: String.cc:190
CPPUNIT_TEST(testCmpDefault)
int main(int argc, char *argv[])
Definition: testString.cc:94
CPPUNIT_TEST_SUITE(TestString)
void testSubstr()
Definition: testString.cc:77
void startup() override
Definition: testString.cc:90

 

Introduction

Documentation

Support

Miscellaneous