Adding support for in-place use of ProducerConsumerQueue.
[folly.git] / folly / test / RangeTest.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 //
18 // @author Kristina Holst (kholst@fb.com)
19 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
20
21 #include <boost/range/concepts.hpp>
22 #include <gtest/gtest.h>
23 #include "folly/Range.h"
24
25 using namespace folly;
26 using namespace std;
27
28 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
29
30 TEST(StringPiece, All) {
31   const char* foo = "foo";
32   const char* foo2 = "foo";
33   string fooStr(foo);
34   string foo2Str(foo2);
35
36   // we expect the compiler to optimize things so that there's only one copy
37   // of the string literal "foo", even though we've got it in multiple places
38   EXPECT_EQ(foo, foo2);  // remember, this uses ==, not strcmp, so it's a ptr
39                          // comparison rather than lexical
40
41   // the string object creates copies though, so the c_str of these should be
42   // distinct
43   EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
44
45   // test the basic StringPiece functionality
46   StringPiece s(foo);
47   EXPECT_EQ(s.size(), 3);
48
49   EXPECT_EQ(s.start(), foo);              // ptr comparison
50   EXPECT_NE(s.start(), fooStr.c_str());   // ptr comparison
51   EXPECT_NE(s.start(), foo2Str.c_str());  // ptr comparison
52
53   EXPECT_EQ(s.toString(), foo);              // lexical comparison
54   EXPECT_EQ(s.toString(), fooStr.c_str());   // lexical comparison
55   EXPECT_EQ(s.toString(), foo2Str.c_str());  // lexical comparison
56
57   EXPECT_EQ(s, foo);                      // lexical comparison
58   EXPECT_EQ(s, fooStr);                   // lexical comparison
59   EXPECT_EQ(s, foo2Str);                  // lexical comparison
60   EXPECT_EQ(foo, s);
61
62   // check using StringPiece to reference substrings
63   const char* foobarbaz = "foobarbaz";
64
65   // the full "foobarbaz"
66   s.reset(foobarbaz, strlen(foobarbaz));
67   EXPECT_EQ(s.size(), 9);
68   EXPECT_EQ(s.start(), foobarbaz);
69   EXPECT_EQ(s, "foobarbaz");
70
71   // only the 'foo'
72   s.assign(foobarbaz, foobarbaz + 3);
73   EXPECT_EQ(s.size(), 3);
74   EXPECT_EQ(s.start(), foobarbaz);
75   EXPECT_EQ(s, "foo");
76
77   // find
78   s.reset(foobarbaz, strlen(foobarbaz));
79   EXPECT_EQ(s.find("bar"), 3);
80   EXPECT_EQ(s.find("ba", 3), 3);
81   EXPECT_EQ(s.find("ba", 4), 6);
82   EXPECT_EQ(s.find("notfound"), StringPiece::npos);
83   EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
84   EXPECT_EQ(s.find("bar", 4), StringPiece::npos);  // starting position too far
85   // starting pos that is obviously past the end -- This works for std::string
86   EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
87   EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
88   EXPECT_EQ(s.find("z", 55), StringPiece::npos);
89
90   // just "barbaz"
91   s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
92   EXPECT_EQ(s.size(), 6);
93   EXPECT_EQ(s.start(), foobarbaz + 3);
94   EXPECT_EQ(s, "barbaz");
95
96   // just "bar"
97   s.reset(foobarbaz + 3, 3);
98   EXPECT_EQ(s.size(), 3);
99   EXPECT_EQ(s, "bar");
100
101   // clear
102   s.clear();
103   EXPECT_EQ(s.toString(), "");
104
105   // test an empty StringPiece
106   StringPiece s2;
107   EXPECT_EQ(s2.size(), 0);
108
109   // Test comparison operators
110   foo = "";
111   EXPECT_LE(s, foo);
112   EXPECT_LE(foo, s);
113   EXPECT_GE(s, foo);
114   EXPECT_GE(foo, s);
115   EXPECT_EQ(s, foo);
116   EXPECT_EQ(foo, s);
117
118   foo = "abc";
119   EXPECT_LE(s, foo);
120   EXPECT_LT(s, foo);
121   EXPECT_GE(foo, s);
122   EXPECT_GT(foo, s);
123   EXPECT_NE(s, foo);
124
125   EXPECT_LE(s, s);
126   EXPECT_LE(s, s);
127   EXPECT_GE(s, s);
128   EXPECT_GE(s, s);
129   EXPECT_EQ(s, s);
130   EXPECT_EQ(s, s);
131
132   s = "abc";
133   s2 = "abc";
134   EXPECT_LE(s, s2);
135   EXPECT_LE(s2, s);
136   EXPECT_GE(s, s2);
137   EXPECT_GE(s2, s);
138   EXPECT_EQ(s, s2);
139   EXPECT_EQ(s2, s);
140 }