File: simplestring_xform.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (149 lines) | stat: -rw-r--r-- 4,076 bytes parent folder | download
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
// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "unit_tests.hh"
#include "simplestring_xform.hh"

#include "vocab.hh"
#include <set>

using std::set;
using std::string;
using std::vector;

UNIT_TEST(caseconv)
{
  UNIT_TEST_CHECK(uppercase("hello") == "HELLO");
  UNIT_TEST_CHECK(uppercase("heLlO") == "HELLO");
  UNIT_TEST_CHECK(lowercase("POODLE DAY") == "poodle day");
  UNIT_TEST_CHECK(lowercase("PooDLe DaY") == "poodle day");
  UNIT_TEST_CHECK(uppercase("!@#$%^&*()") == "!@#$%^&*()");
  UNIT_TEST_CHECK(lowercase("!@#$%^&*()") == "!@#$%^&*()");
}

UNIT_TEST(join_lines)
{
  vector<string> strs;
  string joined;

  strs.clear();
  join_lines(strs, joined);
  UNIT_TEST_CHECK(joined == "");

  strs.push_back("hi");
  join_lines(strs, joined);
  UNIT_TEST_CHECK(joined == "hi\n");

  strs.push_back("there");
  join_lines(strs, joined);
  UNIT_TEST_CHECK(joined == "hi\nthere\n");

  strs.push_back("user");
  join_lines(strs, joined);
  UNIT_TEST_CHECK(joined == "hi\nthere\nuser\n");
}

UNIT_TEST(join_words)
{
  vector< utf8 > v;
  set< utf8 > s;

  v.clear();
  UNIT_TEST_CHECK(join_words(v)() == "");

  v.clear();
  v.push_back(utf8("a"));
  UNIT_TEST_CHECK(join_words(v)() == "a");
  UNIT_TEST_CHECK(join_words(v, ", ")() == "a");

  s.clear();
  s.insert(utf8("a"));
  UNIT_TEST_CHECK(join_words(s)() == "a");
  UNIT_TEST_CHECK(join_words(s, ", ")() == "a");

  v.clear();
  v.push_back(utf8("a"));
  v.push_back(utf8("b"));
  UNIT_TEST_CHECK(join_words(v)() == "a b");
  UNIT_TEST_CHECK(join_words(v, ", ")() == "a, b");

  s.clear();
  s.insert(utf8("b"));
  s.insert(utf8("a"));
  UNIT_TEST_CHECK(join_words(s)() == "a b");
  UNIT_TEST_CHECK(join_words(s, ", ")() == "a, b");

  v.clear();
  v.push_back(utf8("a"));
  v.push_back(utf8("b"));
  v.push_back(utf8("c"));
  UNIT_TEST_CHECK(join_words(v)() == "a b c");
  UNIT_TEST_CHECK(join_words(v, ", ")() == "a, b, c");

  s.clear();
  s.insert(utf8("b"));
  s.insert(utf8("a"));
  s.insert(utf8("c"));
  UNIT_TEST_CHECK(join_words(s)() == "a b c");
  UNIT_TEST_CHECK(join_words(s, ", ")() == "a, b, c");
}

UNIT_TEST(split_into_words)
{
  vector< utf8 > words;

  words = split_into_words(utf8(""));
  UNIT_TEST_CHECK(words.empty());

  words = split_into_words(utf8("foo"));
  UNIT_TEST_CHECK(words.size() == 1);
  UNIT_TEST_CHECK(words[0]() == "foo");

  words = split_into_words(utf8("foo bar"));
  UNIT_TEST_CHECK(words.size() == 2);
  UNIT_TEST_CHECK(words[0]() == "foo");
  UNIT_TEST_CHECK(words[1]() == "bar");

  // describe() in commands.cc assumes this behavior.  If it ever changes,
  // remember to modify that function accordingly!
  words = split_into_words(utf8("foo  bar"));
  UNIT_TEST_CHECK(words.size() == 3);
  UNIT_TEST_CHECK(words[0]() == "foo");
  UNIT_TEST_CHECK(words[1]() == "");
  UNIT_TEST_CHECK(words[2]() == "bar");
}

UNIT_TEST(trimming)
{
  UNIT_TEST_CHECK(trim_right(":foobar:", ":") == ":foobar");
  UNIT_TEST_CHECK(trim_left(":foobar:", ":") == "foobar:");
  UNIT_TEST_CHECK(trim(":foobar:", ":") == "foobar");

  UNIT_TEST_CHECK(trim("\n  leading space") == "leading space");
  UNIT_TEST_CHECK(trim("trailing space  \n") == "trailing space");
  UNIT_TEST_CHECK(trim("\t\n both \r \n\r\n") == "both");

  // strings with nothing but whitespace should trim to nothing
  UNIT_TEST_CHECK(trim_left("   \r\n\r\n\t\t\n\n\r\n   ") == "");
  UNIT_TEST_CHECK(trim_right("   \r\n\r\n\t\t\n\n\r\n   ") == "");
  UNIT_TEST_CHECK(trim("   \r\n\r\n\t\t\n\n\r\n   ") == "");

  UNIT_TEST_CHECK(remove_ws("  I like going\tfor walks\n  ")
              == "Ilikegoingforwalks");

}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: