Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <fstream> |
| 11 | |
Howard Hinnant | 66dbf0d | 2010-08-22 00:26:48 +0000 | [diff] [blame] | 12 | // pos_type seekoff(off_type off, ios_base::seekdir way, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | // ios_base::openmode which = ios_base::in | ios_base::out); |
Howard Hinnant | 66dbf0d | 2010-08-22 00:26:48 +0000 | [diff] [blame] | 14 | // pos_type seekpos(pos_type sp, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
| 22 | template <class CharT> |
| 23 | struct 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 | |
| 39 | int 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 | } |