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// <fstream>
11
12// pos_type seekoff(off_type off, ios_base::seekdir way,
13//                  ios_base::openmode which = ios_base::in | ios_base::out);
14// pos_type seekpos(pos_type sp,
15//                  ios_base::openmode which = ios_base::in | ios_base::out);
16
17// This test is not entirely portable
18
19#include <fstream>
20#include <cassert>
21
22int main()
23{
24    {
25        char buf[10];
26        typedef std::filebuf::pos_type pos_type;
27        std::filebuf f;
28        f.pubsetbuf(buf, sizeof(buf));
29        assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
30                                                       | std::ios_base::trunc) != 0);
31        assert(f.is_open());
32        f.sputn("abcdefghijklmnopqrstuvwxyz", 26);
33        assert(buf[0] == 'v');
34        pos_type p = f.pubseekoff(-15, std::ios_base::cur);
35        assert(p == 11);
36        assert(f.sgetc() == 'l');
37        f.pubseekoff(0, std::ios_base::beg);
38        assert(f.sgetc() == 'a');
39        f.pubseekoff(-1, std::ios_base::end);
40        assert(f.sgetc() == 'z');
41        assert(f.pubseekpos(p) == p);
42        assert(f.sgetc() == 'l');
43    }
44    std::remove("seekoff.dat");
45    {
46        wchar_t buf[10];
47        typedef std::filebuf::pos_type pos_type;
48        std::wfilebuf f;
49        f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0]));
50        assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
51                                                       | std::ios_base::trunc) != 0);
52        assert(f.is_open());
53        f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26);
54        assert(buf[0] == L'v');
55        pos_type p = f.pubseekoff(-15, std::ios_base::cur);
56        assert(p == 11);
57        assert(f.sgetc() == L'l');
58        f.pubseekoff(0, std::ios_base::beg);
59        assert(f.sgetc() == L'a');
60        f.pubseekoff(-1, std::ios_base::end);
61        assert(f.sgetc() == L'z');
62        assert(f.pubseekpos(p) == p);
63        assert(f.sgetc() == L'l');
64    }
65    std::remove("seekoff.dat");
66}
67