blob: 01a56563445e41a582e08c12ee49d9c0f5b0c720 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <fstream>
11
Howard Hinnant66dbf0d2010-08-22 00:26:48 +000012// pos_type seekoff(off_type off, ios_base::seekdir way,
Howard Hinnant3e519522010-05-11 19:42:16 +000013// ios_base::openmode which = ios_base::in | ios_base::out);
Howard Hinnant66dbf0d2010-08-22 00:26:48 +000014// pos_type seekpos(pos_type sp,
Howard Hinnant3e519522010-05-11 19:42:16 +000015// 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
22template <class CharT>
23struct test_buf
24 : public std::basic_filebuf<CharT>
25{
26 typedef std::basic_filebuf<CharT> base;
27 typedef typename base::char_type char_type;
28 typedef typename base::int_type int_type;
29 typedef typename base::pos_type pos_type;
30
31 char_type* eback() const {return base::eback();}
32 char_type* gptr() const {return base::gptr();}
33 char_type* egptr() const {return base::egptr();}
34 void gbump(int n) {base::gbump(n);}
35
36 virtual int_type underflow() {return base::underflow();}
37};
38
39int main()
40{
41 {
42 char buf[10];
43 typedef std::filebuf::pos_type pos_type;
44 std::filebuf f;
45 f.pubsetbuf(buf, sizeof(buf));
46 assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
47 | std::ios_base::trunc) != 0);
48 assert(f.is_open());
49 f.sputn("abcdefghijklmnopqrstuvwxyz", 26);
50 assert(buf[0] == 'v');
51 pos_type p = f.pubseekoff(-15, std::ios_base::cur);
52 assert(p == 11);
53 assert(f.sgetc() == 'l');
54 f.pubseekoff(0, std::ios_base::beg);
55 assert(f.sgetc() == 'a');
56 f.pubseekoff(-1, std::ios_base::end);
57 assert(f.sgetc() == 'z');
58 assert(f.pubseekpos(p) == p);
59 assert(f.sgetc() == 'l');
60 }
61 std::remove("seekoff.dat");
62 {
63 wchar_t buf[10];
64 typedef std::filebuf::pos_type pos_type;
65 std::wfilebuf f;
66 f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0]));
67 assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
68 | std::ios_base::trunc) != 0);
69 assert(f.is_open());
70 f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26);
71 assert(buf[0] == L'v');
72 pos_type p = f.pubseekoff(-15, std::ios_base::cur);
73 assert(p == 11);
74 assert(f.sgetc() == L'l');
75 f.pubseekoff(0, std::ios_base::beg);
76 assert(f.sgetc() == L'a');
77 f.pubseekoff(-1, std::ios_base::end);
78 assert(f.sgetc() == L'z');
79 assert(f.pubseekpos(p) == p);
80 assert(f.sgetc() == L'l');
81 }
82 std::remove("seekoff.dat");
83}