testCharacterSet.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 "base/CharacterSet.h"
11#include "compat/cppunit.h"
12#include "unitTestMain.h"
13
14#include <string>
15
16class TestCharacterSet : public CPPUNIT_NS::TestFixture
17{
27
28protected:
30 void CharacterSetAdd();
33 void CharacterSetUnion();
36};
37
39
40void
42{
43 {
44 CharacterSet t(nullptr,"");
45 CPPUNIT_ASSERT_EQUAL(std::string("anonymous"),std::string(t.name));
46 }
47 {
48 CharacterSet t("test","");
49 CPPUNIT_ASSERT_EQUAL(std::string("test"),std::string(t.name));
50 }
51 {
52 CharacterSet t("test","");
53 for (int j = 0; j < 256; ++j)
54 CPPUNIT_ASSERT_EQUAL(false,t[j]);
55 }
56 {
57 CharacterSet t("test","0");
58 CPPUNIT_ASSERT_EQUAL(true,t['0']);
59 for (int j = 0; j < 256; ++j) {
60 if (j != '0') {
61 CPPUNIT_ASSERT_EQUAL(false,t[j]);
62 } else {
63 CPPUNIT_ASSERT_EQUAL(true,t[j]);
64 }
65 }
66 }
67}
68
69void
71{
72 CharacterSet t("test","0");
73 t.add(0);
74 CPPUNIT_ASSERT_EQUAL(true,t['\0']);
75 CPPUNIT_ASSERT_EQUAL(true,t['0']);
76}
77
78void
80{
81 CharacterSet t("test","");
82 t.addRange('0','9');
83 CPPUNIT_ASSERT_EQUAL(true,t['0']);
84 CPPUNIT_ASSERT_EQUAL(true,t['5']);
85 CPPUNIT_ASSERT_EQUAL(true,t['9']);
86 CPPUNIT_ASSERT_EQUAL(false,t['a']);
87}
88
89void
91{
92 CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['a']);
93 CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['z']);
94 CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['A']);
95 CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['Z']);
96 CPPUNIT_ASSERT_EQUAL(false,CharacterSet::ALPHA['5']);
97}
98
99void
101{
102 {
103 CharacterSet hex("hex","");
104 hex += CharacterSet::DIGIT;
105 hex += CharacterSet(nullptr,"aAbBcCdDeEfF");
106 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, hex);
107 for (int j = 0; j < 256; ++j)
108 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]);
109 }
110 {
111 CharacterSet hex(nullptr,"");
112 hex = CharacterSet::DIGIT + CharacterSet(nullptr,"aAbBcCdDeEfF");
113 for (int j = 0; j < 256; ++j)
114 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]);
115 }
116}
117
118void
120{
121 CPPUNIT_ASSERT_EQUAL(CharacterSet::ALPHA, CharacterSet::ALPHA);
122 CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet(nullptr,"01"));
123 CPPUNIT_ASSERT_EQUAL(CharacterSet(nullptr,"01"), CharacterSet(nullptr,"01"));
124 CPPUNIT_ASSERT_EQUAL(CharacterSet(nullptr,"01"), CharacterSet("","01"));
125 CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit",'0','1'));
126 CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit", {{'0','1'}}));
127 CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit", {{'0','0'},{'1','1'}}));
128}
129
130void
132{
133 CharacterSet sample(nullptr, "0123456789aAbBcCdDeEfFz");
134
135 sample -= CharacterSet(nullptr, "z"); //character in set
136 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample);
137
138 sample -= CharacterSet(nullptr, "z"); // character not in set
139 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample);
140
141 sample += CharacterSet(nullptr, "z");
142 // one in set, one not; test operator-
143 CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample - CharacterSet(nullptr, "qz"));
144}
145
146int
147main(int argc, char *argv[])
148{
149 return TestProgram().run(argc, argv);
150}
151
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:18
CharacterSet & addRange(unsigned char low, unsigned char high)
add a list of character ranges, expressed as pairs [low,high], including both ends
Definition: CharacterSet.cc:61
CharacterSet & add(const unsigned char c)
add a given character to the character set
Definition: CharacterSet.cc:47
static const CharacterSet DIGIT
Definition: CharacterSet.h:84
static const CharacterSet ALPHA
Definition: CharacterSet.h:76
static const CharacterSet HEXDIG
Definition: CharacterSet.h:88
const char * name
optional set label for debugging (default: "anonymous")
Definition: CharacterSet.h:72
static const CharacterSet BIT
Definition: CharacterSet.h:78
CPPUNIT_TEST_SUITE(TestCharacterSet)
void CharacterSetConstruction()
CPPUNIT_TEST(CharacterSetConstruction)
CPPUNIT_TEST(CharacterSetSubtract)
CPPUNIT_TEST(CharacterSetAdd)
CPPUNIT_TEST(CharacterSetConstants)
CPPUNIT_TEST(CharacterSetUnion)
CPPUNIT_TEST(CharacterSetEqualityOp)
CPPUNIT_TEST(CharacterSetAddRange)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestCharacterSet)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors