summaryrefslogtreecommitdiff
path: root/tests/test_tags.cc
blob: f80c1b84e1940b85b8211ce982c86b32703cba6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Tester for the tag parser.
//
//   Copyright (C) 2005, 2008 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#ifndef HAVE_EPT

#include <cppunit/extensions/HelperMacros.h>

#include <generic/apt/tags.h>

#include <string.h>

#include <sstream>

std::ostream &operator<<(std::ostream &out, const tag &t)
{
  return out << t.str();
}

#define CPPUNIT_ASSERT_GT(a, b) \
  do { if(!((a) > (b))) \
         {std::ostringstream out; \
          out << "comparison assertion failed" << std::endl; \
          out << "\"" << (a) << "\" is not > \"" << (b) << "\"" << std::endl; \
          CppUnit :: Asserter :: fail(out.str()); } } while(0)

#define CPPUNIT_ASSERT_LT(a, b) \
  do { if(!((a) < (b))) \
         {std::ostringstream out; \
          out << "comparison assertion failed" << std::endl; \
          out << "\"" << (a) << "\" is not < \"" << (b) << "\"" << std::endl; \
          CppUnit :: Asserter :: fail(out.str()); } } while(0)

#define CPPUNIT_ASSERT_LTEQUAL(a, b) \
  do { if(!((a) <= (b))) \
         {std::ostringstream out; \
          out << "comparison assertion failed" << std::endl; \
          out << "\"" << (a) << "\" is not <= \"" << (b) << "\"" << std::endl; \
          CppUnit :: Asserter :: fail(out.str()); } } while(0)

#define CPPUNIT_ASSERT_GTEQUAL(a, b) \
  do { if(!((a) >= (b))) \
         {std::ostringstream out; \
          out << "comparison assertion failed" << std::endl; \
          out << "\"" << (a) << "\" is not >= \"" << (b) << "\"" << std::endl; \
          CppUnit :: Asserter :: fail(out.str()); } } while(0)

class TagTest : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(TagTest);

  CPPUNIT_TEST(testTagTraverse);
  CPPUNIT_TEST(testTagStr);
  CPPUNIT_TEST(testTagEqual);
  CPPUNIT_TEST(testTagOrder);
  CPPUNIT_TEST(testTagListTraverse);

  CPPUNIT_TEST_SUITE_END();
private:
  static tag parseTag(const std::string &s)
  {
    return tag(s.begin(), s.end());
  }

  // These should all parse to (tag1, tag2, tag3):
  static const char *basicTagEntries[];
  // Erroneous entries.  Should parse as having no tags at all.
  static const char *badEntry1;
  static const char *badEntry2;
  static const char *badEntry3;
  static const char *badEntry4;

  // Tag order checks.  The tag ordering function should say that
  // forall n . tagorder[n]<tagorder[n+1]
  static const char *tagorder[];
  // Tag list checks.
  static const char *tagLists[];
  // One more test.
  static const char *tagListLast;

  // Checks that the given tag is "tag1::tag2::tag3".
  void testTag123(const tag &t)
  {
    tag::const_iterator ti = t.begin();

    CPPUNIT_ASSERT(ti != t.end());
    CPPUNIT_ASSERT_EQUAL(string("tag1"), *ti);

    ++ti;


    CPPUNIT_ASSERT(ti != t.end());
    CPPUNIT_ASSERT_EQUAL(string("tag2"), *ti);

    ++ti;


    CPPUNIT_ASSERT(ti != t.end());
    CPPUNIT_ASSERT_EQUAL(string("tag3"), *ti);

    ++ti;

    CPPUNIT_ASSERT(ti == t.end());
  }

  void testTagTraverse()
  {
    const char **s=basicTagEntries;
    while(*s)
      {
	tag parsed = parseTag(*s);
	testTag123(parsed);
	++s;
      }
  }

  void testTagStr()
  {
    const char **s=basicTagEntries;
    while(*s)
      {
	tag parsed=parseTag(*s);
	CPPUNIT_ASSERT_EQUAL(string("tag1::tag2::tag3"), parsed.str());
	++s;
      }
  }

  void testTagEqual()
  {
    for(const char **s=basicTagEntries; *s; ++s)
      for(const char **t=basicTagEntries; *t; ++t)
	CPPUNIT_ASSERT_EQUAL(parseTag(*s), parseTag(*t));
  }

  void testTagOrder()
  {
    for(const char **s = tagorder; *s; ++s)
      {
	tag ps = parseTag(*s);

	CPPUNIT_ASSERT_EQUAL(ps, ps);

	for(const char **t = s; *t; ++t)
	  {
	    tag pt = parseTag(*t);

	    CPPUNIT_ASSERT_LTEQUAL(ps, pt);
	    CPPUNIT_ASSERT_GTEQUAL(pt, ps);

	    if(t != s)
	      {
		CPPUNIT_ASSERT_LT(ps, pt);
		CPPUNIT_ASSERT_GT(pt, ps);
	      }
	  }
      }
  }

  void testTagListTraverse()
  {
    for(const char **l = tagLists; *l; ++l)
      {
	tag_list lp(*l, (*l)+strlen(*l));

	tag_list :: const_iterator i = lp.begin();

	CPPUNIT_ASSERT(i != lp.end());
	CPPUNIT_ASSERT_EQUAL(string("a::b"), (*i).str());

	++i;

	CPPUNIT_ASSERT(i != lp.end());
	CPPUNIT_ASSERT_EQUAL(string("c::d"), (*i).str());

	++i;

	CPPUNIT_ASSERT(i != lp.end());
	CPPUNIT_ASSERT_EQUAL(string("e::f"), (*i).str());

	++i;

	CPPUNIT_ASSERT(i == lp.end());
      }
  }
};

const char *TagTest::basicTagEntries[]={
  "tag1::tag2::tag3",
  "  tag1::tag2::tag3",
  "tag1::tag2::tag3  ",
  "  tag1::tag2::tag3  ",
  0
};

// Erroneous entries.  Should parse as having no tags at all.
const char *TagTest::badEntry1="";
const char *TagTest::badEntry2=0;
const char *TagTest::badEntry3="   ";
const char *TagTest::badEntry4="   ::    ";

// Tag order checks.  The tag ordering function should say that
// forall n . tagorder[n]<tagorder[n+1]
const char *TagTest::tagorder[]={
  "tag1::tag2::tag3  ",
  "  tag1::tag3::tag3",
  "tag2::tag1::tag1",
  " tag3::tag2::tag2",
  "tag3::tag2::tag3",
  0
};

// Tag list checks.
const char *TagTest::tagLists[] = {
  "a::b,c::d,e::f",
  "a::b, c::d, e::f",
  "  a::b,c::d,    e::f",
  "a::b   ,   c::d   ,   e::f",
  0
};

// One more test.
const char *TagTest::tagListLast="  a  :: b";

CPPUNIT_TEST_SUITE_REGISTRATION(TagTest);

#endif