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 // <sstream>
11
12 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
13 // class basic_stringbuf
14
15 // pos_type seekoff(off_type off, ios_base::seekdir way,
16 // ios_base::openmode which = ios_base::in | ios_base::out);
17
18 #include <sstream>
19 #include <cassert>
20
main()21 int main()
22 {
23 {
24 std::stringbuf sb("0123456789", std::ios_base::in);
25 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
26 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);
27 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == -1);
28 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
29 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
30 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
31 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == 3);
32 assert(sb.sgetc() == '3');
33 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == 6);
34 assert(sb.sgetc() == '6');
35 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == 7);
36 assert(sb.sgetc() == '7');
37 }
38 {
39 std::stringbuf sb("0123456789", std::ios_base::out);
40 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == -1);
41 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == -1);
42 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == -1);
43 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out | std::ios_base::in) == -1);
44 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out | std::ios_base::in) == -1);
45 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out | std::ios_base::in) == -1);
46 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == 3);
47 assert(sb.sputc('a') == 'a');
48 assert(sb.str() == "012a456789");
49 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == 7);
50 assert(sb.sputc('b') == 'b');
51 assert(sb.str() == "012a456b89");
52 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == 7);
53 assert(sb.sputc('c') == 'c');
54 assert(sb.str() == "012a456c89");
55 }
56 {
57 std::stringbuf sb("0123456789");
58 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == 3);
59 assert(sb.sgetc() == '3');
60 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == 6);
61 assert(sb.sgetc() == '6');
62 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == 7);
63 assert(sb.sgetc() == '7');
64 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out | std::ios_base::in) == 3);
65 assert(sb.sgetc() == '3');
66 assert(sb.sputc('a') == 'a');
67 assert(sb.str() == "012a456789");
68 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out | std::ios_base::in) == -1);
69 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out | std::ios_base::in) == 7);
70 assert(sb.sgetc() == '7');
71 assert(sb.sputc('c') == 'c');
72 assert(sb.str() == "012a456c89");
73 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == 3);
74 assert(sb.sputc('3') == '3');
75 assert(sb.str() == "0123456c89");
76 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == 7);
77 assert(sb.sputc('7') == '7');
78 assert(sb.str() == "0123456789");
79 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == 7);
80 assert(sb.sputc('c') == 'c');
81 assert(sb.str() == "0123456c89");
82 }
83 {
84 std::wstringbuf sb(L"0123456789", std::ios_base::in);
85 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
86 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);
87 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == -1);
88 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
89 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
90 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
91 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == 3);
92 assert(sb.sgetc() == L'3');
93 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == 6);
94 assert(sb.sgetc() == L'6');
95 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == 7);
96 assert(sb.sgetc() == L'7');
97 }
98 {
99 std::wstringbuf sb(L"0123456789", std::ios_base::out);
100 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == -1);
101 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == -1);
102 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == -1);
103 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out | std::ios_base::in) == -1);
104 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out | std::ios_base::in) == -1);
105 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out | std::ios_base::in) == -1);
106 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == 3);
107 assert(sb.sputc(L'a') == L'a');
108 assert(sb.str() == L"012a456789");
109 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == 7);
110 assert(sb.sputc(L'b') == L'b');
111 assert(sb.str() == L"012a456b89");
112 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == 7);
113 assert(sb.sputc(L'c') == L'c');
114 assert(sb.str() == L"012a456c89");
115 }
116 {
117 std::wstringbuf sb(L"0123456789");
118 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == 3);
119 assert(sb.sgetc() == L'3');
120 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == 6);
121 assert(sb.sgetc() == L'6');
122 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == 7);
123 assert(sb.sgetc() == L'7');
124 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out | std::ios_base::in) == 3);
125 assert(sb.sgetc() == L'3');
126 assert(sb.sputc(L'a') == L'a');
127 assert(sb.str() == L"012a456789");
128 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out | std::ios_base::in) == -1);
129 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out | std::ios_base::in) == 7);
130 assert(sb.sgetc() == L'7');
131 assert(sb.sputc(L'c') == L'c');
132 assert(sb.str() == L"012a456c89");
133 assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == 3);
134 assert(sb.sputc(L'3') == L'3');
135 assert(sb.str() == L"0123456c89");
136 assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == 7);
137 assert(sb.sputc(L'7') == L'7');
138 assert(sb.str() == L"0123456789");
139 assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == 7);
140 assert(sb.sputc(L'c') == L'c');
141 assert(sb.str() == L"0123456c89");
142 }
143 }
144