xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/seekoff.pass.cpp (revision af8c49dc1ec44339d915d988ffe0f38da68ca0e7)
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 #include <fstream>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
main(int,char **)21 int main(int, char**)
22 {
23     {
24         char buf[10];
25         typedef std::filebuf::pos_type pos_type;
26         std::filebuf f;
27         f.pubsetbuf(buf, sizeof(buf));
28         assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
29                                                        | std::ios_base::trunc) != 0);
30         assert(f.is_open());
31         f.sputn("abcdefghijklmnopqrstuvwxyz", 26);
32         LIBCPP_ASSERT(buf[0] == 'v');
33         pos_type p = f.pubseekoff(-15, std::ios_base::cur);
34         assert(p == 11);
35         assert(f.sgetc() == 'l');
36         f.pubseekoff(0, std::ios_base::beg);
37         assert(f.sgetc() == 'a');
38         f.pubseekoff(-1, std::ios_base::end);
39         assert(f.sgetc() == 'z');
40         assert(f.pubseekpos(p) == p);
41         assert(f.sgetc() == 'l');
42     }
43     std::remove("seekoff.dat");
44 
45 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
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 #endif
68 
69   return 0;
70 }
71