testCharacterSet.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "testCharacterSet.h"
12 #include "unitTestMain.h"
13 
14 #include <string>
15 
17 
18 void
20 {
21  {
22  CharacterSet t(nullptr,"");
23  CPPUNIT_ASSERT_EQUAL(std::string("anonymous"),std::string(t.name));
24  }
25  {
26  CharacterSet t("test","");
27  CPPUNIT_ASSERT_EQUAL(std::string("test"),std::string(t.name));
28  }
29  {
30  CharacterSet t("test","");
31  for (int j = 0; j < 256; ++j)
32  CPPUNIT_ASSERT_EQUAL(false,t[j]);
33  }
34  {
35  CharacterSet t("test","0");
36  CPPUNIT_ASSERT_EQUAL(true,t['0']);
37  for (int j = 0; j < 256; ++j) {
38  if (j != '0') {
39  CPPUNIT_ASSERT_EQUAL(false,t[j]);
40  } else {
41  CPPUNIT_ASSERT_EQUAL(true,t[j]);
42  }
43  }
44  }
45 }
46 
47 void
49 {
50  CharacterSet t("test","0");
51  t.add(0);
52  CPPUNIT_ASSERT_EQUAL(true,t['\0']);
53  CPPUNIT_ASSERT_EQUAL(true,t['0']);
54 }
55 
56 void
58 {
59  CharacterSet t("test","");
60  t.addRange('0','9');
61  CPPUNIT_ASSERT_EQUAL(true,t['0']);
62  CPPUNIT_ASSERT_EQUAL(true,t['5']);
63  CPPUNIT_ASSERT_EQUAL(true,t['9']);
64  CPPUNIT_ASSERT_EQUAL(false,t['a']);
65 }
66 
67 void
69 {
70  CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['a']);
71  CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['z']);
72  CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['A']);
73  CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['Z']);
74  CPPUNIT_ASSERT_EQUAL(false,CharacterSet::ALPHA['5']);
75 }
76 
77 void
79 {
80  {
81  CharacterSet hex("hex","");
82  hex += CharacterSet::DIGIT;
83  hex += CharacterSet(nullptr,"aAbBcCdDeEfF");
84  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, hex);
85  for (int j = 0; j < 256; ++j)
86  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]);
87  }
88  {
89  CharacterSet hex(nullptr,"");
90  hex = CharacterSet::DIGIT + CharacterSet(nullptr,"aAbBcCdDeEfF");
91  for (int j = 0; j < 256; ++j)
92  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]);
93  }
94 }
95 
96 void
98 {
99  CPPUNIT_ASSERT_EQUAL(CharacterSet::ALPHA, CharacterSet::ALPHA);
100  CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet(nullptr,"01"));
101  CPPUNIT_ASSERT_EQUAL(CharacterSet(nullptr,"01"), CharacterSet(nullptr,"01"));
102  CPPUNIT_ASSERT_EQUAL(CharacterSet(nullptr,"01"), CharacterSet("","01"));
103  CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit",'0','1'));
104  CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit", {{'0','1'}}));
105  CPPUNIT_ASSERT_EQUAL(CharacterSet::BIT, CharacterSet("bit", {{'0','0'},{'1','1'}}));
106 }
107 
108 void
110 {
111  CharacterSet sample(nullptr, "0123456789aAbBcCdDeEfFz");
112 
113  sample -= CharacterSet(nullptr, "z"); //character in set
114  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample);
115 
116  sample -= CharacterSet(nullptr, "z"); // character not in set
117  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample);
118 
119  sample += CharacterSet(nullptr, "z");
120  // one in set, one not; test operator-
121  CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG, sample - CharacterSet(nullptr, "qz"));
122 }
123 
const char * name
optional set label for debugging (default: "anonymous")
Definition: CharacterSet.h:69
CPPUNIT_TEST_SUITE_REGISTRATION(testCharacterSet)
static const CharacterSet ALPHA
Definition: CharacterSet.h:73
static const CharacterSet HEXDIG
Definition: CharacterSet.h:85
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:81
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:17
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:54
static const CharacterSet BIT
Definition: CharacterSet.h:75
void CharacterSetConstruction()

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors