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 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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 48 { 49 wchar_t buf[10]; 50 typedef std::filebuf::pos_type pos_type; 51 std::wfilebuf f; 52 f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0])); 53 assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out 54 | std::ios_base::trunc) != 0); 55 assert(f.is_open()); 56 f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26); 57 LIBCPP_ASSERT(buf[0] == L'v'); 58 pos_type p = f.pubseekoff(-15, std::ios_base::cur); 59 assert(p == 11); 60 assert(f.sgetc() == L'l'); 61 f.pubseekoff(0, std::ios_base::beg); 62 assert(f.sgetc() == L'a'); 63 f.pubseekoff(-1, std::ios_base::end); 64 assert(f.sgetc() == L'z'); 65 assert(f.pubseekpos(p) == p); 66 assert(f.sgetc() == L'l'); 67 } 68 std::remove("seekoff.dat"); 69 #endif 70 71 return 0; 72 } 73