• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <fstream>
10 
11 // pos_type seekoff(off_type off, ios_base::seekdir way,
12 //                  ios_base::openmode which = ios_base::in | ios_base::out);
13 // pos_type seekpos(pos_type sp,
14 //                  ios_base::openmode which = ios_base::in | ios_base::out);
15 
16 // FILE_DEPENDENCIES: underflow.dat
17 
18 #include <fstream>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
main(int,char **)23 int main(int, char**)
24 {
25     {
26         char buf[10];
27         typedef std::filebuf::pos_type pos_type;
28         std::filebuf f;
29         f.pubsetbuf(buf, sizeof(buf));
30         assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
31                                                        | std::ios_base::trunc) != 0);
32         assert(f.is_open());
33         f.sputn("abcdefghijklmnopqrstuvwxyz", 26);
34         LIBCPP_ASSERT(buf[0] == 'v');
35         pos_type p = f.pubseekoff(-15, std::ios_base::cur);
36         assert(p == 11);
37         assert(f.sgetc() == 'l');
38         f.pubseekoff(0, std::ios_base::beg);
39         assert(f.sgetc() == 'a');
40         f.pubseekoff(-1, std::ios_base::end);
41         assert(f.sgetc() == 'z');
42         assert(f.pubseekpos(p) == p);
43         assert(f.sgetc() == 'l');
44     }
45     std::remove("seekoff.dat");
46     {
47         wchar_t buf[10];
48         typedef std::filebuf::pos_type pos_type;
49         std::wfilebuf f;
50         f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0]));
51         assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
52                                                        | std::ios_base::trunc) != 0);
53         assert(f.is_open());
54         f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26);
55         LIBCPP_ASSERT(buf[0] == L'v');
56         pos_type p = f.pubseekoff(-15, std::ios_base::cur);
57         assert(p == 11);
58         assert(f.sgetc() == L'l');
59         f.pubseekoff(0, std::ios_base::beg);
60         assert(f.sgetc() == L'a');
61         f.pubseekoff(-1, std::ios_base::end);
62         assert(f.sgetc() == L'z');
63         assert(f.pubseekpos(p) == p);
64         assert(f.sgetc() == L'l');
65     }
66     std::remove("seekoff.dat");
67 
68   return 0;
69 }
70