1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// size_type rfind(const basic_string& str, size_type pos = npos) const;
13
14#include <string>
15#include <cassert>
16
17#include "min_allocator.h"
18
19template <class S>
20void
21test(const S& s, const S& str, typename S::size_type pos, typename S::size_type x)
22{
23    assert(s.rfind(str, pos) == x);
24    if (x != S::npos)
25        assert(x <= pos && x + str.size() <= s.size());
26}
27
28template <class S>
29void
30test(const S& s, const S& str, typename S::size_type x)
31{
32    assert(s.rfind(str) == x);
33    if (x != S::npos)
34        assert(0 <= x && x + str.size() <= s.size());
35}
36
37template <class S>
38void test0()
39{
40    test(S(""), S(""), 0, 0);
41    test(S(""), S("abcde"), 0, S::npos);
42    test(S(""), S("abcdeabcde"), 0, S::npos);
43    test(S(""), S("abcdeabcdeabcdeabcde"), 0, S::npos);
44    test(S(""), S(""), 1, 0);
45    test(S(""), S("abcde"), 1, S::npos);
46    test(S(""), S("abcdeabcde"), 1, S::npos);
47    test(S(""), S("abcdeabcdeabcdeabcde"), 1, S::npos);
48    test(S("abcde"), S(""), 0, 0);
49    test(S("abcde"), S("abcde"), 0, 0);
50    test(S("abcde"), S("abcdeabcde"), 0, S::npos);
51    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 0, S::npos);
52    test(S("abcde"), S(""), 1, 1);
53    test(S("abcde"), S("abcde"), 1, 0);
54    test(S("abcde"), S("abcdeabcde"), 1, S::npos);
55    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 1, S::npos);
56    test(S("abcde"), S(""), 2, 2);
57    test(S("abcde"), S("abcde"), 2, 0);
58    test(S("abcde"), S("abcdeabcde"), 2, S::npos);
59    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 2, S::npos);
60    test(S("abcde"), S(""), 4, 4);
61    test(S("abcde"), S("abcde"), 4, 0);
62    test(S("abcde"), S("abcdeabcde"), 4, S::npos);
63    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 4, S::npos);
64    test(S("abcde"), S(""), 5, 5);
65    test(S("abcde"), S("abcde"), 5, 0);
66    test(S("abcde"), S("abcdeabcde"), 5, S::npos);
67    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 5, S::npos);
68    test(S("abcde"), S(""), 6, 5);
69    test(S("abcde"), S("abcde"), 6, 0);
70    test(S("abcde"), S("abcdeabcde"), 6, S::npos);
71    test(S("abcde"), S("abcdeabcdeabcdeabcde"), 6, S::npos);
72    test(S("abcdeabcde"), S(""), 0, 0);
73    test(S("abcdeabcde"), S("abcde"), 0, 0);
74    test(S("abcdeabcde"), S("abcdeabcde"), 0, 0);
75    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 0, S::npos);
76    test(S("abcdeabcde"), S(""), 1, 1);
77    test(S("abcdeabcde"), S("abcde"), 1, 0);
78    test(S("abcdeabcde"), S("abcdeabcde"), 1, 0);
79    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 1, S::npos);
80    test(S("abcdeabcde"), S(""), 5, 5);
81    test(S("abcdeabcde"), S("abcde"), 5, 5);
82    test(S("abcdeabcde"), S("abcdeabcde"), 5, 0);
83    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 5, S::npos);
84    test(S("abcdeabcde"), S(""), 9, 9);
85    test(S("abcdeabcde"), S("abcde"), 9, 5);
86    test(S("abcdeabcde"), S("abcdeabcde"), 9, 0);
87    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 9, S::npos);
88    test(S("abcdeabcde"), S(""), 10, 10);
89    test(S("abcdeabcde"), S("abcde"), 10, 5);
90    test(S("abcdeabcde"), S("abcdeabcde"), 10, 0);
91    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 10, S::npos);
92    test(S("abcdeabcde"), S(""), 11, 10);
93    test(S("abcdeabcde"), S("abcde"), 11, 5);
94    test(S("abcdeabcde"), S("abcdeabcde"), 11, 0);
95    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), 11, S::npos);
96    test(S("abcdeabcdeabcdeabcde"), S(""), 0, 0);
97    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 0, 0);
98    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 0, 0);
99    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 0, 0);
100    test(S("abcdeabcdeabcdeabcde"), S(""), 1, 1);
101    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 1, 0);
102    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 1, 0);
103    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 1, 0);
104    test(S("abcdeabcdeabcdeabcde"), S(""), 10, 10);
105    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 10, 10);
106    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 10, 10);
107    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 10, 0);
108    test(S("abcdeabcdeabcdeabcde"), S(""), 19, 19);
109    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 19, 15);
110    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 19, 10);
111    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 19, 0);
112    test(S("abcdeabcdeabcdeabcde"), S(""), 20, 20);
113    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 20, 15);
114    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 20, 10);
115    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 20, 0);
116    test(S("abcdeabcdeabcdeabcde"), S(""), 21, 20);
117    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 21, 15);
118    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 21, 10);
119    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 21, 0);
120}
121
122template <class S>
123void test1()
124{
125    test(S(""), S(""), 0);
126    test(S(""), S("abcde"), S::npos);
127    test(S(""), S("abcdeabcde"), S::npos);
128    test(S(""), S("abcdeabcdeabcdeabcde"), S::npos);
129    test(S("abcde"), S(""), 5);
130    test(S("abcde"), S("abcde"), 0);
131    test(S("abcde"), S("abcdeabcde"), S::npos);
132    test(S("abcde"), S("abcdeabcdeabcdeabcde"), S::npos);
133    test(S("abcdeabcde"), S(""), 10);
134    test(S("abcdeabcde"), S("abcde"), 5);
135    test(S("abcdeabcde"), S("abcdeabcde"), 0);
136    test(S("abcdeabcde"), S("abcdeabcdeabcdeabcde"), S::npos);
137    test(S("abcdeabcdeabcdeabcde"), S(""), 20);
138    test(S("abcdeabcdeabcdeabcde"), S("abcde"), 15);
139    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcde"), 10);
140    test(S("abcdeabcdeabcdeabcde"), S("abcdeabcdeabcdeabcde"), 0);
141}
142
143int main()
144{
145    {
146    typedef std::string S;
147    test0<S>();
148    test1<S>();
149    }
150#if __cplusplus >= 201103L
151    {
152    typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
153    test0<S>();
154    test1<S>();
155    }
156#endif
157}
158